KickJava   Java API By Example, From Geeks To Geeks.

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


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.wsdl.symbolTable.BindingEntry;
19 import org.apache.axis.wsdl.symbolTable.Parameter;
20 import org.apache.axis.wsdl.symbolTable.Parameters;
21 import org.apache.axis.wsdl.symbolTable.SymbolTable;
22 import org.apache.axis.wsdl.symbolTable.TypeEntry;
23
24 import javax.wsdl.Binding;
25 import javax.wsdl.BindingOperation;
26 import javax.wsdl.Operation;
27 import javax.wsdl.OperationType;
28 import javax.xml.rpc.holders.BooleanHolder JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * This is Wsdl2java's implementation template writer. It writes the <BindingName>Impl.java
36  * file which contains the <bindingName>Impl class.
37  */

38 public class JavaImplWriter extends JavaClassWriter {
39
40     /** Field binding */
41     protected Binding binding;
42
43     /** Field symbolTable */
44     protected SymbolTable symbolTable;
45
46     /** Field bEntry */
47     protected BindingEntry bEntry;
48
49     /**
50      * Constructor.
51      *
52      * @param emitter
53      * @param bEntry
54      * @param symbolTable
55      */

56     protected JavaImplWriter(Emitter emitter, BindingEntry bEntry,
57                              SymbolTable symbolTable)
58     {
59         super(emitter, ( emitter.getImplementationClassName() == null ?
60                 bEntry.getName() + "Impl" :
61                 emitter.getImplementationClassName()), "templateImpl");
62         
63         this.binding = bEntry.getBinding();
64         this.symbolTable = symbolTable;
65         this.bEntry = bEntry;
66     } // ctor
67

68     /**
69      * Write the body of the binding's stub file.
70      *
71      * @param pw
72      * @throws IOException
73      */

74     protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc {
75
76         List JavaDoc operations = binding.getBindingOperations();
77
78         for (int i = 0; i < operations.size(); ++i) {
79             BindingOperation operation = (BindingOperation) operations.get(i);
80             Operation ptOperation = operation.getOperation();
81             OperationType type = ptOperation.getStyle();
82             Parameters parameters =
83                     bEntry.getParameters(operation.getOperation());
84
85             // These operation types are not supported. The signature
86
// will be a string stating that fact.
87
if ((OperationType.NOTIFICATION.equals(type))
88                     || (OperationType.SOLICIT_RESPONSE.equals(type))) {
89                 pw.println(parameters.signature);
90                 pw.println();
91             } else {
92                 writeOperation(pw, parameters);
93             }
94         }
95     } // writeFileBody
96

97     /**
98      * Returns the appropriate implements text
99      *
100      * @return " implements <classes>"
101      */

102     protected String JavaDoc getImplementsText() {
103
104         String JavaDoc portTypeName =
105                 (String JavaDoc) bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
106         String JavaDoc implementsText = "implements " + portTypeName;
107
108         return implementsText;
109     }
110
111     /**
112      * Write the implementation template for the given operation.
113      *
114      * @param pw
115      * @param parms
116      * @throws IOException
117      */

118     protected void writeOperation(PrintWriter JavaDoc pw, Parameters parms)
119             throws IOException JavaDoc {
120
121         pw.println(parms.signature + " {");
122
123         // Fill in any out parameter holders
124
Iterator JavaDoc iparam = parms.list.iterator();
125
126         while (iparam.hasNext()) {
127             Parameter param = (Parameter) iparam.next();
128
129             if (param.getMode() == Parameter.OUT) {
130
131                 // write a constructor for each of the parameters
132
BooleanHolder JavaDoc bThrow = new BooleanHolder JavaDoc(false);
133                 String JavaDoc constructorString =
134                         Utils.getConstructorForParam(param, symbolTable, bThrow);
135
136                 if (bThrow.value) {
137                     pw.println(" try {");
138                 }
139
140                 pw.println(" " + Utils.xmlNameToJava(param.getName())
141                         + ".value = " + constructorString + ";");
142
143                 if (bThrow.value) {
144                     pw.println(" } catch (Exception e) {");
145                     pw.println(" }");
146                 }
147             }
148         }
149
150         // Print the return statement
151
Parameter returnParam = parms.returnParam;
152         if (returnParam != null) {
153             TypeEntry returnType = returnParam.getType();
154
155             pw.print(" return ");
156
157             if (!returnParam.isOmittable() &&
158                     Utils.isPrimitiveType(returnType)) {
159                 String JavaDoc returnString = returnType.getName();
160
161                 if ("boolean".equals(returnString)) {
162                     pw.println("false;");
163                 } else if ("byte".equals(returnString)) {
164                     pw.println("(byte)-3;");
165                 } else if ("short".equals(returnString)) {
166                     pw.println("(short)-3;");
167                 } else {
168                     pw.println("-3;");
169                 }
170             } else {
171                 pw.println("null;");
172             }
173         }
174
175         pw.println(" }");
176         pw.println();
177     } // writeOperation
178
} // class JavaImplWriter
179
Popular Tags