1 19 package org.netbeans.lib.jmi.xmi; 20 21 import java.util.Locale ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.List ; 27 28 32 public abstract class XmiUtils { 33 private static final String TEXT_NODE_NAME = "#text"; private static final String COMMENT_NODE_NAME = "#comment"; 36 private static HashMap namespaces = new HashMap (); 37 38 static { 39 namespaces.put("Model", "Model"); namespaces.put("UML", "Model_Management"); namespaces.put("CWMOLAP", "Olap"); namespaces.put("CWMTFM", "Transformation"); namespaces.put("CWM", "BusinessInformation"); namespaces.put("CWMRDB", "Relational"); } 46 47 public static String getXmiAttrValueAsString(Node node, String attributeName) { 48 String 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 getXmiRefValue(Node node, String attributeName) { 57 List value = new ArrayList (); 58 if (node != null) { 59 String attr = getAttributeValueAsString(node, getShortName(attributeName)); 60 if (attr == null) { 61 Node refNode = getChildNode(node, attributeName); 62 if (refNode != null) { 63 NodeList 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 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 getAttributeValueAsString(Node node, String attributeName) { 85 String result = null; 86 if ( (node != null) && (node.hasAttributes()) ) { 87 Node attribute = node.getAttributes().getNamedItem(attributeName); 88 if (attribute != null) result = attribute.getNodeValue(); 89 } 90 return result; 91 } 92 93 public static String getElementValueAsString(Node node, String attributeName) { 94 String result = null; 95 if (node != null) { 96 Node attributeNode = getChildNode( node, attributeName ); 97 if (attributeNode != null) { 98 attributeNode = attributeNode.getFirstChild(); 99 if (attributeNode == null) { 100 result = ""; } else { 102 result = attributeNode.getNodeValue(); 103 } 104 } 105 } 106 return result; 107 } 108 109 public static Node getChildNode(Node parentNode, String childNodeName) { 110 Node result = null; 111 if ((parentNode != null) && (childNodeName != null)) { 112 NodeList children = parentNode.getChildNodes(); 113 for (int cnt = 0; cnt < children.getLength(); cnt++) { 114 Node child = children.item(cnt); 115 if (childNodeName.equals(resolveFullName(child))) { 117 result = child; 118 break; 119 } 120 } 121 } 122 return result; 123 } 124 125 public static String resolveFullName(Node element) { 126 if (isXmiNode(element)) return element.getNodeName(); 127 return resolveFullNameAsString( element.getNodeName() ); 128 } 129 130 public static String resolveFullNameAsString(String fullName) { 131 if (fullName != null) { 135 int index; 136 if ((index = fullName.indexOf(':')) > 0) { 137 return namespaces.get(fullName.substring(0, index)) + "." + fullName.substring(index + 1); } else { 140 return fullName; 141 } 142 } 143 else 144 return null; 145 } 146 147 public static String getShortName(String fullyQualifiedName) { 148 String 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 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 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 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 node = null; 184 private String attrName; 185 private String attrValue; 186 private String nodeName; 187 188 private NodeList childNodes = null; 189 private Node currentNode = null; 190 private int index = 0; 191 192 public XmiNodeIterator(Node node,String 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 node,String attrName,String 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 node) { 211 this( node, null, null ); 212 } 213 214 public boolean hasNext() { 215 return currentNode != null; 216 } 217 218 public Node next() { 219 Node 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 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 |