KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > jmi > xmi > XmiUtils


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.lib.jmi.xmi;
20
21 import java.util.Locale JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List JavaDoc;
27
28 /**
29
30  * @version
31  */

32 public abstract class XmiUtils {
33     private static final String JavaDoc TEXT_NODE_NAME = "#text"; //NOI18N
34
private static final String JavaDoc COMMENT_NODE_NAME = "#comment"; //NOI18N
35

36     private static HashMap JavaDoc namespaces = new HashMap JavaDoc();
37
38     static {
39         namespaces.put("Model", "Model"); //NOI18N
40
namespaces.put("UML", "Model_Management"); //NOI18N
41
namespaces.put("CWMOLAP", "Olap"); //NOI18N
42
namespaces.put("CWMTFM", "Transformation"); //NOI18N
43
namespaces.put("CWM", "BusinessInformation"); //NOI18N
44
namespaces.put("CWMRDB", "Relational"); //NOI18N
45
}
46
47     public static String JavaDoc getXmiAttrValueAsString(Node JavaDoc node, String JavaDoc attributeName) {
48         String JavaDoc result = null;
49         if (node != null) {
50             result = getAttributeValueAsString( node, getShortName(attributeName));
51             if (result == null) result = getElementValueAsString( node, attributeName );
52         }
53         return result;
54     }
55
56     public static List JavaDoc getXmiRefValue(Node JavaDoc node, String JavaDoc attributeName) {
57         List JavaDoc value = new ArrayList JavaDoc();
58         if (node != null) {
59             String JavaDoc attr = getAttributeValueAsString(node, getShortName(attributeName));
60             if (attr == null) {
61                 Node JavaDoc refNode = getChildNode(node, attributeName);
62                 if (refNode != null) {
63                     NodeList JavaDoc values = refNode.getChildNodes();
64                     for (int i = 0; i < values.getLength(); i++) {
65                         if (isTextNode(values.item(i))) continue;
66                         value.add(getAttributeValueAsString(values.item(i), XmiConstants.XMI_IDREF));
67                     }
68                 }
69             } else {
70                 // parse the string
71
int pos;
72                 while ((pos = attr.indexOf(' ')) > -1) {
73                     if (pos > 0) {
74                         value.add(attr.substring(0, pos));
75                     }
76                     attr = attr.substring(pos + 1);
77                 }
78                 value.add(attr);
79             }
80         }
81         return value;
82     }
83     
84     public static String JavaDoc getAttributeValueAsString(Node JavaDoc node, String JavaDoc attributeName) {
85         String JavaDoc result = null;
86         if ( (node != null) && (node.hasAttributes()) ) {
87             Node JavaDoc attribute = node.getAttributes().getNamedItem(attributeName);
88             if (attribute != null) result = attribute.getNodeValue();
89         }
90         return result;
91     }
92     
93     public static String JavaDoc getElementValueAsString(Node JavaDoc node, String JavaDoc attributeName) {
94         String JavaDoc result = null;
95         if (node != null) {
96             Node JavaDoc attributeNode = getChildNode( node, attributeName );
97             if (attributeNode != null) {
98                 attributeNode = attributeNode.getFirstChild();
99                 if (attributeNode == null) {
100                     result = ""; //NOI18N
101
} else {
102                     result = attributeNode.getNodeValue();
103                 }
104             }
105         }
106         return result;
107     }
108
109     public static Node JavaDoc getChildNode(Node JavaDoc parentNode, String JavaDoc childNodeName) {
110         Node JavaDoc result = null;
111         if ((parentNode != null) && (childNodeName != null)) {
112             NodeList JavaDoc children = parentNode.getChildNodes();
113             for (int cnt = 0; cnt < children.getLength(); cnt++) {
114                 Node JavaDoc child = children.item(cnt);
115                 // [PENDING] this is ugly !!! namespaces should be resolved correctly
116
if (childNodeName.equals(resolveFullName(child))) {
117                     result = child;
118                     break;
119                 }
120             }
121         }
122         return result;
123     }
124
125     public static String JavaDoc resolveFullName(Node JavaDoc element) {
126         if (isXmiNode(element)) return element.getNodeName();
127         return resolveFullNameAsString( element.getNodeName() );
128     }
129
130     public static String JavaDoc resolveFullNameAsString(String JavaDoc fullName) {
131         // [PENDING] here should be resolved by what should be the namespace name replaced
132
// the following line works ok for MOF, because namespace Model represents package Model
133
// Logger.getDefault().log("resolving full name: " + fullName);
134
if (fullName != null) {
135             int index;
136             if ((index = fullName.indexOf(':')) > 0) {
137 // Logger.getDefault().log(": found, the result is: " + namespaces.get(fullName.substring(0, index)) + "." + fullName.substring(index + 1));
138
return namespaces.get(fullName.substring(0, index)) + "." + fullName.substring(index + 1); //NOI18N
139
} else {
140                 return fullName;
141             }
142         }
143         else
144             return null;
145     }
146
147     public static String JavaDoc getShortName(String JavaDoc fullyQualifiedName) {
148         String JavaDoc result = fullyQualifiedName;
149         if ((fullyQualifiedName != null) && (fullyQualifiedName.toUpperCase(Locale.US).indexOf( XmiConstants.XMI_PREFIX ) > -1)) return fullyQualifiedName;
150         if ((result != null) && (result.indexOf(XmiConstants.NS_SEPARATOR)>-1)) result = result.substring(result.indexOf(XmiConstants.NS_SEPARATOR)+1);
151         if ((result != null) && (result.indexOf(XmiConstants.DOT_SEPARATOR)>-1)) result = result.substring(result.lastIndexOf(XmiConstants.DOT_SEPARATOR)+1);
152         return result;
153     }
154
155     public static boolean isTextNode(Node JavaDoc node) {
156         boolean result = false;
157         if (node != null) result = (node.getNodeName().equals( TEXT_NODE_NAME ) || node.getNodeName().equals( COMMENT_NODE_NAME ));
158         return result;
159     }
160
161     public static boolean isXmiNavigationNode(Node JavaDoc node) {
162         boolean result = false;
163         if (node != null) {
164             result = (node.getNodeName().equals( XmiConstants.XMI_ID ) || result);
165             result = (node.getNodeName().equals( XmiConstants.XMI_UUID ) || result);
166             result = (node.getNodeName().equals( XmiConstants.XMI_LABEL ) || result);
167             result = (node.getNodeName().equals( XmiConstants.XMI_IDREF ) || result);
168         }
169         return result;
170     }
171     
172     public static boolean isXmiNode(Node JavaDoc node) {
173         boolean result = false;
174         if (node != null) {
175             result |= isXmiNavigationNode(node);
176             result |= node.getNodeName().equals(XmiConstants.XMI_ANY_TYPE);
177         }
178         return result;
179     }
180
181     public static class XmiNodeIterator {
182
183         private Node JavaDoc node = null;
184         private String JavaDoc attrName;
185         private String JavaDoc attrValue;
186         private String JavaDoc nodeName;
187
188         private NodeList JavaDoc childNodes = null;
189         private Node JavaDoc currentNode = null;
190         private int index = 0;
191
192         public XmiNodeIterator(Node JavaDoc node,String JavaDoc nodeName) {
193             this.attrName = null;
194             this.attrValue = null;
195             this.nodeName= nodeName;
196             this.node = node;
197             this.childNodes = node.getChildNodes();
198             findNext();
199         }
200
201         public XmiNodeIterator(Node JavaDoc node,String JavaDoc attrName,String JavaDoc attrValue) {
202             this.attrName = attrName;
203             this.attrValue = attrValue;
204             this.nodeName = null;
205             this.node = node;
206             this.childNodes = node.getChildNodes();
207             findNext();
208         }
209
210         public XmiNodeIterator(Node JavaDoc node) {
211             this( node, null, null );
212         }
213
214         public boolean hasNext() {
215             return currentNode != null;
216         }
217
218         public Node JavaDoc next() {
219             Node JavaDoc result = currentNode;
220             findNext();
221             return result;
222         }
223
224         private void findNext() {
225
226             for( int i = index; i < childNodes.getLength(); i++ ) {
227
228                 Node JavaDoc sn = childNodes.item( i );
229                 if ( isTextNode( sn ) ) continue;
230
231                 if ( attrName != null && (
232                 getXmiAttrValueAsString( sn, attrName ) == null ||
233                 !getXmiAttrValueAsString( sn, attrName ).equals( attrValue ) ) ) {
234                     continue;
235                 }
236
237                 if ( nodeName != null && (!resolveFullName(sn).substring(nodeName.lastIndexOf('.') + 1).equals(nodeName.substring(nodeName.lastIndexOf('.') + 1)))) {
238                     continue;
239                 }
240
241                 currentNode = sn;
242                 index = i + 1;
243                 return;
244             }
245
246             currentNode = null;
247             index = childNodes.getLength();
248         }
249     }
250 }
251
Popular Tags