Search in sources :

Example 1 with TemplatesHandler

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

the class TransformerFactoryImpl method newTemplates.

/**
   * Process the source into a Templates object, which is likely
   * a compiled representation of the source. This Templates object
   * may then be used concurrently across multiple threads.  Creating
   * a Templates object allows the TransformerFactory to do detailed
   * performance optimization of transformation instructions, without
   * penalizing runtime transformation.
   *
   * @param source An object that holds a URL, input stream, etc.
   * @return A Templates object capable of being used for transformation purposes.
   *
   * @throws TransformerConfigurationException May throw this during the parse when it
   *            is constructing the Templates object and fails.
   */
public Templates newTemplates(Source source) throws TransformerConfigurationException {
    String baseID = source.getSystemId();
    if (null != baseID) {
        baseID = SystemIDResolver.getAbsoluteURI(baseID);
    }
    if (source instanceof DOMSource) {
        DOMSource dsource = (DOMSource) source;
        Node node = dsource.getNode();
        if (null != node)
            return processFromNode(node, baseID);
        else {
            String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
            throw new IllegalArgumentException(messageStr);
        }
    }
    TemplatesHandler builder = newTemplatesHandler();
    builder.setSystemId(baseID);
    try {
        InputSource isource = SAXSource.sourceToInputSource(source);
        isource.setSystemId(baseID);
        XMLReader reader = null;
        if (source instanceof SAXSource)
            reader = ((SAXSource) source).getXMLReader();
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (m_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 you set the namespaces to true, we'll end up getting double 
        // xmlns attributes.  Needs to be fixed.  -sb
        // reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        reader.setContentHandler(builder);
        reader.parse(isource);
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(se.getMessage(), se);
        }
    } catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
                return null;
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(e.getMessage(), e);
        }
    }
    return builder.getTemplates();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 2 with TemplatesHandler

use of javax.xml.transform.sax.TemplatesHandler in project j2objc by google.

the class TransformerFactoryImpl method processFromNode.

public javax.xml.transform.Templates processFromNode(Node node) throws TransformerConfigurationException {
    try {
        TemplatesHandler builder = newTemplatesHandler();
        TreeWalker walker = new TreeWalker(builder, new org.apache.xml.utils.DOM2Helper(), builder.getSystemId());
        walker.traverse(node);
        return builder.getTemplates();
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            // se.printStackTrace();
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se);
        //"processFromNode failed", se);
        }
    } catch (TransformerConfigurationException tce) {
        // Assume it's already been reported to the error listener.
        throw tce;
    }/* catch (TransformerException tce)
    {
      // Assume it's already been reported to the error listener.
      throw new TransformerConfigurationException(tce.getMessage(), tce);
    }*/
     catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            //"processFromNode failed",
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e);
        //e);
        }
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TreeWalker(org.apache.xml.utils.TreeWalker) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Example 3 with TemplatesHandler

use of javax.xml.transform.sax.TemplatesHandler in project sling by apache.

the class SlingTransformer method createTransformerHandler.

private TransformerHandler createTransformerHandler() throws Exception {
    SAXTransformerFactory transformerFactory = (SAXTransformerFactory) TransformerFactory.newInstance();
    TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler();
    XMLReader xmlReader = XMLReaderFactory.createXMLReader();
    xmlReader.setContentHandler(templatesHandler);
    InputSource inputSource = new InputSource(getXsltSource());
    xmlReader.parse(inputSource);
    // Create transformer handler
    final TransformerHandler handler = transformerFactory.newTransformerHandler(templatesHandler.getTemplates());
    return handler;
}
Also used : InputSource(org.xml.sax.InputSource) TransformerHandler(javax.xml.transform.sax.TransformerHandler) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) XMLReader(org.xml.sax.XMLReader)

Example 4 with TemplatesHandler

use of javax.xml.transform.sax.TemplatesHandler in project j2objc by google.

the class TransformerFactoryImpl method newTemplates.

/**
   * Process the source into a Templates object, which is likely
   * a compiled representation of the source. This Templates object
   * may then be used concurrently across multiple threads.  Creating
   * a Templates object allows the TransformerFactory to do detailed
   * performance optimization of transformation instructions, without
   * penalizing runtime transformation.
   *
   * @param source An object that holds a URL, input stream, etc.
   * @return A Templates object capable of being used for transformation purposes.
   *
   * @throws TransformerConfigurationException May throw this during the parse when it
   *            is constructing the Templates object and fails.
   */
