KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 package org.netbeans.modules.xml.wsdl.ui.api.property;
21
22 import java.util.Collection JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.xml.namespace.QName JavaDoc;
26
27 import org.netbeans.modules.xml.schema.model.GlobalElement;
28 import org.netbeans.modules.xml.schema.model.GlobalType;
29 import org.netbeans.modules.xml.schema.model.Schema;
30 import org.netbeans.modules.xml.wsdl.model.WSDLModel;
31 import org.netbeans.modules.xml.wsdl.ui.actions.NameGenerator;
32 import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
33 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent;
34
35 public class ElementOrType {
36     GlobalElement mElement;
37     GlobalType mType;
38     QName JavaDoc mQName;
39     boolean isElement;
40     WSDLModel mModel;
41     
42     public ElementOrType(GlobalElement ele, WSDLModel model) {
43         mElement = ele;
44         mModel = model;
45     }
46     
47     public ElementOrType(GlobalType type, WSDLModel model) {
48         mType = type;
49         mModel = model;
50     }
51     
52     public ElementOrType(QName JavaDoc qname, WSDLModel model, boolean isElement) {
53         this.isElement = isElement;
54         mQName = qname;
55         mModel = model;
56         List JavaDoc<Schema> schemas = mModel.findSchemas(qname.getNamespaceURI());
57         if (schemas != null) {
58             for (Schema schema : schemas) {
59                 if (isElement) {
60                     Collection JavaDoc<GlobalElement> ges = schema.findAllGlobalElements();
61                     if (ges != null) {
62                         for (GlobalElement ele : ges) {
63                             if (qname.getLocalPart().equals(ele.getName())) {
64                                 mElement = ele;
65                             }
66                         }
67                     }
68                 } else {
69                     Collection JavaDoc<GlobalType> gts = schema.findAllGlobalTypes();
70                     if (gts != null) {
71                         for (GlobalType type : gts) {
72                             if (qname.getLocalPart().equals(type.getName())) {
73                                 mType = type;
74                             }
75                         }
76                     }
77                 }
78             }
79         }
80     }
81     
82     public boolean isElement() {
83         return isElement || mElement != null;
84     }
85     
86     public GlobalElement getElement() {
87         return mElement;
88     }
89     
90     public GlobalType getType() {
91         return mType;
92     }
93     
94     @Override JavaDoc
95     public String JavaDoc toString() {
96         if (mQName != null) {
97             return Utility.fromQNameToString(mQName);
98         }
99         
100         String JavaDoc namespace = null;
101         String JavaDoc localPart = null;
102         if (mElement != null) {
103             namespace = mElement.getModel().getSchema().getTargetNamespace();
104             localPart = mElement.getName();
105         }
106         if (mType != null) {
107             namespace = mType.getModel().getSchema().getTargetNamespace();
108             localPart = mType.getName();
109         }
110         
111         if (namespace == null) {
112             return localPart;
113         }
114         if (mModel == null) {
115             return new QName JavaDoc(namespace, localPart).toString();
116         }
117         String JavaDoc namespacePrefix = Utility.getNamespacePrefix(namespace, mModel);
118         if (namespacePrefix == null) {
119             namespacePrefix = NameGenerator.getInstance().generateNamespacePrefix(null, mModel);
120             boolean isInTransaction = Utility.startTransaction(mModel);
121             ((AbstractDocumentComponent)mModel.getDefinitions()).addPrefix(namespacePrefix, namespace);
122             
123             Utility.endTransaction(mModel, isInTransaction);
124         }
125         
126         return namespacePrefix + ":" + localPart;
127     }
128 }
129
Popular Tags