Search in sources :

Example 6 with SourceLocator

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

the class DefaultErrorHandler method ensureLocationSet.

public static void ensureLocationSet(TransformerException exception) {
    // SourceLocator locator = exception.getLocator();
    SourceLocator locator = null;
    Throwable cause = exception;
    // Try to find the locator closest to the cause.
    do {
        if (cause instanceof SAXParseException) {
            locator = new SAXSourceLocator((SAXParseException) cause);
        } else if (cause instanceof TransformerException) {
            SourceLocator causeLocator = ((TransformerException) cause).getLocator();
            if (null != causeLocator)
                locator = causeLocator;
        }
        if (cause instanceof TransformerException)
            cause = ((TransformerException) cause).getCause();
        else if (cause instanceof SAXException)
            cause = ((SAXException) cause).getException();
        else
            cause = null;
    } while (null != cause);
    exception.setLocator(locator);
}
Also used : SourceLocator(javax.xml.transform.SourceLocator) SAXParseException(org.xml.sax.SAXParseException) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 7 with SourceLocator

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

the class XPath method error.

/**
   * Tell the user of an error, and probably throw an
   * exception.
   *
   * @param xctxt The XPath runtime context.
   * @param sourceNode Not used.
   * @param msg An error msgkey that corresponds to one of the constants found 
   *            in {@link org.apache.xpath.res.XPATHErrorResources}, which is 
   *            a key for a format string.
   * @param args An array of arguments represented in the format string, which 
   *             may be null.
   *
   * @throws TransformerException if the current ErrorListoner determines to 
   *                              throw an exception.
   */
public void error(XPathContext xctxt, int sourceNode, String msg, Object[] args) throws javax.xml.transform.TransformerException {
    String fmsg = XSLMessages.createXPATHMessage(msg, args);
    ErrorListener ehandler = xctxt.getErrorListener();
    if (null != ehandler) {
        ehandler.fatalError(new TransformerException(fmsg, (SAXSourceLocator) xctxt.getSAXLocator()));
    } else {
        SourceLocator slocator = xctxt.getSAXLocator();
        System.out.println(fmsg + "; file " + slocator.getSystemId() + "; line " + slocator.getLineNumber() + "; column " + slocator.getColumnNumber());
    }
}
Also used : ErrorListener(javax.xml.transform.ErrorListener) SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) TransformerException(javax.xml.transform.TransformerException)

Example 8 with SourceLocator

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

the class FuncDocument method getDoc.

/**
   * Get the document from the given URI and base
   *
   * @param xctxt The XPath runtime state.
   * @param context The current context node
   * @param uri Relative(?) URI of the document
   * @param base Base to resolve relative URI from.
   *
   * @return The document Node pointing to the document at the given URI
   * or null
   *
   * @throws javax.xml.transform.TransformerException
   */
