Search in sources :

Example 11 with URIResolver

use of javax.xml.transform.URIResolver in project j2objc by google.

the class ProcessorInclude method getSourceFromUriResolver.

/**
 * Get the Source object for the included or imported stylesheet module
 * obtained from the user's URIResolver, if there is no user provided
 * URIResolver null is returned.
 */
private Source getSourceFromUriResolver(StylesheetHandler handler) throws TransformerException {
    Source s = null;
    TransformerFactoryImpl processor = handler.getStylesheetProcessor();
    URIResolver uriresolver = processor.getURIResolver();
    if (uriresolver != null) {
        String href = getHref();
        String base = handler.getBaseIdentifier();
        s = uriresolver.resolve(href, base);
    }
    return s;
}
Also used : URIResolver(javax.xml.transform.URIResolver) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource)

Example 12 with URIResolver

use of javax.xml.transform.URIResolver in project j2objc by google.

the class ProcessorInclude method parse.

/**
 * Set off a new parse for an included or imported stylesheet.  This will
 * set the {@link StylesheetHandler} to a new state, and recurse in with
 * a new set of parse events.  Once this function returns, the state of
 * the StylesheetHandler should be restored.
 *
 * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
 * @param uri The Namespace URI, which should be the XSLT namespace.
 * @param localName The local name (without prefix), which should be "include" or "import".
 * @param rawName The qualified name (with prefix).
 * @param attributes The list of attributes on the xsl:include or xsl:import element.
 *
 * @throws org.xml.sax.SAXException Any SAX exception, possibly
 *            wrapping another exception.
 */
protected void parse(StylesheetHandler handler, String uri, String localName, String rawName, Attributes attributes) throws org.xml.sax.SAXException {
    TransformerFactoryImpl processor = handler.getStylesheetProcessor();
    URIResolver uriresolver = processor.getURIResolver();
    try {
        Source source = null;
        if (null != uriresolver) {
            // There is a user provided URI resolver.
            // At the startElement() call we would
            // have tried to obtain a Source from it
            // which we now retrieve
            source = handler.peekSourceFromURIResolver();
            if (null != source && source instanceof DOMSource) {
                Node node = ((DOMSource) source).getNode();
                // There is a user provided URI resolver.
                // At the startElement() call we would
                // have already pushed the system ID, obtained
                // from either the source.getSystemId(), if non-null
                // or from SystemIDResolver.getAbsoluteURI() as a backup
                // which we now retrieve.
                String systemId = handler.peekImportURL();
                // stylesheet module onto the stack.
                if (systemId != null)
                    handler.pushBaseIndentifier(systemId);
                TreeWalker walker = new TreeWalker(handler, new org.apache.xml.utils.DOM2Helper(), systemId);
                try {
                    walker.traverse(node);
                } catch (org.xml.sax.SAXException se) {
                    throw new TransformerException(se);
                }
                if (systemId != null)
                    handler.popBaseIndentifier();
                return;
            }
        }
        if (null == source) {
            String absURL = SystemIDResolver.getAbsoluteURI(getHref(), handler.getBaseIdentifier());
            source = new StreamSource(absURL);
        }
        // possible callback to a class that over-rides this method.
        source = processSource(handler, source);
        XMLReader reader = null;
        if (source instanceof SAXSource) {
            SAXSource saxSource = (SAXSource) source;
            // may be null
            reader = saxSource.getXMLReader();
        }
        InputSource inputSource = SAXSource.sourceToInputSource(source);
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (handler.getStylesheetProcessor().isSecureProcessing()) {
                    try {
                        factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
                    } catch (org.xml.sax.SAXException se) {
                    }
                }
                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();
        if (null != reader) {
            reader.setContentHandler(handler);
            // Push the absolute URI of the included/imported
            // stylesheet module onto the stack.
            handler.pushBaseIndentifier(inputSource.getSystemId());
            try {
                reader.parse(inputSource);
            } finally {
                handler.popBaseIndentifier();
            }
        }
    } catch (IOException ioe) {
        handler.error(XSLTErrorResources.ER_IOEXCEPTION, new Object[] { getHref() }, ioe);
    } catch (TransformerException te) {
        handler.error(te.getMessage(), te);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Node(org.w3c.dom.Node) URIResolver(javax.xml.transform.URIResolver) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException) XMLReader(org.xml.sax.XMLReader) StreamSource(javax.xml.transform.stream.StreamSource) TreeWalker(org.apache.xml.utils.TreeWalker) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource)

