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.xml.wsdl.ui.api.property;
042:
043: import javax.xml.namespace.QName;
044:
045: import org.netbeans.modules.xml.schema.model.GlobalElement;
046: import org.netbeans.modules.xml.schema.model.GlobalType;
047: import org.netbeans.modules.xml.wsdl.model.Message;
048: import org.netbeans.modules.xml.wsdl.model.Part;
049: import org.netbeans.modules.xml.wsdl.model.WSDLModel;
050: import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
051: import org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrTypeOrMessagePartProvider.ParameterType;
052: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
053: import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
054:
055: public class ElementOrTypeOrMessagePart {
056: private GlobalElement mElement;
057: private GlobalType mType;
058: private Part mPart;
059: private Message mMessage;
060: private QName mQName;
061: private ParameterType pType = ParameterType.NONE;
062: private WSDLModel mModel;
063: private String mPartName;
064:
065: public ElementOrTypeOrMessagePart(QName elementOrTypeQName,
066: WSDLModel model, ParameterType elementOrType) {
067: this .pType = elementOrType;
068: mQName = elementOrTypeQName;
069: mModel = model;
070: if (pType == ParameterType.ELEMENT) {
071: mElement = Utility.findGlobalElement(model,
072: elementOrTypeQName);
073: } else if (pType == ParameterType.TYPE) {
074: mType = Utility.findGlobalType(model, elementOrTypeQName);
075: }
076: }
077:
078: public Part getMessagePart() {
079: return mPart;
080: }
081:
082: public ParameterType getParameterType() {
083: return pType;
084: }
085:
086: public ElementOrTypeOrMessagePart(QName messageQName,
087: WSDLModel model, String partName) {
088: pType = ParameterType.MESSAGEPART;
089: mModel = model;
090: mQName = messageQName;
091: mPartName = partName;
092: mMessage = mModel.findComponentByName(messageQName,
093: Message.class);
094: if (mMessage != null) {
095: for (Part part : mMessage.getParts()) {
096: if (part.getName().equals(partName)) {
097: mPart = part;
098: break;
099: }
100: }
101: }
102: }
103:
104: public ElementOrTypeOrMessagePart(GlobalElement element,
105: WSDLModel model) {
106: mElement = element;
107: mModel = model;
108: pType = ParameterType.ELEMENT;
109: }
110:
111: public ElementOrTypeOrMessagePart(GlobalType type, WSDLModel model) {
112: mType = type;
113: mModel = model;
114: pType = ParameterType.TYPE;
115: }
116:
117: public ElementOrTypeOrMessagePart(Part part, WSDLModel model) {
118: mPart = part;
119: mMessage = (Message) part.getParent();
120: mModel = model;
121: pType = ParameterType.MESSAGEPART;
122: }
123:
124: public GlobalElement getElement() {
125: return mElement;
126: }
127:
128: public GlobalType getType() {
129: return mType;
130: }
131:
132: @Override
133: public String toString() {
134: if (mQName != null) {
135: String str = Utility.fromQNameToString(mQName);
136: if (pType == ParameterType.MESSAGEPART) {
137: str = str + "/" + mPartName;
138: }
139: return str;
140: }
141:
142: String namespace = null;
143: String localPart = "";
144: if (mElement != null) {
145: namespace = mElement.getModel().getSchema()
146: .getTargetNamespace();
147: localPart = mElement.getName();
148: }
149: if (mType != null) {
150: namespace = mType.getModel().getSchema()
151: .getTargetNamespace();
152: localPart = mType.getName();
153: }
154:
155: if (mMessage != null) {
156: namespace = mMessage.getModel().getDefinitions()
157: .getTargetNamespace();
158: localPart = mMessage.getName();
159: }
160:
161: if (namespace == null) {
162: return localPart;
163: }
164: if (mModel == null) {
165: return new QName(namespace, localPart).toString();
166: }
167: String namespacePrefix = Utility.getNamespacePrefix(namespace,
168: mModel);
169: if (namespacePrefix == null) {
170: namespacePrefix = NameGenerator.getInstance()
171: .generateNamespacePrefix(null, mModel);
172: boolean isInTransaction = Utility.startTransaction(mModel);
173: ((AbstractDocumentComponent) mModel.getDefinitions())
174: .addPrefix(namespacePrefix, namespace);
175:
176: Utility.endTransaction(mModel, isInTransaction);
177: }
178: if (pType == ParameterType.MESSAGEPART) {
179: return namespacePrefix + ":" + localPart + "/"
180: + mPart.getName();
181: }
182: return namespacePrefix + ":" + localPart;
183: }
184: }
|