001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.tools.wsdlto.frontend.jaxws.processor.internal;
019:
020: import java.io.File;
021: import java.io.IOException;
022: import java.net.MalformedURLException;
023: import java.net.URL;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.List;
027:
028: import javax.xml.namespace.QName;
029:
030: import org.w3c.dom.DOMException;
031: import org.w3c.dom.Document;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.NamedNodeMap;
034: import org.w3c.dom.Node;
035:
036: import org.apache.cxf.helpers.JavaUtils;
037: import org.apache.cxf.jaxb.JAXBUtils;
038: import org.apache.cxf.service.model.MessagePartInfo;
039: import org.apache.cxf.service.model.ServiceInfo;
040: import org.apache.cxf.tools.common.ToolContext;
041: import org.apache.cxf.tools.common.model.DefaultValueWriter;
042: import org.apache.cxf.tools.util.ClassCollector;
043: import org.apache.cxf.tools.util.NameUtil;
044: import org.apache.cxf.tools.util.URIParserUtil;
045: import org.apache.cxf.tools.wsdlto.core.DataBindingProfile;
046: import org.apache.cxf.wsdl11.WSDLServiceBuilder;
047: import org.apache.ws.commons.schema.XmlSchema;
048: import org.apache.ws.commons.schema.XmlSchemaCollection;
049: import org.apache.ws.commons.schema.XmlSchemaComplexType;
050: import org.apache.ws.commons.schema.XmlSchemaElement;
051: import org.apache.ws.commons.schema.XmlSchemaForm;
052: import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
053: import org.apache.ws.commons.schema.XmlSchemaSequence;
054:
055: public final class ProcessorUtil {
056: private static final String KEYWORDS_PREFIX = "_";
057:
058: private ProcessorUtil() {
059: }
060:
061: public static String resolvePartName(MessagePartInfo part) {
062: return NameUtil.mangleNameToVariableName(part.getName()
063: .getLocalPart());
064: }
065:
066: public static String getPartType(MessagePartInfo part) {
067: return part.getConcreteName().getLocalPart();
068: }
069:
070: public static String resolvePartType(MessagePartInfo part) {
071: return NameUtil.mangleNameToClassName(getPartType(part));
072: }
073:
074: public static String getType(MessagePartInfo part,
075: ToolContext context, boolean fullname) {
076: String type = "";
077: DataBindingProfile dataBinding = context
078: .get(DataBindingProfile.class);
079: if (part.isElement()) {
080: type = dataBinding.getType(getElementName(part), true);
081: } else {
082: type = dataBinding.getType(part.getTypeQName(), false);
083: }
084: if (type == null) {
085: type = resolvePartType(part);
086: }
087: return type;
088: }
089:
090: public static DefaultValueWriter getDefaultValueWriter(
091: MessagePartInfo part, ToolContext context) {
092: DataBindingProfile dataBinding = context
093: .get(DataBindingProfile.class);
094: if (part.isElement()) {
095: return dataBinding.createDefaultValueWriter(
096: getElementName(part), true);
097: }
098: return dataBinding.createDefaultValueWriter(
099: part.getTypeQName(), false);
100: }
101:
102: public static DefaultValueWriter getDefaultValueWriterForWrappedElement(
103: MessagePartInfo part, ToolContext context, QName subElement) {
104: DataBindingProfile dataBinding = context
105: .get(DataBindingProfile.class);
106: return dataBinding.createDefaultValueWriterForWrappedElement(
107: part.getElementQName(), subElement);
108: }
109:
110: public static QName getElementName(MessagePartInfo part) {
111: return part == null ? null : part.getConcreteName();
112: }
113:
114: //
115: // support multiple -p options
116: // if user change the package name through -p namespace=package name
117: //
118: public static QName getMappedElementName(MessagePartInfo part,
119: ToolContext env) {
120: QName origin = getElementName(part);
121: if (origin == null) {
122: return null;
123: }
124: if (!env.hasNamespace(origin.getNamespaceURI())) {
125: return origin;
126: }
127: return new QName(env.getCustomizedNS(origin.getNamespaceURI()),
128: origin.getLocalPart());
129: }
130:
131: public static String resolvePartType(MessagePartInfo part,
132: ToolContext env) {
133: if (env != null) {
134: return resolvePartType(part, env, false);
135: } else {
136: return resolvePartType(part);
137: }
138: }
139:
140: public static String resolvePartType(MessagePartInfo part,
141: ToolContext context, boolean fullName) {
142: DataBindingProfile dataBinding = context
143: .get(DataBindingProfile.class);
144: if (dataBinding == null) {
145: String primitiveType = JAXBUtils.builtInTypeToJavaType(part
146: .getTypeQName().getLocalPart());
147: if (part.getTypeQName() != null && primitiveType != null) {
148: return primitiveType;
149: } else {
150: return resolvePartType(part);
151: }
152: }
153: String name = "";
154: if (part.isElement()) {
155: name = dataBinding.getType(getElementName(part), true);
156: } else {
157: name = dataBinding.getType(part.getTypeQName(), false);
158: }
159: return name;
160: }
161:
162: public static String resolvePartNamespace(MessagePartInfo part) {
163: return part.getConcreteName().getNamespaceURI();
164: }
165:
166: public static String mangleNameToVariableName(String vName) {
167: String result = NameUtil.mangleNameToVariableName(vName);
168: if (JavaUtils.isJavaKeyword(result)) {
169: return KEYWORDS_PREFIX + result;
170: } else {
171: return result;
172: }
173: }
174:
175: public static String parsePackageName(String namespace,
176: String defaultPackageName) {
177: String packageName = (defaultPackageName != null && defaultPackageName
178: .trim().length() > 0) ? defaultPackageName : null;
179:
180: if (packageName == null) {
181: packageName = URIParserUtil.getPackageName(namespace);
182: }
183: return packageName;
184: }
185:
186: public static String getAbsolutePath(String location)
187: throws IOException {
188: if (location.startsWith("http://")) {
189: return location;
190: } else {
191: return resolvePath(new File(location).getAbsolutePath());
192: }
193:
194: }
195:
196: public static URL getWSDLURL(String location) throws Exception {
197: if (location.startsWith("http://")) {
198: return new URL(location);
199: } else {
200: return new File(getAbsolutePath(location)).toURI().toURL();
201: }
202: }
203:
204: private static String resolvePath(String path) {
205: return path.replace('\\', '/');
206: }
207:
208: public static String classNameToFilePath(String className) {
209: String str;
210: if (className.indexOf(".") < 0) {
211: return className;
212: } else {
213: str = className.replaceAll("\\.", "/");
214: }
215: return str;
216: }
217:
218: //
219: // the non-wrapper style will get the type info from the part directly
220: //
221: public static String getFullClzName(MessagePartInfo part,
222: ToolContext context, boolean primitiveType) {
223: DataBindingProfile dataBinding = context
224: .get(DataBindingProfile.class);
225: String jtype = null;
226: QName xmlTypeName = getElementName(part);
227:
228: // if this flag is true , mapping to java Type first;
229: // if not found , findd the primitive type : int ,long
230: // if not found, find in the generated class
231:
232: if (!primitiveType && dataBinding != null) {
233: jtype = dataBinding.getType(xmlTypeName, true);
234: }
235:
236: if (!primitiveType && dataBinding == null) {
237: Class holderClass = JAXBUtils.holderClass(xmlTypeName
238: .getLocalPart());
239: jtype = holderClass == null ? null : holderClass.getName();
240: if (jtype == null) {
241: jtype = JAXBUtils.builtInTypeToJavaType(xmlTypeName
242: .getLocalPart());
243: }
244: }
245:
246: if (primitiveType) {
247: jtype = JAXBUtils.builtInTypeToJavaType(xmlTypeName
248: .getLocalPart());
249: }
250:
251: String namespace = xmlTypeName.getNamespaceURI();
252: String type = resolvePartType(part, context, true);
253: String userPackage = context.mapPackageName(namespace);
254:
255: ClassCollector collector = context.get(ClassCollector.class);
256: if (jtype == null) {
257: jtype = collector.getTypesFullClassName(parsePackageName(
258: namespace, userPackage), type);
259: }
260:
261: if (jtype == null) {
262: if (!resolvePartType(part).equals(type)) {
263: jtype = resolvePartType(part, context, true);
264: } else {
265: jtype = parsePackageName(namespace, userPackage) + "."
266: + type;
267: }
268: }
269:
270: return jtype;
271: }
272:
273: public static String getFileOrURLName(String fileOrURL) {
274: try {
275: try {
276: return escapeSpace(new URL(fileOrURL).toExternalForm());
277: } catch (MalformedURLException e) {
278: return new File(fileOrURL).getCanonicalFile().toURI()
279: .toURL().toExternalForm();
280: }
281: } catch (Exception e) {
282: return fileOrURL;
283: }
284: }
285:
286: private static String escapeSpace(String url) {
287: StringBuffer buf = new StringBuffer();
288: for (int i = 0; i < url.length(); i++) {
289: if (url.charAt(i) == ' ') {
290: buf.append("%20");
291: } else {
292: buf.append(url.charAt(i));
293: }
294: }
295: return buf.toString();
296: }
297:
298: public static String absolutize(String name) {
299: // absolutize all the system IDs in the input,
300: // so that we can map system IDs to DOM trees.
301: try {
302: URL baseURL = new File(".").getCanonicalFile().toURI()
303: .toURL();
304: return new URL(baseURL, name.replaceAll(" ", "%20"))
305: .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 Node cloneNode(Document document, Node node,
317: boolean deep) throws DOMException {
318: if (document == null || node == null) {
319: return null;
320: }
321: int type = node.getNodeType();
322:
323: if (node.getOwnerDocument() == document) {
324: return node.cloneNode(deep);
325: }
326: Node clone;
327: switch (type) {
328: case Node.CDATA_SECTION_NODE:
329: clone = document.createCDATASection(node.getNodeValue());
330: break;
331: case Node.COMMENT_NODE:
332: clone = document.createComment(node.getNodeValue());
333: break;
334: case Node.ENTITY_REFERENCE_NODE:
335: clone = document.createEntityReference(node.getNodeName());
336: break;
337: case Node.ELEMENT_NODE:
338: clone = document.createElement(node.getNodeName());
339: NamedNodeMap attributes = node.getAttributes();
340: for (int i = 0; i < attributes.getLength(); i++) {
341: ((Element) clone).setAttribute(attributes.item(i)
342: .getNodeName(), attributes.item(i)
343: .getNodeValue());
344: }
345: break;
346:
347: case Node.TEXT_NODE:
348: clone = document.createTextNode(node.getNodeValue());
349: break;
350: default:
351: return null;
352: }
353: if (deep && type == Node.ELEMENT_NODE) {
354: Node child = node.getFirstChild();
355: while (child != null) {
356: clone.appendChild(cloneNode(document, child, true));
357: child = child.getNextSibling();
358: }
359: }
360: return clone;
361: }
362:
363: public static List<QName> getWrappedElement(ToolContext context,
364: QName partElement) {
365: List<QName> qnames = new ArrayList<QName>();
366:
367: ServiceInfo serviceInfo = (ServiceInfo) context
368: .get(ServiceInfo.class);
369: XmlSchemaCollection schema = (XmlSchemaCollection) serviceInfo
370: .getProperty(WSDLServiceBuilder.WSDL_SCHEMA_LIST);
371:
372: XmlSchemaElement elementByName = schema
373: .getElementByQName(partElement);
374:
375: XmlSchemaComplexType type = (XmlSchemaComplexType) elementByName
376: .getSchemaType();
377:
378: XmlSchemaSequence seq = (XmlSchemaSequence) type.getParticle();
379:
380: if (seq != null) {
381:
382: XmlSchemaObjectCollection items = seq.getItems();
383:
384: Iterator ite = items.getIterator();
385:
386: while (ite.hasNext()) {
387: XmlSchemaElement subElement = (XmlSchemaElement) ite
388: .next();
389:
390: if (subElement.getQName() != null) {
391: qnames.add(subElement.getQName());
392: } else {
393: qnames.add(subElement.getRefName());
394: }
395: }
396: }
397: return qnames;
398: }
399:
400: public static boolean isSchemaFormQualified(ToolContext context,
401: QName partElement) {
402: ServiceInfo serviceInfo = (ServiceInfo) context
403: .get(ServiceInfo.class);
404: XmlSchemaCollection schemaCol = (XmlSchemaCollection) serviceInfo
405: .getProperty(WSDLServiceBuilder.WSDL_SCHEMA_LIST);
406: for (int i = 0; i < schemaCol.getXmlSchemas().length; i++) {
407: XmlSchema schema = schemaCol.getXmlSchemas().clone()[i];
408: if (schema.getElementByName(partElement) != null) {
409: return schema.getElementFormDefault().getValue()
410: .equals(XmlSchemaForm.QUALIFIED);
411: }
412: }
413: return false;
414:
415: }
416: }
|