1 19 20 package org.netbeans.test.xml.schema.core.lib.dom.parser; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.util.Collection ; 25 import java.util.Iterator ; 26 import java.util.TreeMap ; 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.ParserConfigurationException ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.NamedNodeMap ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 import org.xml.sax.SAXException ; 35 import org.netbeans.test.xml.schema.core.lib.types.ComponentCategories; 36 37 38 42 public class SchemaDOMBuilder { 43 44 public static final int SCHEMA_LEVEL = 1; 45 public static final int GLOBAL_COMPONENTS_LEVEL = 2; 46 47 Node m_schema = null; 48 TreeMap <String , Node > m_globalAttributes = new TreeMap <String , Node >(); 49 TreeMap <String , Node > m_globalAttributeGroups = new TreeMap <String , Node >(); 50 TreeMap <String , Node > m_globalComplexTypes = new TreeMap <String , Node >(); 51 TreeMap <String , Node > m_globalElements = new TreeMap <String , Node >(); 52 TreeMap <String , Node > m_globalGroups = new TreeMap <String , Node >(); 53 TreeMap <String , Node > m_referencedSchemas = new TreeMap <String , Node >(); 54 TreeMap <String , Node > m_globalSimpleTypes = new TreeMap <String , Node >(); 55 56 public int m_lineNmb; 57 private String m_strFileName; 58 59 private Document m_doc = null; 60 61 64 public SchemaDOMBuilder() { 65 } 66 67 70 71 public void setFileName(String fileName) { 72 m_strFileName = fileName; 73 } 74 75 public void setInitialLineNumber(int lineNmb) { 76 m_lineNmb = lineNmb; 77 } 78 79 public NodeIterator getNodeIterator(ComponentCategories category) { 80 NodeIterator iterator = null; 81 82 switch (category) { 83 case ATTRIBUTES: 84 iterator = new NodeIterator(m_globalAttributes); 85 break; 86 case ATTRIBUTE_GROUPS: 87 iterator = new NodeIterator(m_globalAttributeGroups); 88 break; 89 case COMPLEX_TYPES: 90 iterator = new NodeIterator(m_globalComplexTypes); 91 break; 92 case ELEMENTS: 93 iterator = new NodeIterator(m_globalElements); 94 break; 95 case GROUPS: 96 iterator = new NodeIterator(m_globalGroups); 97 break; 98 case REFERENCED_SCHEMAS: 99 iterator = new NodeIterator(m_referencedSchemas); 100 break; 101 case SIMPLE_TYPES: 102 iterator = new NodeIterator(m_globalSimpleTypes); 103 break; 104 } 105 return iterator; 106 } 107 108 public void build() { 109 File file = new File (m_strFileName); 110 111 try { 112 m_doc = getDocumentBuilder().parse(file); 113 } catch (SAXException e) { 114 System.out.println("SAXException: " + e.getMessage()); 115 return; 116 } catch (IOException e) { 117 System.out.println("IOException: " + e.getMessage()); 118 return; 119 } 120 121 categorizeNodes(m_doc, 0); 122 123 adaptDocToColumnView(m_doc); 124 } 125 126 127 private void adaptDocToColumnView(Node node) { 128 129 if (node.getNodeType() == Node.ELEMENT_NODE) { 130 String strComponentName = removePrefix(node.getNodeName()); 131 132 if (strComponentName.equals("simpleType")) { 133 NodeList childList = node.getChildNodes(); 134 135 for (int i = 0; i < childList.getLength(); i++) { 136 Node child = childList.item(i); 137 String childName = removePrefix(child.getNodeName()); 138 if (childName.equals("restriction")) { 139 NodeList childList1 = child.getChildNodes(); 140 for (int j = 0; j < childList1.getLength(); j++) { 141 Node child1 = childList1.item(j); 142 String childName1 = removePrefix(child1.getNodeName()); 143 if (childName1.equals("enumeration")) { 144 node.appendChild(child1); 145 j--; 146 } else { 147 child.removeChild(child1); 148 j--; 149 } 150 } 151 node.removeChild(child); 152 i--; 153 } else if (childName.equals("list")) { 154 node.removeChild(child); 155 i--; 156 } 157 } 158 } else if (strComponentName.equals("complexType")) { 159 NodeList childList = node.getChildNodes(); 160 161 for (int i = 0; i < childList.getLength(); i++) { 162 Node child = childList.item(i); 163 String childName = removePrefix(child.getNodeName()); 164 if (childName.equals("simpleContent") || childName.equals("complexContent")) { 165 NodeList childList1 = child.getChildNodes(); 166 for (int j = 0; j < childList1.getLength(); j++) { 167 Node child1 = childList1.item(j); 168 String childName1 = removePrefix(child1.getNodeName()); 169 if (childName1.equals("restriction") || childName1.equals("extension")) { 170 NodeList childList2 = child1.getChildNodes(); 171 for (int k = 0; k < childList2.getLength(); k++) { 172 Node child2 = childList2.item(k); 173 node.appendChild(child2); 174 k--; 175 } 176 child.removeChild(child1); 177 j--; 178 } 179 node.removeChild(child); 180 i--; 181 } 182 } 183 } 184 } 185 } 186 187 NodeList childList = node.getChildNodes(); 188 189 for (int i = 0; i < childList.getLength(); i++) { 190 adaptDocToColumnView(childList.item(i)); 191 } 192 193 } 194 195 public static void main(String [] args) { 196 197 SchemaDOMBuilder builder = new SchemaDOMBuilder(); 198 199 builder.setFileName(args[0]); 200 201 builder.build(); 202 203 builder.printNodes(builder.m_doc, 0); 204 } 205 206 private void printGlobalComponents(TreeMap <String , Node > map) { 207 Collection c = map.values(); 208 Iterator iterator = c.iterator(); 209 while(iterator.hasNext()) { 210 Node node = (Node ) iterator.next(); 211 ExtraNodeInfo sn = ExtraNodeInfo.getExtraNodeInfo(node); 212 printNodeInfo(node); 213 System.out.println("\n line " + sn.getLineNmb()); 214 System.out.println(" component name " + sn.getComponentName()); 215 } 216 } 217 218 private static DocumentBuilder getDocumentBuilder() { 219 220 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 221 222 DocumentBuilder builder = null; 223 224 try { 225 builder = factory.newDocumentBuilder(); 226 } catch (ParserConfigurationException e) {} 227 228 return builder; 229 } 230 231 private void categorizeNodes(Node node, int level) { 232 233 while (true) { 234 String strValue = node.getNodeValue(); 235 if (strValue != null) { 236 char[] value = strValue.toCharArray(); 237 238 for (int i = 0; i < value.length; i++) { 239 if (value[i] == '\n') { 240 m_lineNmb++; 241 } 242 } 243 } 244 245 switch (node.getNodeType()) { 246 case Node.TEXT_NODE: 247 strValue = strValue.replace("\r\n", "").trim(); 248 if (strValue.length() == 0) { 249 Node parent = node.getParentNode(); 250 Node nextNode = node.getNextSibling(); 251 parent.removeChild(node); 252 node = nextNode; 253 if (node != null) continue; 254 return; 255 } 256 case Node.COMMENT_NODE: 257 Node parent1 = node.getParentNode(); 258 Node nextNode1 = node.getNextSibling(); 259 parent1.removeChild(node); 260 node = nextNode1; 261 if (node != null) continue; 262 return; 263 } 264 break; 265 } 266 267 String strComponentName = removePrefix(node.getNodeName()); 268 ExtraNodeInfo schemaNode = new ExtraNodeInfo(m_lineNmb, strComponentName, node, level == GLOBAL_COMPONENTS_LEVEL); 269 node.setUserData("", schemaNode, null); 270 271 switch (level) { 272 case GLOBAL_COMPONENTS_LEVEL: 273 NamedNodeMap map = node.getAttributes(); 274 if (map != null) { 275 Node nameAttrNode = map.getNamedItem("name"); 276 if (strComponentName.equals("attribute")) { 277 String strName = nameAttrNode.getNodeValue(); 278 m_globalAttributes.put(strName, node); 279 } else if (strComponentName.equals("attributeGroup")) { 280 String strName = nameAttrNode.getNodeValue(); 281 m_globalAttributeGroups.put(strName, node); 282 } else if (strComponentName.equals("complexType")) { 283 String strName = nameAttrNode.getNodeValue(); 284 m_globalComplexTypes.put(strName, node); 285 } else if (strComponentName.equals("element")) { 286 String strName = nameAttrNode.getNodeValue(); 287 m_globalElements.put(strName, node); 288 } else if (strComponentName.equals("group")) { 289 String strName = nameAttrNode.getNodeValue(); 290 m_globalGroups.put(strName, node); 291 } else if (strComponentName.equals("include") || strComponentName.equals("import") || strComponentName.equals("redefine")) { 292 } else if (strComponentName.equals("simpleType")) { 293 String strName = nameAttrNode.getNodeValue(); 294 m_globalSimpleTypes.put(strName, node); 295 } 296 } 297 break; 298 case SCHEMA_LEVEL: 299 m_schema = node; 300 break; 301 } 302 303 NodeList childList = node.getChildNodes(); 304 305 for (int i = 0; i < childList.getLength(); i++) { 306 categorizeNodes(childList.item(i), level+1); 307 } 308 } 309 310 private void printNodes(Node node, int level) { 311 312 System.out.println(); 313 System.out.print("*"); 314 315 for (int j = 0; j < level; j++) { 316 System.out.print("|"); 317 } 318 319 printNodeInfo(node); 320 321 for (Node child = node.getFirstChild(); child != null; child = child.getNextSibling()) { 322 printNodes(child, level+1); 323 } 324 } 325 326 private void printNodeInfo(Node node) { 327 System.out.print("Component [" + node.getNodeName() + "], Node type [" + node.getNodeType() + "], Node value [" + node.getNodeValue() + "]"); 328 NamedNodeMap attrMap = node.getAttributes(); 329 330 if (attrMap != null && attrMap.getLength() > 0) { 331 System.out.print(", Attrs: "); 332 for (int i = 0; i < attrMap.getLength(); i++) { 333 System.out.print(attrMap.item(i).getNodeName() + "=\"" + attrMap.item(i).getNodeValue() + "\" "); 334 } 335 } 336 } 337 338 private String removePrefix(String qualifiedName) { 339 return qualifiedName.substring(qualifiedName.lastIndexOf(":")+1); 340 } 341 } 342 | Popular Tags |