Java Code Examples for org.xml.sax.XMLReader#setProperty()

The following examples show how to use org.xml.sax.XMLReader#setProperty() . 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: XML_SAX_StAX_FI.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
    documentSerializer.setOutputStream(finf);

    SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
    reader.setContentHandler(saxTostax);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));

    xml.close();
    finf.close();
}
 
Example 2
Source File: SAXBufferCreator.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
    if (_buffer == null) {
        createBuffer();
    }
    _buffer.setSystemId(systemId);
    reader.setContentHandler(this);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);

    try {
        setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
    } catch (SAXException e) {
    }


    if (systemId != null) {
        InputSource s = new InputSource(systemId);
        s.setByteStream(in);
        reader.parse(s);
    } else {
        reader.parse(new InputSource(in));
    }

    return getXMLStreamBuffer();
}
 
Example 3
Source File: Parser.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Parses a stylesheet and builds the internal abstract syntax tree
 * @param input A SAX2 InputSource can be passed to a SAX reader
 * @return The root of the abstract syntax tree
 */
public SyntaxTreeNode parse(InputSource input) {
    final XMLReader reader = JdkXmlUtils.getXMLReader(_overrideDefaultParser,
            _xsltc.isSecureProcessing());

    JdkXmlUtils.setXMLReaderPropertyIfSupport(reader, XMLConstants.ACCESS_EXTERNAL_DTD,
            _xsltc.getProperty(XMLConstants.ACCESS_EXTERNAL_DTD), true);

    String lastProperty = "";
    try {
        XMLSecurityManager securityManager =
                (XMLSecurityManager) _xsltc.getProperty(XalanConstants.SECURITY_MANAGER);
        for (XMLSecurityManager.Limit limit : XMLSecurityManager.Limit.values()) {
            lastProperty = limit.apiProperty();
            reader.setProperty(lastProperty, securityManager.getLimitValueAsString(limit));
        }
        if (securityManager.printEntityCountInfo()) {
            lastProperty = XalanConstants.JDK_ENTITY_COUNT_INFO;
            reader.setProperty(XalanConstants.JDK_ENTITY_COUNT_INFO, XalanConstants.JDK_YES);
        }
    } catch (SAXException se) {
        XMLSecurityManager.printWarning(reader.getClass().getName(), lastProperty, se);
    }

    return (parse(reader, input));
}
 
Example 4
Source File: XML_SAX_StAX_FI.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
    documentSerializer.setOutputStream(finf);

    SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
    reader.setContentHandler(saxTostax);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));

    xml.close();
    finf.close();
}
 
Example 5
Source File: EntityCharacterEventOrder.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
    public static void main(String[] args) {
        TestRunner.run(JDK6770436Test.class);
    }
*/
    @Test
    public void entityCallbackOrderJava() throws SAXException, IOException {
        final String input = "<element> &amp; some more text</element>";

        final MockContentHandler handler = new MockContentHandler();
        final XMLReader xmlReader = XMLReaderFactory.createXMLReader();

        xmlReader.setContentHandler(handler);
        xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);

        xmlReader.parse(new InputSource(new StringReader(input)));

        final List<String> events = handler.getEvents();
        printEvents(events);
        assertCallbackOrder(events); //regression from JDK5
    }
 
Example 6
Source File: XML_SAX_StAX_FI.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
    documentSerializer.setOutputStream(finf);

    SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
    reader.setContentHandler(saxTostax);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));

    xml.close();
    finf.close();
}
 
Example 7
Source File: XML_SAX_StAX_FI.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
    documentSerializer.setOutputStream(finf);

    SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
    reader.setContentHandler(saxTostax);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));

    xml.close();
    finf.close();
}
 
Example 8
Source File: XML_SAX_StAX_FI.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    StAXDocumentSerializer documentSerializer = new StAXDocumentSerializer();
    documentSerializer.setOutputStream(finf);

    SAX2StAXWriter saxTostax = new SAX2StAXWriter(documentSerializer);

    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", saxTostax);
    reader.setContentHandler(saxTostax);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));

    xml.close();
    finf.close();
}
 
Example 9
Source File: SAXBufferCreator.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public MutableXMLStreamBuffer create(XMLReader reader, InputStream in, String systemId) throws IOException, SAXException {
    if (_buffer == null) {
        createBuffer();
    }
    _buffer.setSystemId(systemId);
    reader.setContentHandler(this);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, this);

    try {
        setHasInternedStrings(reader.getFeature(Features.STRING_INTERNING_FEATURE));
    } catch (SAXException e) {
    }


    if (systemId != null) {
        InputSource s = new InputSource(systemId);
        s.setByteStream(in);
        reader.parse(s);
    } else {
        reader.parse(new InputSource(in));
    }

    return getXMLStreamBuffer();
}
 
