1 22 package org.jboss.xb.binding.sunday.unmarshalling; 23 24 import java.util.ArrayList ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.HashSet ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 import javax.xml.namespace.QName ; 32 33 import org.jboss.logging.Logger; 34 import org.jboss.xb.binding.JBossXBRuntimeException; 35 import org.xml.sax.Attributes ; 36 37 41 public abstract class ModelGroupBinding 42 extends TermBinding 43 implements Cloneable 44 { 45 protected final Logger log = Logger.getLogger(getClass()); 46 47 protected boolean requiredParticle; 48 protected ParticleHandler handler = DefaultHandlers.ELEMENT_HANDLER; 49 50 protected ModelGroupBinding(SchemaBinding schema) 51 { 52 super(schema); 53 } 54 55 public ParticleHandler getHandler() 56 { 57 return handler; 58 } 59 60 public void setHandler(ParticleHandler handler) 61 { 62 this.handler = handler; 63 } 64 65 public abstract ElementBinding getArrayItem(); 66 67 72 public void addParticle(ParticleBinding particle) 73 { 74 if(particle.isRequired()) 75 { 76 requiredParticle = true; 77 } 78 } 79 80 public abstract Collection getParticles(); 81 82 public boolean hasRequiredParticle() 83 { 84 return requiredParticle; 85 } 86 87 93 public boolean mayStartWith(QName qName) 94 { 95 return mayStartWith(qName, Collections.EMPTY_SET); 96 } 97 98 public abstract Cursor newCursor(ParticleBinding particle); 99 100 public Object clone() throws CloneNotSupportedException 101 { 102 return super.clone(); 103 } 104 105 107 protected abstract boolean mayStartWith(QName qName, Set set); 108 109 public boolean isSkip() 110 { 111 return skip == null ? true : skip.booleanValue(); 112 } 113 114 public boolean isModelGroup() 115 { 116 return true; 117 } 118 119 public boolean isWildcard() 120 { 121 return false; 122 } 123 124 public boolean isElement() 125 { 126 return false; 127 } 128 129 131 public abstract class Cursor 132 { 133 protected final ParticleBinding particle; 134 protected final boolean trace = log.isTraceEnabled(); 135 136 protected Cursor(ParticleBinding particle) 137 { 138 this.particle = particle; 139 } 140 141 public ParticleBinding getParticle() 142 { 143 return particle; 144 } 145 146 public ModelGroupBinding getModelGroup() 147 { 148 return (ModelGroupBinding)particle.getTerm(); 149 } 150 151 public abstract ParticleBinding getCurrentParticle(); 152 153 public abstract ElementBinding getElement(); 154 155 public abstract boolean isPositioned(); 156 157 public List startElement(QName qName, Attributes attrs) 158 { 159 return startElement(qName, attrs, Collections.EMPTY_SET, Collections.EMPTY_LIST, true); 160 } 161 162 public ElementBinding getElement(QName qName, Attributes attrs, boolean ignoreWildcards) 163 { 164 return getElement(qName, attrs, Collections.EMPTY_SET, ignoreWildcards); 165 } 166 167 public abstract void endElement(QName qName); 168 169 public abstract int getOccurence(); 170 171 173 protected abstract List startElement(QName qName, 174 Attributes atts, 175 Set passedGroups, 176 List groupStack, 177 boolean required); 178 179 protected abstract ElementBinding getElement(QName qName, Attributes atts, Set passedGroups, boolean ignoreWildcards); 180 181 protected ElementBinding getElement(List group, QName qName, Attributes atts, Set passedGroups, boolean ignoreWildcards) 182 { 183 ElementBinding element = null; 184 for(int i = 0; i < group.size(); ++i) 185 { 186 ParticleBinding nextParticle = (ParticleBinding)group.get(i); 187 TermBinding item = nextParticle.getTerm(); 188 if(item.isElement()) 189 { 190 ElementBinding choice = (ElementBinding)item; 191 if(qName.equals(choice.getQName())) 192 { 193 element = choice; 194 break; 195 } 196 } 197 else if(item.isModelGroup()) 198 { 199 ModelGroupBinding modelGroup = (ModelGroupBinding)item; 200 if(!passedGroups.contains(modelGroup)) 201 { 202 switch(passedGroups.size()) 203 { 204 case 0: 205 passedGroups = Collections.singleton(this); 206 break; 207 case 1: 208 passedGroups = new HashSet (passedGroups); 209 default: 210 passedGroups.add(this); 211 } 212 213 ElementBinding e = modelGroup.newCursor(nextParticle).getElement(qName, atts, passedGroups, ignoreWildcards); 214 if(e != null) 215 { 216 element = e; 217 if(!qName.equals(e.getQName())) 218 { 219 throw new JBossXBRuntimeException( 220 "There is a bug in ModelGroupBinding.Cursor.getElement(QName,Attributes) impl" 221 ); 222 } 223 break; 224 } 225 } 226 } 227 else if(!ignoreWildcards) 228 { 229 WildcardBinding wildcard = (WildcardBinding)item; 230 ElementBinding e = wildcard.getElement(qName, atts); 231 if(e != null) 232 { 233 element = e; 234 if(!qName.equals(e.getQName())) 235 { 236 throw new JBossXBRuntimeException( 237 "There is a bug in ModelGroupBinding.Cursor.getElement(QName,Attributes) impl" 238 ); 239 } 240 break; 241 } 242 } 243 } 244 return element; 245 } 246 247 protected List addItem(List list, Object o) 248 { 249 switch(list.size()) 250 { 251 case 0: 252 list = Collections.singletonList(o); 253 break; 254 case 1: 255 list = new ArrayList (list); 256 default: 257 list.add(o); 258 } 259 return list; 260 } 261 } 262 } 263 | Popular Tags |