1 22 package org.jboss.xb.binding.sunday.unmarshalling; 23 24 import java.util.Set ; 25 import java.util.Map ; 26 import java.util.Collections ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Collection ; 30 import javax.xml.namespace.QName ; 31 import org.jboss.xb.binding.JBossXBRuntimeException; 32 import org.jboss.logging.Logger; 33 import org.xml.sax.Attributes ; 34 35 36 40 public class AllBinding 41 extends ModelGroupBinding 42 { 43 private static final Logger log = Logger.getLogger(AllBinding.class); 44 45 private Map elements = Collections.EMPTY_MAP; 46 47 public AllBinding(SchemaBinding schema) 48 { 49 super(schema); 50 } 51 52 public ElementBinding getArrayItem() 53 { 54 return null; 55 } 56 57 public void addParticle(ParticleBinding particle) 58 { 59 if(!particle.getTerm().isElement()) 60 { 61 throw new JBossXBRuntimeException("Model group all may contain only elements!"); 62 } 63 64 ElementBinding element = (ElementBinding)particle.getTerm(); 65 switch(elements.size()) 66 { 67 case 0: 68 elements = Collections.singletonMap(element.getQName(), particle); 69 break; 70 case 1: 71 elements = new HashMap (elements); 72 default: 73 elements.put(element.getQName(), particle); 74 } 75 super.addParticle(particle); 76 } 77 78 public Collection getParticles() 79 { 80 return Collections.unmodifiableCollection(elements.values()); 81 } 82 83 public Cursor newCursor(ParticleBinding particle) 84 { 85 return new Cursor(particle) 86 { 87 private ParticleBinding curParticle; 88 private int occurence; 89 90 public ParticleBinding getCurrentParticle() 91 { 92 if(curParticle == null) 93 { 94 throw new JBossXBRuntimeException("The cursor in all group has not been positioned yet!"); 95 } 96 return curParticle; 97 } 98 99 public ElementBinding getElement() 100 { 101 return (ElementBinding)getCurrentParticle().getTerm(); 102 } 103 104 public boolean isPositioned() 105 { 106 return curParticle != null; 107 } 108 109 public void endElement(QName qName) 110 { 111 if(curParticle == null || !getElement().getQName().equals(qName)) 112 { 113 throw new JBossXBRuntimeException("Failed to process endElement for " + qName + 114 " since the current element is " + (curParticle == null ? null : getElement().getQName()) 115 ); 116 } 117 } 118 119 public int getOccurence() 120 { 121 return occurence; 122 } 123 124 protected List startElement(QName qName, Attributes atts, Set passedGroups, List groupStack, boolean required) 125 { 126 ParticleBinding particle = (ParticleBinding)elements.get(qName); 127 if(particle != null) 128 { 129 if(curParticle == particle) 130 { 131 ++occurence; 132 } 133 else 134 { 135 curParticle = particle; 136 occurence = 1; 137 } 138 groupStack = addItem(groupStack, this); 139 } 140 else 141 { 142 log.warn("Element " + qName + " not found in " + elements.keySet()); 143 } 144 return groupStack; 145 } 146 147 protected ElementBinding getElement(QName qName, Attributes atts, Set passedGroups, boolean ignoreWildcards) 148 { 149 ParticleBinding particle = (ParticleBinding)elements.get(qName); 150 return particle == null ? null : (ElementBinding)particle.getTerm(); 151 } 152 }; 153 } 154 155 protected boolean mayStartWith(QName qName, Set set) 156 { 157 return elements.containsKey(qName); 158 } 159 } 160 | Popular Tags |