Example 10
Source File: PositionXmlParser.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@NonNull
private static Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom,
                              boolean checkDtd)
        throws ParserConfigurationException, SAXException, IOException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        if (checkDtd) {
            factory.setFeature(NAMESPACE_FEATURE, true);
            factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
            factory.setFeature(PROVIDE_XMLNS_URIS, true);
        } else {
            factory.setFeature(LOAD_EXTERNAL_DTD, false);
        }
        SAXParser parser = factory.newSAXParser();
        DomBuilder handler = new DomBuilder(xml);
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setProperty(
                "http://xml.org/sax/properties/lexical-handler",
                handler
        );
        parser.parse(input, handler);
        return handler.getDocument();
    } catch (SAXException e) {
        if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
            // Byte order mark in the string? Skip it. There are many markers
            // (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
            // just skip those up to the XML prolog beginning character, <
            xml = xml.replaceFirst("^([\\W]+)<","<");  //$NON-NLS-1$ //$NON-NLS-2$
            return parse(xml, new InputSource(new StringReader(xml)), false, checkDtd);
        }
        throw e;
    }
}
 
Example 11
Source File: TestSAXEncoder.java    From exificient with MIT License 5 votes vote down vote up
@Override
public void encodeTo(InputStream xmlInput, OutputStream exiOutput)
		throws Exception {
	// XML reader
	XMLReader xmlReader = getXMLReader();

	exiResult.setOutputStream(exiOutput);

	// set EXI as content & lexical handler
	// EXIResult saxResult = new EXIResult(exiOutput, ef);
	xmlReader.setContentHandler(exiResult.getHandler());

	// set LexicalHandler
	xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler",
			exiResult.getLexicalHandler());
	// set DeclHandler
	xmlReader.setProperty(
			"http://xml.org/sax/properties/declaration-handler",
			exiResult.getLexicalHandler());
	// set DTD handler
	xmlReader.setDTDHandler((DTDHandler) exiResult.getHandler());

	if (isFragment) {
		xmlInput = updateInputStreamToFragment(xmlInput);
		xmlReader = updateXMLReaderToFragment(xmlReader);
	}

	xmlReader.parse(new InputSource(xmlInput));
}
 
Example 12
Source File: XMLReaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * XMLReader.setProperty/getProperty for LEXICAL_HANDLER unit test.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void xrProperty05() throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    MyLexicalHandler myLexicalHandler = new MyLexicalHandler();
    xmlReader.setProperty(LEXICAL_HANDLER, myLexicalHandler);
    assertNotNull(xmlReader.getProperty(LEXICAL_HANDLER));
}
 
Example 13
Source File: PositionXmlParser.java    From buck with Apache License 2.0 5 votes vote down vote up
@NonNull
private static Document parse(@NonNull String xml, @NonNull InputSource input, boolean checkBom,
                              boolean checkDtd)
        throws ParserConfigurationException, SAXException, IOException {
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        if (checkDtd) {
            factory.setFeature(NAMESPACE_FEATURE, true);
            factory.setFeature(NAMESPACE_PREFIX_FEATURE, true);
            factory.setFeature(PROVIDE_XMLNS_URIS, true);
        } else {
            factory.setFeature(LOAD_EXTERNAL_DTD, false);
        }
        SAXParser parser = factory.newSAXParser();
        DomBuilder handler = new DomBuilder(xml);
        XMLReader xmlReader = parser.getXMLReader();
        xmlReader.setProperty(
                "http://xml.org/sax/properties/lexical-handler",
                handler
        );
        parser.parse(input, handler);
        return handler.getDocument();
    } catch (SAXException e) {
        if (checkBom && e.getMessage().contains("Content is not allowed in prolog")) {
            // Byte order mark in the string? Skip it. There are many markers
            // (see http://en.wikipedia.org/wiki/Byte_order_mark) so here we'll
            // just skip those up to the XML prolog beginning character, <
            xml = xml.replaceFirst("^([\\W]+)<","<");  //$NON-NLS-1$ //$NON-NLS-2$
            return parse(xml, new InputSource(new StringReader(xml)), false, checkDtd);
        }
        throw e;
    }
}
 
Example 14
Source File: CustomSAXSVGDocumentFactory.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Document createDocument(InputSource is) throws IOException {
    try {
        final XMLReader parser = new SAXParser();

        parser.setContentHandler(this);
        parser.setDTDHandler(this);
        parser.setEntityResolver(this);
        parser.setErrorHandler((errorHandler == null) ? this : errorHandler);

        parser.setFeature("http://xml.org/sax/features/namespaces",
                true);
        parser.setFeature("http://xml.org/sax/features/namespace-prefixes",
                true);
        parser.setFeature("http://xml.org/sax/features/validation",
                isValidating);
        parser.setProperty("http://xml.org/sax/properties/lexical-handler",
                this);
        parser.parse(is);
    } catch (final SAXException e) {
        final Exception ex = e.getException();
        if (ex != null && ex instanceof InterruptedIOException) {
            throw (InterruptedIOException) ex;
        }
        throw new IOException(e.getMessage());
    }

    currentNode = null;
    final Document ret = document;
    document = null;
    locator = null;
    return ret;
}
 
