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