Search in sources :

Example 16 with SourceLocator

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

the class StylesheetHandler method getBaseIdentifier.

/**
 * Return the base identifier.
 *
 * @return The base identifier of the current stylesheet.
 */
public String getBaseIdentifier() {
    // Try to get the baseIdentifier from the baseIdentifier's stack,
    // which may not be the same thing as the value found in the
    // SourceLocators stack.
    String base = (String) (m_baseIdentifiers.isEmpty() ? null : m_baseIdentifiers.peek());
    // Otherwise try the stylesheet.
    if (null == base) {
        SourceLocator locator = getLocator();
        base = (null == locator) ? "" : locator.getSystemId();
    }
    return base;
}
Also used : SourceLocator(javax.xml.transform.SourceLocator) SAXSourceLocator(org.apache.xml.utils.SAXSourceLocator)

Example 17 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 18 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 19 with SourceLocator

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

the class DefaultErrorHandler method printLocation.

public static void printLocation(PrintWriter pw, Throwable exception) {
    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 WrappedRuntimeException)
            cause = ((WrappedRuntimeException) cause).getException();
        else if (cause instanceof SAXException)
            cause = ((SAXException) cause).getException();
        else
            cause = null;
    } while (null != cause);
    if (null != locator) {
        // getErrorWriter().println("Parser fatal error: "+exception.getMessage());
        String id = (null != locator.getPublicId()) ? locator.getPublicId() : (null != locator.getSystemId()) ? locator.getSystemId() : // "SystemId Unknown";
        XMLMessages.createXMLMessage(XMLErrorResources.ER_SYSTEMID_UNKNOWN, null);
        pw.print(id + "; " + XMLMessages.createXMLMessage("line", null) + locator.getLineNumber() + "; " + XMLMessages.createXMLMessage("column", null) + locator.getColumnNumber() + "; ");
    } else
        pw.print("(" + XMLMessages.createXMLMessage(XMLErrorResources.ER_LOCATION_UNKNOWN, null) + ")");
}
Also used : SourceLocator(javax.xml.transform.SourceLocator) SAXParseException(org.xml.sax.SAXParseException) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Example 20 with SourceLocator

use of javax.xml.transform.SourceLocator in project walkmod-core by walkmod.

the class LocationUtils method getLocation.

/**
 * Get the location of an object. Some well-known located classes built in
 * the JDK are handled by this method. Handling of other located classes can
 * be handled by adding new location finders.
 *
 * @param obj
 *            the object of which to get the location
 * @param description
 *            an optional description of the object's location, used if a
 *            Location object has to be created.
 * @return the object's location, or {@link LocationImpl#UNKNOWN} if no
 *         location could be found
 */
public static Location getLocation(Object obj, String description) {
    if (obj instanceof Location) {
        return (Location) obj;
    }
    if (obj instanceof Locatable) {
        return ((Locatable) obj).getLocation();
    }
    if (obj instanceof SAXParseException) {
        SAXParseException spe = (SAXParseException) obj;
        if (spe.getSystemId() != null) {
            return new LocationImpl(description, spe.getSystemId(), spe.getLineNumber(), spe.getColumnNumber());
        } else {
            return LocationImpl.UNKNOWN;
        }
    }
    if (obj instanceof TransformerException) {
        TransformerException ex = (TransformerException) obj;
        SourceLocator locator = ex.getLocator();
        if (locator != null && locator.getSystemId() != null) {
            return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
        } else {
            return LocationImpl.UNKNOWN;
        }
    }
    if (obj instanceof Locator) {
        Locator locator = (Locator) obj;
        if (locator.getSystemId() != null) {
            return new LocationImpl(description, locator.getSystemId(), locator.getLineNumber(), locator.getColumnNumber());
        } else {
            return LocationImpl.UNKNOWN;
        }
    }
    if (obj instanceof Element) {
        return LocationAttributes.getLocation((Element) obj);
    }
    List<WeakReference<LocationFinder>> currentFinders = finders;
    int size = currentFinders.size();
    for (int i = 0; i < size; i++) {
        WeakReference<LocationFinder> ref = currentFinders.get(i);
        LocationFinder finder = ref.get();
        if (finder == null) {
            synchronized (LocationFinder.class) {
                List<WeakReference<LocationFinder>> newFinders = new ArrayList<WeakReference<LocationFinder>>(finders);
                newFinders.remove(ref);
                finders = newFinders;
            }
        }
        Location result = finder.getLocation(obj, description);
        if (result != null) {
            return result;
        }
    }
    if (obj instanceof Throwable) {
        Throwable t = (Throwable) obj;
        StackTraceElement[] stack = t.getStackTrace();
        if (stack != null && stack.length > 0) {
            StackTraceElement trace = stack[0];
            if (trace.getLineNumber() >= 0) {
                String uri = trace.getClassName();
                if (trace.getFileName() != null) {
                    uri = uri.replace('.', '/');
                    uri = uri.substring(0, uri.lastIndexOf('/') + 1);
                    uri = uri + trace.getFileName();
                    URL url = ClassLoaderUtil.getResource(uri, LocationUtils.class);
                    if (url != null) {
                        uri = url.toString();
                    }
                }
                if (description == null) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("Class: ").append(trace.getClassName()).append("\n");
                    sb.append("File: ").append(trace.getFileName()).append("\n");
                    sb.append("Method: ").append(trace.getMethodName()).append("\n");
                    sb.append("Line: ").append(trace.getLineNumber());
                    description = sb.toString();
                }
                return new LocationImpl(description, uri, trace.getLineNumber(), -1);
            }
        }
    }
    return LocationImpl.UNKNOWN;
}
Also used : Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) URL(java.net.URL) SourceLocator(javax.xml.transform.SourceLocator) Locator(org.xml.sax.Locator) SAXParseException(org.xml.sax.SAXParseException) SourceLocator(javax.xml.transform.SourceLocator) WeakReference(java.lang.ref.WeakReference) TransformerException(javax.xml.transform.TransformerException)

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