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

The following examples show how to use javax.xml.transform.sax.SAXSource#setXMLReader() . 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: EXIficientCMD.java    From exificient with MIT License 6 votes vote down vote up
protected void decode(String input, EXIFactory exiFactory, String output)
		throws EXIException, TransformerException, IOException {
	OutputStream xmlOutput = new FileOutputStream(output);

	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer transformer = tf.newTransformer();
	SAXSource exiSource = new SAXSource(new InputSource(
			new FileInputStream(input)));
	exiSource.setXMLReader(new SAXFactory(exiFactory).createEXIReader());

	if (exiFactory.isFragment()) {
		transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
				"yes");
	}

	transformer.transform(exiSource, new StreamResult(xmlOutput));

	xmlOutput.flush();
	xmlOutput.close();
}
 
Example 2
Source File: RsPrettyXml.java    From takes with MIT License 5 votes vote down vote up
/**
 * Format body with proper indents using SAX.
 * @param body Response body
 * @return New properly formatted body
 * @throws IOException If fails
 */
private static byte[] transform(final InputStream body) throws IOException {
    final SAXSource source = new SAXSource(new InputSource(body));
    final ByteArrayOutputStream result = new ByteArrayOutputStream();
    try {
        final XMLReader xmlreader = SAXParserFactory.newInstance()
            .newSAXParser().getXMLReader();
        source.setXMLReader(xmlreader);
        xmlreader.setFeature(
            RsPrettyXml.LOAD_EXTERNAL_DTD, false
        );
        final String yes = "yes";
        final Transformer transformer = TransformerFactory.newInstance()
            .newTransformer();
        // @checkstyle MultipleStringLiteralsCheck (2 line)
        transformer.setOutputProperty(
            OutputKeys.OMIT_XML_DECLARATION, yes
        );
        RsPrettyXml.prepareDocType(body, transformer);
        transformer.setOutputProperty(OutputKeys.INDENT, yes);
        transformer.transform(source, new StreamResult(result));
    } catch (final TransformerException
        | ParserConfigurationException
        | SAXException ex) {
        throw new IOException(ex);
    }
    return result.toByteArray();
}
 
Example 3
Source File: EXIficientDemo.java    From exificient with MIT License 5 votes vote down vote up
protected void decode(XMLReader exiReader, String exiLocation)
		throws SAXException, IOException, TransformerException {

	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer transformer = tf.newTransformer();

	InputStream exiIS = new FileInputStream(exiLocation);
	SAXSource exiSource = new SAXSource(new InputSource(exiIS));
	exiSource.setXMLReader(exiReader);

	OutputStream os = new FileOutputStream(exiLocation + XML_EXTENSION);
	transformer.transform(exiSource, new StreamResult(os));
	os.close();
}
 
Example 4
Source File: TestSAXDecoder.java    From exificient with MIT License 5 votes vote down vote up
@Override
public void decodeTo(InputStream exiDocument, OutputStream xmlOutput)
		throws Exception {

	Result result = new StreamResult(xmlOutput);
	InputSource is = new InputSource(exiDocument);
	SAXSource exiSource = new SAXSource(is);
	exiSource.setXMLReader(exiReader);
	transformer.transform(exiSource, result);

	// this.decodeTo(ef, exiDocument, xmlOutput, transformer);
}
 
Example 5
Source File: AbstractProperties.java    From exificient with MIT License 5 votes vote down vote up
protected String decodeEXIToXML(InputStream isEXI) throws IOException,
		SAXException, TransformerException, EXIException {
	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer transformer = tf.newTransformer();

	SAXSource exiSource = new SAXSource(new InputSource(isEXI));
	exiSource.setXMLReader(new SAXFactory(factory).createEXIReader());

	ByteArrayOutputStream xmlDecoded = new ByteArrayOutputStream();
	transformer.transform(exiSource, new StreamResult(xmlDecoded));

	return xmlDecoded.toString();
}
 
Example 6
Source File: MultipleStreamTest.java    From exificient with MIT License 5 votes vote down vote up
protected void _testDecode(EXIFactory exiFactory, byte[] isBytes,
		int numberOfEXIDocuments, InputStream isEXI) throws EXIException,
		TransformerException, AssertionFailedError, IOException,
		ParserConfigurationException, SAXException {
	// decode EXI
	TransformerFactory tf = TransformerFactory.newInstance();
	Transformer transformer = tf.newTransformer();

	for (int i = 0; i < numberOfEXIDocuments; i++) {
		// InputSource is = new InputSource(isEXI);
		InputSource is = new InputSource(isEXI);
		SAXSource exiSource = new SAXSource(is);
		XMLReader exiReader = new SAXFactory(exiFactory).createEXIReader();
		exiSource.setXMLReader(exiReader);
		ByteArrayOutputStream xmlOutput = new ByteArrayOutputStream();
		Result result = new StreamResult(xmlOutput);
		transformer.transform(exiSource, result);
		byte[] xmlDec = xmlOutput.toByteArray();
		// System.out.println("Decode #" + (i+1) + new String(xmlDec));

		ByteArrayInputStream baisControl = new ByteArrayInputStream(isBytes); // testing
																				// only
		checkXMLEquality(exiFactory, baisControl, new ByteArrayInputStream(
				xmlDec));
		// checkXMLValidity(exiFactory, new ByteArrayInputStream(xmlDec));
	}
}
 
