Search in sources :

Example 56 with TransformerException

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

the class SerializerUtils method processNSDecls.

/**
     * Copy <KBD>xmlns:</KBD> attributes in if not already in scope.
     *
     * As a quick hack to support ClonerToResultTree, this can also be used
     * to copy an individual namespace node.
     *
     * @param src Source Node
     * NEEDSDOC @param type
     * NEEDSDOC @param dtm
     *
     * @throws TransformerException
     */
public static void processNSDecls(SerializationHandler handler, int src, int type, DTM dtm) throws TransformerException {
    try {
        if (type == DTM.ELEMENT_NODE) {
            for (int namespace = dtm.getFirstNamespaceNode(src, true); DTM.NULL != namespace; namespace = dtm.getNextNamespaceNode(src, namespace, true)) {
                // String prefix = dtm.getPrefix(namespace);
                String prefix = dtm.getNodeNameX(namespace);
                String desturi = handler.getNamespaceURIFromPrefix(prefix);
                //            String desturi = getURI(prefix);
                String srcURI = dtm.getNodeValue(namespace);
                if (!srcURI.equalsIgnoreCase(desturi)) {
                    handler.startPrefixMapping(prefix, srcURI, false);
                }
            }
        } else if (type == DTM.NAMESPACE_NODE) {
            String prefix = dtm.getNodeNameX(src);
            // Brian M. - some changes here to get desturi
            String desturi = handler.getNamespaceURIFromPrefix(prefix);
            String srcURI = dtm.getNodeValue(src);
            if (!srcURI.equalsIgnoreCase(desturi)) {
                handler.startPrefixMapping(prefix, srcURI, false);
            }
        }
    } catch (org.xml.sax.SAXException se) {
        throw new TransformerException(se);
    }
}
Also used : SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException)

Example 57 with TransformerException

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

the class StylesheetHandler method warning.

/**
   * Receive notification of a XSLT processing warning.
   *
   * @param e The warning information encoded as an exception.
   *
   * @throws org.xml.sax.SAXException that wraps a
   * {@link javax.xml.transform.TransformerException} if the current
   * {@link javax.xml.transform.ErrorListener#warning}
   * method chooses to flag this condition as an error.
   */
public void warning(org.xml.sax.SAXParseException e) throws org.xml.sax.SAXException {
    String formattedMsg = e.getMessage();
    SAXSourceLocator locator = getLocator();
    ErrorListener handler = m_stylesheetProcessor.getErrorListener();
    try {
        handler.warning(new TransformerException(formattedMsg, locator));
    } catch (TransformerException te) {
        throw new org.xml.sax.SAXException(te);
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 58 with TransformerException

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

the class StylesheetHandler method error.

/**
   * Tell the user of an error, and probably throw an
   * exception.
   *
   * @param msg An error message.
   * @param e An error which the SAXException should wrap.
   *
   * @throws org.xml.sax.SAXException that wraps a
   * {@link javax.xml.transform.TransformerException} if the current
   * {@link javax.xml.transform.ErrorListener#error}
   * method chooses to flag this condition as an error.
   * @xsl.usage internal
   */
protected void error(String msg, Exception e) throws org.xml.sax.SAXException {
    SAXSourceLocator locator = getLocator();
    ErrorListener handler = m_stylesheetProcessor.getErrorListener();
    TransformerException pe;
    if (!(e instanceof TransformerException)) {
        pe = (null == e) ? new TransformerException(msg, locator) : new TransformerException(msg, locator, e);
    } else
        pe = (TransformerException) e;
    if (null != handler) {
        try {
            handler.error(pe);
        } catch (TransformerException te) {
            throw new org.xml.sax.SAXException(te);
        }
    } else
        throw new org.xml.sax.SAXException(pe);
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 59 with TransformerException

use of javax.xml.transform.TransformerException 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 60 with TransformerException

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

the class XSLTAttributeDef method processENUM_OR_PQNAME.

/**
   * Process an attribute string of that is either an enumerated value or a qname-but-not-ncname.
   * Returns an AVT, if this attribute support AVT; otherwise returns int or qname.
   *
   * @param handler non-null reference to current StylesheetHandler that is constructing the Templates.
   * @param uri The Namespace URI, or an empty string.
   * @param name The local name (without prefix), or empty string if not namespace processing.
   * @param rawName The qualified name (with prefix).
   * @param value non-null string that represents an enumerated value that is
   * valid for this element.
   * @param owner
   *
   * @return AVT if attribute supports AVT. An Integer representation of the enumerated value if
   *         attribute does not support AVT and an enumerated value was used.  Otherwise a qname
   *         is returned.
   */
Object processENUM_OR_PQNAME(StylesheetHandler handler, String uri, String name, String rawName, String value, ElemTemplateElement owner) throws org.xml.sax.SAXException {
    Object objToReturn = null;
    if (getSupportsAVT()) {
        try {
            AVT avt = new AVT(handler, uri, name, rawName, value, owner);
            if (!avt.isSimple())
                return avt;
            else
                objToReturn = avt;
        } catch (TransformerException te) {
            throw new org.xml.sax.SAXException(te);
        }
    }
    // An avt wasn't used.
    int key = this.getEnum(value);
    if (key != StringToIntTable.INVALID_KEY) {
        if (objToReturn == null)
            objToReturn = new Integer(key);
    } else // enum not used.  Validate qname-but-not-ncname.
    {
        try {
            QName qname = new QName(value, handler, true);
            if (objToReturn == null)
                objToReturn = qname;
            if (qname.getPrefix() == null) {
                StringBuffer enumNamesList = getListOfEnums();
                enumNamesList.append(" <qname-but-not-ncname>");
                handleError(handler, XSLTErrorResources.INVALID_ENUM, new Object[] { name, value, enumNamesList.toString() }, null);
                return null;
            }
        } catch (IllegalArgumentException ie) {
            StringBuffer enumNamesList = getListOfEnums();
            enumNamesList.append(" <qname-but-not-ncname>");
            handleError(handler, XSLTErrorResources.INVALID_ENUM, new Object[] { name, value, enumNamesList.toString() }, ie);
            return null;
        } catch (RuntimeException re) {
            StringBuffer enumNamesList = getListOfEnums();
            enumNamesList.append(" <qname-but-not-ncname>");
            handleError(handler, XSLTErrorResources.INVALID_ENUM, new Object[] { name, value, enumNamesList.toString() }, re);
            return null;
        }
    }
    return objToReturn;
}
Also used : QName(org.apache.xml.utils.QName) TransformerException(javax.xml.transform.TransformerException) AVT(org.apache.xalan.templates.AVT)

Aggregations

TransformerException (javax.xml.transform.TransformerException)808 Transformer (javax.xml.transform.Transformer)364 StreamResult (javax.xml.transform.stream.StreamResult)362 DOMSource (javax.xml.transform.dom.DOMSource)311 IOException (java.io.IOException)277 TransformerFactory (javax.xml.transform.TransformerFactory)184 Document (org.w3c.dom.Document)161 StringWriter (java.io.StringWriter)159 SAXException (org.xml.sax.SAXException)157 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)156 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)131 Source (javax.xml.transform.Source)100 StreamSource (javax.xml.transform.stream.StreamSource)94 Element (org.w3c.dom.Element)91 DocumentBuilder (javax.xml.parsers.DocumentBuilder)83 File (java.io.File)74 Node (org.w3c.dom.Node)65 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)64 ByteArrayOutputStream (java.io.ByteArrayOutputStream)62 StringReader (java.io.StringReader)59