KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > ProcessingException


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon;
17
18 import java.util.List JavaDoc;
19
20 import org.apache.cocoon.util.location.LocatedException;
21 import org.apache.cocoon.util.location.LocatedRuntimeException;
22 import org.apache.cocoon.util.location.Location;
23 import org.apache.cocoon.util.location.MultiLocatable;
24
25 /**
26  * This Exception is thrown every time there is a problem in processing
27  * a request.
28  *
29  * @author <a HREF="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
30  * (Apache Software Foundation)
31  * @version CVS $Id: ProcessingException.java 280628 2005-09-13 19:14:35Z sylvain $
32  */

33 public class ProcessingException extends LocatedException implements MultiLocatable {
34     
35     /**
36      * Construct a new <code>ProcessingException</code> instance.
37      */

38     public ProcessingException(String JavaDoc message) {
39         super(message);
40     }
41     
42     /**
43      * Creates a new <code>ProcessingException</code> instance.
44      *
45      * @param ex an <code>Exception</code> value
46      */

47     public ProcessingException(Exception JavaDoc ex) {
48         super(ex.getMessage(), ex);
49     }
50     
51     /**
52      * Construct a new <code>ProcessingException</code> that references
53      * a parent Exception.
54      */

55     public ProcessingException(String JavaDoc message, Throwable JavaDoc t) {
56         super(message, t);
57     }
58     
59     /**
60      * Construct a new <code>ProcessingException</code> that has an associated location.
61      */

62     public ProcessingException(String JavaDoc message, Location location) {
63         super(message, location);
64     }
65     
66     /**
67      * Construct a new <code>ProcessingException</code> that has a parent exception
68      * and an associated location.
69      * <p>
70      * This constructor is protected to enforce the use of {@link #throwLocated(String, Throwable, Location)}
71      * which limits exception nesting as far as possible.
72      */

73     protected ProcessingException(String JavaDoc message, Throwable JavaDoc t, Location location) {
74         super(message, t, location);
75     }
76     
77     /**
78      * Throw a located exception given an existing exception and the location where
79      * this exception was catched.
80      * <p>
81      * If the exception is already a <code>ProcessingException</code> or a {@link LocatedRuntimeException},
82      * the location is added to the original exception's location chain and the original exception
83      * is rethrown (<code>description</code> is ignored) to limit exception nesting. Otherwise, a new
84      * <code>ProcessingException</code> is thrown, wrapping the original exception.
85      * <p>
86      * Note: this method returns an exception as a convenience if you want to keep the <code>throw</code>
87      * semantics in the caller code, i.e. write<br>
88      * <code>&nbsp;&nbsp;throw ProcessingException.throwLocated(...);</code><br>
89      * instead of<br>
90      * <code>&nbsp;&nbsp;ProcessingException.throwLocated(...);</code><br>
91      * <code>&nbsp;&nbsp;return;</code>
92      *
93      * @param message a message (can be <code>null</code>)
94      * @param thr the original exception (can be <code>null</code>)
95      * @param location the location (can be <code>null</code>)
96      * @return a (fake) located exception
97      * @throws ProcessingException or <code>LocatedRuntimeException</code>
98      */

99     public static ProcessingException throwLocated(String JavaDoc message, Throwable JavaDoc thr, Location location) throws ProcessingException {
100         if (thr instanceof ProcessingException) {
101             ProcessingException pe = (ProcessingException)thr;
102             pe.addLocation(location);
103             throw pe;
104
105         } else if (thr instanceof LocatedRuntimeException) {
106             LocatedRuntimeException re = (LocatedRuntimeException)thr;
107             re.addLocation(location);
108             // Rethrow
109
throw re;
110         }
111         
112         throw new ProcessingException(message, thr, location);
113     }
114     
115     /**
116      * Throw a located exception given an existing exception and the locations where
117      * this exception was catched.
118      * <p>
119      * If the exception is already a <code>ProcessingException</code> or a {@link LocatedRuntimeException},
120      * the locations are added to the original exception's location chain and the original exception
121      * is rethrown (<code>description</code> is ignored) to limit exception nesting. Otherwise, a new
122      * <code>ProcessingException</code> is thrown, wrapping the original exception.
123      * <p>
124      * Note: this method returns an exception as a convenience if you want to keep the <code>throw</code>
125      * semantics in the caller code, i.e. write<br>
126      * <code>&nbsp;&nbsp;throw ProcessingException.throwLocated(...);</code><br>
127      * instead of<br>
128      * <code>&nbsp;&nbsp;ProcessingException.throwLocated(...);</code><br>
129      * <code>&nbsp;&nbsp;return;</code>
130      *
131      * @param message a message (can be <code>null</code>)
132      * @param thr the original exception (can be <code>null</code>)
133      * @param locations the locations (can be <code>null</code>)
134      * @return a (fake) located exception
135      * @throws ProcessingException or <code>LocatedRuntimeException</code>
136      */

137     public static ProcessingException throwLocated(String JavaDoc message, Throwable JavaDoc thr, List JavaDoc locations) throws ProcessingException {
138         MultiLocatable multiloc;
139         if (thr instanceof ProcessingException) {
140             multiloc = (ProcessingException)thr;
141         } else if (thr instanceof LocatedRuntimeException) {
142             multiloc = (LocatedRuntimeException)thr;
143         } else {
144             multiloc = new ProcessingException(message, thr);
145         }
146         
147         if (locations != null) {
148             for (int i = 0; i < locations.size(); i++) {
149                 multiloc.addLocation((Location)locations.get(i));
150             }
151         }
152         
153         if (multiloc instanceof LocatedRuntimeException) {
154             throw (LocatedRuntimeException)multiloc;
155         } else {
156             throw (ProcessingException)multiloc;
157         }
158     }
159 }
160
Popular Tags