Example 7
Source File: DocViewFormat.java    From jackrabbit-filevault with Apache License 2.0 5 votes vote down vote up
/** internally formats the given file and computes their checksum
 * 
 * @param file the file
 * @param original checksum of the original file
 * @param formatted checksum of the formatted file
 * @return the formatted bytes
 * @throws IOException if an error occurs */
private byte[] format(File file, Checksum original, Checksum formatted) throws IOException {
    try (InputStream in = new CheckedInputStream(new BufferedInputStream(new FileInputStream(file)), original)) {
        @SuppressWarnings("resource")
        ByteArrayOutputStream buffer = formattingBuffer != null ? formattingBuffer.get() : null;
        if (buffer == null) {
            buffer = new ByteArrayOutputStream();
            formattingBuffer = new WeakReference<>(buffer);
        } else {
            buffer.reset();
        }

        try (OutputStream out = new CheckedOutputStream(buffer, formatted);
             FormattingXmlStreamWriter writer = FormattingXmlStreamWriter.create(out, format)) {
            // cannot use XMlStreamReader due to comment handling:
            // https://stackoverflow.com/questions/15792007/why-does-xmlstreamreader-staxsource-strip-comments-from-xml
            TransformerFactory tf = TransformerFactory.newInstance();
            tf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            SAXSource saxSource = new SAXSource(new InputSource(in));
            SAXParserFactory sf = SAXParserFactory.newInstance();
            sf.setNamespaceAware(true);
            sf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
            sf.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            saxSource.setXMLReader(new NormalizingSaxFilter(sf.newSAXParser().getXMLReader()));
            Transformer t = tf.newTransformer();
            StAXResult result = new StAXResult(writer);
            t.transform(saxSource, result);
        }
        return buffer.toByteArray();
    } catch (TransformerException | XMLStreamException | FactoryConfigurationError | ParserConfigurationException | SAXException ex) {
        throw new IOException(ex);
    }
}
 
Example 8
Source File: CatalogResolverImpl.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Establish an entityResolver for newly resolved URIs.
 * <p>
 * This is called from the URIResolver to set an EntityResolver on the SAX
 * parser to be used for new XML documents that are encountered as a result
 * of the document() function, xsl:import, or xsl:include. This is done
 * because the XSLT processor calls out to the SAXParserFactory itself to
 * create a new SAXParser to parse the new document. The new parser does not
 * automatically inherit the EntityResolver of the original (although
 * arguably it should). Quote from JAXP specification on Class
 * SAXTransformerFactory:
 * <p>
 * {@code If an application wants to set the ErrorHandler or EntityResolver
 * for an XMLReader used during a transformation, it should use a URIResolver
 * to return the SAXSource which provides (with getXMLReader) a reference to
 * the XMLReader}
 *
 */
private void setEntityResolver(SAXSource source) {
    XMLReader reader = source.getXMLReader();
    if (reader == null) {
        SAXParserFactory spFactory = new SAXParserFactoryImpl();
        spFactory.setNamespaceAware(true);
        try {
            reader = spFactory.newSAXParser().getXMLReader();
        } catch (ParserConfigurationException | SAXException ex) {
            CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex);
        }
    }
    if (entityResolver != null) {
        entityResolver = new CatalogResolverImpl(catalog);
    }
    reader.setEntityResolver(entityResolver);
    source.setXMLReader(reader);
}
 
Example 9
Source File: CatalogResolverImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Establish an entityResolver for newly resolved URIs.
 * <p>
 * This is called from the URIResolver to set an EntityResolver on the SAX
 * parser to be used for new XML documents that are encountered as a result
 * of the document() function, xsl:import, or xsl:include. This is done
 * because the XSLT processor calls out to the SAXParserFactory itself to
 * create a new SAXParser to parse the new document. The new parser does not
 * automatically inherit the EntityResolver of the original (although
 * arguably it should). Quote from JAXP specification on Class
 * SAXTransformerFactory:
 * <p>
 * {@code If an application wants to set the ErrorHandler or EntityResolver
 * for an XMLReader used during a transformation, it should use a URIResolver
 * to return the SAXSource which provides (with getXMLReader) a reference to
 * the XMLReader}
 *
 */
private void setEntityResolver(SAXSource source) {
    XMLReader reader = source.getXMLReader();
    if (reader == null) {
        SAXParserFactory spFactory = new SAXParserFactoryImpl();
        spFactory.setNamespaceAware(true);
        try {
            reader = spFactory.newSAXParser().getXMLReader();
        } catch (ParserConfigurationException | SAXException ex) {
            CatalogMessages.reportRunTimeError(CatalogMessages.ERR_PARSER_CONF, ex);
        }
    }
    if (entityResolver != null) {
        entityResolver = new CatalogResolverImpl(catalog);
    }
    reader.setEntityResolver(entityResolver);
    source.setXMLReader(reader);
}
 
