Search in sources :

Example 66 with XMLReader

use of org.xml.sax.XMLReader in project spring-framework by spring-projects.

the class AbstractMarshaller method createXmlReader.

/**
	 * Create an {@code XMLReader} that this marshaller will when passed an empty {@code SAXSource}.
	 * @return the XMLReader
	 * @throws SAXException if thrown by JAXP methods
	 */
// on JDK 9
@SuppressWarnings("deprecation")
protected XMLReader createXmlReader() throws SAXException {
    XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
    xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
    if (!isProcessExternalEntities()) {
        xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
    }
    return xmlReader;
}
Also used : XMLReader(org.xml.sax.XMLReader)

Example 67 with XMLReader

use of org.xml.sax.XMLReader in project spring-framework by spring-projects.

the class CastorUnmarshallerTests method unmarshalSaxSourceWithXmlOptions.

@Test
public void unmarshalSaxSourceWithXmlOptions() throws Exception {
    final AtomicReference<XMLReader> result = new AtomicReference<>();
    CastorMarshaller marshaller = new CastorMarshaller() {

        @Override
        protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) {
            result.set(xmlReader);
            return null;
        }
    };
    // 1. external-general-entities and dtd support disabled (default)
    marshaller.unmarshal(new SAXSource(new InputSource("1")));
    assertNotNull(result.get());
    assertEquals(true, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
    // 2. external-general-entities and dtd support enabled
    result.set(null);
    marshaller.setSupportDtd(true);
    marshaller.setProcessExternalEntities(true);
    marshaller.unmarshal(new SAXSource(new InputSource("1")));
    assertNotNull(result.get());
    assertEquals(false, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) AtomicReference(java.util.concurrent.atomic.AtomicReference) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 68 with XMLReader

use of org.xml.sax.XMLReader in project spring-framework by spring-projects.

the class CastorUnmarshallerTests method unmarshalStreamSourceWithXmlOptions.

@Test
public void unmarshalStreamSourceWithXmlOptions() throws Exception {
    final AtomicReference<XMLReader> result = new AtomicReference<>();
    CastorMarshaller marshaller = new CastorMarshaller() {

        @Override
        protected Object unmarshalSaxReader(XMLReader xmlReader, InputSource inputSource) {
            result.set(xmlReader);
            return null;
        }
    };
    // 1. external-general-entities and dtd support disabled (default)
    marshaller.unmarshal(new StreamSource("1"));
    assertNotNull(result.get());
    assertEquals(true, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(false, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
    // 2. external-general-entities and dtd support enabled
    result.set(null);
    marshaller.setSupportDtd(true);
    marshaller.setProcessExternalEntities(true);
    marshaller.unmarshal(new StreamSource("1"));
    assertNotNull(result.get());
    assertEquals(false, result.get().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(true, result.get().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Also used : InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) AtomicReference(java.util.concurrent.atomic.AtomicReference) XMLReader(org.xml.sax.XMLReader) Test(org.junit.Test)

Example 69 with XMLReader

use of org.xml.sax.XMLReader in project robovm by robovm.

the class SAXParserTest method testGetReader.

public void testGetReader() {
    spf = SAXParserFactory.newInstance();
    try {
        XMLReader reader = spf.newSAXParser().getXMLReader();
        assertNotNull(reader);
    } catch (Exception e) {
        throw new RuntimeException("Unexpected exception", e);
    }
}
Also used : XMLReader(org.xml.sax.XMLReader) SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException)

Example 70 with XMLReader

use of org.xml.sax.XMLReader in project robovm by robovm.

the class NamespacedAttributesLookupTest method getStartElements.

public List<String> getStartElements(String xml, final boolean namespace, boolean namespacePrefixes) throws Exception {
    final List<String> result = new ArrayList<String>();
    XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
    reader.setFeature(SAX_PROPERTY_NS, namespace);
    reader.setFeature(SAX_PROPERTY_NS_PREFIXES, namespacePrefixes);
    reader.setContentHandler(new DefaultHandler() {

        @Override
        public final void startElement(String uri, String localName, String qName, Attributes attributes) {
            StringBuilder serialized = new StringBuilder();
            /*
                 * Only supply the uri+localName or qname depending on whether namespaces are
                 * enabled. It's an optional parameter and the RI only supplies one or the other.
                 */
            if (namespace) {
                serialized.append(uri).append(",");
                serialized.append(localName);
            } else {
                serialized.append(qName);
            }
            for (int i = 0; i < attributes.getLength(); i++) {
                serialized.append("\n  ");
                if (namespace) {
                    serialized.append(attributes.getURI(i)).append(",");
                    serialized.append(attributes.getLocalName(i));
                } else {
                    serialized.append(attributes.getQName(i));
                }
            }
            serialized.append("\n  http://bar+c=").append(attributes.getValue("http://bar", "c")).append(",").append("\n  bar:c=").append(attributes.getValue("bar:c")).append("\n");
            result.add(serialized.toString());
        }
    });
    reader.parse(new InputSource(new StringReader(xml)));
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) ArrayList(java.util.ArrayList) Attributes(org.xml.sax.Attributes) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Aggregations

XMLReader (org.xml.sax.XMLReader)234 InputSource (org.xml.sax.InputSource)186 SAXException (org.xml.sax.SAXException)82 IOException (java.io.IOException)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)51 SAXSource (javax.xml.transform.sax.SAXSource)48 SAXParser (javax.xml.parsers.SAXParser)42 StringReader (java.io.StringReader)37 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)35 InputStream (java.io.InputStream)28 ExpatReader (org.apache.harmony.xml.ExpatReader)24 ContentHandler (org.xml.sax.ContentHandler)20 TransformerException (javax.xml.transform.TransformerException)19 DOMSource (javax.xml.transform.dom.DOMSource)18 StreamSource (javax.xml.transform.stream.StreamSource)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileReader (java.io.FileReader)16 InputStreamReader (java.io.InputStreamReader)12 SAXParseException (org.xml.sax.SAXParseException)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10