KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > tools > strongtyper > ExceptionGenerator


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.tools.strongtyper;
31
32 public class ExceptionGenerator extends Generator
33 {
34   public ExceptionGenerator()
35   {
36   }
37
38   public String JavaDoc getPackageName()
39   {
40     return getItfPackageName();
41   }
42
43   public String JavaDoc getClassName()
44   {
45     return getExceptionClassName() + (isAbstract() ? getGeneratedSuffix() : "");
46   }
47
48   public String JavaDoc getCode() throws Exception JavaDoc
49   {
50     StringBuffer JavaDoc code = new StringBuffer JavaDoc(5000);
51     if (getPackageName().trim().length() > 0) code.append("package " + getPackageName() + ";\n\n");
52
53     code.append("import java.io.*;\n" + "import com.genimen.djeneric.repository.exceptions.*;\n" + "\n");
54
55     code.append(StrongTyper.getRegenerationTags(1));
56
57     code
58         .append("public class "
59                 + getClassName()
60                 + " extends DjenericException implements Serializable\n"
61                 + "{ private static final String CHAINED_MSG = \"Chained to: \";\n"
62                 + "\n"
63                 + " private String _exceptionId = \"ERRORTYPE_UNKNOWN\";\n"
64                 + " private String _stackTrace = null;\n"
65                 + " public static String OBJECT_NOT_FOUND = \"OBJECT_NOT_FOUND\";\n"
66                 + " public static String MULTIPLE_OBJECTS_FOUND = \"MULTIPLE_OBJECTS_FOUND\";\n"
67                 + " transient private boolean _ignoreMyOwnStack = false; // transient flag to recognize remote (serialized) exceptions\n"
68                 + "\n" + " public " + getClassName() + "()\n" + " {\n" + " storeStack(this);\n" + " }\n" + "\n"
69                 + " public " + getClassName() + "(Throwable chainThis)\n" + " {\n"
70                 + " super(chainThis.getMessage());\n" + " storeStack(chainThis);\n" + " chain(chainThis);\n"
71                 + " }\n" + "\n" + " public " + getClassName() + "(String message)\n" + " {\n"
72                 + " super(message);\n" + " storeStack(this);\n" + " }\n" + "\n" + " public " + getClassName()
73                 + "(String message, String exceptionId)\n" + " {\n" + " super(message);\n"
74                 + " _exceptionId = exceptionId;\n" + " storeStack(this);\n" + " }\n" + "\n" + " public "
75                 + getClassName() + "(String message, String exceptionId, Throwable chainThis)\n" + " {\n"
76                 + " super(message);\n" + " _exceptionId = exceptionId;\n" + " storeStack(chainThis);\n"
77                 + " chain(chainThis);\n" + " }\n" + "\n" + " private void storeStack(Throwable theEx)\n" + " {\n"
78                 + " _stackTrace = stack2String(theEx);\n"
79                 + " _ignoreMyOwnStack = true; // transient flag to recognize remote (serialized) exceptions\n"
80                 + " }\n" + "\n" + " private void chain(Throwable chainThis)\n" + " {\n"
81                 + " if(chainThis instanceof " + getClassName() + ")\n" + " { " + getClassName() + " bex = ("
82                 + getClassName() + ") chainThis;\n" + " _exceptionId = bex._exceptionId;\n" + " }\n"
83                 + " }\n" + "\n" + " public String getExceptionId()\n" + " {\n" + " return _exceptionId;\n"
84                 + " }\n" + "\n" + " /**\n" + " * Prints the stack trace of the Thrown exception.\n" + " */\n"
85                 + " public void printStackTrace()\n" + " {\n" + " if(_stackTrace != null)\n"
86                 + " { System.err.println(_stackTrace);\n"
87                 + " if(!_ignoreMyOwnStack) System.err.print(CHAINED_MSG);\n" + " }\n"
88                 + " if(!_ignoreMyOwnStack) super.printStackTrace();\n" + " }\n" + "\n" + " /**\n"
89                 + " * Prints the stack trace of the Thrown exception in a printStream.\n" + " *\n"
90                 + " * @param printStream printStream which should be used to print the stacktrace.\n" + " */\n"
91                 + " public void printStackTrace(PrintStream printStream)\n" + " {\n"
92                 + " if(_stackTrace != null)\n" + " { printStream.println(_stackTrace);\n"
93                 + " if(!_ignoreMyOwnStack) printStream.print(CHAINED_MSG);\n" + " }\n"
94                 + " if(!_ignoreMyOwnStack) super.printStackTrace(printStream);\n" + " }\n" + " /**\n"
95                 + " * Prints the stack trace of the thrown exception in a printWriter.\n" + " *\n"
96                 + " * @param writer printWriter which should be used to print the stacktrace.\n" + " */\n"
97                 + " public void printStackTrace(PrintWriter writer)\n" + " {\n" + " if(_stackTrace != null)\n"
98                 + " { writer.println(_stackTrace);\n"
99                 + " if(!_ignoreMyOwnStack) writer.print(CHAINED_MSG);\n" + " }\n"
100                 + " if(!_ignoreMyOwnStack) super.printStackTrace(writer);\n" + " }\n" + "\n"
101                 + " public void setExceptionID(String exceptionId)\n" + " {\n" + " _exceptionId = exceptionId;\n"
102                 + " }\n" + "\n" + " public String toString()\n" + " {\n"
103                 + " String exceptionMessage = getClass().getName()+\": \";\n" + "\n"
104                 + " if(getMessage() != null) exceptionMessage += getMessage() + \"\\n\";\n"
105                 + " exceptionMessage += \"Exception ID = \" + _exceptionId + \"\\n\";\n"
106                 + " return exceptionMessage.trim();\n" + " }\n" + "\n"
107                 + " private String stack2String(Throwable x)\n" + " {\n" + " if(x == null) return \"\";\n" + "\n"
108                 + " StringWriter stringWriter = new StringWriter();\n"
109                 + " java.io.PrintWriter stackTrace = new java.io.PrintWriter(stringWriter);\n"
110                 + " x.printStackTrace(stackTrace);\n" + " String result = stringWriter.toString().trim();\n"
111                 + " if(result.length() == 0) result = null;\n" + "\n" + " return result;\n" + " }\n");
112     code.append(StrongTyper.getRegenerationTags(0));
113     code.append("}\n");
114     return code.toString();
115   }
116 }
Popular Tags