001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.websvc.rest.support;
042:
043: import java.util.HashMap;
044: import java.util.List;
045: import java.util.Map;
046: import javax.xml.XMLConstants;
047: import javax.xml.namespace.QName;
048: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlOperation;
049: import org.netbeans.modules.websvc.api.jaxws.wsdlmodel.WsdlPort;
050: import org.netbeans.modules.xml.schema.model.GlobalElement;
051: import org.netbeans.modules.xml.schema.model.GlobalSimpleType;
052: import org.netbeans.modules.xml.schema.model.LocalSimpleType;
053: import org.netbeans.modules.xml.schema.model.SimpleTypeRestriction;
054: import org.netbeans.modules.xml.wsdl.model.Binding;
055: import org.netbeans.modules.xml.wsdl.model.BindingInput;
056: import org.netbeans.modules.xml.wsdl.model.BindingOperation;
057: import org.netbeans.modules.xml.wsdl.model.Part;
058: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
059: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPBinding;
060: import org.netbeans.modules.xml.wsdl.model.extensions.soap.SOAPHeader;
061: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
062:
063: /**
064: *
065: * @author nam
066: */
067: public class JaxWsUtils {
068:
069: private static Map<String, String> xsdToJavaTypes = null;
070:
071: public static String toJavaType(String xsdType) {
072: if (xsdToJavaTypes == null) {
073: xsdToJavaTypes = new HashMap<String, String>();
074: xsdToJavaTypes.put("string", "java.lang.String"); //NOI18N
075: xsdToJavaTypes.put("boolean", "java.lang.Boolean"); //NOI18N
076: xsdToJavaTypes.put("date", "java.sql.Date"); //NOI18N
077: xsdToJavaTypes.put("dateTime", "java.sql.Date"); //NOI18N
078: xsdToJavaTypes.put("time", "java.sql.Time"); //NOI18N
079: xsdToJavaTypes.put("token", "java.lang.String"); //NOI18N
080: xsdToJavaTypes.put("double", "java.lang.Double"); //NOI18N
081: xsdToJavaTypes.put("float", "java.lang.Float"); //NOI18N
082: xsdToJavaTypes.put("byte", "java.lang.Byte"); //NOI18N
083: xsdToJavaTypes.put("int", "java.lang.Integer"); //NOI18N
084: xsdToJavaTypes.put("long", "java.lang.Long"); //NOI18N
085: xsdToJavaTypes.put("short", "java.lang.Short"); //NOI18N
086: xsdToJavaTypes.put("unsignedByte", "java.lang.Byte"); //NOI18N
087: xsdToJavaTypes.put("unsignedInt", "java.lang.Integer"); //NOI18N
088: xsdToJavaTypes.put("unsignedLong", "java.lang.Long"); //NOI18N
089: xsdToJavaTypes.put("unsignedShort", "java.lang.Short"); //NOI18N
090: xsdToJavaTypes.put("nonNegativeInteger",
091: "java.lang.Integer"); //NOI18N
092: xsdToJavaTypes.put("nonPositiveInteger",
093: "java.lang.Integer"); //NOI18N
094: xsdToJavaTypes.put("negativeInteger", "java.lang.Integer"); //NOI18N
095: xsdToJavaTypes.put("positiveInteger", "java.lang.Integer"); //NOI18N
096: }
097: return xsdToJavaTypes.get(xsdType);
098: }
099:
100: public static Map<QName, String> getSoapHandlerParameters(
101: WSDLModel model, WsdlPort wsPort, WsdlOperation wsOperation) {
102: Map<QName, String> paramMap = new HashMap<QName, String>();
103:
104: QName portQName = new QName(wsPort.getNamespaceURI(), wsPort
105: .getName());
106: Binding binding = null;
107: for (Binding b : model.getDefinitions().getBindings()) {
108: if (b.getType().getQName().equals(portQName)) {
109: binding = b;
110: break;
111: }
112: }
113: if (binding == null) {
114: return paramMap;
115: }
116:
117: List<SOAPBinding> soapBindings = binding
118: .getExtensibilityElements(SOAPBinding.class);
119: if (soapBindings.isEmpty()) {
120: return paramMap;
121: }
122:
123: BindingOperation bindingOperation = null;
124: for (BindingOperation bOp : binding.getBindingOperations()) {
125: if (bOp.getOperation().get().getName().equals(
126: wsOperation.getOperationName())) {
127: bindingOperation = bOp;
128: break;
129: }
130: }
131: if (bindingOperation == null) {
132: return paramMap;
133: }
134:
135: BindingInput bindingInput = bindingOperation.getBindingInput();
136: for (SOAPHeader header : bindingInput
137: .getExtensibilityElements(SOAPHeader.class)) {
138: if (header.getPartRef() == null) {
139: continue;
140: }
141: Part part = header.getPartRef().get();
142: if (part != null && part.getElement() != null) {
143: GlobalElement element = part.getElement().get();
144: paramMap.put(part.getElement().getQName(),
145: guessSimpleJavaType(element));
146: }
147: }
148:
149: return paramMap;
150: }
151:
152: public static String guessSimpleJavaType(GlobalElement element) {
153: String xsdType = null;
154: String javaType = Object.class.getName();
155: if (element.getType() != null
156: && element.getType().getEffectiveNamespace().equals(
157: XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
158: xsdType = element.getType().get().getName();
159: } else if (element.getInlineType() instanceof LocalSimpleType) {
160: LocalSimpleType lst = (LocalSimpleType) element
161: .getInlineType();
162: if (lst.getDefinition() instanceof SimpleTypeRestriction) {
163: NamedComponentReference<GlobalSimpleType> ref = ((SimpleTypeRestriction) lst
164: .getDefinition()).getBase();
165: if (ref.getEffectiveNamespace().equals(
166: XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
167: xsdType = ref.get().getName();
168: }
169: }
170: }
171: if (xsdType != null) {
172: javaType = toJavaType(xsdType);
173: }
174: return javaType;
175: }
176: }
|