Example 10
Source File: SAXFilterFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected Source getSource(String xslFileName) throws SAXException, ParserConfigurationException {
    SAXSource saxsource = new SAXSource(new InputSource(filenameToURL(xslFileName)));
    saxsource.setXMLReader(getXMLReader());
    return saxsource;
}
 
Example 11
Source File: TypeEmptyTest.java    From exificient with MIT License 4 votes vote down vote up
public void testComplexType04Code() throws Exception {
	// acceptance.xsd
	String schema = "<xsd:schema targetNamespace=\"urn:foo\"\r\n           xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\r\n           xmlns:foo=\"urn:foo\" xmlns:hoo=\"urn:hoo\">\r\n\r\n<!--<xsd:import namespace=\"urn:goo\" schemaLocation=\"acceptance_imported_goo.xsd\"/>\r\n<xsd:import namespace=\"urn:hoo\" schemaLocation=\"acceptance_imported_hoo.xsd\"/>\r\n<xsd:import namespace=\"urn:ioo\" schemaLocation=\"acceptance_imported_ioo.xsd\"/>\r\n<xsd:import schemaLocation=\"acceptance_imported_default.xsd\"/>-->\r\n\r\n<xsd:simpleType name=\"finalString\" final=\"#all\">\r\n  <xsd:restriction base=\"xsd:string\" />\r\n</xsd:simpleType>\r\n\r\n<xsd:complexType name=\"extendedDate\">\r\n  <xsd:simpleContent>\r\n    <xsd:extension base=\"xsd:date\">\r\n      <xsd:attribute ref=\"foo:aA\" />\r\n    </xsd:extension>\r\n  </xsd:simpleContent>\r\n</xsd:complexType>\r\n\r\n<xsd:simpleType name=\"listOfBytes\">\r\n  <xsd:list>\r\n    <xsd:simpleType>\r\n      <xsd:restriction base=\"xsd:byte\"/>\r\n    </xsd:simpleType>\r\n  </xsd:list>\r\n</xsd:simpleType>\r\n\r\n<xsd:element name=\"ANY\" type=\"xsd:anyType\"/>\r\n\r\n<xsd:element name=\"nillable_ANY\" type=\"xsd:anyType\" nillable=\"true\" />\r\n\r\n<xsd:complexType name=\"anyType\">\r\n  <xsd:complexContent mixed=\"true\">\r\n    <xsd:restriction base=\"xsd:anyType\">\r\n      <xsd:sequence>\r\n        <xsd:any namespace=\"##any\" processContents=\"lax\" minOccurs=\"0\" maxOccurs=\"unbounded\" />\r\n      </xsd:sequence>\r\n      <xsd:anyAttribute namespace=\"##any\" processContents=\"lax\" />\r\n    </xsd:restriction>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"A\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AB\"/>\r\n        <xsd:element ref=\"foo:AC\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n      </xsd:sequence>\r\n      <xsd:sequence minOccurs=\"1\"/>\r\n      <xsd:element ref=\"foo:AD\"/>\r\n      <xsd:element ref=\"foo:AE\" minOccurs=\"0\"/>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:complexType name=\"B\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AB\"/>\r\n    <xsd:element ref=\"foo:AC\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n    <xsd:element ref=\"foo:AD\" minOccurs=\"0\"/>\r\n  </xsd:sequence>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"restricted_B\">\r\n  <xsd:complexContent>\r\n    <xsd:restriction base=\"foo:B\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AB\"/>\r\n        <xsd:element ref=\"foo:AC\" minOccurs=\"0\"/>\r\n        <xsd:element ref=\"foo:AD\" minOccurs=\"0\"/>\r\n      </xsd:sequence>\r\n    </xsd:restriction>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"extended_B\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:B\">\r\n      <xsd:attribute ref=\"foo:aA\" use=\"required\"/>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"B\" type=\"foo:B\" nillable=\"false\"/>\r\n\r\n<xsd:element name=\"nillable_B\" type=\"foo:B\" nillable=\"true\" />\r\n\r\n<xsd:complexType name=\"C\">\r\n  <xsd:all>\r\n    <xsd:element ref=\"foo:AB\" minOccurs=\"0\" />\r\n    <xsd:element ref=\"foo:AC\" />\r\n  </xsd:all>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"C\" type=\"foo:C\"/>\r\n\r\n<xsd:element name=\"AB\" type=\"xsd:anySimpleType\" nillable=\"false\" />\r\n<xsd:element name=\"nillable_AB\" type=\"xsd:anySimpleType\" nillable=\"true\" />\r\n<xsd:element name=\"AC\" type=\"xsd:anySimpleType\"/>\r\n<xsd:element name=\"AD\" type=\"xsd:anySimpleType\"/>\r\n<xsd:element name=\"AE\" type=\"xsd:anySimpleType\"/>\r\n<xsd:element name=\"AF\" type=\"xsd:date\"/>\r\n\r\n<xsd:element name=\"D\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:sequence>\r\n        <xsd:element name=\"A\" minOccurs=\"0\" maxOccurs=\"2\" />\r\n        <xsd:sequence maxOccurs=\"2\">\r\n          <xsd:element name=\"B\" />\r\n          <xsd:element name=\"C\" minOccurs=\"0\" />\r\n          <xsd:element name=\"D\" minOccurs=\"0\" />\r\n        </xsd:sequence>\r\n      </xsd:sequence>\r\n      <xsd:element name=\"E\" minOccurs=\"0\" />\r\n      <xsd:element name=\"F\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"E\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:choice>\r\n        <xsd:sequence maxOccurs=\"2\">\r\n          <xsd:element name=\"A\" minOccurs=\"0\" maxOccurs=\"2\" />\r\n          <xsd:element name=\"B\" />\r\n          <xsd:element name=\"C\" minOccurs=\"0\" />\r\n        </xsd:sequence>\r\n        <xsd:sequence minOccurs=\"0\">\r\n          <xsd:element name=\"D\" />\r\n          <xsd:element name=\"E\" />\r\n          <xsd:element name=\"F\" />\r\n        </xsd:sequence>\r\n      </xsd:choice>\r\n      <xsd:element name=\"G\" minOccurs=\"0\" />\r\n      <xsd:choice minOccurs=\"1\"/>\r\n      <xsd:element name=\"H\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:attribute name=\"aA\" />\r\n<xsd:attribute name=\"aB\" />\r\n<xsd:attribute name=\"aC\" />\r\n<xsd:attribute name=\"aD\" />\r\n<xsd:attribute name=\"aE\" />\r\n<xsd:attribute name=\"aF\" />\r\n<xsd:attribute name=\"aG\" />\r\n<xsd:attribute name=\"aH\" />\r\n<xsd:attribute name=\"aI\" />\r\n<xsd:attribute name=\"aJ\" />\r\n<xsd:attribute name=\"aK\" type=\"xsd:date\" />\r\n<xsd:attribute name=\"aL\" type=\"xsd:integer\" />\r\n<xsd:attribute name=\"aM\" type=\"xsd:base64Binary\" />\r\n<xsd:attribute name=\"aN\" type=\"foo:listOfBytes\" />\r\n\r\n<xsd:attribute name=\"aBoolean\" type=\"xsd:boolean\" />\r\n\r\n<xsd:complexType name=\"F\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AB\"/>\r\n  </xsd:sequence>\r\n  <xsd:attribute ref=\"foo:aB\" />\r\n  <xsd:attribute ref=\"foo:aC\" />\r\n  <xsd:attribute ref=\"foo:aA\" use=\"required\"/>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"extended_F\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:F\">\r\n      <xsd:anyAttribute namespace=\"##any\" />\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"F\" type=\"foo:F\" nillable=\"true\"/>\r\n\r\n<xsd:element name=\"G\" nillable=\"true\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:element ref=\"foo:AB\" minOccurs=\"0\"/>\r\n    </xsd:sequence>\r\n    <xsd:attribute ref=\"foo:aB\" />\r\n    <xsd:attribute ref=\"foo:aC\" />\r\n    <xsd:attribute ref=\"foo:aA\" use=\"required\"/>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"G2\">\r\n  <xsd:complexType><!-- Don't make it a named type -->\r\n      <xsd:anyAttribute namespace=\"##any\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"G3\"><!-- Don't make it nillable -->\r\n  <xsd:complexType>\r\n      <xsd:sequence>\r\n        <xsd:element name=\"A\" />\r\n      </xsd:sequence>\r\n      <xsd:anyAttribute namespace=\"##any\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"G4\">\r\n  <xsd:complexType>\r\n    <xsd:attribute name=\"aK\" type=\"xsd:positiveInteger\" use=\"required\"/>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"H\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:element name=\"A\" minOccurs=\"0\"/>\r\n      <xsd:any namespace=\"urn:eoo urn:goo\" />\r\n      <xsd:element name=\"B\" />\r\n      <xsd:any namespace=\"##other\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"H2\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:any namespace=\"##other\" minOccurs=\"0\" />\r\n      <xsd:any namespace=\"##targetNamespace ##local\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"H3\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:any namespace=\"urn:none_01\" minOccurs=\"0\" /> <!-- so that \"urn:none_01\" gets in uri partition. -->\r\n      <xsd:element ref=\"foo:AB\" />\r\n      <xsd:any namespace=\"##any\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<!--<xsd:element name=\"H4\">\r\n  <xsd:complexType>\r\n    <xsd:sequence>\r\n      <xsd:sequence>\r\n        <xsd:any namespace=\"##local ##targetNamespace\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n        <xsd:element ref=\"hoo:AC\" minOccurs=\"0\"/>\r\n        <xsd:sequence>\r\n          <xsd:any namespace=\"urn:goo\" minOccurs=\"0\" />\r\n          <xsd:element ref=\"hoo:AB\" minOccurs=\"0\"/>\r\n        </xsd:sequence>\r\n      </xsd:sequence>\r\n      <xsd:any namespace=\"urn:ioo\" minOccurs=\"0\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>-->\r\n\r\n<xsd:element name=\"I\" nillable=\"true\">\r\n  <xsd:complexType>\r\n    <xsd:choice>\r\n      <xsd:element name=\"A\">\r\n        <xsd:complexType>\r\n          <xsd:simpleContent>\r\n            <xsd:extension base=\"xsd:anySimpleType\">\r\n              <xsd:anyAttribute namespace=\"urn:hoo urn:none_02 urn:goo urn:foo urn:hoo urn:hoo ##local\" />\r\n            </xsd:extension>\r\n          </xsd:simpleContent>\r\n        </xsd:complexType>\r\n      </xsd:element>\r\n      <xsd:element name=\"B\">\r\n        <xsd:complexType>\r\n          <xsd:simpleContent>\r\n            <xsd:extension base=\"xsd:anySimpleType\">\r\n              <xsd:anyAttribute namespace=\"##other\" />\r\n            </xsd:extension>\r\n          </xsd:simpleContent>\r\n        </xsd:complexType>\r\n      </xsd:element>\r\n    </xsd:choice>\r\n    <xsd:attribute ref=\"foo:aF\" />\r\n    <xsd:attribute ref=\"foo:aI\" use=\"required\" />\r\n    <xsd:attribute ref=\"foo:aC\" />\r\n    <xsd:anyAttribute namespace=\"##any\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:complexType name=\"I2\">\r\n  <xsd:sequence>\r\n    <xsd:element name=\"A\" minOccurs=\"0\" />\r\n    <xsd:element name=\"B\" minOccurs=\"0\" />\r\n  </xsd:sequence>\r\n  <xsd:attribute ref=\"foo:aF\" />\r\n  <xsd:attribute ref=\"foo:aI\" use=\"required\" />\r\n  <xsd:attribute ref=\"foo:aC\" />\r\n  <xsd:anyAttribute namespace=\"urn:hoo urn:none_03 urn:goo\" />\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"extended_I2\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:I2\">\r\n      <xsd:anyAttribute namespace=\"##targetNamespace ##local\" />\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"I2\" type=\"foo:I2\" nillable=\"true\" />\r\n\r\n<xsd:element name=\"J\">\r\n  <xsd:complexType>\r\n    <xsd:sequence maxOccurs=\"2\">\r\n      <xsd:element ref=\"foo:AB\"/>\r\n      <xsd:element ref=\"foo:AC\" minOccurs=\"0\" maxOccurs=\"2\"/>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"K\" type=\"foo:finalString\"/>\r\n\r\n<xsd:element name=\"L\">\r\n  <xsd:complexType>\r\n    <xsd:sequence minOccurs=\"2\" maxOccurs=\"2\">\r\n      <xsd:element ref=\"foo:AB\"/>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"M\">\r\n  <xsd:complexType>\r\n    <xsd:attribute ref=\"foo:aA\" />\r\n    <xsd:attribute ref=\"foo:aB\" use=\"required\" />\r\n    <xsd:attribute ref=\"foo:aC\" />\r\n    <xsd:attribute ref=\"foo:aD\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:attribute name=\"aJ4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aK4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aL4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aM4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aN4M2\" type=\"xsd:hexBinary\" />\r\n<xsd:attribute name=\"aO4M2\" type=\"xsd:hexBinary\" />\r\n\r\n<xsd:element name=\"M2\">\r\n  <xsd:complexType>\r\n    <xsd:sequence maxOccurs=\"unbounded\">\r\n      <xsd:element name=\"A\">\r\n        <xsd:complexType>\r\n          <xsd:attribute ref=\"foo:aK\" /><!-- xsd:date -->\r\n          <xsd:attribute ref=\"foo:aL\" /><!-- xsd:integer -->\r\n          <xsd:attribute ref=\"foo:aM\" /><!-- xsd:base64Binary -->\r\n          <xsd:attribute ref=\"foo:aN\" /><!-- xsd:listOfBytes -->\r\n        </xsd:complexType>\r\n      </xsd:element>\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"N\">\r\n  <xsd:complexType>\r\n    <xsd:sequence maxOccurs=\"unbounded\">\r\n      <xsd:element name=\"A\" type=\"foo:extendedDate\" />\r\n    </xsd:sequence>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"P\">\r\n  <xsd:complexType>\r\n    <xsd:choice maxOccurs=\"unbounded\">\r\n      <xsd:element ref=\"foo:P1\" />\r\n      <xsd:element ref=\"foo:P2\" />\r\n      <xsd:element ref=\"foo:P3\" />\r\n    </xsd:choice>\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:complexType name=\"P1\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AB\" />\r\n  </xsd:sequence>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"P1\" type=\"foo:P1\" />\r\n\r\n<xsd:complexType name=\"extended_P1\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:P1\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AB\" />\r\n      </xsd:sequence>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"P2\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AC\" />\r\n  </xsd:sequence>\r\n  <xsd:attribute ref=\"foo:aA\" />\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"P2\" type=\"foo:P2\" />\r\n\r\n<xsd:complexType name=\"extended_P2\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:P2\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AC\" />\r\n      </xsd:sequence>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:complexType name=\"P3\">\r\n  <xsd:sequence>\r\n    <xsd:element ref=\"foo:AD\" />\r\n  </xsd:sequence>\r\n  <xsd:anyAttribute namespace=\"urn:eoo urn:foo\" />\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"P3\" type=\"foo:P3\" />\r\n\r\n<xsd:complexType name=\"extended_P3\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:P3\">\r\n      <xsd:sequence>\r\n        <xsd:element ref=\"foo:AD\" />\r\n      </xsd:sequence>\r\n    </xsd:extension>\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n<xsd:element name=\"Q\">\r\n  <xsd:complexType mixed=\"true\">\r\n    <xsd:sequence>\r\n      <xsd:element name=\"Qc\" form=\"qualified\" minOccurs=\"0\">\r\n        <xsd:complexType/>\r\n      </xsd:element>\r\n      <xsd:any namespace=\"##other\" minOccurs=\"0\" />\r\n      <xsd:element ref=\"foo:Qb\" minOccurs=\"0\" /><!-- element Qb constitutes a substitution group. see below. -->\r\n      <xsd:any namespace=\"##local\" minOccurs=\"0\"/>\r\n      <xsd:element name=\"Qa\" form=\"qualified\" minOccurs=\"0\">\r\n        <xsd:complexType/>\r\n      </xsd:element>\r\n    </xsd:sequence>\r\n    <xsd:attribute ref=\"foo:aL\" /><!-- xsd:integer -->\r\n    <xsd:attribute ref=\"foo:aK\" /><!-- xsd:date -->\r\n    <xsd:anyAttribute namespace=\"urn:hoo urn:goo ##local urn:foo\" />\r\n  </xsd:complexType>\r\n</xsd:element>\r\n\r\n<xsd:element name=\"Qb\" type=\"foo:tQb\" />\r\n<xsd:element name=\"Qz\" type=\"foo:tQz\" substitutionGroup=\"foo:Qb\" />\r\n\r\n<xsd:complexType name=\"tQb\"/>\r\n<xsd:complexType name=\"tQz\">\r\n  <xsd:complexContent>\r\n    <xsd:extension base=\"foo:tQb\" />\r\n  </xsd:complexContent>\r\n</xsd:complexType>\r\n\r\n</xsd:schema>\r\n";

	Grammars g = SchemaInformedTest.getGrammarFromSchemaAsString(schema);

	String xml = "<foo:F xmlns:foo='urn:foo' xsi:nil='true'" + " \n "
			+ " foo:aA='' foo:aB='' foo:aC=''" + " \n "
			+ " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"
			+ " \n " + "  <!-- No content here. -->" + " \n " + "</foo:F>";

	EXIFactory ef = DefaultEXIFactory.newInstance();
	ef.setGrammars(g);
	// ef.getFidelityOptions().setFidelity(FidelityOptions.FEATURE_COMMENT,
	// true);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	// encode
	{
		EXIResult exiResult = new EXIResult(ef);
		exiResult.setOutputStream(baos);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(exiResult.getHandler());
		xmlReader.parse(new InputSource(new ByteArrayInputStream(xml
				.getBytes())));
	}

	// decode
	ByteArrayOutputStream baosDecXML = new ByteArrayOutputStream();
	{
		InputSource is = new InputSource(new ByteArrayInputStream(
				baos.toByteArray()));
		XMLReader exiReader = new SAXFactory(ef).createEXIReader();

		Result result = new StreamResult(baosDecXML);
		SAXSource exiSource = new SAXSource(is);
		exiSource.setXMLReader(exiReader);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);
	}

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

}
 
