1 package com.calipso.xmleditor; 2 3 import org.exolab.castor.xml.schema.*; 4 5 import java.util.Enumeration ; 6 7 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.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 current, Enumeration 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 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 elements, String current) { 57 boolean found = false; 58 while(elements.hasMoreElements()) { 59 Object object = elements.nextElement(); 60 if(object instanceof Group) { 61 Group group = (Group) object; 62 ContentModelGroup modelGroup = group.getContentModelGroup(); 63 Enumeration 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 |