Java Code Examples for javax.xml.transform.sax.SAXSource#setInputSource()

The following examples show how to use javax.xml.transform.sax.SAXSource#setInputSource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SAXTFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * SAXTFactory.newTransformerhandler() method which takes SAXSource as
 * argument can be set to XMLReader. SAXSource has input XML file as its
 * input source. XMLReader has a content handler which write out the result
 * to output file. Test verifies output file is same as golden file.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase02() throws Exception {
    String outputFile = USER_DIR + "saxtf002.out";
    String goldFile = GOLDEN_DIR + "saxtf002GF.out";

    try (FileOutputStream fos = new FileOutputStream(outputFile);
            FileInputStream fis = new FileInputStream(XSLT_FILE)) {
        XMLReader reader = XMLReaderFactory.createXMLReader();
        SAXTransformerFactory saxTFactory
                = (SAXTransformerFactory) TransformerFactory.newInstance();
        SAXSource ss = new SAXSource();
        ss.setInputSource(new InputSource(fis));

        TransformerHandler handler = saxTFactory.newTransformerHandler(ss);
        Result result = new StreamResult(fos);
        handler.setResult(result);
        reader.setContentHandler(handler);
        reader.parse(XML_FILE);
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}
 
Example 2
Source File: SAXTFactoryTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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));
}
 
Example 3
Source File: HWSAXSourceMessageProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
public SAXSource invoke(SAXSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }

    SAXSource response = new SAXSource();
    try {
        SOAPMessage msg = factory.createMessage();
        msg.getSOAPPart().setContent(request);
        SOAPBody body = msg.getSOAPBody();
        Node n = body.getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setInputSource(sayHiInputSource);
        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setInputSource(greetMeInputSource);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 4
Source File: TfClearParamTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains transformer's parameter whose initiated with a sax source with
 * the a name that set before. Value should be same as set one.
 * @throws Exception If any errors occur.
 */
@Test
public void clear07() throws Exception {
    try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
        SAXSource saxSource = new SAXSource();
        saxSource.setInputSource(new InputSource(fis));

        Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
        transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
        assertEquals(transformer.getParameter(LONG_PARAM_NAME), PARAM_VALUE);
    }
}
 
Example 5
Source File: TfClearParamTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Obtains transformer's parameter whose initiated with a sax source with
 * the a name that wasn't set before. Null is expected.
 * @throws Exception If any errors occur.
 */
@Test
public void clear08() throws Exception {
    try (FileInputStream fis = new FileInputStream(XSL_FILE)) {
        SAXSource saxSource = new SAXSource();
        saxSource.setInputSource(new InputSource(fis));

        Transformer transformer = TransformerFactory.newInstance().newTransformer(saxSource);
        transformer.setParameter(LONG_PARAM_NAME, PARAM_VALUE);
        transformer.clearParameters();
        assertNull(transformer.getParameter(LONG_PARAM_NAME));
    }
}
 
Example 6
Source File: TransformerEncodeTest.java    From exificient with MIT License 5 votes vote down vote up
private static Document decode(final EXIFactory exiFactory,
		final InputSource is) throws Exception {
	final DOMResult result = new DOMResult();
	final SAXSource exiSource = new EXISource(exiFactory);
	exiSource.setInputSource(is);
	final TransformerFactory tf = TransformerFactory.newInstance();
	final Transformer transformer = tf.newTransformer();
	transformer.transform(exiSource, result);
	return (Document) result.getNode();
}
 
Example 7
Source File: IssueTestCase.java    From exificient with MIT License 5 votes vote down vote up
public void testIssue20() throws Exception {
	EXIFactory exiFactory = DefaultEXIFactory.newInstance();
	exiFactory.setValueMaxLength(16);
	// exiFactory.setCodingMode(CodingMode.COMPRESSION);

	String sxmlIssue20 = "./data/issues/issue20/treebank_e.xml";
	
	File fEXI = File.createTempFile("testIssue20", ".exi");
	fEXI.deleteOnExit();
	
	
	// encode
	{
		long startEncode = System.currentTimeMillis();
		OutputStream osEXI = new FileOutputStream(fEXI); // EXI output
		EXIResult exiResult = new EXIResult(exiFactory);
		exiResult.setOutputStream(osEXI);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler( exiResult.getHandler() );
		xmlReader.parse(sxmlIssue20); // parse XML input
		osEXI.close();
		long endEncode = System.currentTimeMillis();
		System.out.println("Encode time for " + sxmlIssue20 + " is " + (endEncode-startEncode) + "ms to " + fEXI.length() +"Bytes, " + System.getProperty("java.version"));
	}
	
	// decode
	{
		long startDecode = System.currentTimeMillis();
		Result result = new StreamResult(File.createTempFile("testIssue20", ".xml"));
		InputSource is = new InputSource(new FileInputStream(fEXI));
		SAXSource exiSource = new EXISource(exiFactory);
		exiSource.setInputSource(is);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);	
		long endDecode = System.currentTimeMillis();
		System.out.println("Decode time for " + sxmlIssue20 + " is " + (endDecode-startDecode) + "ms");
	}
}
 
