01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.samples.errorhandling;
18:
19: import org.apache.avalon.framework.parameters.Parameters;
20:
21: import org.apache.cocoon.ProcessingException;
22: import org.apache.cocoon.ResourceNotFoundException;
23: import org.apache.cocoon.acting.AbstractAction;
24: import org.apache.cocoon.environment.Redirector;
25: import org.apache.cocoon.environment.SourceResolver;
26:
27: import org.xml.sax.SAXException;
28:
29: import java.io.IOException;
30: import java.util.Map;
31:
32: /**
33: * Exception action. Throws different kinds of exception depending on
34: * value of src attribute.
35: *
36: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
37: * @version $Id: ExceptionAction.java 433543 2006-08-22 06:22:54Z crossley $
38: */
39: public class ExceptionAction extends AbstractAction {
40:
41: public Map act(Redirector redirector, SourceResolver resolver,
42: Map objectModel, String source, Parameters parameters)
43: throws Exception {
44: String exception = parameters.getParameter("exception", source);
45: int code = parameters.getParameterAsInteger("code", 1);
46: exception(exception, code);
47: return null;
48: }
49:
50: public static String exception(String exception, int code)
51: throws ProcessingException, SAXException, IOException {
52: if (exception == null) {
53: return "No exception occured.";
54: } else if (exception.equals("validation")) {
55: throw new ProcessingException(new ValidationException(
56: "Validation Exception Message"));
57: } else if (exception.equals("application")) {
58: throw new ProcessingException(new ApplicationException(
59: code, "Application Exception " + code + " Message"));
60: } else if (exception.equals("processing")) {
61: throw new ProcessingException(
62: "Processing Exception Message");
63: } else if (exception.equals("notFound")) {
64: throw new ResourceNotFoundException(
65: "Resource Not Found Exception Message");
66: } else if (exception.equals("sax")) {
67: throw new SAXException("SAX Exception Message");
68: } else if (exception.equals("saxWrapped")) {
69: throw new SAXException(
70: new ProcessingException(
71: "Processing Exception Wrapped In SAX Exception Message"));
72: } else if (exception.equals("nullPointer")) {
73: throw new NullPointerException(
74: "Null Pointer Exception Message");
75: } else if (exception.equals("io")) {
76: throw new IOException("IO Exception Message");
77: } else if (exception.equals("error")) {
78: throw new Error("Error Message");
79: } else {
80: return "Unknown exception requested.";
81: }
82: }
83: }
|