Search in sources :

Example 21 with SAXSource

use of javax.xml.transform.sax.SAXSource in project robovm by robovm.

the class SourceTreeManager method getXMLReader.

/**
   * This method returns the SAX2 parser to use with the InputSource
   * obtained from this URI.
   * It may return null if any SAX2-conformant XML parser can be used,
   * or if getInputSource() will also return null. The parser must
   * be free for use (i.e.
   * not currently in use for another parse().
   *
   * @param inputSource The value returned from the URIResolver.
   * @return a SAX2 XMLReader to use to resolve the inputSource argument.
   * @param locator The location of the original caller, for diagnostic purposes.
   *
   * @throws TransformerException if the reader can not be created.
   */
public static XMLReader getXMLReader(Source inputSource, SourceLocator locator) throws TransformerException {
    try {
        XMLReader reader = (inputSource instanceof SAXSource) ? ((SAXSource) inputSource).getXMLReader() : null;
        if (null == reader) {
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser();
                reader = jaxpParser.getXMLReader();
            } catch (javax.xml.parsers.ParserConfigurationException ex) {
                throw new org.xml.sax.SAXException(ex);
            } catch (javax.xml.parsers.FactoryConfigurationError ex1) {
                throw new org.xml.sax.SAXException(ex1.toString());
            } catch (NoSuchMethodError ex2) {
            } catch (AbstractMethodError ame) {
            }
            if (null == reader)
                reader = XMLReaderFactory.createXMLReader();
        }
        try {
            reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        } catch (org.xml.sax.SAXException se) {
        // What can we do?
        // TODO: User diagnostics.
        }
        return reader;
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se.getMessage(), locator, se);
    }
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 22 with SAXSource

use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.

the class Jaxb2Marshaller method loadSchema.

// on JDK 9
@SuppressWarnings("deprecation")
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
    if (logger.isDebugEnabled()) {
        logger.debug("Setting validation schema to " + StringUtils.arrayToCommaDelimitedString(this.schemaResources));
    }
    Assert.notEmpty(resources, "No resources given");
    Assert.hasLength(schemaLanguage, "No schema language provided");
    Source[] schemaSources = new Source[resources.length];
    XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    for (int i = 0; i < resources.length; i++) {
        Assert.notNull(resources[i], "Resource is null");
        Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
        InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
        schemaSources[i] = new SAXSource(xmlReader, inputSource);
    }
    SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
    if (this.schemaResourceResolver != null) {
        schemaFactory.setResourceResolver(this.schemaResourceResolver);
    }
    return schemaFactory.newSchema(schemaSources);
}
Also used : SchemaFactory(javax.xml.validation.SchemaFactory) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) DOMSource(javax.xml.transform.dom.DOMSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) DataSource(javax.activation.DataSource) XMLReader(org.xml.sax.XMLReader)

Example 23 with SAXSource

use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method unmarshalSaxSourceWithXmlOptions.

// SPR-10806
@Test
public void unmarshalSaxSourceWithXmlOptions() throws Exception {
    final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {

        @Override
        protected javax.xml.bind.Unmarshaller createUnmarshaller() {
            return unmarshaller;
        }
    };
    // 1. external-general-entities and dtd support disabled (default)
    marshaller.unmarshal(new SAXSource(new InputSource("1")));
    ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class);
    verify(unmarshaller).unmarshal(sourceCaptor.capture());
    SAXSource result = sourceCaptor.getValue();
    assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
    // 2. external-general-entities and dtd support enabled
    reset(unmarshaller);
    marshaller.setProcessExternalEntities(true);
    marshaller.setSupportDtd(true);
    marshaller.unmarshal(new SAXSource(new InputSource("1")));
    verify(unmarshaller).unmarshal(sourceCaptor.capture());
    result = sourceCaptor.getValue();
    assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) Test(org.junit.Test)

Example 24 with SAXSource

use of javax.xml.transform.sax.SAXSource in project spring-framework by spring-projects.

the class Jaxb2MarshallerTests method unmarshalStreamSourceWithXmlOptions.

// SPR-10806
@Test
public void unmarshalStreamSourceWithXmlOptions() throws Exception {
    final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {

        @Override
        protected javax.xml.bind.Unmarshaller createUnmarshaller() {
            return unmarshaller;
        }
    };
    // 1. external-general-entities and dtd support disabled (default)
    marshaller.unmarshal(new StreamSource("1"));
    ArgumentCaptor<SAXSource> sourceCaptor = ArgumentCaptor.forClass(SAXSource.class);
    verify(unmarshaller).unmarshal(sourceCaptor.capture());
    SAXSource result = sourceCaptor.getValue();
    assertEquals(true, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(false, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
    // 2. external-general-entities and dtd support enabled
    reset(unmarshaller);
    marshaller.setProcessExternalEntities(true);
    marshaller.setSupportDtd(true);
    marshaller.unmarshal(new StreamSource("1"));
    verify(unmarshaller).unmarshal(sourceCaptor.capture());
    result = sourceCaptor.getValue();
    assertEquals(false, result.getXMLReader().getFeature("http://apache.org/xml/features/disallow-doctype-decl"));
    assertEquals(true, result.getXMLReader().getFeature("http://xml.org/sax/features/external-general-entities"));
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) StreamSource(javax.xml.transform.stream.StreamSource) Test(org.junit.Test)

Example 25 with SAXSource

use of javax.xml.transform.sax.SAXSource 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)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10