1 package com.calipso.xmleditor; 2 3 import org.exolab.castor.xml.schema.*; 4 import org.exolab.castor.xml.schema.ComplexType; 5 import org.exolab.castor.xml.schema.reader.SchemaReader; 6 import org.xml.sax.InputSource ; 7 8 import java.io.IOException ; 9 import java.io.FileInputStream ; 10 import java.util.Enumeration ; 11 12 19 20 public class XmlEditorTreeDefinition { 21 22 private Schema schema; 23 private String rootName; 24 private XmlEditorTreeDefinitionNode root; 25 26 public XmlEditorTreeDefinition(String schemaLocation) throws XmlEditorException{ 27 try { 28 initialize(schemaLocation); 29 this.rootName = SchemaRootSearcher.searchRootFrom(schema); 30 buildTree(); 31 } catch (IOException e) { 32 throw new XmlEditorException(e); 33 } 34 } 35 36 private void buildTree() throws XmlEditorException{ 37 ElementDecl elementDecl = schema.getElementDecl(rootName); 38 ComplexType complexType = (ComplexType) elementDecl.getType(); 39 Enumeration groups = complexType.enumerate(); 40 XmlEditorTreeDefinitionNode current = getRoot().addNodeFrom(elementDecl.getName()); 41 current.setMustHaveChilds(complexType.getMinOccurs()); 42 current.addNodeAttributes(complexType.getAttributeDecls()); 43 while(groups.hasMoreElements()) { 44 Group group = (Group) groups.nextElement(); 45 ContentModelGroup modelGroup = group.getContentModelGroup(); 46 Enumeration rootChilds = modelGroup.enumerate(); 47 fillTree(rootChilds, getRoot()); 48 } 49 } 50 51 private void fillTree(Enumeration elements, XmlEditorTreeDefinitionNode node) throws XmlEditorException{ 52 while(elements.hasMoreElements()) { 53 Object object = elements.nextElement(); 54 if(object instanceof Group) { 55 Group group = (Group) object; 56 ContentModelGroup modelGroup = group.getContentModelGroup(); 57 Enumeration enumeration = modelGroup.enumerate(); 58 fillTree(enumeration, node); 59 } 60 else { 61 ComplexType type = null; 62 ElementDecl elementDecl = (ElementDecl) object; 63 XmlEditorTreeDefinitionNode current = node.addNodeFrom(elementDecl.getName()); 64 if(schema.getElementDecl(current.getValue()) == null) { 65 type = (ComplexType) elementDecl.getType(); 66 } else { 67 type = (ComplexType) (schema.getElementDecl(current.getValue())).getType(); 68 } 69 current.setMustHaveChilds(type.getMinOccurs()); 70 current.addNodeAttributes(type.getAttributeDecls()); 71 fillTree(type.enumerate(), current); 72 } 73 } 74 } 75 76 private XmlEditorTreeDefinitionNode getRoot() throws XmlEditorException{ 77 if(root == null) { 78 root = new XmlEditorTreeDefinitionNode(rootName); 79 root.addNodeAttributes(((ComplexType)schema.getElementDecl(rootName).getType()).getAttributeDecls()); 80 root.setMustHaveChilds(1); 81 } 82 return root; 83 } 84 85 private void initialize(String schemaLocation) throws IOException { 86 this.schema = loadSchema(schemaLocation); 87 } 88 89 private Schema loadSchema(String url) throws IOException { 90 FileInputStream stream = new FileInputStream (url); 91 InputSource inputSource = new InputSource (stream); 92 SchemaReader reader = new SchemaReader(inputSource); 93 return reader.read(); 94 } 95 96 public String getRootName() { 97 return rootName; 98 } 99 100 public XmlEditorTreeDefinitionNode getRootDefinition() { 101 return root; 102 } 103 } 104 | Popular Tags |