KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.calipso.xmleditor;
2
3 import org.exolab.castor.xml.schema.*;
4
5 import java.util.Enumeration JavaDoc;
6
7 /**
8  *
9  * User: soliveri
10  * Date: 25-sep-2003
11  * Time: 14:01:55
12  *
13  */

14
15 public class SchemaRootSearcher {
16
17   public static String JavaDoc searchRootFrom(Schema schema) {
18     String JavaDoc root = null;
19     Enumeration JavaDoc enumeration = schema.getElementDecls();
20     for(int i = 0 ; enumeration.hasMoreElements() ; i++) {
21       ElementDecl elemenentDecl = (ElementDecl) enumeration.nextElement();
22       boolean possibleRoot = isRoot(elemenentDecl.getName(), schema.getElementDecls(), i);
23       if(possibleRoot) {
24         root = elemenentDecl.getName();
25         System.out.println(root);
26         break;
27       }
28     }
29     return root;
30   }
31
32   private static boolean isRoot(String JavaDoc current, Enumeration JavaDoc enumeration, int j) {
33     int i;
34     int count = 0;
35     for(i = 0 ; enumeration.hasMoreElements() ; i ++) {
36       if(i != j) {
37         ElementDecl elementDecl = (ElementDecl) enumeration.nextElement();
38         XMLType xmlType = elementDecl.getType();
39          if(xmlType != null) {
40           if(xmlType.isComplexType()) {
41             ComplexType type = (ComplexType) elementDecl.getType();
42             Enumeration JavaDoc elements = type.enumerate();
43             if(!isElementIncluded(elements, current)) {
44               count ++;
45             }
46           }
47         }
48       }
49     }
50     if(count == i - 1) {
51       return true;
52     }
53     return false;
54   }
55
56   private static boolean isElementIncluded(Enumeration JavaDoc elements, String JavaDoc current) {
57     boolean found = false;
58     while(elements.hasMoreElements()) {
59       Object JavaDoc object = elements.nextElement();
60       if(object instanceof Group) {
61         Group group = (Group) object;
62         ContentModelGroup modelGroup = group.getContentModelGroup();
63         Enumeration JavaDoc enumeration = modelGroup.enumerate();
64         if(isElementIncluded(enumeration, current)) {
65           found = true;
66         }
67       } else if(object instanceof ElementDecl) {
68           ElementDecl elementDecl = (ElementDecl) object;
69           if(elementDecl.getName().equals(current)) {
70             return true;
71           }
72       }
73     }
74     return found;
75   }
76 }
77
Popular Tags