KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > api > property > ElementOrTypeOrMessagePart


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.wsdl.ui.api.property;
20
21 import java.util.Collection JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.xml.namespace.QName JavaDoc;
25
26 import org.netbeans.modules.xml.schema.model.GlobalElement;
27 import org.netbeans.modules.xml.schema.model.GlobalType;
28 import org.netbeans.modules.xml.schema.model.Schema;
29 import org.netbeans.modules.xml.wsdl.model.Message;
30 import org.netbeans.modules.xml.wsdl.model.Part;
31 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
32 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
33 import org.netbeans.modules.xml.wsdl.ui.api.property.ElementOrTypeOrMessagePartProvider.ParameterType;
34 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
35 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
36
37 public class ElementOrTypeOrMessagePart {
38     GlobalElement mElement;
39     GlobalType mType;
40     Part mPart;
41     Message mMessage;
42     QName JavaDoc mQName;
43     ParameterType pType = ParameterType.NONE;
44     WSDLModel mModel;
45
46     public ElementOrTypeOrMessagePart(QName JavaDoc qname, WSDLModel model, ParameterType pType) {
47         this.pType = pType;
48         mQName = qname;
49         mModel = model;
50         List JavaDoc<Schema> schemas = mModel.findSchemas(qname.getNamespaceURI());
51         if (schemas != null) {
52             for (Schema schema : schemas) {
53                 if (pType == ParameterType.ELEMENT) {
54                     Collection JavaDoc<GlobalElement> ges = schema.findAllGlobalElements();
55                     if (ges != null) {
56                         for (GlobalElement ele : ges) {
57                             if (qname.getLocalPart().equals(ele.getName())) {
58                                 mElement = ele;
59                             }
60                         }
61                     }
62                 } else if (pType == ParameterType.TYPE){
63                     Collection JavaDoc<GlobalType> gts = schema.findAllGlobalTypes();
64                     if (gts != null) {
65                         for (GlobalType type : gts) {
66                             if (qname.getLocalPart().equals(type.getName())) {
67                                 mType = type;
68                             }
69                         }
70                     }
71                 }
72             }
73         }
74     }
75     
76     public Part getMessagePart() {
77         return mPart;
78     }
79
80     public ParameterType getParameterType() {
81         return pType;
82     }
83
84     public ElementOrTypeOrMessagePart(QName JavaDoc qname, WSDLModel model, String JavaDoc partName) {
85         pType = ParameterType.MESSAGEPART;
86         mModel = model;
87         mMessage = mModel.findComponentByName(qname, Message.class);
88         if (mMessage != null) {
89             for (Part part : mMessage.getParts()) {
90                 if (part.getName().equals(partName)) {
91                     mPart = part;
92                     break;
93                 }
94             }
95         }
96     }
97
98     public ElementOrTypeOrMessagePart(GlobalElement element, WSDLModel model) {
99         mElement = element;
100         mModel = model;
101         pType = ParameterType.ELEMENT;
102     }
103
104     public ElementOrTypeOrMessagePart(GlobalType type, WSDLModel model) {
105         mType= type;
106         mModel = model;
107         pType = ParameterType.TYPE;
108     }
109     
110     public ElementOrTypeOrMessagePart(Part part, WSDLModel model) {
111         mPart= part;
112         mMessage = (Message) part.getParent();
113         mModel = model;
114         pType = ParameterType.MESSAGEPART;
115     }
116     
117     public GlobalElement getElement() {
118         return mElement;
119     }
120     
121     public GlobalType getType() {
122         return mType;
123     }
124     
125     @Override JavaDoc
126     public String JavaDoc toString() {
127         if (mQName != null) {
128             return Utility.fromQNameToString(mQName);
129         }
130         
131         String JavaDoc namespace = null;
132         String JavaDoc localPart = null;
133         if (mElement != null) {
134             namespace = mElement.getModel().getSchema().getTargetNamespace();
135             localPart = mElement.getName();
136         }
137         if (mType != null) {
138             namespace = mType.getModel().getSchema().getTargetNamespace();
139             localPart = mType.getName();
140         }
141         
142         if (mMessage != null) {
143             namespace = mMessage.getModel().getDefinitions().getTargetNamespace();
144             localPart = mMessage.getName();
145         }
146         
147         if (namespace == null) {
148             return localPart;
149         }
150         if (mModel == null) {
151             return new QName JavaDoc(namespace, localPart).toString();
152         }
153         String JavaDoc namespacePrefix = Utility.getNamespacePrefix(namespace, mModel);
154         if (namespacePrefix == null) {
155             namespacePrefix = NameGenerator.getInstance().generateNamespacePrefix(null, mModel);
156             boolean isInTransaction = Utility.startTransaction(mModel);
157             ((AbstractDocumentComponent)mModel.getDefinitions()).addPrefix(namespacePrefix, namespace);
158             
159             Utility.endTransaction(mModel, isInTransaction);
160         }
161         if (pType == ParameterType.MESSAGEPART) {
162             return namespacePrefix + ":" + localPart + "/" + mPart.getName();
163         }
164         return namespacePrefix + ":" + localPart;
165     }
166 }
167
Popular Tags