1 package org.apache.axis.message.addressing.tools.wsdl;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.PrintWriter;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Map;
9 import java.util.Set;
10
11 import javax.wsdl.Binding;
12 import javax.wsdl.Port;
13
14 import org.apache.axis.wsdl.symbolTable.BindingEntry;
15 import org.apache.axis.wsdl.symbolTable.ServiceEntry;
16 import org.apache.axis.wsdl.symbolTable.SymbolTable;
17 import org.apache.axis.wsdl.toJava.Emitter;
18 import org.apache.axis.wsdl.toJava.JavaBindingWriter;
19 import org.apache.axis.wsdl.toJava.JavaClassWriter;
20
21
22
23
24
25
26
27
28 public abstract class JavaClassWithImportsWriter extends JavaClassWriter {
29
30
31
32
33 protected final ServiceEntry sEntry;
34
35
36
37
38 protected final SymbolTable symbolTable;
39
40
41
42
43
44 private final Map<String, String> imports = new HashMap<String, String>();
45
46
47
48
49 private final Set<String> implicits = new HashSet<String>();
50
51
52
53
54
55
56
57
58
59
60 protected JavaClassWithImportsWriter(Emitter emitter, ServiceEntry sEntry,
61 SymbolTable symbolTable, String entryName, String type) {
62 super(emitter, entryName, "service");
63 this.sEntry = sEntry;
64 this.symbolTable = symbolTable;
65 }
66
67
68
69
70 @Override
71 public void generate() throws IOException {
72 initImports();
73 super.generate();
74 }
75
76
77
78
79 protected void initImports() {
80 implicits.add(getPackage());
81 implicits.add("java.lang");
82
83 addImport("org.apache.axis.message.addressing.EndpointReferenceType");
84 addImport("javax.xml.rpc.ServiceException");
85
86
87 Map<?, ?> ports = sEntry.getService().getPorts();
88 for (Object item : ports.values()) {
89 Port p = (Port)item;
90 String className = getBindingType(p);
91 if (className != null) {
92 addImport(className);
93 }
94 }
95 }
96
97
98
99
100
101
102 protected final void addImport(String className) {
103 int pos = className.lastIndexOf('.');
104 String packageName = className.substring(0, pos);
105 String simpleName = className.substring(pos + 1);
106
107 if (!implicits.contains(packageName)) {
108 imports.put(className, simpleName);
109 }
110 }
111
112
113
114
115
116
117
118 protected final String resolve(String className) {
119 String pkg = this.getPackage();
120
121 int pos = className.lastIndexOf('.');
122 String packageName = className.substring(0, pos);
123 String simpleName = className.substring(pos + 1);
124
125 if ("java.lang".equals(packageName)) {
126 return simpleName;
127 } else if (packageName.equals(pkg)) {
128 return simpleName;
129 } else {
130 String resolved = imports.get(className);
131 return (resolved == null) ? className : resolved;
132 }
133 }
134
135
136
137
138
139
140
141 protected final String getBindingType(Port port) {
142 Binding binding = port.getBinding();
143
144 if (binding == null) {
145 return null;
146 }
147
148 BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
149
150
151 if (bEntry == null || bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
152 return null;
153 }
154
155 return (String) bEntry.getDynamicVar(JavaBindingWriter.INTERFACE_NAME);
156
157 }
158
159
160
161
162
163
164 protected final void writeImports(PrintWriter pw) {
165 for (String className : imports.keySet()) {
166 pw.print("import ");
167 pw.print(className);
168 pw.println(';');
169 }
170 pw.println();
171 }
172
173
174
175
176 @Override
177 protected final void writeHeaderComments(PrintWriter pw) throws IOException {
178 int pos = this.getFileName().lastIndexOf(File.separatorChar);
179
180 pw.println("/*");
181 pw.println(" * " + this.getFileName().substring(pos + 1));
182 pw.println(" *");
183 pw.println(" * This file was auto-generated from WSDL");
184 pw.println(" * by the Addressing WSDL2Java emitter");
185 pw.println(" */");
186 }
187
188
189
190
191 @Override
192 protected final void writeFileHeader(PrintWriter pw) throws IOException {
193 writeHeaderComments(pw);
194 writePackage(pw);
195 writeImports(pw);
196
197 writeClassComment(pw);
198
199 StringBuffer sb = new StringBuffer();
200 sb.append(getClassModifiers());
201 sb.append(getClassText());
202 sb.append(getClassName());
203
204 String extendsText = getExtendsText();
205 if (extendsText != null) {
206 pw.println(sb);
207 sb.setLength(0);
208 sb.append(" ");
209 sb.append(getExtendsText());
210 }
211
212 String implementsText = getImplementsText();
213 if (implementsText != null) {
214 pw.println(sb);
215 sb.setLength(0);
216 sb.append(" ");
217 sb.append(getImplementsText());
218 }
219 sb.append(" {");
220 pw.println(sb);
221 }
222
223
224
225
226
227
228
229 protected abstract void writeClassComment(PrintWriter pw) throws IOException;
230 }