01: package com.calipso.xmleditor;
02:
03: import org.exolab.castor.xml.schema.*;
04:
05: import java.util.Enumeration;
06:
07: /**
08: *
09: * User: soliveri
10: * Date: 25-sep-2003
11: * Time: 14:01:55
12: *
13: */
14:
15: public class SchemaRootSearcher {
16:
17: public static String searchRootFrom(Schema schema) {
18: String root = null;
19: Enumeration enumeration = schema.getElementDecls();
20: for (int i = 0; enumeration.hasMoreElements(); i++) {
21: ElementDecl elemenentDecl = (ElementDecl) enumeration
22: .nextElement();
23: boolean possibleRoot = isRoot(elemenentDecl.getName(),
24: schema.getElementDecls(), i);
25: if (possibleRoot) {
26: root = elemenentDecl.getName();
27: System.out.println(root);
28: break;
29: }
30: }
31: return root;
32: }
33:
34: private static boolean isRoot(String current,
35: Enumeration enumeration, int j) {
36: int i;
37: int count = 0;
38: for (i = 0; enumeration.hasMoreElements(); i++) {
39: if (i != j) {
40: ElementDecl elementDecl = (ElementDecl) enumeration
41: .nextElement();
42: XMLType xmlType = elementDecl.getType();
43: if (xmlType != null) {
44: if (xmlType.isComplexType()) {
45: ComplexType type = (ComplexType) elementDecl
46: .getType();
47: Enumeration elements = type.enumerate();
48: if (!isElementIncluded(elements, current)) {
49: count++;
50: }
51: }
52: }
53: }
54: }
55: if (count == i - 1) {
56: return true;
57: }
58: return false;
59: }
60:
61: private static boolean isElementIncluded(Enumeration elements,
62: String current) {
63: boolean found = false;
64: while (elements.hasMoreElements()) {
65: Object object = elements.nextElement();
66: if (object instanceof Group) {
67: Group group = (Group) object;
68: ContentModelGroup modelGroup = group
69: .getContentModelGroup();
70: Enumeration enumeration = modelGroup.enumerate();
71: if (isElementIncluded(enumeration, current)) {
72: found = true;
73: }
74: } else if (object instanceof ElementDecl) {
75: ElementDecl elementDecl = (ElementDecl) object;
76: if (elementDecl.getName().equals(current)) {
77: return true;
78: }
79: }
80: }
81: return found;
82: }
83: }
|