001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath;
018
019/**
020 * Thrown in various situations by JXPath; may contain a nested exception.
021 *
022 * @author Dmitri Plotnikov
023 * @version $Revision: 618149 $ $Date: 2008-02-04 03:04:13 +0100 (Mo, 04 Feb 2008) $
024 */
025
026public class JXPathException extends RuntimeException {
027    private static final long serialVersionUID = 4306409701468017766L;
028
029    /** @serial */
030    private Throwable exception;
031
032    /**
033     * Create a new <code>JXPathException</code> with no
034     * detail mesage.
035     */
036
037     public JXPathException() {
038         super();
039         this.exception = null;
040     }
041
042    /**
043     * Create a new <code>JXPathException</code> with
044     * the <code>String </code> specified as an error message.
045     *
046     * @param msg The error message for the exception.
047     */
048    public JXPathException(String msg) {
049        super(msg);
050        this.exception = null;
051    }
052
053
054    /**
055     * Create a new <code>JXPathException</code> with a
056     * given <code>Throwable</code> base cause of the error.
057     *
058     * @param e The exception to be encapsulated in a
059     * JXPathException.
060     */
061    public JXPathException(Throwable e) {
062        super(e.toString());
063        this.exception = e;
064    }
065
066    /**
067     * Create a new <code>JXPathException</code> with the
068     * given <code>Exception</code> base cause and detail message.
069     *
070     * @param msg The detail message.
071     * @param e The exception to be encapsulated in a JXPathException
072     */
073    public JXPathException(String msg, Throwable e) {
074        super(msg);
075        this.exception = e;
076    }
077
078
079    /**
080     * Return the message (if any) for this error . If there is no
081     * message for the exception and there is an encapsulated
082     * exception then the message of that exception will be returned.
083     *
084     * @return The error message.
085     */
086    public String getMessage() {
087        String message = super.getMessage();
088        if (exception == null) {
089            return message;
090        }
091        StringBuffer buf = new StringBuffer();
092        if (message != null) {
093            buf.append(message).append("; ");
094        }
095        String eMsg = exception.getMessage();
096        buf.append(eMsg == null ? exception.getClass().getName() : eMsg);
097        return buf.toString();
098    }
099
100    /**
101     * Return the actual exception (if any) that caused this exception to
102     * be raised.
103     *
104     * @return The encapsulated exception, or null if there is none.
105     */
106    public Throwable getException() {
107        return exception;
108    }
109
110    /**
111     * Same as {@link #getException() getException()}
112     * @return The encapsulated exception, or null if there is none.
113     */
114    public Throwable getCause() {
115        return exception;
116    }
117
118}