int getDoc(XPathContext xctxt, int context, String uri, String base) throws javax.xml.transform.TransformerException {
    // System.out.println("base: "+base+", uri: "+uri);
    SourceTreeManager treeMgr = xctxt.getSourceTreeManager();
    Source source;
    int newDoc;
    try {
        source = treeMgr.resolveURI(base, uri, xctxt.getSAXLocator());
        newDoc = treeMgr.getNode(source);
    } catch (IOException ioe) {
        throw new TransformerException(ioe.getMessage(), (SourceLocator) xctxt.getSAXLocator(), ioe);
    } catch (TransformerException te) {
        throw new TransformerException(te);
    }
    if (DTM.NULL != newDoc)
        return newDoc;
    // If the uri length is zero, get the uri of the stylesheet.
    if (uri.length() == 0) {
        // Hmmm... this seems pretty bogus to me... -sb
        uri = xctxt.getNamespaceContext().getBaseIdentifier();
        try {
            source = treeMgr.resolveURI(base, uri, xctxt.getSAXLocator());
        } catch (IOException ioe) {
            throw new TransformerException(ioe.getMessage(), (SourceLocator) xctxt.getSAXLocator(), ioe);
        }
    }
    String diagnosticsString = null;
    try {
        if ((null != uri) && (uri.length() > 0)) {
            newDoc = treeMgr.getSourceTree(source, xctxt.getSAXLocator(), xctxt);
        // System.out.println("newDoc: "+((Document)newDoc).getDocumentElement().getNodeName());
        } else
            warn(xctxt, XSLTErrorResources.WG_CANNOT_MAKE_URL_FROM, //"Can not make URL from: "+((base == null) ? "" : base )+uri);
            new Object[] { ((base == null) ? "" : base) + uri });
    } catch (Throwable throwable) {
        // throwable.printStackTrace();
        newDoc = DTM.NULL;
        // path.warn(XSLTErrorResources.WG_ENCODING_NOT_SUPPORTED_USING_JAVA, new Object[]{((base == null) ? "" : base )+uri}); //"Can not load requested doc: "+((base == null) ? "" : base )+uri);
        while (throwable instanceof org.apache.xml.utils.WrappedRuntimeException) {
            throwable = ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException();
        }
        if ((throwable instanceof NullPointerException) || (throwable instanceof ClassCastException)) {
            throw new org.apache.xml.utils.WrappedRuntimeException((Exception) throwable);
        }
        StringWriter sw = new StringWriter();
        PrintWriter diagnosticsWriter = new PrintWriter(sw);
        if (throwable instanceof TransformerException) {
            TransformerException spe = (TransformerException) throwable;
            {
                Throwable e = spe;
                while (null != e) {
                    if (null != e.getMessage()) {
                        diagnosticsWriter.println(" (" + e.getClass().getName() + "): " + e.getMessage());
                    }
                    if (e instanceof TransformerException) {
                        TransformerException spe2 = (TransformerException) e;
                        SourceLocator locator = spe2.getLocator();
                        if ((null != locator) && (null != locator.getSystemId()))
                            diagnosticsWriter.println("   ID: " + locator.getSystemId() + " Line #" + locator.getLineNumber() + " Column #" + locator.getColumnNumber());
                        e = spe2.getException();
                        if (e instanceof org.apache.xml.utils.WrappedRuntimeException)
                            e = ((org.apache.xml.utils.WrappedRuntimeException) e).getException();
                    } else
                        e = null;
                }
            }
        } else {
            diagnosticsWriter.println(" (" + throwable.getClass().getName() + "): " + throwable.getMessage());
        }
        //sw.toString();
        diagnosticsString = throwable.getMessage();
    }
    if (DTM.NULL == newDoc) {
        // System.out.println("what?: "+base+", uri: "+uri);
        if (null != diagnosticsString) {
            warn(xctxt, XSLTErrorResources.WG_CANNOT_LOAD_REQUESTED_DOC, //"Can not load requested doc: "+((base == null) ? "" : base )+uri);
            new Object[] { diagnosticsString });
        } else
            warn(xctxt, XSLTErrorResources.WG_CANNOT_LOAD_REQUESTED_DOC, new Object[] { uri == null ? ((base == null) ? "" : base) + uri : //"Can not load requested doc: "+((base == null) ? "" : base )+uri);
            uri.toString() });
    } else {
    // %REVIEW%
    // TBD: What to do about XLocator?
    // xctxt.getSourceTreeManager().associateXLocatorToNode(newDoc, url, null);
    }
    return newDoc;
}
Also used : SourceTreeManager(org.apache.xpath.SourceTreeManager) IOException(java.io.IOException) XMLString(org.apache.xml.utils.XMLString) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) WrongNumberArgsException(org.apache.xpath.functions.WrongNumberArgsException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SourceLocator(javax.xml.transform.SourceLocator) XObject(org.apache.xpath.objects.XObject) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Example 9 with SourceLocator

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

the class ElemCallTemplate method execute.

/**
   * Invoke a named template.
   * @see <a href="http://www.w3.org/TR/xslt#named-templates">named-templates in XSLT Specification</a>
   *
   * @param transformer non-null reference to the the current transform-time state.
   *
   * @throws TransformerException
   */
public void execute(TransformerImpl transformer) throws TransformerException {
    if (null != m_template) {
        XPathContext xctxt = transformer.getXPathContext();
        VariableStack vars = xctxt.getVarStack();
        int thisframe = vars.getStackFrame();
        int nextFrame = vars.link(m_template.m_frameSize);
        // so that the default param evaluation will work correctly.
        if (m_template.m_inArgsSize > 0) {
            vars.clearLocalSlots(0, m_template.m_inArgsSize);
            if (null != m_paramElems) {
                int currentNode = xctxt.getCurrentNode();
                vars.setStackFrame(thisframe);
                int size = m_paramElems.length;
                for (int i = 0; i < size; i++) {
                    ElemWithParam ewp = m_paramElems[i];
                    if (ewp.m_index >= 0) {
                        XObject obj = ewp.getValue(transformer, currentNode);
                        // Note here that the index for ElemWithParam must have been
                        // statically made relative to the xsl:template being called, 
                        // NOT this xsl:template.
                        vars.setLocalVariable(ewp.m_index, obj, nextFrame);
                    }
                }
                vars.setStackFrame(nextFrame);
            }
        }
        SourceLocator savedLocator = xctxt.getSAXLocator();
        try {
            xctxt.setSAXLocator(m_template);
            // template.executeChildTemplates(transformer, sourceNode, mode, true);
            transformer.pushElemTemplateElement(m_template);
            m_template.execute(transformer);
        } finally {
            transformer.popElemTemplateElement();
            xctxt.setSAXLocator(savedLocator);
            // When we entered this function, the current 
            // frame buffer (cfb) index in the variable stack may 
            // have been manually set.  If we just call 
            // unlink(), however, it will restore the cfb to the 
            // previous link index from the link stack, rather than 
            // the manually set cfb.  So, 
            // the only safe solution is to restore it back 
            // to the same position it was on entry, since we're 
            // really not working in a stack context here. (Bug4218)
            vars.unlink(thisframe);
        }
    } else {
        transformer.getMsgMgr().error(this, XSLTErrorResources.ER_TEMPLATE_NOT_FOUND, //"Could not find template named: '"+templateName+"'");
        new Object[] { m_templateName });
    }
}
Also used : VariableStack(org.apache.xpath.VariableStack) SourceLocator(javax.xml.transform.SourceLocator) XPathContext(org.apache.xpath.XPathContext) XObject(org.apache.xpath.objects.XObject)

Example 10 with SourceLocator

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

the class TransformerImpl method createSerializationHandler.

/**
     * Create a ContentHandler from a Result object and an OutputProperties.
     *
     * @param outputTarget Where the transform result should go,
     * should not be null.
     * @param format The OutputProperties object that will contain
     * instructions on how to serialize the output.
     *
     * @return A valid ContentHandler that will create the
     * result tree when it is fed SAX events.
     *
     * @throws TransformerException
     */
public SerializationHandler createSerializationHandler(Result outputTarget, OutputProperties format) throws TransformerException {
    SerializationHandler xoh;
    // If the Result object contains a Node, then create
    // a ContentHandler that will add nodes to the input node.
    org.w3c.dom.Node outputNode = null;
    if (outputTarget instanceof DOMResult) {
        outputNode = ((DOMResult) outputTarget).getNode();
        org.w3c.dom.Node nextSibling = ((DOMResult) outputTarget).getNextSibling();
        org.w3c.dom.Document doc;
        short type;
        if (null != outputNode) {
            type = outputNode.getNodeType();
            doc = (org.w3c.dom.Node.DOCUMENT_NODE == type) ? (org.w3c.dom.Document) outputNode : outputNode.getOwnerDocument();
        } else {
            boolean isSecureProcessing = m_stylesheetRoot.isSecureProcessing();
            doc = org.apache.xml.utils.DOMHelper.createDocument(isSecureProcessing);
            outputNode = doc;
            type = outputNode.getNodeType();
            ((DOMResult) outputTarget).setNode(outputNode);
        }
        DOMBuilder handler = (org.w3c.dom.Node.DOCUMENT_FRAGMENT_NODE == type) ? new DOMBuilder(doc, (org.w3c.dom.DocumentFragment) outputNode) : new DOMBuilder(doc, outputNode);
        if (nextSibling != null)
            handler.setNextSibling(nextSibling);
        String encoding = format.getProperty(OutputKeys.ENCODING);
        xoh = new ToXMLSAXHandler(handler, (LexicalHandler) handler, encoding);
    } else if (outputTarget instanceof SAXResult) {
        ContentHandler handler = ((SAXResult) outputTarget).getHandler();
        if (null == handler)
            throw new IllegalArgumentException("handler can not be null for a SAXResult");
        LexicalHandler lexHandler;
        if (handler instanceof LexicalHandler)
            lexHandler = (LexicalHandler) handler;
        else
            lexHandler = null;
        String encoding = format.getProperty(OutputKeys.ENCODING);
        String method = format.getProperty(OutputKeys.METHOD);
        ToXMLSAXHandler toXMLSAXHandler = new ToXMLSAXHandler(handler, lexHandler, encoding);
        toXMLSAXHandler.setShouldOutputNSAttr(false);
        xoh = toXMLSAXHandler;
        String publicID = format.getProperty(OutputKeys.DOCTYPE_PUBLIC);
        String systemID = format.getProperty(OutputKeys.DOCTYPE_SYSTEM);
        if (systemID != null)
            xoh.setDoctypeSystem(systemID);
        if (publicID != null)
            xoh.setDoctypePublic(publicID);
        if (handler instanceof TransformerClient) {
            XalanTransformState state = new XalanTransformState();
            ((TransformerClient) handler).setTransformState(state);
            ((ToSAXHandler) xoh).setTransformState(state);
        }
    } else // result tree to either a stream or a writer.
    if (outputTarget instanceof StreamResult) {
        StreamResult sresult = (StreamResult) outputTarget;
        try {
            SerializationHandler serializer = (SerializationHandler) SerializerFactory.getSerializer(format.getProperties());
            if (null != sresult.getWriter())
                serializer.setWriter(sresult.getWriter());
            else if (null != sresult.getOutputStream())
                serializer.setOutputStream(sresult.getOutputStream());
            else if (null != sresult.getSystemId()) {
                String fileURL = sresult.getSystemId();
                if (fileURL.startsWith("file:///")) {
                    if (fileURL.substring(8).indexOf(":") > 0)
                        fileURL = fileURL.substring(8);
                    else
                        fileURL = fileURL.substring(7);
                } else if (fileURL.startsWith("file:/")) {
                    if (fileURL.substring(6).indexOf(":") > 0)
                        fileURL = fileURL.substring(6);
                    else
                        fileURL = fileURL.substring(5);
                }
                m_outputStream = new java.io.FileOutputStream(fileURL);
                serializer.setOutputStream(m_outputStream);
                xoh = serializer;
            } else
                //"No output specified!");
                throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_OUTPUT_SPECIFIED, null));
            // handler = serializer.asContentHandler();
            //  this.setSerializer(serializer);
            xoh = serializer;
        }//        }
         catch (IOException ioe) {
            throw new TransformerException(ioe);
        }
    } else {
        //"Can't transform to a Result of type "
        throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_TRANSFORM_TO_RESULT_TYPE, new Object[] { outputTarget.getClass().getName() }));
    //+ outputTarget.getClass().getName()
    //+ "!");
    }
    // before we forget, lets make the created handler hold a reference
    // to the current TransformImpl object
    xoh.setTransformer(this);
    SourceLocator srcLocator = getStylesheet();
    xoh.setSourceLocator(srcLocator);
    return xoh;
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SerializationHandler(org.apache.xml.serializer.SerializationHandler) DOMBuilder(org.apache.xml.utils.DOMBuilder) ContentHandler(org.xml.sax.ContentHandler) LexicalHandler(org.xml.sax.ext.LexicalHandler) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) SAXResult(javax.xml.transform.sax.SAXResult) SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator) XObject(org.apache.xpath.objects.XObject) ToXMLSAXHandler(org.apache.xml.serializer.ToXMLSAXHandler)

Aggregations

SourceLocator (javax.xml.transform.SourceLocator)14 TransformerException (javax.xml.transform.TransformerException)10 SAXSourceLocator (org.apache.xml.utils.SAXSourceLocator)6 XObject (org.apache.xpath.objects.XObject)6 IOException (java.io.IOException)4 SAXException (org.xml.sax.SAXException)4 SAXParseException (org.xml.sax.SAXParseException)4 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ErrorListener (javax.xml.transform.ErrorListener)2 Source (javax.xml.transform.Source)2 DOMResult (javax.xml.transform.dom.DOMResult)2 SAXResult (javax.xml.transform.sax.SAXResult)2 StreamResult (javax.xml.transform.stream.StreamResult)2 SerializationHandler (org.apache.xml.serializer.SerializationHandler)2 ToXMLSAXHandler (org.apache.xml.serializer.ToXMLSAXHandler)2 DOMBuilder (org.apache.xml.utils.DOMBuilder)2 XMLString (org.apache.xml.utils.XMLString)2 SourceTreeManager (org.apache.xpath.SourceTreeManager)2 VariableStack (org.apache.xpath.VariableStack)2