KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-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.environment.SourceResolver;
22 import org.apache.cocoon.generation.AbstractGenerator;
23 import org.apache.cocoon.xml.XMLUtils;
24
25 import org.xml.sax.SAXException JavaDoc;
26
27 import java.io.IOException JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * Exception generator. Throws different kinds of exception depending on
32  * value of src attribute.
33  *
34  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
35  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
36  * @version $Id: ExceptionGenerator.java 164808 2005-04-26 16:07:03Z vgritsenko $
37  */

38 public class ExceptionGenerator extends AbstractGenerator {
39
40     private String JavaDoc exception;
41     private int code;
42
43     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters parameters)
44     throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
45         super.setup(resolver, objectModel, src, parameters);
46
47         this.exception = parameters.getParameter("exception", super.source);
48         this.code = Integer.parseInt(parameters.getParameter("code", "0"));
49
50         // Throw exception in the setup phase?
51
if (parameters.getParameterAsBoolean("setup", false)) {
52             ExceptionAction.exception(this.exception, this.code);
53         }
54     }
55
56     /**
57      * Overridden from superclass.
58      */

59     public void generate()
60     throws ProcessingException , SAXException JavaDoc, IOException JavaDoc {
61         this.contentHandler.startDocument();
62         this.contentHandler.startElement("", "html", "html", XMLUtils.EMPTY_ATTRIBUTES);
63         this.contentHandler.startElement("", "body", "body", XMLUtils.EMPTY_ATTRIBUTES);
64         this.contentHandler.startElement("", "p", "p", XMLUtils.EMPTY_ATTRIBUTES);
65
66         String JavaDoc text = ExceptionAction.exception(this.exception, this.code);
67         this.contentHandler.characters(text.toCharArray(), 0, text.length());
68
69         this.contentHandler.endElement("", "p", "p");
70         this.contentHandler.endElement("", "body", "body");
71         this.contentHandler.endElement("", "html", "html");
72         this.contentHandler.endDocument();
73     }
74 }
75
Popular Tags