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: */
017: package org.apache.cocoon;
018:
019: import java.io.IOException;
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022:
023: import org.apache.avalon.framework.CascadingThrowable;
024: import org.xml.sax.SAXParseException;
025: import javax.xml.transform.TransformerException;
026: import javax.xml.transform.SourceLocator;
027:
028: /**
029: * This is a wrapping IOException.
030: *
031: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
032: * @version CVS $Id: CascadingIOException.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034:
035: public class CascadingIOException extends IOException implements
036: CascadingThrowable {
037:
038: /**
039: * The Throwable that caused this exception to be thrown.
040: */
041: private final Throwable m_throwable;
042:
043: /**
044: * Construct a new <code>ProcessingException</code> instance.
045: */
046: public CascadingIOException(String message) {
047: this (message, null);
048: }
049:
050: /**
051: * Creates a new <code>ProcessingException</code> instance.
052: *
053: * @param ex an <code>Exception</code> value
054: */
055: public CascadingIOException(Exception ex) {
056: this (ex.getMessage(), ex);
057: }
058:
059: /**
060: * Construct a new <code>ProcessingException</code> that references
061: * a parent Exception.
062: */
063: public CascadingIOException(String message, Throwable t) {
064: super (message);
065: this .m_throwable = t;
066: }
067:
068: /**
069: * Retrieve root cause of the exception.
070: *
071: * @return the root cause
072: */
073: public final Throwable getCause() {
074: return this .m_throwable;
075: }
076:
077: public String toString() {
078: StringBuffer s = new StringBuffer();
079: s.append(super .toString());
080: final Throwable t = getCause();
081: if (t != null) {
082: s.append(": ");
083: // be more verbose try to get location info
084: s.append(extraInfo(t));
085: s.append(t.toString());
086: }
087: return s.toString();
088: }
089:
090: /**
091: * Examine Throwable and try to figure out location information.
092: * <p>
093: * At the moment only SAXParseException, and TransformerException
094: * are considered.
095: * </p>
096: *
097: * @return String containing location information of the format
098: * <code>{file-name}:{line}:{column}:</code>, if no location info is
099: * available return empty string
100: */
101: private String extraInfo(Throwable t) {
102: StringBuffer sb = new StringBuffer();
103: if (t instanceof SAXParseException) {
104: SAXParseException spe = (SAXParseException) t;
105: sb.append(String.valueOf(spe.getSystemId()));
106: sb.append(":");
107: sb.append(String.valueOf(spe.getLineNumber()));
108: sb.append(":");
109: sb.append(String.valueOf(spe.getColumnNumber()));
110: sb.append(":");
111: } else if (t instanceof TransformerException) {
112: TransformerException transformerException = (TransformerException) t;
113: SourceLocator sourceLocator = transformerException
114: .getLocator();
115:
116: if (null != sourceLocator) {
117: sb.append(String.valueOf(sourceLocator.getSystemId()));
118: sb.append(":");
119: sb
120: .append(String.valueOf(sourceLocator
121: .getLineNumber()));
122: sb.append(":");
123: sb.append(String.valueOf(sourceLocator
124: .getColumnNumber()));
125: sb.append(":");
126: }
127: }
128: return sb.toString();
129: }
130:
131: public void printStackTrace() {
132: super .printStackTrace();
133: if (getCause() != null)
134: getCause().printStackTrace();
135: }
136:
137: public void printStackTrace(PrintStream s) {
138: super .printStackTrace(s);
139: if (getCause() != null)
140: getCause().printStackTrace(s);
141: }
142:
143: public void printStackTrace(PrintWriter s) {
144: super.printStackTrace(s);
145: if (getCause() != null)
146: getCause().printStackTrace(s);
147: }
148:
149: }
|