KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > wsdl > toJava > JavaFaultWriter


1 /*
2  * Copyright 2001-2004 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.axis.wsdl.toJava;
17
18 import org.apache.axis.constants.Use;
19 import org.apache.axis.wsdl.symbolTable.FaultInfo;
20 import org.apache.axis.wsdl.symbolTable.Parameter;
21 import org.apache.axis.wsdl.symbolTable.SymbolTable;
22
23 import javax.wsdl.Message;
24 import java.io.IOException JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * This is Wsdl2java's Fault Writer. It writes the <faultName>.java file.
30  * <p/>
31  * NOTE: This only writes simple type faults, the JavaTypeWriter emits
32  * faults that are complex types.
33  */

34 public class JavaFaultWriter extends JavaClassWriter {
35
36     /** Field faultMessage */
37     private Message faultMessage;
38
39     /** Field symbolTable */
40     private SymbolTable symbolTable;
41
42     /** Field literal */
43     private boolean literal;
44
45     /** Field faultName */
46     private String JavaDoc faultName;
47
48     /**
49      * Constructor.
50      *
51      * @param emitter
52      * @param symbolTable
53      * @param faultInfo
54      */

55     protected JavaFaultWriter(Emitter emitter, SymbolTable symbolTable,
56                               FaultInfo faultInfo) {
57
58         super(emitter,
59                 Utils.getFullExceptionName(faultInfo.getMessage(), symbolTable),
60                 "fault");
61
62         this.literal = faultInfo.getUse().equals(Use.LITERAL);
63         this.faultMessage = faultInfo.getMessage();
64         this.symbolTable = symbolTable;
65         this.faultName = faultInfo.getName();
66     } // ctor
67

68     /**
69      * Return "extends org.apache.axis.AxisFault ".
70      *
71      * @return
72      */

73     protected String JavaDoc getExtendsText() {
74         return "extends org.apache.axis.AxisFault ";
75     } // getExtendsText
76

77     /**
78      * Write the body of the Fault file.
79      *
80      * @param pw
81      * @throws IOException
82      */

83     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
84
85         Vector JavaDoc params = new Vector JavaDoc();
86
87         symbolTable.getParametersFromParts(params,
88                 faultMessage.getOrderedParts(null),
89                 literal, faultName, null);
90
91         // Write data members of the exception and getter methods for them
92
for (int i = 0; i < params.size(); i++) {
93             Parameter param = (Parameter) params.get(i);
94             String JavaDoc type = param.getType().getName();
95             String JavaDoc variable = Utils.xmlNameToJava(param.getName());
96
97             pw.println(" public " + type + " " + variable + ";");
98             pw.println(" public " + type + " get"
99                     + Utils.capitalizeFirstChar(variable) + "() {");
100             pw.println(" return this." + variable + ";");
101             pw.println(" }");
102         }
103
104         // Default contructor
105
pw.println();
106         pw.println(" public " + className + "() {");
107         pw.println(" }");
108         pw.println();
109
110         // Additional contructors
111
pw.println(" public " + className + "(java.lang.Exception target) {");
112         pw.println(" super(target);");
113         pw.println(" }");
114         pw.println();
115         pw.println(" public " + className + "(java.lang.String message, java.lang.Throwable t) {");
116         pw.println(" super(message, t);");
117         pw.println(" }");
118         pw.println();
119
120         // contructor that initializes data
121
if (params.size() > 0) {
122             pw.print(" public " + className + "(");
123
124             for (int i = 0; i < params.size(); i++) {
125                 if (i != 0) {
126                     pw.print(", ");
127                 }
128
129                 Parameter param = (Parameter) params.get(i);
130                 String JavaDoc type = param.getType().getName();
131                 String JavaDoc variable = Utils.xmlNameToJava(param.getName());
132
133                 pw.print(type + " " + variable);
134             }
135
136             pw.println(") {");
137
138             for (int i = 0; i < params.size(); i++) {
139                 Parameter param = (Parameter) params.get(i);
140                 String JavaDoc variable = Utils.xmlNameToJava(param.getName());
141
142                 pw.println(" this." + variable + " = " + variable + ";");
143             }
144
145             pw.println(" }");
146         }
147
148         // Method that serializes exception data (writeDetail)
149
// The QName of the element is passed in by the runtime and is found
150
// via the fault meta-data in the WSDD.
151
// NOTE: This function is also written in JavaBeanFaultWriter.java
152
pw.println();
153         pw.println(" /**");
154         pw.println(" * Writes the exception data to the faultDetails");
155         pw.println(" */");
156         pw.println(
157                 " public void writeDetails(javax.xml.namespace.QName qname, org.apache.axis.encoding.SerializationContext context) throws java.io.IOException {");
158
159         for (int i = 0; i < params.size(); i++) {
160             Parameter param = (Parameter) params.get(i);
161             String JavaDoc variable = Utils.xmlNameToJava(param.getName());
162
163             pw.println(" context.serialize(qname, null, "
164                     + Utils.wrapPrimitiveType(param.getType(), variable)
165                     + ");");
166         }
167
168         pw.println(" }");
169     } // writeFileBody
170
} // class JavaFaultWriter
171
Popular Tags