KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > util > 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.mdr.util;
20
21 import java.util.*;
22 import javax.jmi.reflect.*;
23
24 import org.w3c.dom.*;
25
26 import org.netbeans.api.mdr.*;
27
28 import org.netbeans.lib.jmi.util.*;
29
30 /**
31  * @author Marek Sedliak
32  * @version
33  */

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