KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > wsdl > ui > view > 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.view;
21
22 import java.util.Map 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.wsdl.ui.actions.NameGenerator;
29 import org.openide.util.NbBundle;
30
31 public class ElementOrType {
32     GlobalElement mElement;
33     GlobalType mType;
34     QName JavaDoc mQName;
35     boolean isElement;
36     Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap;
37     String JavaDoc namespacePrefix;
38     String JavaDoc localPart;
39     
40     public ElementOrType(GlobalElement ele, Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap) {
41         mElement = ele;
42         this.namespaceToPrefixMap = namespaceToPrefixMap;
43         namespacePrefix = generateNamespacePrefixIfNotPresent(mElement.getModel().getSchema().getTargetNamespace());
44         localPart = mElement.getName();
45     }
46     
47
48
49     public ElementOrType(GlobalType type, Map JavaDoc<String JavaDoc, String JavaDoc> namespaceToPrefixMap) {
50         mType = type;
51         this.namespaceToPrefixMap = namespaceToPrefixMap;
52         namespacePrefix = generateNamespacePrefixIfNotPresent(mType.getModel().getSchema().getTargetNamespace());
53         localPart = mType.getName();
54     }
55     
56         
57     public boolean isElement() {
58         return mElement != null;
59     }
60     
61     public GlobalElement getElement() {
62         return mElement;
63     }
64     
65     public GlobalType getType() {
66         return mType;
67     }
68     
69     @Override JavaDoc
70     public String JavaDoc toString() {
71         if (namespacePrefix == null)
72             return localPart;
73         return namespacePrefix + ":" + localPart;
74     }
75     
76     
77     public String JavaDoc generateNamespacePrefix(String JavaDoc optionalPrefixNameString) {
78         String JavaDoc prefix = null;
79         if(optionalPrefixNameString == null) {
80             optionalPrefixNameString = NbBundle.getMessage(NameGenerator.class, "NameGenerator_DEFAULT_PREFIX");
81         }
82         
83         int prefixCounter = 0;
84         String JavaDoc prefixStr = optionalPrefixNameString;
85         prefix = prefixStr;
86         
87         while(namespaceToPrefixMap.containsValue(prefix)) {
88             prefix = prefixStr + prefixCounter++;
89         }
90         
91         
92         
93         return prefix;
94     }
95     
96     private String JavaDoc generateNamespacePrefixIfNotPresent(String JavaDoc namespace) {
97         String JavaDoc nsPrefix = namespaceToPrefixMap.get(namespace);
98         if (nsPrefix == null) {
99             nsPrefix = generateNamespacePrefix(null);
100             namespaceToPrefixMap.put(namespace, nsPrefix);
101         }
102         return nsPrefix;
103     }
104     
105 // private String generateNamespacePrefixIfNotPresent(SchemaModel sModel) {
106
// String schemaTNS = sModel.getSchema().getTargetNamespace();
107
// FileObject fo = (FileObject) sModel.getModelSource().getLookup().lookup(FileObject.class);
108
// String prefixName = fo.getName();
109
// if(prefixName.length() > 5) {
110
// prefixName = prefixName.substring(0, 5);
111
// }
112
// String prefix = NameGenerator.getInstance().generateNamespacePrefix(prefixName, def);
113
// ((AbstractDocumentComponent) def).addPrefix(prefix, schemaTNS);
114
// }
115
}
116
Popular Tags