1 22 23 package org.xquark.schema.validation; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import org.xquark.schema.*; 29 30 public abstract class ContentIterator implements SchemaConstants { 31 private static final String RCSRevision = "$Revision: 1.2 $"; 32 private static final String RCSName = "$Name: $"; 33 34 public static final int MATCHED = 0; 35 public static final int INVALID = 1; 36 public static final int DONE = 2; 37 public static final int TOO_MANY = 3; 38 public static final int UNKNOWN = 4; 39 40 protected boolean endRequired = false; 41 protected boolean done = false; 42 protected Particle parentParticle = null; 43 protected int minOccurs = 1; 44 protected int maxOccurs = 1; 45 protected ContentReporter reporter = null; 46 protected ArrayList exceptions = null; 47 protected Type currentType = null; 48 49 public ContentIterator() {} 50 51 public ContentIterator(int minOccurs, int maxOccurs) { 52 this.minOccurs = minOccurs; 53 this.maxOccurs = maxOccurs; 54 } 55 56 public ContentIterator(Particle particle) { 57 this(particle.getMinOccurs(), particle.getMaxOccurs()); 58 parentParticle = particle; 59 } 60 61 public boolean isValid() { 62 return exceptions == null; 63 } 64 65 public ArrayList getExceptions() { 66 return exceptions; 67 } 68 69 void resetExceptions() { 70 exceptions = null; 71 } 72 73 public ContentReporter getContentReporter() { 74 return reporter; 75 } 76 77 public void setContentReporter(ContentReporter reporter) { 78 this.reporter = reporter; 79 } 80 81 public void startElement(String namespace, String localName, Type localType) throws SchemaException { 82 startElement(namespace, localName, localType, localType != null); 83 } 84 85 public void startElement(String namespace, String localName, Type localType, boolean localTypePresent) 86 throws SchemaException { 87 if (done) 88 illegalState("Iterator terminated for element {" + namespace + "}" + localName); 89 else if (endRequired) 90 illegalState("Missing endElement statement before element {" + namespace + "}" + localName); 91 92 switch (nextElement(namespace, localName)) { 93 case MATCHED : 94 endRequired = true; 95 done = false; 96 currentType = checkDeclarationAndType(localType, localTypePresent); 97 break; 98 case DONE : 99 case INVALID : 100 case TOO_MANY : 101 endRequired = false; 102 done = true; 103 break; 104 } 105 if (!isValid()) 106 throw new SchemaException(exceptions); 107 } 108 109 public final void startElement(String namespace, String localName) throws SchemaException { 110 startElement(namespace, localName, null, false); 111 } 112 113 public final void startElement(org.w3c.dom.Element node, Type localType) throws SchemaException { 114 startElement(node.getNamespaceURI(), node.getLocalName(), localType, true); 115 } 116 117 public final void startElement(org.w3c.dom.Element node) throws SchemaException { 118 startElement(node.getNamespaceURI(), node.getLocalName(), null, false); 119 } 120 121 public void endElement(String namespace, String localName) throws SchemaException { 122 if (done) 123 illegalState("Iterator terminated for element {" + namespace + "}" + localName); 124 else if (!endRequired) 125 illegalState("Unexpected endElement statement for element {" + namespace + "}" + localName); 126 endRequired = false; 127 done = false; 128 } 129 130 public final void endElement(org.w3c.dom.Element node) throws SchemaException { 131 endElement(node.getNamespaceURI(), node.getLocalName()); 132 } 133 134 public final List nextValidElements() { 135 if (endRequired || done) 136 return null; 137 ArrayList result = new ArrayList (); 138 boolean canStop = nextValidElements(result); 139 if (canStop) 140 result.add(null); 141 return result; 142 } 143 144 public final boolean canStopProcessing() { 145 if (endRequired || done) 146 return true; 147 return nextValidElements(null); 148 } 149 150 protected final Type checkDeclarationAndType(Type localType, boolean localTypePresent) throws SchemaException { 151 ElementDeclaration matchedDecl = getMatchedDeclaration(); 152 ElementDeclaration origDecl = getModelDeclaration(); 153 Type result = null; 154 if (matchedDecl == null) { 155 result = localType; 156 } else { 157 Type declType = matchedDecl.getType(); 158 if (matchedDecl.isAbstract()) 159 invalidElement("cvc-elt.2", matchedDecl); 160 if (localType == null) { 161 if (localTypePresent) 162 result = null; 163 else 164 result = declType; 165 } else { 166 int exclusions = declType.getBlock() | matchedDecl.getBlock(); 167 if (origDecl != null) 168 exclusions |= origDecl.getBlock(); 169 String errCode = localType.checkTypeDerivationOK(declType, exclusions, false); 170 if (errCode != null) 171 invalidElement("cvc-elt.4.3", matchedDecl, new SchemaException(errCode, localType)); 172 result = localType; 173 } 174 } 175 if (result != null && result.isAbstract()) { 176 invalidElement("cvc-complex-type.1", result); 177 } 178 return result; 179 } 180 181 protected final int invalidElement(SchemaException se) { 182 if (exceptions == null) 183 exceptions = new ArrayList (); 184 exceptions.add(se); 185 return INVALID; 186 } 187 188 protected final int invalidElement(List exList) { 189 if (exceptions == null) 190 exceptions = new ArrayList (); 191 exceptions.addAll(exList); 192 return INVALID; 193 } 194 195 protected final int invalidElement(String errCode, String errMsg) { 196 return invalidElement(new SchemaException(errCode, errMsg)); 197 } 198 199 protected final int invalidElement(String errCode, Object obj) { 200 return invalidElement(new SchemaException(errCode, obj)); 201 } 202 203 protected final int invalidElement(String errCode, Object obj, SchemaException se) { 204 return invalidElement(new SchemaException(errCode, obj, se)); 205 } 206 207 protected final int invalidElement(String errCode, Object obj, List exList) { 208 return invalidElement(new SchemaException(errCode, obj, exList)); 209 } 210 211 protected final void illegalState(String message) { 212 throw new IllegalStateException (message); 213 } 214 215 public int getMinOccurs() { 216 return minOccurs; 217 } 218 219 public int getMaxOccurs() { 220 return maxOccurs; 221 } 222 223 public Type getType() { 224 return currentType; 225 } 226 227 public Particle getParticle() { 228 return parentParticle; 229 } 230 231 void setType(Type type) { 232 currentType = type; 233 } 234 235 public abstract ElementDeclaration getMatchedDeclaration(); 236 237 public ElementDeclaration getModelDeclaration() { 238 return null; 239 } 240 241 public abstract int getProcessContents(); 242 243 protected abstract int nextElement(String namespace, String localName); 244 245 public abstract int stopProcessing(); 246 247 protected abstract boolean nextValidElements(List elements); 248 249 } 250 | Popular Tags |