KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.wsdl.toJava;
56
57 import org.jboss.axis.wsdl.symbolTable.BindingEntry;
58 import org.jboss.axis.wsdl.symbolTable.Parameter;
59 import org.jboss.axis.wsdl.symbolTable.Parameters;
60 import org.jboss.axis.wsdl.symbolTable.SymbolTable;
61 import org.jboss.axis.wsdl.symbolTable.TypeEntry;
62
63 import javax.wsdl.Binding;
64 import javax.wsdl.BindingOperation;
65 import javax.wsdl.Operation;
66 import javax.wsdl.OperationType;
67 import javax.xml.rpc.holders.BooleanHolder JavaDoc;
68 import java.io.IOException JavaDoc;
69 import java.io.PrintWriter JavaDoc;
70 import java.util.Iterator JavaDoc;
71 import java.util.List JavaDoc;
72
73 /**
74  * This is Wsdl2java's implementation template writer. It writes the <BindingName>Impl.java
75  * file which contains the <bindingName>Impl class.
76  */

77 public class JavaImplWriter extends JavaClassWriter
78 {
79    protected Binding binding;
80    protected SymbolTable symbolTable;
81    protected BindingEntry bEntry;
82
83    /**
84     * Constructor.
85     */

86    protected JavaImplWriter(Emitter emitter,
87                             BindingEntry bEntry,
88                             SymbolTable symbolTable)
89    {
90       super(emitter, bEntry.getName() + "Impl", "templateImpl");
91       this.binding = bEntry.getBinding();
92       this.symbolTable = symbolTable;
93       this.bEntry = bEntry;
94    } // ctor
95

96    /**
97     * Write the body of the binding's stub file.
98     */

99    protected void writeFileBody(PrintWriter JavaDoc pw) throws IOException JavaDoc
100    {
101       List JavaDoc operations = binding.getBindingOperations();
102       for (int i = 0; i < operations.size(); ++i)
103       {
104          BindingOperation operation = (BindingOperation)operations.get(i);
105          Operation ptOperation = operation.getOperation();
106          OperationType type = ptOperation.getStyle();
107          Parameters parameters =
108                  bEntry.getParameters(operation.getOperation());
109
110          // These operation types are not supported. The signature
111
// will be a string stating that fact.
112
if (type == OperationType.NOTIFICATION
113                  || type == OperationType.SOLICIT_RESPONSE)
114          {
115             pw.println(parameters.signature);
116             pw.println();
117          }
118          else
119          {
120             writeOperation(pw, parameters);
121          }
122       }
123    } // writeFileBody
124

125    /**
126     * Returns the appropriate implements text
127     *
128     * @return " implements <classes>"
129     */

130    protected String JavaDoc getImplementsText()
131    {
132       String JavaDoc portTypeName = (String JavaDoc)bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
133       String JavaDoc implementsText = "implements " + portTypeName;
134       return implementsText;
135    }
136
137    /**
138     * Write the implementation template for the given operation.
139     */

140    protected void writeOperation(PrintWriter JavaDoc pw, Parameters parms) throws IOException JavaDoc
141    {
142       pw.println(parms.signature + " {");
143
144       // Fill in any out parameter holders
145
Iterator JavaDoc iparam = parms.list.iterator();
146       while (iparam.hasNext())
147       {
148          Parameter param = (Parameter)iparam.next();
149          if (param.getMode() == Parameter.OUT)
150          {
151             // write a constructor for each of the parameters
152

153             BooleanHolder JavaDoc bThrow = new BooleanHolder JavaDoc(false);
154             String JavaDoc constructorString =
155                     Utils.getConstructorForParam(param, symbolTable, bThrow);
156             if (bThrow.value)
157             {
158                pw.println(" try {");
159             }
160             pw.println(" " + Utils.xmlNameToJava(param.getName())
161                     + ".value = " + constructorString + ";");
162             if (bThrow.value)
163             {
164                pw.println(" } catch (Exception e) {");
165                pw.println(" }");
166             }
167          }
168       }
169
170       // Print the return statement
171
if (parms.returnParam != null)
172       {
173          TypeEntry returnType = parms.returnParam.getType();
174          pw.print(" return ");
175
176          if (Utils.isPrimitiveType(returnType))
177          {
178             String JavaDoc returnString = returnType.getName();
179             if ("boolean".equals(returnString))
180             {
181                pw.println("false;");
182             }
183             else if ("byte".equals(returnString))
184             {
185                pw.println("(byte)-3;");
186             }
187             else if ("short".equals(returnString))
188             {
189                pw.println("(short)-3;");
190             }
191             else
192             {
193                pw.println("-3;");
194             }
195          }
196          else
197          {
198             pw.println("null;");
199          }
200       }
201       pw.println(" }");
202       pw.println();
203    } // writeOperation
204

205 } // class JavaImplWriter
206
Popular Tags