home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-cocoon-addon-1.4.9-installer.exe / ExceptionGenerator.class (.txt) < prev    next >
Encoding:
Java Class File  |  2004-07-12  |  2.6 KB  |  59 lines

  1. package org.apache.cocoon.samples.errorhandling;
  2.  
  3. import java.io.IOException;
  4. import org.apache.cocoon.ProcessingException;
  5. import org.apache.cocoon.ResourceNotFoundException;
  6. import org.apache.cocoon.environment.ObjectModelHelper;
  7. import org.apache.cocoon.environment.Request;
  8. import org.apache.cocoon.generation.AbstractGenerator;
  9. import org.xml.sax.Attributes;
  10. import org.xml.sax.SAXException;
  11. import org.xml.sax.helpers.AttributesImpl;
  12.  
  13. public class ExceptionGenerator extends AbstractGenerator {
  14.    public static final String PAR_EXCEPTION = "exception";
  15.    public static final String PAR_CODE = "code";
  16.  
  17.    public void generate() throws IOException, SAXException, ProcessingException {
  18.       Request request = ObjectModelHelper.getRequest(this.objectModel);
  19.       String exception = request.getParameter("exception");
  20.       String text = null;
  21.       if (exception == null) {
  22.          text = "No exception occured.";
  23.       } else {
  24.          if (exception.equals("validation")) {
  25.             throw new ProcessingException(new ValidationException());
  26.          }
  27.  
  28.          if (exception.equals("application")) {
  29.             throw new ProcessingException(new ApplicationException(Integer.parseInt(request.getParameter("code"))));
  30.          }
  31.  
  32.          if (exception.equals("resourceNotFound")) {
  33.             throw new ProcessingException(new ResourceNotFoundException(""));
  34.          }
  35.  
  36.          if (exception.equals("nullPointer")) {
  37.             throw new NullPointerException();
  38.          }
  39.  
  40.          if (exception.equals("error")) {
  41.             throw new Error("Error");
  42.          }
  43.  
  44.          text = "Unknown exception requested.";
  45.       }
  46.  
  47.       Attributes noAttrs = new AttributesImpl();
  48.       this.contentHandler.startDocument();
  49.       this.contentHandler.startElement("", "html", "html", noAttrs);
  50.       this.contentHandler.startElement("", "body", "body", noAttrs);
  51.       this.contentHandler.startElement("", "p", "p", noAttrs);
  52.       this.contentHandler.characters(text.toCharArray(), 0, text.length());
  53.       this.contentHandler.endElement("", "p", "p");
  54.       this.contentHandler.endElement("", "body", "body");
  55.       this.contentHandler.endElement("", "html", "html");
  56.       this.contentHandler.endDocument();
  57.    }
  58. }
  59.