KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > tools > utils > ProcessorUtil


1 package org.objectweb.celtix.tools.utils;
2
3 import java.io.File JavaDoc;
4 import java.io.IOException JavaDoc;
5 import java.net.MalformedURLException JavaDoc;
6 import java.net.URL JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.StringTokenizer JavaDoc;
12 import java.util.logging.Logger JavaDoc;
13
14 import javax.wsdl.Message;
15 import javax.wsdl.Operation;
16 import javax.wsdl.Part;
17 import javax.xml.namespace.QName JavaDoc;
18
19 import com.sun.tools.xjc.api.Property;
20 import com.sun.tools.xjc.api.S2JJAXBModel;
21 import com.sun.tools.xjc.api.TypeAndAnnotation;
22 import com.sun.xml.bind.api.JAXBRIContext;
23
24 import org.objectweb.celtix.common.logging.LogUtils;
25 import org.objectweb.celtix.tools.common.ProcessorEnvironment;
26 import org.objectweb.celtix.tools.common.ToolConstants;
27 import org.objectweb.celtix.tools.common.ToolException;
28 import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector;
29
30 public final class ProcessorUtil {
31     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ProcessorUtil.class);
32     private ProcessorUtil() {
33     }
34
35     public static String JavaDoc resolvePartName(Part part) {
36         return mangleNameToVariableName(part.getName());
37     }
38
39     public static String JavaDoc getPartType(Part part) {
40         String JavaDoc typeName;
41         if (part.getElementName() != null) {
42             typeName = part.getElementName().getLocalPart();
43         } else if (part.getTypeName() != null) {
44             typeName = part.getTypeName().getLocalPart();
45         } else {
46             typeName = "BadType";
47         }
48         return typeName;
49     }
50
51     public static String JavaDoc resolvePartType(Part part) {
52         return mangleNameToClassName(getPartType(part));
53     }
54
55     public static QName JavaDoc getElementName(Part part) {
56         if (part == null) {
57             return null;
58         }
59         QName JavaDoc elementName = part.getElementName();
60         if (elementName == null) {
61             elementName = part.getTypeName();
62         }
63         return elementName;
64     }
65
66     //
67
// support multiple -p options
68
// if user change the package name through -p namespace=package name
69
//
70
public static QName JavaDoc getMappedElementName(Part part, ProcessorEnvironment env) {
71         QName JavaDoc origin = getElementName(part);
72         if (origin == null) {
73             return null;
74         }
75         if (!env.hasNamespace(origin.getNamespaceURI())) {
76             return origin;
77         }
78         return new QName JavaDoc(env.getCustomizedNS(origin.getNamespaceURI()), origin.getLocalPart());
79     }
80
81     public static String JavaDoc resolvePartType(Part part, ProcessorEnvironment env) {
82         if (env != null) {
83             return resolvePartType(part, (S2JJAXBModel)env.get(ToolConstants.RAW_JAXB_MODEL));
84         } else {
85             return resolvePartType(part);
86         }
87     }
88
89     public static String JavaDoc resolvePartType(Part part, S2JJAXBModel jaxbModel) {
90         return resolvePartType(part, jaxbModel, false);
91     }
92
93     public static String JavaDoc resolvePartType(Part part, S2JJAXBModel jaxbModel, boolean fullName) {
94         if (jaxbModel == null) {
95             return resolvePartType(part);
96         }
97         com.sun.tools.xjc.api.Mapping mapping = jaxbModel.get(getElementName(part));
98         if (mapping == null) {
99             return resolvePartType(part);
100         }
101         if (fullName) {
102             return mapping.getType().getTypeClass().fullName();
103         } else {
104             return mapping.getType().getTypeClass().name();
105         }
106     }
107
108     public static String JavaDoc resolvePartNamespace(Part part) {
109         QName JavaDoc qname = part.getElementName();
110         if (qname == null) {
111             qname = part.getTypeName();
112         }
113         if (qname != null) {
114             return qname.getNamespaceURI();
115         } else {
116             return null;
117         }
118     }
119
120     public static String JavaDoc mangleNameToClassName(String JavaDoc clzName) {
121         return JAXBRIContext.mangleNameToClassName(clzName);
122     }
123
124     public static String JavaDoc mangleNameToVariableName(String JavaDoc vName) {
125         return JAXBRIContext.mangleNameToVariableName(vName);
126     }
127
128     public static String JavaDoc parsePackageName(String JavaDoc namespace, String JavaDoc defaultPackageName) {
129         String JavaDoc packageName = (defaultPackageName != null && defaultPackageName.trim().length() > 0)
130             ? defaultPackageName : null;
131
132         if (packageName == null) {
133             packageName = URIParserUtil.getPackageName(namespace);
134         }
135         return packageName;
136     }
137
138     public static String JavaDoc getAbsolutePath(String JavaDoc location) throws IOException JavaDoc {
139         if (location.startsWith("http://")) {
140             return location;
141         } else {
142             return resolvePath(new File JavaDoc(location).getCanonicalPath());
143         }
144     }
145
146     public static URL JavaDoc getWSDLURL(String JavaDoc location) throws Exception JavaDoc {
147         if (location.startsWith("http://")) {
148             return new URL JavaDoc(location);
149         } else {
150             return new File JavaDoc(getAbsolutePath(location)).toURL();
151         }
152     }
153
154     private static String JavaDoc resolvePath(String JavaDoc path) {
155         return path.replace('\\', '/');
156     }
157
158     //
159
// the wrapper style will get the type info from the properties in the block
160
//
161
public static List JavaDoc<? extends Property> getBlock(Part part, ProcessorEnvironment env)
162         throws ToolException {
163         if (part == null) {
164             return new ArrayList JavaDoc<Property>();
165         }
166
167         S2JJAXBModel jaxbModel = (S2JJAXBModel)env.get(ToolConstants.RAW_JAXB_MODEL);
168
169         // QName element = getMappedElementName(part, env);
170
QName JavaDoc element = getElementName(part);
171
172         if (element != null && jaxbModel != null) {
173             com.sun.tools.xjc.api.Mapping mapping = jaxbModel.get(element);
174             if (mapping != null) {
175                 return mapping.getWrapperStyleDrilldown();
176             } else {
177                 org.objectweb.celtix.common.i18n.Message msg =
178                     new org.objectweb.celtix.common.i18n.Message("ELEMENT_MISSING",
179                                                                  LOG,
180                                                                  new Object JavaDoc[]{element.toString(),
181                                                                               part.getName()});
182                 throw new ToolException(msg);
183                 // return new ArrayList<Property>();
184
}
185         } else {
186             return new ArrayList JavaDoc<Property>();
187         }
188     }
189
190     public static URL JavaDoc[] pathToURLs(String JavaDoc path) {
191         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, File.pathSeparator);
192         URL JavaDoc[] urls = new URL JavaDoc[st.countTokens()];
193         int count = 0;
194         while (st.hasMoreTokens()) {
195             File JavaDoc file = new File JavaDoc(st.nextToken());
196             URL JavaDoc url = null;
197             try {
198                 url = file.toURL();
199             } catch (MalformedURLException JavaDoc e) {
200                 e.printStackTrace();
201             }
202             if (url != null) {
203                 urls[count++] = url;
204             }
205         }
206         if (urls.length != count) {
207             URL JavaDoc[] tmp = new URL JavaDoc[count];
208             System.arraycopy(urls, 0, tmp, 0, count);
209             urls = tmp;
210         }
211         return urls;
212     }
213
214     public static String JavaDoc classNameToFilePath(String JavaDoc className) {
215         String JavaDoc str;
216         if (className.indexOf(".") < 0) {
217             return className;
218         } else {
219             str = className.replaceAll("\\.", "/");
220         }
221         return str;
222     }
223
224     //
225
// the non-wrapper style will get the type info from the part directly
226
//
227
public static String JavaDoc getFullClzName(Part part, ProcessorEnvironment env, boolean boxify,
228                                         ClassCollector collector) {
229         S2JJAXBModel jaxbModel = (S2JJAXBModel)env.get(ToolConstants.RAW_JAXB_MODEL);
230
231         QName JavaDoc xmlTypeName = getElementName(part);
232         String JavaDoc jtype = BuiltInTypesJavaMappingUtil.getJType(xmlTypeName, jaxbModel, boxify);
233         String JavaDoc namespace = xmlTypeName.getNamespaceURI();
234         String JavaDoc type = resolvePartType(part, jaxbModel);
235         String JavaDoc userPackage = env.mapPackageName(namespace);
236
237         if (jtype == null) {
238             jtype = collector.getTypesFullClassName(parsePackageName(namespace, userPackage), type);
239         }
240
241         if (jtype == null) {
242             if (!type.equals(resolvePartType(part))) {
243                 jtype = resolvePartType(part, jaxbModel, true);
244             } else {
245                 jtype = parsePackageName(namespace, userPackage) + "." + type;
246             }
247         }
248
249         return jtype;
250     }
251
252     public static String JavaDoc getFullClzName(Part part, ProcessorEnvironment env, ClassCollector collector) {
253         return getFullClzName(part, env, false, collector);
254
255     }
256
257     public static String JavaDoc getFileOrURLName(String JavaDoc fileOrURL) {
258         try {
259             try {
260                 return escapeSpace(new URL JavaDoc(fileOrURL).toExternalForm());
261             } catch (MalformedURLException JavaDoc e) {
262                 return new File JavaDoc(fileOrURL).getCanonicalFile().toURL().toExternalForm();
263             }
264         } catch (Exception JavaDoc e) {
265             return fileOrURL;
266         }
267     }
268
269     private static String JavaDoc escapeSpace(String JavaDoc url) {
270         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
271         for (int i = 0; i < url.length(); i++) {
272             if (url.charAt(i) == ' ') {
273                 buf.append("%20");
274             } else {
275                 buf.append(url.charAt(i));
276             }
277         }
278         return buf.toString();
279     }
280
281     public static String JavaDoc absolutize(String JavaDoc name) {
282         // absolutize all the system IDs in the input,
283
// so that we can map system IDs to DOM trees.
284
try {
285             URL JavaDoc baseURL = new File JavaDoc(".").getCanonicalFile().toURL();
286             return new URL JavaDoc(baseURL, name).toExternalForm();
287         } catch (IOException JavaDoc e) {
288             // ignore
289
}
290         return name;
291     }
292
293     public static String JavaDoc getHandlerConfigFileName(String JavaDoc name) {
294         return name + "_handler";
295     }
296
297     public static String JavaDoc boxify(QName JavaDoc xmlTypeName, S2JJAXBModel jaxbModel) {
298         TypeAndAnnotation typeAndAnnotation = jaxbModel.getJavaType(xmlTypeName);
299         if (typeAndAnnotation == null) {
300             return null;
301         }
302         return typeAndAnnotation.getTypeClass().boxify().fullName();
303     }
304
305     @SuppressWarnings JavaDoc("unchecked")
306     public static boolean isWrapperStyle(Operation operation, ProcessorEnvironment env) throws ToolException {
307
308         Message inputMessage = operation.getInput() == null ? null : operation.getInput().getMessage();
309         Message outputMessage = operation.getOutput() == null ? null : operation.getOutput().getMessage();
310
311         Map JavaDoc<String JavaDoc, Part> inputParts = new HashMap JavaDoc<String JavaDoc, Part>();
312         Map JavaDoc<String JavaDoc, Part> outputParts = new HashMap JavaDoc<String JavaDoc, Part>();
313
314         if (inputMessage != null) {
315             inputParts = inputMessage.getParts();
316         }
317         if (outputMessage != null) {
318             outputParts = outputMessage.getParts();
319         }
320
321         //
322
// RULE No.1:
323
// The operation's input and output message (if present) each contain
324
// only a single part
325
//
326
if (inputParts.size() > 1 || outputParts.size() > 1) {
327             return false;
328         }
329
330         //
331
// RULE No.2:
332
// The input message part refers to a global element decalration whose
333
// localname
334
// is equal to the operation name
335
//
336
Part inputPart = null;
337         if (inputParts.size() == 1) {
338             inputPart = inputParts.values().iterator().next();
339             if (inputPart != null) {
340                 QName JavaDoc inputElement = inputPart.getElementName();
341                 if (inputElement == null) {
342                     return false;
343                 } else if (!operation.getName().equals(inputElement.getLocalPart())) {
344                     return false;
345                 }
346             }
347         }
348         //
349
// RULE No.3:
350
// The output message part refers to a global element decalration
351
//
352
Part outputPart = null;
353         if (outputParts.size() == 1) {
354             outputPart = outputParts.values().iterator().next();
355             if (outputPart != null) {
356                 QName JavaDoc outputElement = outputPart.getElementName();
357                 if (outputElement == null) {
358                     return false;
359                 }
360             }
361         }
362
363         //
364
// RULE No.4 and No5:
365
// wrapper element should be pure complex type
366
//
367
if (ProcessorUtil.getBlock(inputPart, env) == null
368             || ProcessorUtil.getBlock(outputPart, env) == null) {
369             return false;
370         }
371
372         return true;
373     }
374 }
375
Popular Tags