001: /*
002: * Portions Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025: package com.sun.tools.internal.ws.processor.modeler;
026:
027: import java.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import javax.xml.namespace.QName;
032:
033: import com.sun.tools.internal.xjc.api.S2JJAXBModel;
034: import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
035: import com.sun.tools.internal.ws.processor.model.AbstractType;
036: import com.sun.tools.internal.ws.processor.model.Block;
037: import com.sun.tools.internal.ws.processor.model.ModelProperties;
038: import com.sun.tools.internal.ws.processor.model.Parameter;
039: import com.sun.tools.internal.ws.processor.model.java.JavaSimpleType;
040: import com.sun.tools.internal.ws.processor.model.java.JavaStructureMember;
041: import com.sun.tools.internal.ws.processor.model.java.JavaStructureType;
042: import com.sun.tools.internal.ws.processor.model.java.JavaType;
043: import com.sun.tools.internal.ws.processor.model.jaxb.*;
044: import com.sun.tools.internal.ws.util.ClassNameInfo;
045: import com.sun.tools.internal.ws.wsdl.document.Message;
046: import com.sun.tools.internal.ws.wsdl.document.MessagePart;
047:
048: /**
049: * Utilities to be used by modelers such as WSDLModeler and Rmimodeler
050: *
051: * @author Vivek Pandey
052: *
053: */
054: public class ModelerUtils {
055:
056: /**
057: * This method should be called incase of wrapper style operations. This is
058: * equivalent to wrapper style schema component or JAXB Mapping object.
059: *
060: * @param jaxbType JAXBType from which a JAXBStructured type will be created.
061: * @return returns JAXBStructured type
062: */
063: public static JAXBStructuredType createJAXBStructureType(
064: JAXBType jaxbType) {
065: JAXBStructuredType type = new JAXBStructuredType(jaxbType);
066: type.setName(jaxbType.getName());
067: type.setJavaType(jaxbType.getJavaType());
068: return type;
069: }
070:
071: /**
072: * This method uses JAXBStructured type (wrapper style operations) and
073: * unwraps it to create list of parameters.
074: *
075: *
076: * @param jaxbType instance of JAXBType, could be JAXBStructured type.
077: * @param block The Block (body/Header/Attachment) to which the created Parameter belong.
078: * @return list of Parameters
079: */
080: public static List<Parameter> createUnwrappedParameters(
081: JAXBType jaxbType, Block block) {
082: List<Parameter> paramList = new ArrayList<Parameter>();
083: JAXBStructuredType type = null;
084: if (!(jaxbType instanceof JAXBStructuredType))
085: type = createJAXBStructureType(jaxbType);
086: else
087: type = (JAXBStructuredType) jaxbType;
088:
089: JavaStructureType jst = new JavaStructureType(jaxbType
090: .getJavaType().getRealName(), true, type);
091: type.setJavaType(jst);
092: block.setType(type);
093: List memberList = jaxbType.getWrapperChildren();
094: Iterator props = memberList.iterator();
095: while (props.hasNext()) {
096: JAXBProperty prop = (JAXBProperty) props.next();
097: paramList.add(createUnwrappedParameter(prop, jaxbType,
098: block, type, jst));
099: }
100:
101: return paramList;
102: }
103:
104: /**
105: * @param prop
106: * @param jaxbType
107: * @param block
108: * @return
109: */
110: private static Parameter createUnwrappedParameter(
111: JAXBProperty prop, JAXBType jaxbType, Block block,
112: JAXBStructuredType type, JavaStructureType jst) {
113: QName elementName = prop.getElementName();
114: JavaType javaType = new JavaSimpleType(prop.getType());
115: JAXBElementMember eType = new JAXBElementMember(elementName,
116: jaxbType);
117: JavaStructureMember jsm = new JavaStructureMember(elementName
118: .getLocalPart(), javaType, eType);
119: eType.setJavaStructureMember(jsm);
120: jst.add(jsm);
121: eType.setProperty(prop);
122: type.add(eType);
123: JAXBType t = new JAXBType(elementName, javaType, jaxbType
124: .getJaxbMapping(), jaxbType.getJaxbModel());
125: t.setUnwrapped(true);
126: Parameter parameter = createParameter(elementName
127: .getLocalPart(), t, block);
128: parameter.setEmbedded(true);
129: return parameter;
130: }
131:
132: public static List<Parameter> createRpcLitParameters(
133: Message message, Block block, S2JJAXBModel jaxbModel) {
134: RpcLitStructure rpcStruct = (RpcLitStructure) block.getType();
135:
136: List<Parameter> parameters = new ArrayList<Parameter>();
137: for (MessagePart part : message.getParts()) {
138: if (!ModelerUtils.isBoundToSOAPBody(part))
139: continue;
140: QName name = part.getDescriptor();
141: TypeAndAnnotation typeAndAnn = jaxbModel.getJavaType(name);
142: if (typeAndAnn == null) {
143: String msgQName = "{"
144: + message.getDefining().getTargetNamespaceURI()
145: + "}" + message.getName();
146: throw new ModelerException(
147: "wsdlmodeler.rpclit.unkownschematype", name
148: .toString(), part.getName(), msgQName);
149: }
150: String type = typeAndAnn.getTypeClass().fullName();
151: type = ClassNameInfo.getGenericClass(type);
152: RpcLitMember param = new RpcLitMember(new QName("", part
153: .getName()), type);
154: JavaType javaType = new JavaSimpleType(
155: new JAXBTypeAndAnnotation(typeAndAnn));
156: param.setJavaType(javaType);
157: rpcStruct.addRpcLitMember(param);
158: Parameter parameter = ModelerUtils.createParameter(part
159: .getName(), param, block);
160: parameter.setEmbedded(true);
161: parameters.add(parameter);
162: }
163: return parameters;
164: }
165:
166: /**
167: * Called for non-wrapper style operations. It returns a Parameter constructed
168: * using the JAXBType and the Block.
169: *
170: * @param partName typically wsdl:part or any name to be given to the parameter
171: * @param jaxbType type of Parameter
172: * @param block Block to which the parameter belongs to
173: * @return Parameter created.
174: */
175: public static Parameter createParameter(String partName,
176: AbstractType jaxbType, Block block) {
177: Parameter parameter = new Parameter(partName);
178: parameter.setProperty(
179: ModelProperties.PROPERTY_PARAM_MESSAGE_PART_NAME,
180: partName);
181: parameter.setEmbedded(false);
182: parameter.setType(jaxbType);
183: parameter.setTypeName(jaxbType.getJavaType().getType()
184: .getName());
185: parameter.setBlock(block);
186: return parameter;
187: }
188:
189: /**
190: * Get Parameter from the list of parameters.
191: *
192: * @param paramName
193: * @param parameters
194: * @return the Parameter with name paramName from parameters
195: */
196: public static Parameter getParameter(String paramName,
197: List<Parameter> parameters) {
198: if (parameters == null)
199: return null;
200: for (Parameter param : parameters) {
201: //if(param.getName().equals("_return") && paramName.equals("return") || param.getName().equals(paramName)) {
202: if (param.getName().equals(paramName)) {
203: return param;
204: }
205: }
206: return null;
207: }
208:
209: /**
210: * Compares two JAXBStructures.
211: *
212: * @param struct1
213: * @param struct2
214: * @return true if struct1 and struct2 are equivalent.
215: */
216: public static boolean isEquivalentLiteralStructures(
217: JAXBStructuredType struct1, JAXBStructuredType struct2) {
218: if (struct1.getElementMembersCount() != struct2
219: .getElementMembersCount())
220: return false;
221: Iterator members = struct1.getElementMembers();
222: JAXBElementMember member1;
223: JavaStructureMember javaMember1, javaMember2;
224: for (int i = 0; members.hasNext(); i++) {
225: member1 = (JAXBElementMember) members.next();
226: javaMember1 = member1.getJavaStructureMember();
227: javaMember2 = ((JavaStructureType) struct2.getJavaType())
228: .getMemberByName(member1.getJavaStructureMember()
229: .getName());
230: if (javaMember2.getConstructorPos() != i
231: || !javaMember1.getType().equals(
232: javaMember2.getType())) {
233: return false;
234: }
235: }
236: return false;
237: }
238:
239: /**
240: * @param part
241: * @return true if part is bound to Mime content
242: */
243: public static boolean isBoundToMimeContent(MessagePart part) {
244: if ((part != null)
245: && part.getBindingExtensibilityElementKind() == MessagePart.WSDL_MIME_BINDING)
246: return true;
247: return false;
248: }
249:
250: /**
251: * @param part
252: * @return true if part is bound to SOAPBody
253: */
254: public static boolean isBoundToSOAPBody(MessagePart part) {
255: if ((part != null)
256: && part.getBindingExtensibilityElementKind() == MessagePart.SOAP_BODY_BINDING)
257: return true;
258: return false;
259: }
260:
261: /**
262: * @param part
263: * @return true if part is bound to SOAPHeader
264: */
265: public static boolean isBoundToSOAPHeader(MessagePart part) {
266: if ((part != null)
267: && part.getBindingExtensibilityElementKind() == MessagePart.SOAP_HEADER_BINDING)
268: return true;
269: return false;
270: }
271:
272: public static boolean isUnbound(MessagePart part) {
273: if ((part != null)
274: && part.getBindingExtensibilityElementKind() == MessagePart.PART_NOT_BOUNDED)
275: return true;
276: return false;
277: }
278: }
|