001: package com.calipso.xmleditor;
002:
003: import org.exolab.castor.xml.schema.*;
004: import org.exolab.castor.xml.schema.ComplexType;
005: import org.exolab.castor.xml.schema.reader.SchemaReader;
006: import org.xml.sax.InputSource;
007:
008: import java.io.IOException;
009: import java.io.FileInputStream;
010: import java.util.Enumeration;
011:
012: /**
013: *
014: * User: soliveri
015: * Date: 25-sep-2003
016: * Time: 13:54:52
017: *
018: */
019:
020: public class XmlEditorTreeDefinition {
021:
022: private Schema schema;
023: private String rootName;
024: private XmlEditorTreeDefinitionNode root;
025:
026: public XmlEditorTreeDefinition(String schemaLocation)
027: throws XmlEditorException {
028: try {
029: initialize(schemaLocation);
030: this .rootName = SchemaRootSearcher.searchRootFrom(schema);
031: buildTree();
032: } catch (IOException e) {
033: throw new XmlEditorException(e);
034: }
035: }
036:
037: private void buildTree() throws XmlEditorException {
038: ElementDecl elementDecl = schema.getElementDecl(rootName);
039: ComplexType complexType = (ComplexType) elementDecl.getType();
040: Enumeration groups = complexType.enumerate();
041: XmlEditorTreeDefinitionNode current = getRoot().addNodeFrom(
042: elementDecl.getName());
043: current.setMustHaveChilds(complexType.getMinOccurs());
044: current.addNodeAttributes(complexType.getAttributeDecls());
045: while (groups.hasMoreElements()) {
046: Group group = (Group) groups.nextElement();
047: ContentModelGroup modelGroup = group.getContentModelGroup();
048: Enumeration rootChilds = modelGroup.enumerate();
049: fillTree(rootChilds, getRoot());
050: }
051: }
052:
053: private void fillTree(Enumeration elements,
054: XmlEditorTreeDefinitionNode node) throws XmlEditorException {
055: while (elements.hasMoreElements()) {
056: Object object = elements.nextElement();
057: if (object instanceof Group) {
058: Group group = (Group) object;
059: ContentModelGroup modelGroup = group
060: .getContentModelGroup();
061: Enumeration enumeration = modelGroup.enumerate();
062: fillTree(enumeration, node);
063: } else {
064: ComplexType type = null;
065: ElementDecl elementDecl = (ElementDecl) object;
066: XmlEditorTreeDefinitionNode current = node
067: .addNodeFrom(elementDecl.getName());
068: if (schema.getElementDecl(current.getValue()) == null) {
069: type = (ComplexType) elementDecl.getType();
070: } else {
071: type = (ComplexType) (schema.getElementDecl(current
072: .getValue())).getType();
073: }
074: current.setMustHaveChilds(type.getMinOccurs());
075: current.addNodeAttributes(type.getAttributeDecls());
076: fillTree(type.enumerate(), current);
077: }
078: }
079: }
080:
081: private XmlEditorTreeDefinitionNode getRoot()
082: throws XmlEditorException {
083: if (root == null) {
084: root = new XmlEditorTreeDefinitionNode(rootName);
085: root.addNodeAttributes(((ComplexType) schema
086: .getElementDecl(rootName).getType())
087: .getAttributeDecls());
088: root.setMustHaveChilds(1);
089: }
090: return root;
091: }
092:
093: private void initialize(String schemaLocation) throws IOException {
094: this .schema = loadSchema(schemaLocation);
095: }
096:
097: private Schema loadSchema(String url) throws IOException {
098: FileInputStream stream = new FileInputStream(url);
099: InputSource inputSource = new InputSource(stream);
100: SchemaReader reader = new SchemaReader(inputSource);
101: return reader.read();
102: }
103:
104: public String getRootName() {
105: return rootName;
106: }
107:
108: public XmlEditorTreeDefinitionNode getRootDefinition() {
109: return root;
110: }
111: }
|