Search in sources :

Example 1 with URIResolver

use of javax.xml.transform.URIResolver in project robovm by robovm.

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 2 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(expected = LionEngineException.class)
public 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");
        new Xml(Medias.create("normalize.xml")).save(output);
        Assert.assertTrue(output.getFile().delete());
    } finally {
        field.set(DocumentFactory.class, old);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Media(com.b3dgs.lionengine.Media) 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.Test)

Example 3 with URIResolver

use of javax.xml.transform.URIResolver in project webtools.sourceediting by eclipse.

the class DebugRunner method main.

/**
 * Expected arguments:
 *
 * <ol>
 * <li>the class name of the invoker
 * <li>the file name of the XML launch configuration file
 * <li>the URL of the source document
 * <li>the file of the output document
 * <li>not used (anything)
 * <li>the class name of the <code>IXSLDebugger</code> instance
 * <li>the port used for requests
 * <li>the port used for debug events
 * <li>the port used for generate events
 * </ol>
 *
 * @param args
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IllegalAccessException
 * @throws InstantiationException
 * @throws ClassNotFoundException
 */
public static void main(String[] args) throws SAXException, ParserConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException {
    // $NON-NLS-1$//$NON-NLS-2$
    log.info("javax.xml.transform.TransformerFactory=" + System.getProperty("javax.xml.transform.TransformerFactory"));
    // $NON-NLS-1$//$NON-NLS-2$
    log.info("java.endorsed.dirs=" + System.getProperty("java.endorsed.dirs"));
    String invokerClassName = args[0];
    File launchFile = new File(args[1]);
    String src = args[2];
    String target = args[3];
    String debuggerClassName = args[5];
    // $NON-NLS-1$
    log.info("src: " + src);
    // $NON-NLS-1$
    log.info("target: " + target);
    // $NON-NLS-1$
    log.info("launchFile: " + launchFile);
    // $NON-NLS-1$
    log.info("debugger: " + debuggerClassName);
    DebugRunner debugRunner = null;
    try {
        final IXSLDebugger debugger = createDebugger(debuggerClassName);
        // create the invoker
        IProcessorInvoker invoker = new JAXPSAXProcessorInvoker() {

            @Override
            protected TransformerFactory createTransformerFactory() {
                TransformerFactory tFactory = super.createTransformerFactory();
                debugger.setTransformerFactory(tFactory);
                return tFactory;
            }

            @Override
            public void addStylesheet(URL stylesheet, Map parameters, Properties outputProperties, URIResolver resolver) throws TransformerConfigurationException {
                InputSource inputsource = new InputSource(stylesheet.toString());
                // if required in future, parse the document with line
                // numbers (to get the end line numbers)
                // XMLReaderWrapper reader = new
                // XMLReaderWrapper(createReader());
                // SAXSource source = new SAXSource(reader,inputsource);
                addStylesheet(new SAXSource(inputsource), resolver, parameters, outputProperties);
            }

            @Override
            protected Transformer addStylesheet(Source source, URIResolver resolver, Map parameters, Properties outputProperties) throws TransformerConfigurationException {
                Transformer transformer = super.addStylesheet(source, resolver, parameters, outputProperties);
                debugger.addTransformer(transformer);
                return transformer;
            }
        };
        if (args.length == 9) {
            int requestPort = Integer.parseInt(args[6]);
            int eventPort = Integer.parseInt(args[7]);
            int generatePort = Integer.parseInt(args[8]);
            // $NON-NLS-1$
            log.debug("requestPort: " + requestPort);
            // $NON-NLS-1$
            log.debug("eventPort: " + eventPort);
            // $NON-NLS-1$
            log.debug("generatePort: " + generatePort);
            try {
                debugRunner = new DebugRunner(requestPort, eventPort, generatePort);
            } catch (Exception e) {
                handleFatalError("Could not instantiate invoker: " + invokerClassName, // $NON-NLS-1$
                e);
            }
        } else {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            debugRunner = new DebugRunner(br, new PrintWriter(System.out), new PrintWriter(System.err), null);
            // $NON-NLS-1$
            System.out.println("xsl>");
        }
        PipelineDefinition pipeline = new PipelineDefinition(launchFile);
        pipeline.configure(invoker);
        debugger.setInvoker(invoker);
        debugger.setSource(new URL(src));
        debugger.setTarget(new FileWriter(new File(target)));
        debugRunner.loop(debugger);
    } catch (IOException e) {
        handleFatalError(e.getMessage(), e);
    } catch (TransformationException e) {
        handleFatalError(e.getMessage(), e);
    } catch (ConfigurationException e) {
        handleFatalError(e.getMessage(), e);
    } finally {
        if (debugRunner != null) {
            try {
                debugRunner.dispose();
            } catch (IOException e) {
                handleFatalError(e.getMessage(), e);
            }
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) FileWriter(java.io.FileWriter) URIResolver(javax.xml.transform.URIResolver) Properties(java.util.Properties) URL(java.net.URL) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) IProcessorInvoker(org.eclipse.wst.xsl.jaxp.debug.invoker.IProcessorInvoker) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ConfigurationException(org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) PrintWriter(java.io.PrintWriter) TransformationException(org.eclipse.wst.xsl.jaxp.debug.invoker.TransformationException) TransformerFactory(javax.xml.transform.TransformerFactory) InputStreamReader(java.io.InputStreamReader) PipelineDefinition(org.eclipse.wst.xsl.jaxp.debug.invoker.PipelineDefinition) IOException(java.io.IOException) TransformationException(org.eclipse.wst.xsl.jaxp.debug.invoker.TransformationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ConfigurationException(org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) JAXPSAXProcessorInvoker(org.eclipse.wst.xsl.jaxp.debug.invoker.internal.JAXPSAXProcessorInvoker) SAXSource(javax.xml.transform.sax.SAXSource) BufferedReader(java.io.BufferedReader) File(java.io.File) Map(java.util.Map)

Example 4 with URIResolver

use of javax.xml.transform.URIResolver in project webtools.sourceediting by eclipse.

the class PipelineDefinition method configure.

/**
 * Configure the invoker from this.
 *
 * @param invoker
 *            the invoker to configure
 * @throws ConfigurationException
 *             if an exception occurs during configuration
 */
public void configure(IProcessorInvoker invoker) throws ConfigurationException {
    Map<String, Object> attVals = new ConcurrentHashMap<String, Object>();
    for (Iterator<TypedValue> iter = attributes.iterator(); iter.hasNext(); ) {
        TypedValue att = iter.next();
        Object value;
        try {
            value = att.createValue();
        } catch (CreationException e) {
            throw new ConfigurationException(e.getMessage(), e);
        }
        attVals.put(att.name, value);
    }
    invoker.setAttributes(attVals);
    for (Iterator<TransformDefinition> iter = transformDefs.iterator(); iter.hasNext(); ) {
        TransformDefinition tdef = iter.next();
        Map<String, Object> params = setParams(tdef);
        URL url = getStyleSheetURL(tdef);
        Properties properties = tdef.getOutputProperties();
        URIResolver resolver = getResolver(tdef);
        addStyleSheet(invoker, tdef, params, url, properties, resolver);
    }
}
Also used : URIResolver(javax.xml.transform.URIResolver) CreationException(org.eclipse.wst.xsl.jaxp.debug.invoker.internal.CreationException) Properties(java.util.Properties) URL(java.net.URL) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ConfigurationException(org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with URIResolver

use of javax.xml.transform.URIResolver in project webtools.sourceediting by eclipse.

the class PipelineDefinition method getResolver.

private URIResolver getResolver(TransformDefinition tdef) throws ConfigurationException {
    URIResolver resolver = null;
    if (tdef.getResolverClass() != null) {
        try {
            Class resolverClass = Class.forName(tdef.getResolverClass());
            resolver = (URIResolver) resolverClass.newInstance();
        } catch (InstantiationException e) {
        } catch (IllegalAccessException e) {
            throw new ConfigurationException(Messages.getString("PipelineDefinition.10") + tdef.getResolverClass(), // $NON-NLS-1$
            null);
        } catch (ClassNotFoundException e) {
        }
    }
    return resolver;
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ConfigurationException(org.eclipse.wst.xsl.jaxp.debug.invoker.internal.ConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) URIResolver(javax.xml.transform.URIResolver)

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