javax.xml.transform.sax.SAXTransformerFactory#newXMLFilter ( )源码实例Demo

下面列出了javax.xml.transform.sax.SAXTransformerFactory#newXMLFilter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: SAXTFactoryTest.java
/**
 * Unit test for contentHandler setter/getter along reader as handler's
 * parent.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase10() throws Exception {
    String outputFile = USER_DIR + "saxtf010.out";
    String goldFile = GOLDEN_DIR + "saxtf010GF.out";
    // The transformer will use a SAX parser as it's reader.
    XMLReader reader = XMLReaderFactory.createXMLReader();
    SAXTransformerFactory saxTFactory
            = (SAXTransformerFactory)TransformerFactory.newInstance();
    XMLFilter filter =
        saxTFactory.newXMLFilter(new StreamSource(XSLT_FILE));
    filter.setParent(reader);
    filter.setContentHandler(new MyContentHandler(outputFile));

    // Now, when you call transformer.parse, it will set itself as
    // the content handler for the parser object (it's "parent"), and
    // will then call the parse method on the parser.
    filter.parse(new InputSource(XML_FILE));
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
源代码2 项目: openjdk-jdk9   文件: SAXTFactoryTest.java
/**
 * Unit test for contentHandler setter/getter.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase12() throws Exception {
    String outputFile = USER_DIR + "saxtf012.out";
    String goldFile = GOLDEN_DIR + "saxtf012GF.out";
    // The transformer will use a SAX parser as it's reader.
    XMLReader reader = XMLReaderFactory.createXMLReader();

    InputSource is = new InputSource(new FileInputStream(XSLT_FILE));
    SAXSource saxSource = new SAXSource();
    saxSource.setInputSource(is);

    SAXTransformerFactory saxTFactory = (SAXTransformerFactory)TransformerFactory.newInstance();
    XMLFilter filter = saxTFactory.newXMLFilter(saxSource);

    filter.setParent(reader);
    filter.setContentHandler(new MyContentHandler(outputFile));

    // Now, when you call transformer.parse, it will set itself as
    // the content handler for the parser object (it's "parent"), and
    // will then call the parse method on the parser.
    filter.parse(new InputSource(XML_FILE));
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
源代码3 项目: openjdk-jdk9   文件: SAXTFactoryTest.java
/**
 * Unit test for TemplatesHandler setter/getter.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase13() throws Exception {
    String outputFile = USER_DIR + "saxtf013.out";
    String goldFile = GOLDEN_DIR + "saxtf013GF.out";
    try(FileInputStream fis = new FileInputStream(XML_FILE)) {
        // The transformer will use a SAX parser as it's reader.
        XMLReader reader = XMLReaderFactory.createXMLReader();

        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory) TransformerFactory.newInstance();
        TemplatesHandler thandler = saxTFactory.newTemplatesHandler();
        // I have put this as it was complaining about systemid
        thandler.setSystemId("file:///" + USER_DIR);

        reader.setContentHandler(thandler);
        reader.parse(XSLT_FILE);
        XMLFilter filter
                = saxTFactory.newXMLFilter(thandler.getTemplates());
        filter.setParent(reader);

        filter.setContentHandler(new MyContentHandler(outputFile));
        filter.parse(new InputSource(fis));
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
源代码4 项目: openjdk-jdk9   文件: SAXTFactoryTest.java
/**
 * Unit test for contentHandler setter/getter with parent.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase11() throws Exception {
    String outputFile = USER_DIR + "saxtf011.out";
    String goldFile = GOLDEN_DIR + "saxtf011GF.out";
    // The transformer will use a SAX parser as it's reader.
    XMLReader reader = XMLReaderFactory.createXMLReader();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    Document document = docBuilder.parse(new File(XSLT_FILE));
    Node node = (Node)document;
    DOMSource domSource= new DOMSource(node);

    SAXTransformerFactory saxTFactory
            = (SAXTransformerFactory)TransformerFactory.newInstance();
    XMLFilter filter = saxTFactory.newXMLFilter(domSource);

    filter.setParent(reader);
    filter.setContentHandler(new MyContentHandler(outputFile));

    // Now, when you call transformer.parse, it will set itself as
    // the content handler for the parser object (it's "parent"), and
    // will then call the parse method on the parser.
    filter.parse(new InputSource(XML_FILE));
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
源代码5 项目: openccg   文件: RulesExtract.java
public static void extractRules(ExtractionProperties extractProps) throws TransformerException, TransformerConfigurationException,SAXException, IOException,JDOMException{
	
	System.out.println("Extracting rule info:");
	
	File rulesFile = new File(new File(extractProps.destDir), "rules.xml");
	File tempFile = new File(new File(extractProps.tempDir), "temp-rules.xml");
	PrintWriter tempOut=new PrintWriter(new FileOutputStream(tempFile),true);
	
	File ccgbankDir = new File(extractProps.srcDir);
	File[] ccgbankSections=ccgbankDir.listFiles();
	Arrays.sort(ccgbankSections);
	
	RulesTally.RULE_FREQ_CUTOFF = extractProps.ruleFreqCutoff;
       RulesTally.KEEP_UNMATCHED = !extractProps.skipUnmatched;
	
	// add root
	tempOut.println("<rules>");
	
	TransformerFactory tFactory = TransformerFactory.newInstance();
	Transformer transformer = tFactory.newTransformer(ExtractGrammar.getSource("opennlp.ccgbank/transform/rulesExtr.xsl"));
	
	for (int i=extractProps.startSection; i<=extractProps.endSection; i++){
		
		File[] files=ccgbankSections[i].listFiles();
		Arrays.sort(files);
		
		int fileStart = 0; int fileLimit = files.length;
		if (extractProps.fileNum >= 0) {
			fileStart = extractProps.fileNum;
			fileLimit = extractProps.fileNum + 1;
		}
		
		for (int j=fileStart; j<fileLimit; j++){
			String inputFile=files[j].getAbsolutePath();
			if (j == fileStart) System.out.print(files[j].getName() + " ");
			else if (j == (fileLimit-1)) System.out.println(" " + files[j].getName());
			else System.out.print(".");
			if (fileStart == fileLimit-1) System.out.println();
			try {
				transformer.transform(new StreamSource(inputFile),new StreamResult(tempOut));
			}
			catch (Exception exc) {
                   System.out.println("Skipping: " + inputFile);
                   System.out.println(exc.toString());
			}
			tempOut.flush();
		}
	}
	
	tempOut.flush();
	tempOut.println("</rules>");
	tempOut.close();
	
	RulesTally.printTally(extractProps);
	
	System.out.println("Generating rules.xml");
	
	if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)){
		
		SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
		
		// Create an XMLFilter for each stylesheet.
		XMLFilter xmlFilter1 = saxTFactory.newXMLFilter(ExtractGrammar.getSource("opennlp.ccgbank/transform/ccgRules.xsl"));
		

		//XMLFilter xmlFilter3 = saxTFactory.newXMLFilter(new StreamSource("foo3.xsl"));
		
		// Create an XMLReader.
		XMLReader reader = XMLReaderFactory.createXMLReader();
		
		// xmlFilter1 uses the XMLReader as its reader.
		xmlFilter1.setParent(reader);
		
		java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
		xmlProps.setProperty("indent", "yes");
		xmlProps.setProperty("standalone", "no"); 
		xmlProps.setProperty("{http://xml.apache.org/xalan}indent-amount", "2");
		Serializer serializer = SerializerFactory.getSerializer(xmlProps);
		serializer.setOutputStream(new FileOutputStream(rulesFile));


		XMLFilter xmlFilter = xmlFilter1;
		xmlFilter.setContentHandler(serializer.asContentHandler());
		xmlFilter.parse(new InputSource(tempFile.getPath()));
	}
	
	//Deleting the temporory lex file
	//lexiconTempFile.delete();
}