KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > samples > errorhandling > ExceptionAction


1 /*
2  * Copyright 2005 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.samples.errorhandling;
17
18 import org.apache.avalon.framework.parameters.Parameters;
19
20 import org.apache.cocoon.ProcessingException;
21 import org.apache.cocoon.ResourceNotFoundException;
22 import org.apache.cocoon.acting.AbstractAction;
23 import org.apache.cocoon.environment.Redirector;
24 import org.apache.cocoon.environment.SourceResolver;
25
26 import org.xml.sax.SAXException JavaDoc;
27
28 import java.io.IOException JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Exception action. Throws different kinds of exception depending on
33  * value of src attribute.
34  *
35  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
36  * @version $Id: ExceptionAction.java 157096 2005-03-11 16:18:02Z vgritsenko $
37  */

38 public class ExceptionAction extends AbstractAction {
39
40     public Map JavaDoc act(Redirector redirector, SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc source, Parameters parameters)
41     throws Exception JavaDoc {
42         String JavaDoc exception = parameters.getParameter("exception", source);
43         int code = parameters.getParameterAsInteger("code", 1);
44         exception(exception, code);
45         return null;
46     }
47
48     public static String JavaDoc exception(String JavaDoc exception, int code)
49     throws ProcessingException , SAXException JavaDoc, IOException JavaDoc {
50         if (exception == null) {
51             return "No exception occured.";
52         } else if (exception.equals("validation")) {
53             throw new ProcessingException(new ValidationException("Validation Exception Message"));
54         } else if (exception.equals("application")) {
55             throw new ProcessingException(new ApplicationException(code, "Application Exception " + code + " Message"));
56         } else if (exception.equals("processing")) {
57             throw new ProcessingException("Processing Exception Message");
58         } else if (exception.equals("notFound")) {
59             throw new ResourceNotFoundException("Resource Not Found Exception Message");
60         } else if (exception.equals("sax")) {
61             throw new SAXException JavaDoc("SAX Exception Message");
62         } else if (exception.equals("saxWrapped")) {
63             throw new SAXException JavaDoc(new ProcessingException("Processing Exception Wrapped In SAX Exception Message"));
64         } else if (exception.equals("nullPointer")) {
65             throw new NullPointerException JavaDoc("Null Pointer Exception Message");
66         } else if (exception.equals("io")) {
67             throw new IOException JavaDoc("IO Exception Message");
68         } else if (exception.equals("error")) {
69             throw new Error JavaDoc("Error Message");
70         } else {
71             return "Unknown exception requested.";
72         }
73     }
74 }
75
Popular Tags