1 16 package org.apache.ws.jaxme.generator.sg.impl; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.ws.jaxme.generator.sg.ComplexContentSG; 22 import org.apache.ws.jaxme.generator.sg.ComplexContentSGChain; 23 import org.apache.ws.jaxme.generator.sg.ComplexTypeSG; 24 import org.apache.ws.jaxme.generator.sg.Context; 25 import org.apache.ws.jaxme.generator.sg.GroupSG; 26 import org.apache.ws.jaxme.generator.sg.ObjectSG; 27 import org.apache.ws.jaxme.generator.sg.ParticleSG; 28 import org.apache.ws.jaxme.generator.sg.SGFactory; 29 import org.apache.ws.jaxme.logging.Logger; 30 import org.apache.ws.jaxme.logging.LoggerAccess; 31 import org.apache.ws.jaxme.xs.XSGroup; 32 import org.apache.ws.jaxme.xs.XSParticle; 33 import org.apache.ws.jaxme.xs.XSType; 34 import org.apache.ws.jaxme.xs.types.XSAnyType; 35 import org.xml.sax.Locator ; 36 import org.xml.sax.SAXException ; 37 import org.xml.sax.SAXParseException ; 38 39 40 43 public class JAXBComplexContentTypeSG implements ComplexContentSGChain { 44 private static final Logger log = LoggerAccess.getLogger(JAXBComplexContentTypeSG.class); 45 46 private static class Particle { 47 private final GroupSG[] stack; 48 private final ParticleSG particle; 49 Particle(GroupSG[] pStack, ParticleSG pParticle) { 50 stack = pStack; 51 particle = pParticle; 52 } 53 ParticleSG getParticleSG() { return particle; } 54 Object [] getStack() { return stack; } 55 GroupSG getClosestCommonAnchestor(Particle pParticle) { 56 int len = Math.min(stack.length, pParticle.stack.length); 57 GroupSG anchestor = stack[0]; 58 for (int i = 1; i < len; i++) { 59 if (stack[i] != pParticle.stack[i]) { 60 break; 61 } 62 } 63 return anchestor; 64 } 65 } 66 67 private final boolean isEmpty, isMixed; 68 private final ParticleSG rootParticle; 69 70 private boolean isInitialized; 71 private final Locator locator; 72 private final List elementParticles = new ArrayList (); 73 private Particle[] elementParticleArray; 74 private final List mixedContentParticles = new ArrayList (); 75 private final List groupParticlesWithMultiplicityGreaterOne = new ArrayList (); 76 private final List wildcardParticles = new ArrayList (); 77 private final List stack = new ArrayList (); 78 79 protected JAXBComplexContentTypeSG(ComplexTypeSG pComplexTypeSG, XSType pType) throws SAXException { 80 if (pType == XSAnyType.getInstance()) { 81 throw new SAXException ("The type xs:anyType is not supported."); 82 } 83 locator = pType.getLocator(); 84 if (pType.getComplexType().isEmpty()) { 85 isEmpty = true; 86 isMixed = false; 87 rootParticle = null; 88 } else { 89 XSParticle particle = pType.getComplexType().getParticle(); 90 if (particle == null) { 91 throw new NullPointerException ("Missing group particle for type = " + pType.getName()); 92 } 93 if (particle.isGroup()) { 94 rootParticle = newParticleSG(pComplexTypeSG.getTypeSG().getFactory(), particle, pComplexTypeSG.getClassContext()); 95 } else { 96 throw new IllegalStateException ("Expected internal group"); 97 } 98 isEmpty = false; 99 isMixed = pType.getComplexType().isMixed(); 100 } 101 } 102 103 protected ParticleSG newParticleSG(SGFactory pFactory, XSParticle pParticle, Context pContext) throws SAXException { 104 JAXBParticleSG chain = new JAXBParticleSG(pFactory, pParticle, pContext); 105 ParticleSGImpl result = new ParticleSGImpl(chain); 106 result.init(); 107 return result; 108 } 109 110 public Locator getLocator(ComplexContentSG pController) { return locator; } 111 112 public void init(ComplexContentSG pController) throws SAXException { 113 } 114 115 private void initialize(ComplexContentSG pController) throws SAXException { 116 if (!isInitialized && !isEmpty) { 117 isInitialized = true; 118 verify(pController); 119 } 120 } 121 122 private Particle[] getLocalElementParticles(ComplexContentSG pController) 123 throws SAXException { 124 initialize(pController); 125 return elementParticleArray; 126 } 127 128 public ParticleSG[] getElementParticles(ComplexContentSG pController) 129 throws SAXException { 130 Particle[] particles = getLocalElementParticles(pController); 131 if (particles == null) { 132 return new ParticleSG[0]; 133 } 134 ParticleSG[] result = new ParticleSG[particles.length]; 135 for (int i = 0; i < result.length; i++) { 136 result[i] = particles[i].getParticleSG(); 137 } 138 return result; 139 } 140 141 public ParticleSG getRootParticle(ComplexContentSG pController) { 142 return rootParticle; 143 } 144 145 public boolean isEmpty(ComplexContentSG pController) { 146 return isEmpty; 147 } 148 149 public boolean isMixed(ComplexContentSG pController) { 150 return isMixed; 151 } 152 153 154 private void findParticles(GroupSG[] pStack, ParticleSG pParticle) throws SAXException { 155 if (pParticle.isGroup()) { 156 if (pParticle.isMultiple()) { 157 groupParticlesWithMultiplicityGreaterOne.add(new Particle(pStack, pParticle)); 158 } else if (isMixed) { 159 mixedContentParticles.add(new Particle(pStack, pParticle)); 160 } else { 161 findParticles(pParticle.getGroupSG()); 162 } 163 } else if (pParticle.isWildcard()) { 164 wildcardParticles.add(new Particle(pStack, pParticle)); 165 } else if (pParticle.isElement()) { 166 Particle p = new Particle(pStack, pParticle); 167 elementParticles.add(p); 168 } else { 169 throw new SAXParseException ("Invalid particle type", pParticle.getLocator()); 170 } 171 } 172 173 private void findParticles(GroupSG pGroup) throws SAXException { 174 stack.add(pGroup); 175 ParticleSG[] particles = pGroup.getParticles(); 176 GroupSG[] groups = (GroupSG[]) stack.toArray(new GroupSG[stack.size()]); 177 for (int i = 0; i < particles.length; i++) { 178 findParticles(groups, particles[i]); 179 } 180 stack.remove(stack.size()-1); 181 } 182 183 186 private void verify(ComplexContentSG pController) throws SAXException { 187 findParticles(new GroupSG[0], pController.getRootParticle()); 188 189 193 if (pController.isMixed()) { 194 } 196 201 if (groupParticlesWithMultiplicityGreaterOne.size() > 0) { 202 Particle particle = (Particle) groupParticlesWithMultiplicityGreaterOne.get(0); 203 throw new SAXParseException ("Model groups with maxOccurs > 1 are not yet supported.", 204 particle.getParticleSG().getLocator()); 205 } 206 214 if (wildcardParticles.size() > 0) { 215 Particle particle = (Particle) wildcardParticles.get(0); 216 throw new SAXParseException ("Wildcards are unsupported", 217 particle.getParticleSG().getLocator()); 218 } 219 236 elementParticleArray = (Particle[]) elementParticles.toArray(new Particle[elementParticles.size()]); 237 for (int i = 0; i < elementParticleArray.length; i++) { 238 Particle pParticle = elementParticleArray[i]; 239 ParticleSG p = pParticle.getParticleSG(); 240 String name = p.getPropertySG().getXMLFieldName(); 241 for (int j = i+1; j < elementParticles.size(); j++) { 242 Particle qParticle = elementParticleArray[j]; 243 ParticleSG q = qParticle.getParticleSG(); 244 if (name.equals(q.getPropertySG().getXMLFieldName())) { 245 ObjectSG pObject = p.getObjectSG(); 246 ObjectSG qObject = q.getObjectSG(); 247 if (!pObject.isGlobal() || !qObject.isGlobal() || 248 pObject != qObject) { 249 throw new SAXParseException ("Multiple element particles named " + name 250 + ", which aren't references to " 251 + " a common global element, are present in a common" 252 + " complex type. (JAXB 1.0, 5.9.7.4.a) Use jaxb:property/@name" 253 + " for customization.", pController.getLocator()); 254 } 255 GroupSG group = pParticle.getClosestCommonAnchestor(pParticle); 256 if (group.isSequence()) { 257 throw new SAXParseException ("Multiple element particles named " + name 258 + " are contained in a common sequence." 259 + " (JAXB 1.0, 5.9.7.4.b) Use jaxb:property/@name" 260 + " for customization.", pController.getLocator()); 261 } 262 } 263 } 264 } 265 } 266 } 267 | Popular Tags |