Example 12
Source File: DtrMapTestCase.java    From exificient with MIT License 4 votes vote down vote up
public void testMulti4() throws IOException, EXIException, SAXException,
		TransformerException {
	String schemaAsString = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
			+ "  <xs:element name='root'>"
			+ "   <xs:complexType>"
			+ "         <!--  Built-in Type: N-Bit Integer  -->"
			+ "         <xs:sequence>"
			+ "            <xs:element name='byte'  type='xs:byte' minOccurs='0' maxOccurs='unbounded' />"
			+ "         </xs:sequence>"
			+ "         <xs:attribute name='float' type='xs:float' use='optional'/>"
			+ "         <xs:attribute name='double' type='xs:double' use='optional'/>"
			+ "    </xs:complexType>"
			+ "  </xs:element>"
			+ "</xs:schema>"
			+ "";

	// nbitInteger-valid-00.xml
	// <root>
	// <byte>33</byte>
	// </root>

	String xmlAsString = "<root><byte>33</byte></root>";

	GrammarFactory grammarFactory = GrammarFactory.newInstance();
	Grammars g = grammarFactory.createGrammars(new ByteArrayInputStream(
			schemaAsString.getBytes()));

	// factory with mapping
	EXIFactory exiFactory = DefaultEXIFactory.newInstance();
	exiFactory.setGrammars(g);
	exiFactory.setFidelityOptions(FidelityOptions.createStrict());
	/* DTR Map */
	QName type1 = new QName(Constants.XML_SCHEMA_NS_URI, "decimal");
	QName type2 = new QName(Constants.XML_SCHEMA_NS_URI, "double");
	QName representation1 = new QName(Constants.W3C_EXI_NS_URI, "string");
	QName representation2 = new QName(Constants.W3C_EXI_NS_URI, "decimal");
	QName[] dtrMapTypes = { type1, type2 };
	QName[] dtrMapRepresentations = { representation1, representation2 };
	exiFactory.setDatatypeRepresentationMap(dtrMapTypes,
			dtrMapRepresentations);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	// encode
	{
		EXIResult exiResult = new EXIResult(exiFactory);
		exiResult.setOutputStream(baos);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(exiResult.getHandler());
		xmlReader.parse(new InputSource(new ByteArrayInputStream(
				xmlAsString.getBytes())));
	}

	ByteArrayOutputStream baosDecXML = new ByteArrayOutputStream();

	// decode
	{
		InputSource is = new InputSource(new ByteArrayInputStream(
				baos.toByteArray()));
		XMLReader exiReader = new SAXFactory(exiFactory).createEXIReader();

		Result result = new StreamResult(baosDecXML);
		SAXSource exiSource = new SAXSource(is);
		exiSource.setXMLReader(exiReader);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);
	}

	// System.out.println(baosDecXML.toString());

}
 
