325N/A/*
325N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.tools.internal.ws.processor.modeler.wsdl;
325N/A
325N/Aimport com.sun.tools.internal.ws.processor.model.AbstractType;
325N/Aimport com.sun.tools.internal.ws.processor.model.Block;
325N/Aimport com.sun.tools.internal.ws.processor.model.ModelProperties;
325N/Aimport com.sun.tools.internal.ws.processor.model.Parameter;
325N/Aimport com.sun.tools.internal.ws.processor.model.java.JavaSimpleType;
325N/Aimport com.sun.tools.internal.ws.processor.model.java.JavaStructureMember;
325N/Aimport com.sun.tools.internal.ws.processor.model.java.JavaStructureType;
325N/Aimport com.sun.tools.internal.ws.processor.model.java.JavaType;
325N/Aimport com.sun.tools.internal.ws.processor.model.jaxb.*;
325N/Aimport com.sun.tools.internal.ws.resources.ModelerMessages;
325N/Aimport com.sun.tools.internal.ws.util.ClassNameInfo;
325N/Aimport com.sun.tools.internal.ws.wscompile.AbortException;
325N/Aimport com.sun.tools.internal.ws.wscompile.ErrorReceiverFilter;
325N/Aimport com.sun.tools.internal.ws.wsdl.document.Message;
325N/Aimport com.sun.tools.internal.ws.wsdl.document.MessagePart;
325N/Aimport com.sun.tools.internal.xjc.api.S2JJAXBModel;
325N/Aimport com.sun.tools.internal.xjc.api.TypeAndAnnotation;
325N/A
325N/Aimport javax.xml.namespace.QName;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Iterator;
325N/Aimport java.util.List;
325N/A
325N/A/**
325N/A * Utilities to be used by WSDLModeler
325N/A *
325N/A * @author Vivek Pandey
325N/A *
325N/A */
325N/Aclass ModelerUtils {
325N/A
325N/A /**
325N/A * This method should be called incase of wrapper style operations. This is
325N/A * equivalent to wrapper style schema component or JAXB Mapping object.
325N/A *
325N/A * @param jaxbType JAXBType from which a JAXBStructured type will be created.
325N/A * @return returns JAXBStructured type
325N/A */
325N/A public static JAXBStructuredType createJAXBStructureType(JAXBType jaxbType) {
325N/A JAXBStructuredType type = new JAXBStructuredType(jaxbType);
325N/A type.setName(jaxbType.getName());
325N/A type.setJavaType(jaxbType.getJavaType());
325N/A return type;
325N/A }
325N/A
325N/A /**
325N/A * This method uses JAXBStructured type (wrapper style operations) and
325N/A * unwraps it to create list of parameters.
325N/A *
325N/A *
325N/A * @param jaxbType instance of JAXBType, could be JAXBStructured type.
325N/A * @param block The Block (body/Header/Attachment) to which the created Parameter belong.
325N/A * @return list of Parameters
325N/A */
325N/A public static List<Parameter> createUnwrappedParameters(JAXBType jaxbType,
325N/A Block block) {
325N/A List<Parameter> paramList = new ArrayList<Parameter>();
325N/A JAXBStructuredType type = null;
325N/A if (!(jaxbType instanceof JAXBStructuredType))
325N/A type = createJAXBStructureType(jaxbType);
325N/A else
325N/A type = (JAXBStructuredType) jaxbType;
325N/A
325N/A JavaStructureType jst = new JavaStructureType(jaxbType.getJavaType()
325N/A .getRealName(), true, type);
325N/A type.setJavaType(jst);
325N/A block.setType(type);
325N/A List memberList = jaxbType.getWrapperChildren();
325N/A Iterator props = memberList.iterator();
325N/A while (props.hasNext()) {
325N/A JAXBProperty prop = (JAXBProperty) props.next();
325N/A paramList.add(createUnwrappedParameter(prop, jaxbType, block, type,
325N/A jst));
325N/A }
325N/A
325N/A return paramList;
325N/A }
325N/A
325N/A /**
325N/A * @param prop
325N/A * @param jaxbType
325N/A * @param block
325N/A * @return
325N/A */
325N/A private static Parameter createUnwrappedParameter(JAXBProperty prop,
325N/A JAXBType jaxbType, Block block, JAXBStructuredType type,
325N/A JavaStructureType jst) {
325N/A QName elementName = prop.getElementName();
325N/A JavaType javaType = new JavaSimpleType(prop.getType());
325N/A JAXBElementMember eType = new JAXBElementMember(elementName, jaxbType);
325N/A JavaStructureMember jsm = new JavaStructureMember(elementName
325N/A .getLocalPart(), javaType, eType);
325N/A eType.setJavaStructureMember(jsm);
325N/A jst.add(jsm);
325N/A eType.setProperty(prop);
325N/A type.add(eType);
325N/A JAXBType t = new JAXBType(elementName, javaType, jaxbType
325N/A .getJaxbMapping(), jaxbType.getJaxbModel());
325N/A t.setUnwrapped(true);
325N/A Parameter parameter = createParameter(elementName.getLocalPart(), t, block);
325N/A parameter.setEmbedded(true);
325N/A return parameter;
325N/A }
325N/A
325N/A public static List<Parameter> createRpcLitParameters(Message message, Block block, S2JJAXBModel jaxbModel, ErrorReceiverFilter errReceiver){
325N/A RpcLitStructure rpcStruct = (RpcLitStructure)block.getType();
325N/A
325N/A List<Parameter> parameters = new ArrayList<Parameter>();
325N/A for(MessagePart part : message.getParts()){
325N/A if(!ModelerUtils.isBoundToSOAPBody(part))
325N/A continue;
325N/A QName name = part.getDescriptor();
325N/A TypeAndAnnotation typeAndAnn = jaxbModel.getJavaType(name);
325N/A if(typeAndAnn == null){
325N/A String msgQName = "{"+message.getDefining().getTargetNamespaceURI()+"}"+message.getName();
325N/A errReceiver.error(part.getLocator(), ModelerMessages.WSDLMODELER_RPCLIT_UNKOWNSCHEMATYPE(name.toString(),
325N/A part.getName(), msgQName));
325N/A throw new AbortException();
325N/A }
325N/A String type = typeAndAnn.getTypeClass().fullName();
325N/A type = ClassNameInfo.getGenericClass(type);
325N/A RpcLitMember param = new RpcLitMember(new QName("", part.getName()), type);
325N/A JavaType javaType = new JavaSimpleType(new JAXBTypeAndAnnotation(typeAndAnn));
325N/A param.setJavaType(javaType);
325N/A rpcStruct.addRpcLitMember(param);
325N/A Parameter parameter = ModelerUtils.createParameter(part.getName(), param, block);
325N/A parameter.setEmbedded(true);
325N/A parameters.add(parameter);
325N/A }
325N/A return parameters;
325N/A }
325N/A
325N/A /**
325N/A * Called for non-wrapper style operations. It returns a Parameter constructed
325N/A * using the JAXBType and the Block.
325N/A *
325N/A * @param partName typically wsdl:part or any name to be given to the parameter
325N/A * @param jaxbType type of Parameter
325N/A * @param block Block to which the parameter belongs to
325N/A * @return Parameter created.
325N/A */
325N/A public static Parameter createParameter(String partName, AbstractType jaxbType,
325N/A Block block) {
325N/A Parameter parameter = new Parameter(partName, block.getEntity());
325N/A parameter.setProperty(ModelProperties.PROPERTY_PARAM_MESSAGE_PART_NAME,
325N/A partName);
325N/A parameter.setEmbedded(false);
325N/A parameter.setType(jaxbType);
325N/A parameter.setTypeName(jaxbType.getJavaType().getType().getName());
325N/A parameter.setBlock(block);
325N/A return parameter;
325N/A }
325N/A
325N/A /**
325N/A * Get Parameter from the list of parameters.
325N/A *
325N/A * @param paramName
325N/A * @param parameters
325N/A * @return the Parameter with name paramName from parameters
325N/A */
325N/A public static Parameter getParameter(String paramName, List<Parameter> parameters){
325N/A if(parameters == null)
325N/A return null;
325N/A for(Parameter param: parameters){
325N/A //if(param.getName().equals("_return") && paramName.equals("return") || param.getName().equals(paramName)) {
325N/A if(param.getName().equals(paramName)){
325N/A return param;
325N/A }
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A /**
325N/A * Compares two JAXBStructures.
325N/A *
325N/A * @param struct1
325N/A * @param struct2
325N/A * @return true if struct1 and struct2 are equivalent.
325N/A */
325N/A public static boolean isEquivalentLiteralStructures(
325N/A JAXBStructuredType struct1,
325N/A JAXBStructuredType struct2) {
325N/A if (struct1.getElementMembersCount() != struct2.getElementMembersCount())
325N/A return false;
325N/A Iterator members = struct1.getElementMembers();
325N/A JAXBElementMember member1;
325N/A JavaStructureMember javaMember1, javaMember2;
325N/A for (int i = 0; members.hasNext(); i++) {
325N/A member1 = (JAXBElementMember)members.next();
325N/A javaMember1 = member1.getJavaStructureMember();
325N/A javaMember2 =
325N/A ((JavaStructureType)struct2.getJavaType()).getMemberByName(
325N/A member1.getJavaStructureMember().getName());
325N/A if (javaMember2.getConstructorPos() != i
325N/A || !javaMember1.getType().equals(javaMember2.getType())) {
325N/A return false;
325N/A }
325N/A }
325N/A return false;
325N/A }
325N/A
325N/A public static QName getRawTypeName(Parameter parameter) {
325N/A String name = parameter.getName();
325N/A
325N/A if (parameter.getType() instanceof JAXBType) {
325N/A JAXBType jt = (JAXBType)parameter.getType();
325N/A if (jt.isUnwrappable()) {
325N/A List<JAXBProperty> props = jt.getWrapperChildren();
325N/A for(JAXBProperty prop: props) {
325N/A if (prop.getName().equals(name)) {
325N/A return prop.getRawTypeName();
325N/A }
325N/A }
325N/A }
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A /**
325N/A * @param part
325N/A * @return true if part is bound to Mime content
325N/A */
325N/A public static boolean isBoundToMimeContent(MessagePart part) {
325N/A if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.WSDL_MIME_BINDING)
325N/A return true;
325N/A return false;
325N/A }
325N/A
325N/A /**
325N/A * @param part
325N/A * @return true if part is bound to SOAPBody
325N/A */
325N/A public static boolean isBoundToSOAPBody(MessagePart part) {
325N/A if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.SOAP_BODY_BINDING)
325N/A return true;
325N/A return false;
325N/A }
325N/A
325N/A /**
325N/A * @param part
325N/A * @return true if part is bound to SOAPHeader
325N/A */
325N/A public static boolean isBoundToSOAPHeader(MessagePart part) {
325N/A if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.SOAP_HEADER_BINDING)
325N/A return true;
325N/A return false;
325N/A }
325N/A
325N/A public static boolean isUnbound(MessagePart part) {
325N/A if((part != null) && part.getBindingExtensibilityElementKind() == MessagePart.PART_NOT_BOUNDED)
325N/A return true;
325N/A return false;
325N/A }
325N/A}