Example 15
Source File: JdkXmlUtils.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sets the XMLReader instance with the specified property if the the
 * property is supported, ignores error if not, issues a warning if so
 * requested.
 *
 * @param reader an XMLReader instance
 * @param property the name of the property
 * @param value the value of the property
 * @param warn a flag indicating whether a warning should be issued
 */
public static void setXMLReaderPropertyIfSupport(XMLReader reader, String property,
        Object value, boolean warn) {
    try {
        reader.setProperty(property, value);
    } catch (SAXNotRecognizedException | SAXNotSupportedException e) {
        if (warn) {
            XMLSecurityManager.printWarning(reader.getClass().getName(),
                    property, e);
        }
    }
}
 
Example 16
Source File: XML_SAX_FI.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void parse(InputStream xml, OutputStream finf, String workingDirectory) throws Exception {
    SAXParser saxParser = getParser();
    SAXDocumentSerializer documentSerializer = getSerializer(finf);

    XMLReader reader = saxParser.getXMLReader();
    reader.setProperty("http://xml.org/sax/properties/lexical-handler", documentSerializer);
    reader.setContentHandler(documentSerializer);

    if (workingDirectory != null) {
        reader.setEntityResolver(createRelativePathResolver(workingDirectory));
    }
    reader.parse(new InputSource(xml));
}
 
Example 17
Source File: MutableXMLStreamBuffer.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}.
 *
 * <p>
 * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
 *
 * <p>
 * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator}
 * and registering associated handlers on the {@link XMLReader}.
 *
 * @param reader
 * The {@link XMLReader} to use for parsing.
 * @param in
 * The {@link InputStream} to be parsed.
 * @param systemId
 * The system ID of the input stream.
 */
public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException {
    reset();
    SAXBufferCreator c = new SAXBufferCreator(this);

    reader.setContentHandler(c);
    reader.setDTDHandler(c);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c);

    c.create(reader, in, systemId);
}
 
Example 18
Source File: MutableXMLStreamBuffer.java    From openjdk-jdk8u with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}.
 *
 * <p>
 * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
 *
 * <p>
 * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator}
 * and registering associated handlers on the {@link XMLReader}.
 *
 * @param reader
 * The {@link XMLReader} to use for parsing.
 * @param in
 * The {@link InputStream} to be parsed.
 * @param systemId
 * The system ID of the input stream.
 */
public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException {
    reset();
    SAXBufferCreator c = new SAXBufferCreator(this);

    reader.setContentHandler(c);
    reader.setDTDHandler(c);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c);

    c.create(reader, in, systemId);
}
 
Example 19
Source File: MutableXMLStreamBuffer.java    From openjdk-jdk9 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}.
 *
 * <p>
 * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
 *
 * <p>
 * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator}
 * and registering associated handlers on the {@link XMLReader}.
 *
 * @param reader
 * The {@link XMLReader} to use for parsing.
 * @param in
 * The {@link InputStream} to be parsed.
 * @param systemId
 * The system ID of the input stream.
 */
public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException {
    reset();
    SAXBufferCreator c = new SAXBufferCreator(this);

    reader.setContentHandler(c);
    reader.setDTDHandler(c);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c);

    c.create(reader, in, systemId);
}
 
Example 20
Source File: MutableXMLStreamBuffer.java    From openjdk-8 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Create contents of a buffer from a {@link XMLReader} and {@link InputStream}.
 *
 * <p>
 * The MutableXMLStreamBuffer is reset (see {@link #reset}) before creation.
 *
 * <p>
 * The MutableXMLStreamBuffer is created by using an instance of {@link SAXBufferCreator}
 * and registering associated handlers on the {@link XMLReader}.
 *
 * @param reader
 * The {@link XMLReader} to use for parsing.
 * @param in
 * The {@link InputStream} to be parsed.
 * @param systemId
 * The system ID of the input stream.
 */
public void createFromXMLReader(XMLReader reader, InputStream in, String systemId) throws SAXException, IOException {
    reset();
    SAXBufferCreator c = new SAXBufferCreator(this);

    reader.setContentHandler(c);
    reader.setDTDHandler(c);
    reader.setProperty(Properties.LEXICAL_HANDLER_PROPERTY, c);

    c.create(reader, in, systemId);
}