public Templates newTemplates(Source source) throws TransformerConfigurationException {
    String baseID = source.getSystemId();
    if (null != baseID) {
        baseID = SystemIDResolver.getAbsoluteURI(baseID);
    }
    if (source instanceof DOMSource) {
        DOMSource dsource = (DOMSource) source;
        Node node = dsource.getNode();
        if (null != node)
            return processFromNode(node, baseID);
        else {
            String messageStr = XSLMessages.createMessage(XSLTErrorResources.ER_ILLEGAL_DOMSOURCE_INPUT, null);
            throw new IllegalArgumentException(messageStr);
        }
    }
    TemplatesHandler builder = newTemplatesHandler();
    builder.setSystemId(baseID);
    try {
        InputSource isource = SAXSource.sourceToInputSource(source);
        isource.setSystemId(baseID);
        XMLReader reader = null;
        if (source instanceof SAXSource)
            reader = ((SAXSource) source).getXMLReader();
        if (null == reader) {
            // Use JAXP1.1 ( if possible )
            try {
                javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance();
                factory.setNamespaceAware(true);
                if (m_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 you set the namespaces to true, we'll end up getting double 
        // xmlns attributes.  Needs to be fixed.  -sb
        // reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        reader.setContentHandler(builder);
        reader.parse(isource);
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(se.getMessage(), se);
        }
    } catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
                return null;
            } catch (TransformerConfigurationException ex1) {
                throw ex1;
            } catch (TransformerException ex1) {
                throw new TransformerConfigurationException(ex1);
            }
        } else {
            throw new TransformerConfigurationException(e.getMessage(), e);
        }
    }
    return builder.getTemplates();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 5 with TemplatesHandler

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

the class TransformerFactoryImpl method processFromNode.

public javax.xml.transform.Templates processFromNode(Node node) throws TransformerConfigurationException {
    try {
        TemplatesHandler builder = newTemplatesHandler();
        TreeWalker walker = new TreeWalker(builder, new org.apache.xml.utils.DOM2Helper(), builder.getSystemId());
        walker.traverse(node);
        return builder.getTemplates();
    } catch (org.xml.sax.SAXException se) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(se));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            // se.printStackTrace();
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), se);
        //"processFromNode failed", se);
        }
    } catch (TransformerConfigurationException tce) {
        // Assume it's already been reported to the error listener.
        throw tce;
    }/* catch (TransformerException tce)
    {
      // Assume it's already been reported to the error listener.
      throw new TransformerConfigurationException(tce.getMessage(), tce);
    }*/
     catch (Exception e) {
        if (m_errorListener != null) {
            try {
                m_errorListener.fatalError(new TransformerException(e));
            } catch (TransformerConfigurationException ex) {
                throw ex;
            } catch (TransformerException ex) {
                throw new TransformerConfigurationException(ex);
            }
            return null;
        } else {
            //"processFromNode failed",
            throw new TransformerConfigurationException(XSLMessages.createMessage(XSLTErrorResources.ER_PROCESSFROMNODE_FAILED, null), e);
        //e);
        }
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TreeWalker(org.apache.xml.utils.TreeWalker) TemplatesHandler(javax.xml.transform.sax.TemplatesHandler) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) StopParseException(org.apache.xml.utils.StopParseException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Aggregations

TemplatesHandler (javax.xml.transform.sax.TemplatesHandler)5 IOException (java.io.IOException)4 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)4 TransformerException (javax.xml.transform.TransformerException)4 StopParseException (org.apache.xml.utils.StopParseException)4 InputSource (org.xml.sax.InputSource)3 XMLReader (org.xml.sax.XMLReader)3 DOMSource (javax.xml.transform.dom.DOMSource)2 SAXSource (javax.xml.transform.sax.SAXSource)2 TreeWalker (org.apache.xml.utils.TreeWalker)2 Node (org.w3c.dom.Node)2 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)1 TransformerHandler (javax.xml.transform.sax.TransformerHandler)1