286N/A/*
286N/A * reserved comment block
286N/A * DO NOT REMOVE OR ALTER!
286N/A */
286N/A/*
286N/A * Copyright 1999-2004 The Apache Software Foundation.
286N/A *
286N/A * Licensed under the Apache License, Version 2.0 (the "License");
286N/A * you may not use this file except in compliance with the License.
286N/A * You may obtain a copy of the License at
286N/A *
286N/A * http://www.apache.org/licenses/LICENSE-2.0
286N/A *
286N/A * Unless required by applicable law or agreed to in writing, software
286N/A * distributed under the License is distributed on an "AS IS" BASIS,
286N/A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
286N/A * See the License for the specific language governing permissions and
286N/A * limitations under the License.
286N/A */
286N/A/*
286N/A * $Id: XPathException.java,v 1.3 2005/09/28 13:49:30 pvedula Exp $
286N/A */
286N/Apackage com.sun.org.apache.xpath.internal;
286N/A
286N/Aimport javax.xml.transform.TransformerException;
286N/A
286N/Aimport org.w3c.dom.Node;
286N/A
286N/A/**
286N/A * This class implements an exception object that all
286N/A * XPath classes will throw in case of an error. This class
286N/A * extends TransformerException, and may hold other exceptions. In the
286N/A * case of nested exceptions, printStackTrace will dump
286N/A * all the traces of the nested exceptions, not just the trace
286N/A * of this object.
286N/A * @xsl.usage general
286N/A */
286N/Apublic class XPathException extends TransformerException
286N/A{
286N/A static final long serialVersionUID = 4263549717619045963L;
286N/A
286N/A /** The home of the expression that caused the error.
286N/A * @serial */
286N/A Object m_styleNode = null;
286N/A
286N/A /**
286N/A * Get the stylesheet node from where this error originated.
286N/A * @return The stylesheet node from where this error originated, or null.
286N/A */
286N/A public Object getStylesheetNode()
286N/A {
286N/A return m_styleNode;
286N/A }
286N/A
286N/A /**
286N/A * Set the stylesheet node from where this error originated.
286N/A * @param styleNode The stylesheet node from where this error originated, or null.
286N/A */
286N/A public void setStylesheetNode(Object styleNode)
286N/A {
286N/A m_styleNode = styleNode;
286N/A }
286N/A
286N/A
286N/A /** A nested exception.
286N/A * @serial */
286N/A protected Exception m_exception;
286N/A
286N/A /**
286N/A * Create an XPathException object that holds
286N/A * an error message.
286N/A * @param message The error message.
286N/A */
286N/A public XPathException(String message, ExpressionNode ex)
286N/A {
286N/A super(message);
286N/A this.setLocator(ex);
286N/A setStylesheetNode(getStylesheetNode(ex));
286N/A }
286N/A
286N/A /**
286N/A * Create an XPathException object that holds
286N/A * an error message.
286N/A * @param message The error message.
286N/A */
286N/A public XPathException(String message)
286N/A {
286N/A super(message);
286N/A }
286N/A
286N/A
286N/A /**
286N/A * Get the XSLT ElemVariable that this sub-expression references. In order for
286N/A * this to work, the SourceLocator must be the owning ElemTemplateElement.
286N/A * @return The dereference to the ElemVariable, or null if not found.
286N/A */
286N/A public org.w3c.dom.Node getStylesheetNode(ExpressionNode ex)
286N/A {
286N/A
286N/A ExpressionNode owner = getExpressionOwner(ex);
286N/A
286N/A if (null != owner && owner instanceof org.w3c.dom.Node)
286N/A {
286N/A return ((org.w3c.dom.Node)owner);
286N/A }
286N/A return null;
286N/A
286N/A }
286N/A
286N/A /**
286N/A * Get the first non-Expression parent of this node.
286N/A * @return null or first ancestor that is not an Expression.
286N/A */
286N/A protected ExpressionNode getExpressionOwner(ExpressionNode ex)
286N/A {
286N/A ExpressionNode parent = ex.exprGetParent();
286N/A while((null != parent) && (parent instanceof Expression))
286N/A parent = parent.exprGetParent();
286N/A return parent;
286N/A }
286N/A
286N/A
286N/A
286N/A /**
286N/A * Create an XPathException object that holds
286N/A * an error message and the stylesheet node that
286N/A * the error originated from.
286N/A * @param message The error message.
286N/A * @param styleNode The stylesheet node that the error originated from.
286N/A */
286N/A public XPathException(String message, Object styleNode)
286N/A {
286N/A
286N/A super(message);
286N/A
286N/A m_styleNode = styleNode;
286N/A }
286N/A
286N/A /**
286N/A * Create an XPathException object that holds
286N/A * an error message, the stylesheet node that
286N/A * the error originated from, and another exception
286N/A * that caused this exception.
286N/A * @param message The error message.
286N/A * @param styleNode The stylesheet node that the error originated from.
286N/A * @param e The exception that caused this exception.
286N/A */
286N/A public XPathException(String message, Node styleNode, Exception e)
286N/A {
286N/A
286N/A super(message);
286N/A
286N/A m_styleNode = styleNode;
286N/A this.m_exception = e;
286N/A }
286N/A
286N/A /**
286N/A * Create an XPathException object that holds
286N/A * an error message, and another exception
286N/A * that caused this exception.
286N/A * @param message The error message.
286N/A * @param e The exception that caused this exception.
286N/A */
286N/A public XPathException(String message, Exception e)
286N/A {
286N/A
286N/A super(message);
286N/A
286N/A this.m_exception = e;
286N/A }
286N/A
286N/A /**
286N/A * Print the the trace of methods from where the error
286N/A * originated. This will trace all nested exception
286N/A * objects, as well as this object.
286N/A * @param s The stream where the dump will be sent to.
286N/A */
286N/A public void printStackTrace(java.io.PrintStream s)
286N/A {
286N/A
286N/A if (s == null)
286N/A s = System.err;
286N/A
286N/A try
286N/A {
286N/A super.printStackTrace(s);
286N/A }
286N/A catch (Exception e){}
286N/A
286N/A Throwable exception = m_exception;
286N/A
286N/A for (int i = 0; (i < 10) && (null != exception); i++)
286N/A {
286N/A s.println("---------");
286N/A exception.printStackTrace(s);
286N/A
286N/A if (exception instanceof TransformerException)
286N/A {
286N/A TransformerException se = (TransformerException) exception;
286N/A Throwable prev = exception;
286N/A
286N/A exception = se.getException();
286N/A
286N/A if (prev == exception)
286N/A break;
286N/A }
286N/A else
286N/A {
286N/A exception = null;
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Find the most contained message.
286N/A *
286N/A * @return The error message of the originating exception.
286N/A */
286N/A public String getMessage()
286N/A {
286N/A
286N/A String lastMessage = super.getMessage();
286N/A Throwable exception = m_exception;
286N/A
286N/A while (null != exception)
286N/A {
286N/A String nextMessage = exception.getMessage();
286N/A
286N/A if (null != nextMessage)
286N/A lastMessage = nextMessage;
286N/A
286N/A if (exception instanceof TransformerException)
286N/A {
286N/A TransformerException se = (TransformerException) exception;
286N/A Throwable prev = exception;
286N/A
286N/A exception = se.getException();
286N/A
286N/A if (prev == exception)
286N/A break;
286N/A }
286N/A else
286N/A {
286N/A exception = null;
286N/A }
286N/A }
286N/A
286N/A return (null != lastMessage) ? lastMessage : "";
286N/A }
286N/A
286N/A /**
286N/A * Print the the trace of methods from where the error
286N/A * originated. This will trace all nested exception
286N/A * objects, as well as this object.
286N/A * @param s The writer where the dump will be sent to.
286N/A */
286N/A public void printStackTrace(java.io.PrintWriter s)
286N/A {
286N/A
286N/A if (s == null)
286N/A s = new java.io.PrintWriter(System.err);
286N/A
286N/A try
286N/A {
286N/A super.printStackTrace(s);
286N/A }
286N/A catch (Exception e){}
286N/A
286N/A
286N/A boolean isJdk14OrHigher = false;
286N/A try {
286N/A Throwable.class.getMethod("getCause", (Class[]) null);
286N/A isJdk14OrHigher = true;
286N/A } catch (NoSuchMethodException nsme) {
286N/A // do nothing
286N/A }
286N/A
286N/A // The printStackTrace method of the Throwable class in jdk 1.4
286N/A // and higher will include the cause when printing the backtrace.
286N/A // The following code is only required when using jdk 1.3 or lower
286N/A if (!isJdk14OrHigher) {
286N/A
286N/A Throwable exception = m_exception;
286N/A
286N/A for (int i = 0; (i < 10) && (null != exception); i++)
286N/A {
286N/A s.println("---------");
286N/A
286N/A try
286N/A {
286N/A exception.printStackTrace(s);
286N/A }
286N/A catch (Exception e)
286N/A {
286N/A s.println("Could not print stack trace...");
286N/A }
286N/A
286N/A if (exception instanceof TransformerException)
286N/A {
286N/A TransformerException se = (TransformerException) exception;
286N/A Throwable prev = exception;
286N/A
286N/A exception = se.getException();
286N/A
286N/A if (prev == exception)
286N/A {
286N/A exception = null;
286N/A
286N/A break;
286N/A }
286N/A }
286N/A else
286N/A {
286N/A exception = null;
286N/A }
286N/A }
286N/A }
286N/A }
286N/A
286N/A /**
286N/A * Return the embedded exception, if any.
286N/A * Overrides javax.xml.transform.TransformerException.getException().
286N/A *
286N/A * @return The embedded exception, or null if there is none.
286N/A */
286N/A public Throwable getException()
286N/A {
286N/A return m_exception;
286N/A }
286N/A}