1 17 package org.apache.ws.jaxme.xs.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.ws.jaxme.xs.XSModelGroup; 24 import org.apache.ws.jaxme.xs.XSParticle; 25 import org.apache.ws.jaxme.xs.parser.impl.LocSAXException; 26 import org.xml.sax.Locator ; 27 import org.xml.sax.SAXException ; 28 29 30 33 public class XSModelGroupImpl implements XSModelGroup { 34 private final XSModelGroup.Compositor compositor; 35 private final List particles = new ArrayList (); 36 private final Locator locator; 37 38 public XSModelGroupImpl(XSModelGroup.Compositor pCompositor, Locator pLocator) { 39 if (pCompositor == null) { 40 throw new NullPointerException ("The model group compositor must not be null."); 41 } 42 compositor = pCompositor; 43 locator = pLocator; 44 } 45 46 public Compositor getCompositor() { 47 return compositor; 48 } 49 50 public boolean isSequence() { 51 return XSModelGroup.SEQUENCE.equals(compositor); 52 } 53 54 public boolean isChoice() { 55 return XSModelGroup.CHOICE.equals(compositor); 56 } 57 58 public boolean isAll() { 59 return XSModelGroup.ALL.equals(compositor); 60 } 61 62 public void addParticle(XSParticle pParticle) throws SAXException { 63 if (isAll()) { 64 if (pParticle.getMaxOccurs() == -1 || pParticle.getMaxOccurs() > 1) { 65 throw new LocSAXException("Illegal 'maxOccurs' value inside 'all' group: " + pParticle.getMaxOccurs(), 66 pParticle.getLocator()); 67 } 68 } 69 if (pParticle.getMaxOccurs() != -1 && pParticle.getMaxOccurs() < pParticle.getMinOccurs()) { 70 throw new LocSAXException("Illegal 'maxOccurs' value, which is lower than 'minOccurs' value: " + 71 pParticle.getMaxOccurs() + " < " + pParticle.getMinOccurs(), 72 pParticle.getLocator()); 73 } 74 particles.add(pParticle); 75 } 76 77 public XSParticle[] getParticles() { 78 return (XSParticle[]) particles.toArray(new XSParticle[particles.size()]); 79 } 80 81 public void validate() throws SAXException { 82 if (isChoice() && particles.size() == 0) { 83 throw new LocSAXException("A 'choice' model group must have at least one 'group', 'any', or 'element'.", 84 getLocator()); 85 } 86 for (Iterator iter = particles.iterator(); iter.hasNext(); ) { 87 XSParticle particle = (XSParticle) iter.next(); 88 if (particle.isElement()) { 89 particle.getElement().validate(); 90 } else if (particle.isGroup()) { 91 particle.getGroup().validate(); 92 } else if (particle.isWildcard()) { 93 particle.getWildcard().validate(); 94 } else { 95 throw new IllegalStateException ("Invalid particle"); 96 } 97 } 98 } 99 100 public Locator getLocator() { 101 return locator; 102 } 103 } 104 | Popular Tags |