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.environment.SourceResolver;
23: import org.apache.cocoon.generation.AbstractGenerator;
24: import org.apache.cocoon.xml.XMLUtils;
25:
26: import org.xml.sax.SAXException;
27:
28: import java.io.IOException;
29: import java.util.Map;
30:
31: /**
32: * Exception generator. Throws different kinds of exception depending on
33: * value of src attribute.
34: *
35: * @author <a href="mailto:bluetkemeier@s-und-n.de">Björn Lütkemeier</a>
36: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
37: * @version $Id: ExceptionGenerator.java 433543 2006-08-22 06:22:54Z crossley $
38: */
39: public class ExceptionGenerator extends AbstractGenerator {
40:
41: private String exception;
42: private int code;
43:
44: public void setup(SourceResolver resolver, Map objectModel,
45: String src, Parameters parameters)
46: throws ProcessingException, SAXException, IOException {
47: super .setup(resolver, objectModel, src, parameters);
48:
49: this .exception = parameters.getParameter("exception",
50: super .source);
51: this .code = Integer.parseInt(parameters.getParameter("code",
52: "0"));
53:
54: // Throw exception in the setup phase?
55: if (parameters.getParameterAsBoolean("setup", false)) {
56: ExceptionAction.exception(this .exception, this .code);
57: }
58: }
59:
60: /**
61: * Overridden from superclass.
62: */
63: public void generate() throws ProcessingException, SAXException,
64: IOException {
65: this .contentHandler.startDocument();
66: this .contentHandler.startElement("", "html", "html",
67: XMLUtils.EMPTY_ATTRIBUTES);
68: this .contentHandler.startElement("", "body", "body",
69: XMLUtils.EMPTY_ATTRIBUTES);
70: this .contentHandler.startElement("", "p", "p",
71: XMLUtils.EMPTY_ATTRIBUTES);
72:
73: String text = ExceptionAction.exception(this .exception,
74: this .code);
75: this .contentHandler.characters(text.toCharArray(), 0, text
76: .length());
77:
78: this .contentHandler.endElement("", "p", "p");
79: this .contentHandler.endElement("", "body", "body");
80: this .contentHandler.endElement("", "html", "html");
81: this.contentHandler.endDocument();
82: }
83: }
|