Example 13 with URIResolver

use of javax.xml.transform.URIResolver in project lionengine by b3dgs.

the class XmlTest method testTransformerError.

/**
 * Test transformer error.
 *
 * @throws IllegalArgumentException If error.
 * @throws IllegalAccessException If error.
 * @throws NoSuchFieldException If error.
 */
@Test
void testTransformerError() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException {
    final Field field = DocumentFactory.class.getDeclaredField("transformerFactory");
    UtilReflection.setAccessible(field, true);
    final javax.xml.transform.TransformerFactory old = (TransformerFactory) field.get(DocumentFactory.class);
    try {
        field.set(DocumentFactory.class, new javax.xml.transform.TransformerFactory() {

            @Override
            public Transformer newTransformer() throws TransformerConfigurationException {
                throw new TransformerConfigurationException();
            }

            @Override
            public Transformer newTransformer(Source source) throws TransformerConfigurationException {
                return null;
            }

            @Override
            public Templates newTemplates(Source source) throws TransformerConfigurationException {
                return null;
            }

            @Override
            public Source getAssociatedStylesheet(Source source, String media, String title, String charset) throws TransformerConfigurationException {
                return null;
            }

            @Override
            public void setURIResolver(URIResolver resolver) {
            // Mock
            }

            @Override
            public URIResolver getURIResolver() {
                return null;
            }

            @Override
            public void setFeature(String name, boolean value) throws TransformerConfigurationException {
            // Mock
            }

            @Override
            public boolean getFeature(String name) {
                return false;
            }

            @Override
            public void setAttribute(String name, Object value) {
            // Mock
            }

            @Override
            public Object getAttribute(String name) {
                return null;
            }

            @Override
            public void setErrorListener(ErrorListener listener) {
            // Mock
            }

            @Override
            public ErrorListener getErrorListener() {
                return null;
            }
        });
        final Media output = Medias.create("out.xml");
        assertThrows(() -> new Xml(Medias.create("normalize.xml")).save(output), "[out.xml] " + Xml.ERROR_WRITING);
    } finally {
        field.set(DocumentFactory.class, old);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Templates(javax.xml.transform.Templates) URIResolver(javax.xml.transform.URIResolver) Source(javax.xml.transform.Source) ErrorListener(javax.xml.transform.ErrorListener) Field(java.lang.reflect.Field) TransformerFactory(javax.xml.transform.TransformerFactory) Test(org.junit.jupiter.api.Test)

Aggregations

URIResolver (javax.xml.transform.URIResolver)13 Source (javax.xml.transform.Source)8 StreamSource (javax.xml.transform.stream.StreamSource)6 InputSource (org.xml.sax.InputSource)6 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 SAXSource (javax.xml.transform.sax.SAXSource)5 IOException (java.io.IOException)4 Transformer (javax.xml.transform.Transformer)4 TransformerFactory (javax.xml.transform.TransformerFactory)4 DOMSource (javax.xml.transform.dom.DOMSource)4 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)3 TransformerException (javax.xml.transform.TransformerException)3 ConfigurationException (org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException)3 Field (java.lang.reflect.Field)2 URL (java.net.URL)2 Properties (java.util.Properties)2 ErrorListener (javax.xml.transform.ErrorListener)2 Templates (javax.xml.transform.Templates)2 TreeWalker (org.apache.xml.utils.TreeWalker)2 Node (org.w3c.dom.Node)2