001: package org.objectweb.celtix.tools.utils;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.net.MalformedURLException;
006: import java.net.URL;
007: import java.util.ArrayList;
008: import java.util.HashMap;
009: import java.util.List;
010: import java.util.Map;
011: import java.util.StringTokenizer;
012: import java.util.logging.Logger;
013:
014: import javax.wsdl.Message;
015: import javax.wsdl.Operation;
016: import javax.wsdl.Part;
017: import javax.xml.namespace.QName;
018:
019: import com.sun.tools.xjc.api.Property;
020: import com.sun.tools.xjc.api.S2JJAXBModel;
021: import com.sun.tools.xjc.api.TypeAndAnnotation;
022: import com.sun.xml.bind.api.JAXBRIContext;
023:
024: import org.objectweb.celtix.common.logging.LogUtils;
025: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
026: import org.objectweb.celtix.tools.common.ToolConstants;
027: import org.objectweb.celtix.tools.common.ToolException;
028: import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector;
029:
030: public final class ProcessorUtil {
031: private static final Logger LOG = LogUtils
032: .getL7dLogger(ProcessorUtil.class);
033:
034: private ProcessorUtil() {
035: }
036:
037: public static String resolvePartName(Part part) {
038: return mangleNameToVariableName(part.getName());
039: }
040:
041: public static String getPartType(Part part) {
042: String typeName;
043: if (part.getElementName() != null) {
044: typeName = part.getElementName().getLocalPart();
045: } else if (part.getTypeName() != null) {
046: typeName = part.getTypeName().getLocalPart();
047: } else {
048: typeName = "BadType";
049: }
050: return typeName;
051: }
052:
053: public static String resolvePartType(Part part) {
054: return mangleNameToClassName(getPartType(part));
055: }
056:
057: public static QName getElementName(Part part) {
058: if (part == null) {
059: return null;
060: }
061: QName elementName = part.getElementName();
062: if (elementName == null) {
063: elementName = part.getTypeName();
064: }
065: return elementName;
066: }
067:
068: //
069: // support multiple -p options
070: // if user change the package name through -p namespace=package name
071: //
072: public static QName getMappedElementName(Part part,
073: ProcessorEnvironment env) {
074: QName origin = getElementName(part);
075: if (origin == null) {
076: return null;
077: }
078: if (!env.hasNamespace(origin.getNamespaceURI())) {
079: return origin;
080: }
081: return new QName(env.getCustomizedNS(origin.getNamespaceURI()),
082: origin.getLocalPart());
083: }
084:
085: public static String resolvePartType(Part part,
086: ProcessorEnvironment env) {
087: if (env != null) {
088: return resolvePartType(part, (S2JJAXBModel) env
089: .get(ToolConstants.RAW_JAXB_MODEL));
090: } else {
091: return resolvePartType(part);
092: }
093: }
094:
095: public static String resolvePartType(Part part,
096: S2JJAXBModel jaxbModel) {
097: return resolvePartType(part, jaxbModel, false);
098: }
099:
100: public static String resolvePartType(Part part,
101: S2JJAXBModel jaxbModel, boolean fullName) {
102: if (jaxbModel == null) {
103: return resolvePartType(part);
104: }
105: com.sun.tools.xjc.api.Mapping mapping = jaxbModel
106: .get(getElementName(part));
107: if (mapping == null) {
108: return resolvePartType(part);
109: }
110: if (fullName) {
111: return mapping.getType().getTypeClass().fullName();
112: } else {
113: return mapping.getType().getTypeClass().name();
114: }
115: }
116:
117: public static String resolvePartNamespace(Part part) {
118: QName qname = part.getElementName();
119: if (qname == null) {
120: qname = part.getTypeName();
121: }
122: if (qname != null) {
123: return qname.getNamespaceURI();
124: } else {
125: return null;
126: }
127: }
128:
129: public static String mangleNameToClassName(String clzName) {
130: return JAXBRIContext.mangleNameToClassName(clzName);
131: }
132:
133: public static String mangleNameToVariableName(String vName) {
134: return JAXBRIContext.mangleNameToVariableName(vName);
135: }
136:
137: public static String parsePackageName(String namespace,
138: String defaultPackageName) {
139: String packageName = (defaultPackageName != null && defaultPackageName
140: .trim().length() > 0) ? defaultPackageName : null;
141:
142: if (packageName == null) {
143: packageName = URIParserUtil.getPackageName(namespace);
144: }
145: return packageName;
146: }
147:
148: public static String getAbsolutePath(String location)
149: throws IOException {
150: if (location.startsWith("http://")) {
151: return location;
152: } else {
153: return resolvePath(new File(location).getCanonicalPath());
154: }
155: }
156:
157: public static URL getWSDLURL(String location) throws Exception {
158: if (location.startsWith("http://")) {
159: return new URL(location);
160: } else {
161: return new File(getAbsolutePath(location)).toURL();
162: }
163: }
164:
165: private static String resolvePath(String path) {
166: return path.replace('\\', '/');
167: }
168:
169: //
170: // the wrapper style will get the type info from the properties in the block
171: //
172: public static List<? extends Property> getBlock(Part part,
173: ProcessorEnvironment env) throws ToolException {
174: if (part == null) {
175: return new ArrayList<Property>();
176: }
177:
178: S2JJAXBModel jaxbModel = (S2JJAXBModel) env
179: .get(ToolConstants.RAW_JAXB_MODEL);
180:
181: // QName element = getMappedElementName(part, env);
182: QName element = getElementName(part);
183:
184: if (element != null && jaxbModel != null) {
185: com.sun.tools.xjc.api.Mapping mapping = jaxbModel
186: .get(element);
187: if (mapping != null) {
188: return mapping.getWrapperStyleDrilldown();
189: } else {
190: org.objectweb.celtix.common.i18n.Message msg = new org.objectweb.celtix.common.i18n.Message(
191: "ELEMENT_MISSING", LOG, new Object[] {
192: element.toString(), part.getName() });
193: throw new ToolException(msg);
194: // return new ArrayList<Property>();
195: }
196: } else {
197: return new ArrayList<Property>();
198: }
199: }
200:
201: public static URL[] pathToURLs(String path) {
202: StringTokenizer st = new StringTokenizer(path,
203: File.pathSeparator);
204: URL[] urls = new URL[st.countTokens()];
205: int count = 0;
206: while (st.hasMoreTokens()) {
207: File file = new File(st.nextToken());
208: URL url = null;
209: try {
210: url = file.toURL();
211: } catch (MalformedURLException e) {
212: e.printStackTrace();
213: }
214: if (url != null) {
215: urls[count++] = url;
216: }
217: }
218: if (urls.length != count) {
219: URL[] tmp = new URL[count];
220: System.arraycopy(urls, 0, tmp, 0, count);
221: urls = tmp;
222: }
223: return urls;
224: }
225:
226: public static String classNameToFilePath(String className) {
227: String str;
228: if (className.indexOf(".") < 0) {
229: return className;
230: } else {
231: str = className.replaceAll("\\.", "/");
232: }
233: return str;
234: }
235:
236: //
237: // the non-wrapper style will get the type info from the part directly
238: //
239: public static String getFullClzName(Part part,
240: ProcessorEnvironment env, boolean boxify,
241: ClassCollector collector) {
242: S2JJAXBModel jaxbModel = (S2JJAXBModel) env
243: .get(ToolConstants.RAW_JAXB_MODEL);
244:
245: QName xmlTypeName = getElementName(part);
246: String jtype = BuiltInTypesJavaMappingUtil.getJType(
247: xmlTypeName, jaxbModel, boxify);
248: String namespace = xmlTypeName.getNamespaceURI();
249: String type = resolvePartType(part, jaxbModel);
250: String userPackage = env.mapPackageName(namespace);
251:
252: if (jtype == null) {
253: jtype = collector.getTypesFullClassName(parsePackageName(
254: namespace, userPackage), type);
255: }
256:
257: if (jtype == null) {
258: if (!type.equals(resolvePartType(part))) {
259: jtype = resolvePartType(part, jaxbModel, true);
260: } else {
261: jtype = parsePackageName(namespace, userPackage) + "."
262: + type;
263: }
264: }
265:
266: return jtype;
267: }
268:
269: public static String getFullClzName(Part part,
270: ProcessorEnvironment env, ClassCollector collector) {
271: return getFullClzName(part, env, false, collector);
272:
273: }
274:
275: public static String getFileOrURLName(String fileOrURL) {
276: try {
277: try {
278: return escapeSpace(new URL(fileOrURL).toExternalForm());
279: } catch (MalformedURLException e) {
280: return new File(fileOrURL).getCanonicalFile().toURL()
281: .toExternalForm();
282: }
283: } catch (Exception e) {
284: return fileOrURL;
285: }
286: }
287:
288: private static String escapeSpace(String url) {
289: StringBuffer buf = new StringBuffer();
290: for (int i = 0; i < url.length(); i++) {
291: if (url.charAt(i) == ' ') {
292: buf.append("%20");
293: } else {
294: buf.append(url.charAt(i));
295: }
296: }
297: return buf.toString();
298: }
299:
300: public static String absolutize(String name) {
301: // absolutize all the system IDs in the input,
302: // so that we can map system IDs to DOM trees.
303: try {
304: URL baseURL = new File(".").getCanonicalFile().toURL();
305: return new URL(baseURL, name).toExternalForm();
306: } catch (IOException e) {
307: // ignore
308: }
309: return name;
310: }
311:
312: public static String getHandlerConfigFileName(String name) {
313: return name + "_handler";
314: }
315:
316: public static String boxify(QName xmlTypeName,
317: S2JJAXBModel jaxbModel) {
318: TypeAndAnnotation typeAndAnnotation = jaxbModel
319: .getJavaType(xmlTypeName);
320: if (typeAndAnnotation == null) {
321: return null;
322: }
323: return typeAndAnnotation.getTypeClass().boxify().fullName();
324: }
325:
326: @SuppressWarnings("unchecked")
327: public static boolean isWrapperStyle(Operation operation,
328: ProcessorEnvironment env) throws ToolException {
329:
330: Message inputMessage = operation.getInput() == null ? null
331: : operation.getInput().getMessage();
332: Message outputMessage = operation.getOutput() == null ? null
333: : operation.getOutput().getMessage();
334:
335: Map<String, Part> inputParts = new HashMap<String, Part>();
336: Map<String, Part> outputParts = new HashMap<String, Part>();
337:
338: if (inputMessage != null) {
339: inputParts = inputMessage.getParts();
340: }
341: if (outputMessage != null) {
342: outputParts = outputMessage.getParts();
343: }
344:
345: //
346: // RULE No.1:
347: // The operation's input and output message (if present) each contain
348: // only a single part
349: //
350: if (inputParts.size() > 1 || outputParts.size() > 1) {
351: return false;
352: }
353:
354: //
355: // RULE No.2:
356: // The input message part refers to a global element decalration whose
357: // localname
358: // is equal to the operation name
359: //
360: Part inputPart = null;
361: if (inputParts.size() == 1) {
362: inputPart = inputParts.values().iterator().next();
363: if (inputPart != null) {
364: QName inputElement = inputPart.getElementName();
365: if (inputElement == null) {
366: return false;
367: } else if (!operation.getName().equals(
368: inputElement.getLocalPart())) {
369: return false;
370: }
371: }
372: }
373: //
374: // RULE No.3:
375: // The output message part refers to a global element decalration
376: //
377: Part outputPart = null;
378: if (outputParts.size() == 1) {
379: outputPart = outputParts.values().iterator().next();
380: if (outputPart != null) {
381: QName outputElement = outputPart.getElementName();
382: if (outputElement == null) {
383: return false;
384: }
385: }
386: }
387:
388: //
389: // RULE No.4 and No5:
390: // wrapper element should be pure complex type
391: //
392: if (ProcessorUtil.getBlock(inputPart, env) == null
393: || ProcessorUtil.getBlock(outputPart, env) == null) {
394: return false;
395: }
396:
397: return true;
398: }
399: }
|