View Javadoc

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   * Class writer that manages an imports clause list, so generated code can
23   * be simplified.
24   *
25   * @author Rodrigo Ruiz
26   * @version $Revision: 14 $
27   */
28  public abstract class JavaClassWithImportsWriter extends JavaClassWriter {
29  
30    /**
31     * Service.
32     */
33    protected final ServiceEntry sEntry;
34  
35    /**
36     * Symbol table.
37     */
38    protected final SymbolTable symbolTable;
39  
40  
41    /**
42     * Map of imported classes.
43     */
44    private final Map<String, String> imports = new HashMap<String, String>();
45  
46    /**
47     * List of packages that are implicitly imported.
48     */
49    private final Set<String> implicits = new HashSet<String>();
50  
51    /**
52     * Creates an instance.
53     *
54     * @param emitter      Emitter
55     * @param sEntry       Service
56     * @param symbolTable  Symbol table
57     * @param entryName    Name of the generated entry
58     * @param type         Entry type
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     * {@inheritDoc}
69     */
70    @Override
71    public void generate() throws IOException {
72      initImports();
73      super.generate();
74    }
75  
76    /**
77     * Populates the imports map.
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      // Port types
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     * Adds an import clause.
99     *
100    * @param className Class to be imported
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    * Resolves a class name, and returns its simple name if it is imported.
114    *
115    * @param className Fully qualified class name
116    * @return Simplified class name
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    * Gets the name of the binding type associated to the specified Port.
137    *
138    * @param port Port to process
139    * @return Binding class name
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     // If this isn't an SOAP binding, skip it
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    * Writes the imports clauses.
161    *
162    * @param pw To print to
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    * {@inheritDoc}
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    * {@inheritDoc}
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    * Generates the generated class javadoc comment.
225    *
226    * @param pw Writer to print to
227    * @throws IOException If an error occurs
228    */
229   protected abstract void writeClassComment(PrintWriter pw) throws IOException;
230 }