Example 13
Source File: DtrMapTestCase.java    From exificient with MIT License 4 votes vote down vote up
public void testMulti8() throws IOException, EXIException, SAXException,
		TransformerException {
	String schemaAsString = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
			+ "  <xs:simpleType name='stringDerived'>"
			+ "   <xs:restriction base='xs:string'>"
			+ "         <xs:enumeration value='Tokyo'/>"
			+ "         <xs:enumeration value='Osaka'/>"
			+ "         <xs:enumeration value='Nagoya'/>"
			+ "   </xs:restriction>"
			+ "  </xs:simpleType>"
			+ "</xs:schema>" + "";

	// enumerationToInteger_03.xml
	// <foo:A xmlns:foo="urn:foo"
	// xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	// xsi:type="foo:stringDerived">Nagoya</foo:A>

	String xmlAsString = "<foo:A xmlns:foo='urn:foo' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
			+ "xsi:type='foo:stringDerived'>NagoyaX</foo:A>";

	GrammarFactory grammarFactory = GrammarFactory.newInstance();
	Grammars g = grammarFactory.createGrammars(new ByteArrayInputStream(
			schemaAsString.getBytes()));

	// factory with mapping
	EXIFactory exiFactory = DefaultEXIFactory.newInstance();
	exiFactory.setGrammars(g);
	exiFactory.setFidelityOptions(FidelityOptions.createStrict());
	/* DTR Map */
	QName type1 = new QName(Constants.XML_SCHEMA_NS_URI, "string");
	QName representation1 = new QName(Constants.W3C_EXI_NS_URI, "integer");
	QName[] dtrMapTypes = { type1 };
	QName[] dtrMapRepresentations = { representation1 };
	exiFactory.setDatatypeRepresentationMap(dtrMapTypes,
			dtrMapRepresentations);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	// encode
	{
		EXIResult exiResult = new EXIResult(exiFactory);
		exiResult.setOutputStream(baos);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(exiResult.getHandler());
		xmlReader.parse(new InputSource(new ByteArrayInputStream(
				xmlAsString.getBytes())));
	}

	ByteArrayOutputStream baosDecXML = new ByteArrayOutputStream();

	// decode
	{
		InputSource is = new InputSource(new ByteArrayInputStream(
				baos.toByteArray()));
		XMLReader exiReader = new SAXFactory(exiFactory).createEXIReader();

		Result result = new StreamResult(baosDecXML);
		SAXSource exiSource = new SAXSource(is);
		exiSource.setXMLReader(exiReader);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);
	}

	// System.out.println(baosDecXML.toString());

}
 
