1 22 23 package org.xquark.schema.validation; 24 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 29 import org.xquark.schema.*; 30 31 public class ChoiceContentIterator extends ContentIterator { 32 private static final String RCSRevision = "$Revision: 1.1 $"; 33 private static final String RCSName = "$Name: $"; 34 35 private ChoiceModelGroup group; 36 private int count = 0; 37 private ContentIterator iterator = null; 38 39 public ChoiceContentIterator(Particle particle) 40 { 41 super(particle); 42 group = (ChoiceModelGroup)particle.getTerm(); 43 } 44 45 public ElementDeclaration getMatchedDeclaration() { 46 if (iterator != null) 47 return iterator.getMatchedDeclaration(); 48 else return null; 49 } 50 51 public ElementDeclaration getModelDeclaration() { 52 if (iterator != null) 53 return iterator.getModelDeclaration(); 54 else return null; 55 } 56 57 public int getProcessContents() { 58 if (iterator != null) 59 return iterator.getProcessContents(); 60 else return LAX; 61 } 62 63 private ContentIterator selectContentIterator(String namespace, String localName) 64 { 65 Iterator it = group.iterator(); 66 ContentIterator result = null; 67 68 if ( reporter != null && count < maxOccurs && parentParticle.hasNextElement(namespace, localName) ) 69 reporter.startGroup(parentParticle, ModelGroup.CHOICE, count); 70 71 while (it.hasNext()) { 72 Particle particle = (Particle)it.next(); 73 if (particle.hasNextElement(namespace, localName)) { 74 result = ContentIteratorFactory.createIterator(particle, reporter); 75 } 76 else if (reporter != null && parentParticle.hasNextElement(namespace, localName) ) { 80 reporter.alternates(particle); 81 } 82 } 83 return result; 84 } 85 86 protected int nextElement(String namespace, String localName) 87 { 88 ArrayList previousExceptions = null; 90 if (iterator != null) { 91 switch (iterator.nextElement(namespace, localName)) { 92 case MATCHED: 93 return MATCHED; 94 case UNKNOWN: 95 return UNKNOWN; 96 case INVALID: 97 if (iterator.getMatchedDeclaration() != null) 98 return invalidElement(iterator.getExceptions()); 99 else 100 return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions()); 101 case TOO_MANY: 102 previousExceptions = iterator.getExceptions(); 103 case DONE: 105 if (reporter != null) 106 reporter.endGroup(parentParticle, ModelGroup.CHOICE); 107 count++; 108 break; 109 } 111 } 112 113 iterator = selectContentIterator(namespace, localName); 114 if (iterator == null) { 115 if (previousExceptions != null) { 116 return invalidElement("cvc-particle.3.3", parentParticle, previousExceptions); 119 } else if (count == 0 && parentParticle.getMinExtent() > 0) { 120 SchemaException se = SchemaException.computeMinExtentException(parentParticle); 121 return invalidElement(se); 122 } else { 123 return stopProcessing(); 125 } 126 } 127 if (count >= maxOccurs) { 128 if (previousExceptions != null) 129 invalidElement("cvc-particle.3.2", parentParticle, previousExceptions); 130 else 131 invalidElement("cvc-particle.3.2", parentParticle); 132 return TOO_MANY; 133 } 134 switch (iterator.nextElement(namespace, localName)) { 135 case MATCHED: 136 return MATCHED; 137 case UNKNOWN: 138 return UNKNOWN; 139 case INVALID: 140 case TOO_MANY: 141 case DONE: 142 default: 143 if (iterator.getMatchedDeclaration() != null) 144 return invalidElement(iterator.getExceptions()); 145 else 146 return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions()); 147 } 148 } 149 150 public int stopProcessing() { 151 int c; 152 if (iterator != null) { 154 switch (iterator.stopProcessing()) { 155 case INVALID: 156 return invalidElement("cvc-particle.3.3", parentParticle, iterator.getExceptions()); 157 case DONE: 158 } 160 if (reporter != null) 161 reporter.endGroup(parentParticle, ModelGroup.CHOICE); 162 c = count+1; 163 } else { 164 c = count; 165 } 166 if (c < minOccurs && parentParticle.getMinExtent() != 0) { 167 return invalidElement("cvc-particle.3.1", parentParticle); 168 } else if (c < maxOccurs && reporter != null) { 169 reporter.skipped(parentParticle); 170 } 171 172 return DONE; 173 } 174 175 protected boolean nextValidElements(List elements) { 176 int c; 177 if (iterator != null) { 179 boolean canStop = iterator.nextValidElements(elements); 180 if (!canStop) return false; 183 c = count+1; 184 } else { 185 c = count; 186 } 187 if (c < maxOccurs && elements != null) { 189 Iterator it = group.iterator(); 190 while (it.hasNext()) { 191 Particle particle = (Particle)it.next(); 192 elements.addAll(particle.firstValidElements()); 193 } 194 } 195 if (c >= minOccurs || parentParticle.getMinExtent() == 0) return true; 198 else return false; 199 } 200 201 } 202 203 204 | Popular Tags |