1 package com.calipso.xmleditor; 2 3 import org.w3c.dom.*; 4 import javax.xml.parsers.DocumentBuilderFactory ; 5 import javax.xml.parsers.DocumentBuilder ; 6 import javax.swing.tree.DefaultTreeModel ; 7 import java.io.File ; 8 import java.util.*; 9 10 17 18 public class XmlEditorLoader { 19 20 private DefaultTreeModel model; 21 private XmlEditorTreeDefinitionNode definition; 22 private Node domRoot; 23 24 public XmlEditorLoader(DefaultTreeModel model, XmlEditorTreeDefinitionNode definition) { 25 this.model = model; 26 this.definition = definition; 27 } 28 29 public XmlEditorLoader() { 30 } 31 32 39 public DefaultTreeModel loadFrom(File file, boolean hasXsd) throws XmlEditorException{ 40 Document document = getNewDocumentFrom(file); 41 Node docRoot = document.getDocumentElement(); 42 this.domRoot = docRoot; 43 if(hasXsd) { 44 fillModelFrom(docRoot, (XmlEditorTreeModelNode) model.getRoot(), definition); 45 return model; 46 } else { 47 XmlEditorTreeModelNode root = new XmlEditorTreeModelNode(docRoot.getNodeName()); 48 fillModelFrom(root, docRoot); 49 return new DefaultTreeModel (root); 50 } 51 } 52 53 58 private Vector getAttrsFrom(Node node) { 59 Vector vector = new Vector(); 60 NamedNodeMap attrs = node.getAttributes(); 61 for(int i = 0 ; i < attrs.getLength() ; i++) { 62 Node attr = attrs.item(i); 63 vector.add(attr.getNodeName()); 64 } 65 return vector; 66 } 67 68 73 private void fillModelFrom(XmlEditorTreeModelNode root, Node docRoot) { 74 Vector attrsNames = getAttrsFrom(docRoot); 75 XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(getIdValueFrom(docRoot)); newModelNode.addAttributesFrom(attrsNames, getAttrs(docRoot,attrsNames)); 77 root.add(newModelNode); 78 fillModelFrom(docRoot.getChildNodes(), root); 79 } 80 81 private void fillModelFrom(NodeList children, XmlEditorTreeModelNode modelNode) { 82 XmlEditorTreeModelNode newParentModelNode = null; 83 boolean elementCreated = false; 84 for(int i = 0 ; i < children.getLength() ; i++) { 85 Node node = children.item(i); 86 if(node.getNodeType() == Node.ELEMENT_NODE) { 87 if(node.getAttributes().getLength() > 0) { 88 if(!elementCreated) { 89 newParentModelNode = new XmlEditorTreeModelNode(node.getNodeName()); 90 elementCreated = true; 91 modelNode.add(newParentModelNode); 92 } 93 XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(getIdValueFrom(node)); 94 Vector attrsNames = getAttrsFrom(node); 95 newModelNode.addAttributesFrom(attrsNames, getAttrs(node,attrsNames)); 96 newParentModelNode.add(newModelNode); 97 } else { 98 XmlEditorTreeModelNode newModelNode = new XmlEditorTreeModelNode(node.getNodeName()); 99 modelNode.add(newModelNode); 100 fillModelFrom(node.getChildNodes(), newModelNode); 101 } 102 } 103 } 104 } 105 106 private void fillModelFrom(Node domRoot, XmlEditorTreeModelNode modelNode, XmlEditorTreeDefinitionNode definition) { 107 XmlEditorTreeModelNode newModelNode = getModelNodeFrom(modelNode.children(),definition.getValue(),false); 113 Vector attrsNames = getAttrsFrom(definition); 114 newModelNode.addAttributesFrom(attrsNames, getAttrs(domRoot,attrsNames)); 115 fillFrom(domRoot.getChildNodes(), definition); 116 } 117 118 123 private Vector getAttrsFrom(XmlEditorTreeDefinitionNode definition) { 124 Vector vector = new Vector(); 125 Enumeration enumeration = definition.getItemsKeys().elements(); 126 while(enumeration.hasMoreElements()) { 127 vector.add(enumeration.nextElement().toString()); 128 } 129 return vector; 130 } 131 132 private void fillFrom(NodeList childNodes, XmlEditorTreeDefinitionNode rootDefinition) { 133 for(int i = 0 ; i < childNodes.getLength() ; i++) { 134 Node node = childNodes.item(i); 135 if(node.getNodeType() == Node.ELEMENT_NODE) { 136 if(node.getAttributes() != null && node.getAttributes().getLength() > 0) { 137 XmlEditorTreeModelNode parent = getModelNodeFrom(node.getNodeName()); 138 XmlEditorTreeModelNode child = new XmlEditorTreeModelNode(getIdValueFrom(node)); 139 Vector attrNames = getAttrsFrom(getNodeDefinitionFrom(rootDefinition, node.getNodeName())); 140 child.addAttributesFrom(attrNames, getAttrs(node, attrNames)); 141 parent.add(child); 142 } else { 143 fillFrom(node.getChildNodes(), rootDefinition); 144 } 145 } 146 } 147 } 148 149 155 private XmlEditorTreeDefinitionNode getNodeDefinitionFrom(XmlEditorTreeDefinitionNode rootDefinition, String nodeName) { 156 if(rootDefinition.getValue().equals(nodeName)) { 157 return rootDefinition; 158 } else { 159 return getNodeDefinitionFrom(rootDefinition.getSubnodes(), nodeName, false); 160 } 161 } 162 163 private XmlEditorTreeDefinitionNode getNodeDefinitionFrom(Map rootDefinition, String nodeName, boolean found) { 164 XmlEditorTreeDefinitionNode returnVal = null; 165 Iterator iterator = rootDefinition.keySet().iterator(); 166 while(iterator.hasNext() && !found) { 167 XmlEditorTreeDefinitionNode definition = (XmlEditorTreeDefinitionNode) rootDefinition.get(iterator.next().toString()); 168 if(definition.getValue().equals(nodeName)) { 169 return definition; 170 } else { 171 returnVal = getNodeDefinitionFrom(definition.getSubnodes(), nodeName, found); 172 if(returnVal != null) { 173 found = true; 174 } 175 } 176 } 177 return returnVal; 178 } 179 180 private String getIdValueFrom(Node node) { 181 Vector names = getAttrsFrom(node); 182 int pos = names.indexOf("Id"); 183 if(pos >= 0){ 184 return node.getAttributes().item(pos).getNodeValue().toString(); 185 }else if(names.indexOf("Name")>=0){ 186 return node.getAttributes().item(names.indexOf("Name")).getNodeValue().toString(); 187 }else{ 188 Node attrNode = node.getAttributes().item(0); 189 return attrNode.getNodeValue(); 190 } 191 } 192 193 196 197 202 private XmlEditorTreeModelNode getModelNodeFrom(String nodeName) { 203 if(((XmlEditorTreeModelNode)model.getRoot()).getUserObject().equals(nodeName)) { 204 return (XmlEditorTreeModelNode)model.getRoot(); 205 } else { 206 return getModelNodeFrom(((XmlEditorTreeModelNode)model.getRoot()).children(), nodeName, false); 207 } 208 } 209 210 private XmlEditorTreeModelNode getModelNodeFrom(Enumeration children, String nodeName, boolean found) { 211 XmlEditorTreeModelNode returnVal = null; 212 while(children.hasMoreElements() && !found) { 213 XmlEditorTreeModelNode current = (XmlEditorTreeModelNode) children.nextElement(); 214 if(current.getUserObject().equals(nodeName)) { 215 return current; 216 } else { 217 returnVal = getModelNodeFrom(current.children(), nodeName, found); 218 if(returnVal != null) { 219 found = true; 220 } 221 } 222 } 223 return returnVal; 224 } 225 226 private Vector getAttrs(Node node, Vector attrNames) { 227 Vector vector = new Vector(); 228 NamedNodeMap attrs = node.getAttributes(); 229 Enumeration enumeration = attrNames.elements(); 230 while(enumeration.hasMoreElements()) { 231 Node attr = attrs.getNamedItem(enumeration.nextElement().toString()); 232 if(attr == null) { 233 vector.add(""); 234 } else { 235 vector.add(attr.getNodeValue()); 236 } 237 } 238 return vector; 239 } 240 241 247 private Document getNewDocumentFrom(File file) throws XmlEditorException{ 248 Document document = null; 249 try { 250 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 251 DocumentBuilder builder = factory.newDocumentBuilder(); 252 document = builder.parse(file); 253 } catch (Exception e) { 254 throw new XmlEditorException(e); 255 } 256 return document; 257 } 258 259 public Node getDomRoot() { 260 return domRoot; 261 } 262 } 263 | Popular Tags |