Example 14
Source File: DtrMapTestCase.java    From exificient with MIT License 4 votes vote down vote up
public void testMulti2() throws IOException, EXIException, SAXException,
		TransformerException {
	String schemaAsString = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
			+ "  <xs:element name='root'>"
			+ "   <xs:complexType>"
			+ "         <!-- Built-in Type: float -->"
			+ "         <xs:sequence>"
			+ "            <xs:element name='float'  type='xs:float' minOccurs='0' maxOccurs='unbounded' />"
			+ "            <xs:element name='double'  type='xs:double' minOccurs='0' maxOccurs='unbounded' />"
			+ "         </xs:sequence>"
			+ "         <xs:attribute name='float' type='xs:float' use='optional'/>"
			+ "         <xs:attribute name='double' type='xs:double' use='optional'/>"
			+ "    </xs:complexType>"
			+ "  </xs:element>"
			+ "</xs:schema>"
			+ "";

	// float-valid-08.xml
	// <root double="10">
	// <float>-9223372036854775807</float>
	// <float>9223372036854775807</float>
	// <float>9223372036854775808</float>
	// <float>-9223372036854775808</float>
	// <double>4000e-3</double>
	// </root>

	String xmlAsString = "<root double='10'><float>-9223372036854775807</float><float>9223372036854775807</float>"
			+ "  <float>9223372036854775808</float><float>-9223372036854775808</float>"
			+ "<double>4000e-3</double></root>";

	GrammarFactory grammarFactory = GrammarFactory.newInstance();
	Grammars g = grammarFactory.createGrammars(new ByteArrayInputStream(
			schemaAsString.getBytes()));

	// factory with int 2 string mapping
	EXIFactory exiFactory = DefaultEXIFactory.newInstance();
	exiFactory.setGrammars(g);
	/* DTR Map */
	QName type1 = new QName(Constants.XML_SCHEMA_NS_URI, "decimal");
	QName type2 = new QName(Constants.XML_SCHEMA_NS_URI, "decimal");
	QName representation1 = new QName(Constants.W3C_EXI_NS_URI, "double");
	QName representation2 = new QName(Constants.W3C_EXI_NS_URI, "decimal");
	QName[] dtrMapTypes = { type1, type2 };
	QName[] dtrMapRepresentations = { representation1, representation2 };
	exiFactory.setDatatypeRepresentationMap(dtrMapTypes,
			dtrMapRepresentations);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	// encode
	{
		EXIResult exiResult = new EXIResult(exiFactory);
		exiResult.setOutputStream(baos);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(exiResult.getHandler());
		xmlReader.parse(new InputSource(new ByteArrayInputStream(
				xmlAsString.getBytes())));
	}

	ByteArrayOutputStream baosDecXML = new ByteArrayOutputStream();

	// decode
	{
		InputSource is = new InputSource(new ByteArrayInputStream(
				baos.toByteArray()));
		XMLReader exiReader = new SAXFactory(exiFactory).createEXIReader();

		Result result = new StreamResult(baosDecXML);
		SAXSource exiSource = new SAXSource(is);
		exiSource.setXMLReader(exiReader);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);
	}

	// System.out.println(baosDecXML.toString());

}
 
