1 22 23 package org.xquark.xquery.typing; 24 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.xquark.schema.*; 30 import org.xquark.xquery.parser.QName; 31 import org.xquark.xquery.parser.XQueryException; 32 33 public class OccurrenceVisitor extends DefaultSchemaVisitor { 34 private static final String RCSRevision = "$Revision: 1.4 $"; 35 private static final String RCSName = "$Name: $"; 36 private static final String STAR = "*"; 37 38 private int[] occurs = new int[2]; 39 private String namespace = null; 40 private String name = null; 41 private List matches = new ArrayList (); 42 private boolean matchesWildcard = false; 43 private SchemaManager schemamanager = null; 44 45 public void reset(String namespace, String name, SchemaManager schemamanager) { 46 occurs[0] = occurs[1] = 0; 47 matches.clear(); 48 matchesWildcard = false; 49 this.namespace = namespace; 50 this.name = name; 51 this.schemamanager = schemamanager; 52 } 53 54 public QType getQType() throws XQueryException { 55 byte occurrence = QType.OCC_0_0; 57 if (occurs[0] == 0) { 58 if (occurs[1] == 0) 59 occurrence = QType.OCC_0_0; 60 else if (occurs[1] == 1) 61 occurrence = QType.OCC_0_1; 62 else 63 occurrence = QType.OCC_0_N; 64 } else if (occurs[0] == 1) { 65 if (occurs[1] == 1) 66 occurrence = QType.OCC_1_1; 67 else 68 occurrence = QType.OCC_1_N; 69 } 70 if (matchesWildcard) { 71 return new QTypeElement(new QName(namespace, null, name, null, null), schemamanager.getType(SchemaConstants.XMLSCHEMA_URI, TypeVisitor.SC_ANYTYPE), occurrence); 72 } 73 if (!matches.isEmpty()) { 74 if (matches.size() == 1) { 75 return getQTypeFromDeclaration((Declaration) matches.get(0), occurrence); 76 } else { 77 ArrayList qtypes = new ArrayList (matches.size()); 78 for (int i = 0; i < matches.size(); i++) { 79 qtypes.add(getQTypeFromDeclaration((Declaration) matches.get(i), QType.OCC_1_1)); 80 } 81 return new QTypeUnion(qtypes, occurrence); 82 } 83 } 84 return null; 85 } 86 87 94 private void setValues(long min, long max) { 95 if (max > Integer.MAX_VALUE) 96 max = Integer.MAX_VALUE; 97 occurs[0] = (int) min; 98 occurs[1] = (int) max; 99 } 100 101 public void visit(Particle particle) throws SchemaException { 102 ((SchemaVisitable) particle.getTerm()).accept(this); 103 long min = occurs[0] * particle.getMinOccurs(); 104 long max = occurs[1] * particle.getMaxOccurs(); 105 setValues(min, max); 106 } 107 108 public void visit(ModelGroup group) throws SchemaException { 109 long min = 0; 110 long max = 0; 111 Iterator it = group.iterator(); 112 while (it.hasNext()) { 113 occurs[0] = occurs[1] = 0; 114 ((SchemaVisitable) it.next()).accept(this); 115 min += occurs[0]; 116 max += occurs[1]; 117 } 118 setValues(min, max); 119 } 120 121 public void visit(ChoiceModelGroup group) throws SchemaException { 122 long min = Integer.MAX_VALUE; 123 long max = 0; 124 Iterator it = group.iterator(); 125 while (it.hasNext()) { 126 occurs[0] = occurs[1] = 0; 127 ((SchemaVisitable) it.next()).accept(this); 128 if (occurs[0] < min) 129 min = occurs[0]; 130 if (occurs[1] > max) 131 max = occurs[1]; 132 } 133 setValues(min, max); 134 } 135 136 public void visit(ElementDeclaration decl) throws SchemaException { 137 if (namespace != null && namespace.equals(STAR)) { 138 if (name.equals(STAR)) { 139 matches.add(decl); 140 if (decl.getSubstitutionElementList() != null) 141 matches.addAll(decl.getSubstitutionElementList()); 142 } else { 143 if (name.equals(decl.getName())) { 144 matches.add(decl); 145 } 146 List decllist = decl.getSubstitutionElementList(); 147 if (decllist != null && !decllist.isEmpty()) { 148 for (int i = 0; i < decllist.size(); i++) { 149 if (name.equals(((Declaration) decllist.get(i)).getName())) { 150 matches.add(decllist.get(i)); 151 } 152 } 153 } 154 } 155 } else if (name.equals(STAR)) { 156 if (namespace != null && namespace.equals(decl.getNamespace())) { 157 matches.add(decl); 158 } 159 List decllist = decl.getSubstitutionElementList(); 160 if (decllist != null && !decllist.isEmpty()) { 161 for (int i = 0; i < decllist.size(); i++) { 162 if (namespace != null && namespace.equals(((Declaration) decllist.get(i)).getNamespace())) { 163 matches.add(decllist.get(i)); 164 } 165 } 166 } 167 } 168 if (decl.hasName(namespace, name) || (decl = decl.getSubstitutionElement(namespace, name)) != null) { 169 occurs[0] = occurs[1] = 1; 170 matches.add(decl); 171 } 172 } 173 174 public void visit(Wildcard wildcard) throws SchemaException { 175 if (wildcard.isAllowed(namespace)) { 176 occurs[0] = occurs[1] = 1; 177 matchesWildcard = true; 178 } 179 } 180 181 private QType getQTypeFromDeclaration(Declaration decl, byte occurrence) throws XQueryException { 183 QName qname = new QName(decl.getNamespace(), null, decl.getName(), null, null); 184 if (decl instanceof AttributeDeclaration) { 185 return new QTypeAttribute(qname, (AttributeDeclaration)decl, (SimpleType) decl.getType()); 186 } else if (decl instanceof ElementDeclaration) { 187 return new QTypeElement(qname, decl.getType(), occurrence); 188 } 189 return null; 190 } 191 192 } 193 | Popular Tags |