Search in sources :

Example 1 with SourceLocator

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

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 2 with SourceLocator

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

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 3 with SourceLocator

use of javax.xml.transform.SourceLocator in project iaf by ibissource.

the class IbisException method getExceptionSpecificDetails.

public String getExceptionSpecificDetails(Throwable t) {
    String result = null;
    if (t instanceof AddressException) {
        AddressException ae = (AddressException) t;
        String parsedString = ae.getRef();
        if (StringUtils.isNotEmpty(parsedString)) {
            result = addPart(result, " ", "[" + parsedString + "]");
        }
        int column = ae.getPos() + 1;
        if (column > 0) {
            result = addPart(result, " ", "at column [" + column + "]");
        }
    }
    if (t instanceof SAXParseException) {
        SAXParseException spe = (SAXParseException) t;
        int line = spe.getLineNumber();
        int col = spe.getColumnNumber();
        String sysid = spe.getSystemId();
        String locationInfo = null;
        if (StringUtils.isNotEmpty(sysid)) {
            locationInfo = "SystemId [" + sysid + "]";
        }
        if (line >= 0) {
            locationInfo = addPart(locationInfo, " ", "line [" + line + "]");
        }
        if (col >= 0) {
            locationInfo = addPart(locationInfo, " ", "column [" + col + "]");
        }
        result = addPart(locationInfo, ": ", result);
    }
    if (t instanceof TransformerException) {
        TransformerException te = (TransformerException) t;
        SourceLocator locator = te.getLocator();
        if (locator != null) {
            int line = locator.getLineNumber();
            int col = locator.getColumnNumber();
            String sysid = locator.getSystemId();
            String locationInfo = null;
            if (StringUtils.isNotEmpty(sysid)) {
                locationInfo = "SystemId [" + sysid + "]";
            }
            if (line >= 0) {
                locationInfo = addPart(locationInfo, " ", "line [" + line + "]");
            }
            if (col >= 0) {
                locationInfo = addPart(locationInfo, " ", "column [" + col + "]");
            }
            result = addPart(locationInfo, ": ", result);
        }
    }
    if (t instanceof SQLException) {
        SQLException sqle = (SQLException) t;
        int errorCode = sqle.getErrorCode();
        String sqlState = sqle.getSQLState();
        if (errorCode != 0) {
            result = addPart("errorCode [" + errorCode + "]", ", ", result);
        }
        if (StringUtils.isNotEmpty(sqlState)) {
            result = addPart("SQLState [" + sqlState + "]", ", ", result);
        }
    }
    return result;
}
Also used : SQLException(java.sql.SQLException) SAXParseException(org.xml.sax.SAXParseException) SourceLocator(javax.xml.transform.SourceLocator) AddressException(javax.mail.internet.AddressException) TransformerException(javax.xml.transform.TransformerException)

Example 4 with SourceLocator

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

the class XalanStyleFrame method getSourceLocator.

private SourceLocator getSourceLocator() {
    Node sourceNode = event.m_sourceNode;
    SourceLocator locator = null;
    if (sourceNode instanceof DTMNodeProxy) {
        int nodeHandler = ((DTMNodeProxy) sourceNode).getDTMNodeNumber();
        return ((DTMNodeProxy) sourceNode).getDTM().getSourceLocatorFor(nodeHandler);
    }
    return null;
}
Also used : SourceLocator(javax.xml.transform.SourceLocator) Node(org.w3c.dom.Node) DTMNodeProxy(org.apache.xml.dtm.ref.DTMNodeProxy)

Example 5 with SourceLocator

use of javax.xml.transform.SourceLocator in project mycore by MyCoRe-Org.

the class MCRErrorListener method getMyMessageAndLocation.

public static String getMyMessageAndLocation(TransformerException exception) {
    SourceLocator locator = exception.getLocator();
    StringBuilder msg = new StringBuilder();
    if (locator != null) {
        String systemID = locator.getSystemId();
        int line = locator.getLineNumber();
        int col = locator.getColumnNumber();
        if (systemID != null) {
            msg.append("SystemID: ");
            msg.append(systemID);
        }
        if (line != 0) {
            msg.append(" [");
            msg.append(line);
            if (col != 0) {
                msg.append(',');
                msg.append(col);
            }
            msg.append("]");
        }
    }
    msg.append(": ");
    msg.append(exception.getMessage());
    return msg.toString();
}
Also used : SourceLocator(javax.xml.transform.SourceLocator)

Aggregations

SourceLocator (javax.xml.transform.SourceLocator)20 TransformerException (javax.xml.transform.TransformerException)13 SAXSourceLocator (org.apache.xml.utils.SAXSourceLocator)6 XObject (org.apache.xpath.objects.XObject)6 SAXParseException (org.xml.sax.SAXParseException)6 IOException (java.io.IOException)4 SAXException (org.xml.sax.SAXException)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