Example 15
Source File: DtrMapTestCase.java    From exificient with MIT License 4 votes vote down vote up
public void testIntegerToString3() throws IOException, EXIException,
		SAXException, TransformerException {
	String schemaAsString = "<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>"
			+ "  <xs:element name='root'>"
			+ "   <xs:complexType>"
			+ "      <xs:sequence>"
			+ "         <xs:element name='int' type='xs:int' />"
			+ "         <xs:element name='myInt' type='myInt'></xs:element>"
			+ "         <xs:element name='anonInt'>"
			+ "            <xs:simpleType>"
			+ "                <xs:restriction base='xs:int'>"
			+ "                </xs:restriction>"
			+ "            </xs:simpleType>"
			+ "         </xs:element>"
			+ "      </xs:sequence>"
			+ "    </xs:complexType>"
			+ "  </xs:element>"
			+ "  <xs:simpleType name='myInt'>"
			+ "    <xs:restriction base='xs:int'></xs:restriction>"
			+ "  </xs:simpleType>" + "</xs:schema>" + "";

	// String xmlAsString = "<root>"
	// + "  <int>12</int>"
	// + "  <myInt>13</myInt>"
	// + "  <anonInt>14</anonInt>"
	// + "</root>";

	// invalid integers but treated as String it should be OK and valid
	String xmlAsString = "<root>" + "  <int>XXX 12 XX</int>"
			+ "  <myInt>YYY 13 YYY</myInt>"
			+ "  <anonInt>ZZZZ 14 ZZZZ</anonInt>" + "</root>";

	GrammarFactory grammarFactory = GrammarFactory.newInstance();
	Grammars g = grammarFactory.createGrammars(new ByteArrayInputStream(
			schemaAsString.getBytes()));

	// factory with int 2 string mapping
	EXIFactory exiFactory = DefaultEXIFactory.newInstance();
	exiFactory.setGrammars(g);
	exiFactory.setFidelityOptions(FidelityOptions.createStrict());
	/* DTR Map */
	QName type = new QName(Constants.XML_SCHEMA_NS_URI, "int");
	QName representation = new QName(Constants.W3C_EXI_NS_URI, "string");
	QName[] dtrMapTypes = { type };
	QName[] dtrMapRepresentations = { representation };
	exiFactory.setDatatypeRepresentationMap(dtrMapTypes,
			dtrMapRepresentations);

	ByteArrayOutputStream baos = new ByteArrayOutputStream();

	// encode
	{
		EXIResult exiResult = new EXIResult(exiFactory);
		exiResult.setOutputStream(baos);
		XMLReader xmlReader = XMLReaderFactory.createXMLReader();
		xmlReader.setContentHandler(exiResult.getHandler());
		xmlReader.parse(new InputSource(new ByteArrayInputStream(
				xmlAsString.getBytes())));
	}

	ByteArrayOutputStream baosDecXML = new ByteArrayOutputStream();

	// decode
	{
		InputSource is = new InputSource(new ByteArrayInputStream(
				baos.toByteArray()));
		XMLReader exiReader = new SAXFactory(exiFactory).createEXIReader();

		Result result = new StreamResult(baosDecXML);
		SAXSource exiSource = new SAXSource(is);
		exiSource.setXMLReader(exiReader);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer transformer = tf.newTransformer();
		transformer.transform(exiSource, result);
	}

	// System.out.println(baosDecXML.toString());

}