Example 8
Source File: DtrMapTestCase.java    From exificient with MIT License 5 votes vote down vote up
private static void testNoDTR(InputStream xsd, InputStream xml)
		throws EXIException, SAXException, IOException,
		TransformerException {
	/* senseless DTR Map */
	QName type = new QName("", "FooUnknownXYZ");
	QName representation = new QName(Constants.W3C_EXI_NS_URI, "boolean");
	QName[] dtrMapTypes = { type };
	QName[] dtrMapRepresentations = { representation };

	// factory
	EXIFactory ef = DefaultEXIFactory.newInstance();
	ef.setFidelityOptions(FidelityOptions.createStrict());
	ef.setGrammars(GrammarFactory.newInstance().createGrammars(xsd));
	ef.setDatatypeRepresentationMap(dtrMapTypes, dtrMapRepresentations);

	// unset DTR
	ef.setDatatypeRepresentationMap(null, null);
	// encode with unknown DTR
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	EXIResult exiResult = new EXIResult(ef);
	exiResult.setOutputStream(baos);
	XMLReader xmlReader = XMLReaderFactory.createXMLReader();
	xmlReader.setContentHandler(exiResult.getHandler());
	xmlReader.parse(new InputSource(xml)); // parse XML input

	// System.out.println("Size EXI " + baos.size());

	// decode without any DTR --> interoperability
	OutputStream baosXML = new ByteArrayOutputStream();
	Result result = new StreamResult(baosXML);
	InputSource is = new InputSource(new ByteArrayInputStream(
			baos.toByteArray()));
	SAXSource exiSource = new EXISource(ef);
	exiSource.setInputSource(is);
	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer transformer = tf.newTransformer();
	transformer.transform(exiSource, result);
}
 
Example 9
Source File: HWSAXSourcePayloadProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
public SAXSource invoke(SAXSource request) {
    QName qn = (QName)ctx.getMessageContext().get(MessageContext.WSDL_OPERATION);
    if (qn == null) {
        throw new RuntimeException("No Operation Name");
    }
    SAXSource response = new SAXSource();
    try {

        DOMResult domResult = new DOMResult();
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.transform(request, domResult);
        Node n = domResult.getNode().getFirstChild();

        while (n.getNodeType() != Node.ELEMENT_NODE) {
            n = n.getNextSibling();
        }
        if (n.getLocalName().equals(sayHi.getLocalPart())) {
            response.setInputSource(sayHiInputSource);

        } else if (n.getLocalName().equals(greetMe.getLocalPart())) {
            response.setInputSource(greetMeInputSource);
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}
 
Example 10
Source File: DtrMapTestCase.java    From exificient with MIT License 4 votes vote down vote up
private byte[] testEString(String xmlAsString) throws EXIException,
		IOException, SAXException, TransformerException {
	/* DTR Map */
	QName type = new QName("", "es");
	QName representation = new QName(Constants.W3C_EXI_NS_URI, "estring");
	QName[] dtrMapTypes = { type };
	QName[] dtrMapRepresentations = { representation };

	// factory
	EXIFactory ef = DefaultEXIFactory.newInstance();
	ef.setFidelityOptions(FidelityOptions.createStrict());
	ef.setGrammars(GrammarFactory.newInstance().createGrammars(
			new ByteArrayInputStream(xsdAsStringEString.getBytes())));
	ef.setDatatypeRepresentationMap(dtrMapTypes, dtrMapRepresentations);

	// encode
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	EXIResult exiResult = new EXIResult(ef);
	exiResult.setOutputStream(baos);
	XMLReader xmlReader = XMLReaderFactory.createXMLReader();
	xmlReader.setContentHandler(exiResult.getHandler());
	xmlReader.parse(new InputSource(new ByteArrayInputStream(xmlAsString
			.getBytes()))); // parse XML input

	// decode
	ByteArrayOutputStream baosXML = new ByteArrayOutputStream();
	Result result = new StreamResult(baosXML);
	InputSource is = new InputSource(new ByteArrayInputStream(
			baos.toByteArray()));
	SAXSource exiSource = new EXISource(ef);
	exiSource.setInputSource(is);
	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer transformer = tf.newTransformer();
	transformer.transform(exiSource, result);

	// System.out.println(new String(baosXML.toByteArray()));

	XMLUnit.setIgnoreWhitespace(true);
	XMLUnit.setIgnoreAttributeOrder(true);
	XMLUnit.setIgnoreDiffBetweenTextAndCDATA(true);

	XMLAssert.assertXMLEqual(new StringReader(xmlAsString),
			new StringReader(new String(baosXML.toByteArray())));

	return baos.toByteArray();
}