KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > xmleditor > XmlEditorTreeDefinition


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 JavaDoc;
7
8 import java.io.IOException JavaDoc;
9 import java.io.FileInputStream JavaDoc;
10 import java.util.Enumeration JavaDoc;
11
12 /**
13  *
14  * User: soliveri
15  * Date: 25-sep-2003
16  * Time: 13:54:52
17  *
18  */

19
20 public class XmlEditorTreeDefinition {
21
22   private Schema schema;
23   private String JavaDoc rootName;
24   private XmlEditorTreeDefinitionNode root;
25
26   public XmlEditorTreeDefinition(String JavaDoc schemaLocation) throws XmlEditorException{
27     try {
28       initialize(schemaLocation);
29       this.rootName = SchemaRootSearcher.searchRootFrom(schema);
30       buildTree();
31     } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc rootChilds = modelGroup.enumerate();
47       fillTree(rootChilds, getRoot());
48     }
49   }
50
51   private void fillTree(Enumeration JavaDoc elements, XmlEditorTreeDefinitionNode node) throws XmlEditorException{
52     while(elements.hasMoreElements()) {
53       Object JavaDoc object = elements.nextElement();
54       if(object instanceof Group) {
55         Group group = (Group) object;
56         ContentModelGroup modelGroup = group.getContentModelGroup();
57         Enumeration JavaDoc 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 JavaDoc schemaLocation) throws IOException JavaDoc {
86     this.schema = loadSchema(schemaLocation);
87   }
88
89   private Schema loadSchema(String JavaDoc url) throws IOException JavaDoc {
90     FileInputStream JavaDoc stream = new FileInputStream JavaDoc(url);
91     InputSource JavaDoc inputSource = new InputSource JavaDoc(stream);
92     SchemaReader reader = new SchemaReader(inputSource);
93     return reader.read();
94   }
95
96   public String JavaDoc getRootName() {
97     return rootName;
98   }
99
100   public XmlEditorTreeDefinitionNode getRootDefinition() {
101     return root;
102   }
103 }
104
Popular Tags