1 16 package org.apache.ws.jaxme.generator.sg.impl; 17 18 import org.apache.ws.jaxme.generator.sg.Context; 19 import org.apache.ws.jaxme.generator.sg.GroupSG; 20 import org.apache.ws.jaxme.generator.sg.ObjectSG; 21 import org.apache.ws.jaxme.generator.sg.ParticleSG; 22 import org.apache.ws.jaxme.generator.sg.ParticleSGChain; 23 import org.apache.ws.jaxme.generator.sg.PropertySG; 24 import org.apache.ws.jaxme.generator.sg.PropertySGChain; 25 import org.apache.ws.jaxme.generator.sg.SGFactory; 26 import org.apache.ws.jaxme.generator.sg.SGlet; 27 import org.apache.ws.jaxme.generator.sg.TypeSG; 28 import org.apache.ws.jaxme.js.DirectAccessible; 29 import org.apache.ws.jaxme.js.JavaMethod; 30 import org.apache.ws.jaxme.xs.XSAny; 31 import org.apache.ws.jaxme.xs.XSElement; 32 import org.apache.ws.jaxme.xs.XSParticle; 33 import org.xml.sax.Locator ; 34 import org.xml.sax.SAXException ; 35 36 37 40 public class JAXBParticleSG implements ParticleSGChain { 41 private final int minOccurs, maxOccurs; 42 private final XSParticle.Type type; 43 private PropertySG propertySG; 44 private final GroupSG groupSG; 45 private final ObjectSG objectSG; 46 private final Locator locator; 47 private XSElement element; 48 private XSAny wildcard; 49 50 52 public JAXBParticleSG(SGFactory pFactory, XSParticle pParticle, 53 Context pClassContext) throws SAXException { 54 minOccurs = pParticle.getMinOccurs(); 55 maxOccurs = pParticle.getMaxOccurs(); 56 type = pParticle.getType(); 57 if (pParticle.isGroup()) { 58 groupSG = pFactory.getGroupSG(pParticle.getGroup(), pClassContext); 59 objectSG = null; 60 } else if (pParticle.isElement()) { 61 element = pParticle.getElement(); 62 if (element.isGlobal()) { 63 objectSG = pFactory.getObjectSG(element); 64 } else { 65 objectSG = pFactory.getObjectSG(element, pClassContext); 66 } 67 groupSG = null; 68 } else if (pParticle.isWildcard()) { 69 objectSG = pFactory.getObjectSG(pParticle.getWildcard(), pClassContext); 70 groupSG = null; 71 wildcard = pParticle.getWildcard(); 72 } else { 73 throw new IllegalStateException ("Particle is neither group, nor element, or wildcard."); 74 } 75 locator = pParticle.getLocator(); 76 } 77 78 public Object newPropertySGChain(ParticleSG pController) throws SAXException { 79 PropertySGChain result; 80 if (element != null) { 81 result = new JAXBPropertySG(objectSG, element); 82 element = null; 83 } else if (wildcard != null) { 84 result = new AnyElementPropertySG(objectSG, wildcard); 85 wildcard = null; 86 } else { 87 throw new IllegalStateException ("A new PropertySGChain cannot be obtained."); 88 } 89 if (maxOccurs > 1 || maxOccurs == -1) { 90 PropertySG pSG = new PropertySGImpl(result); 93 if ("indexed".equals(pSG.getCollectionType())) { 94 result = new ArrayPropertySG(result, objectSG, minOccurs, maxOccurs); 95 } else { 96 result = new MultiplePropertySG(result, objectSG, minOccurs, maxOccurs); 97 } 98 } 99 return result; 100 } 101 102 public void init(ParticleSG pController) throws SAXException { 103 } 104 105 public Locator getLocator(ParticleSG pController) { return locator; } 106 public int getMinOccurs(ParticleSG pController) { return minOccurs; } 107 public int getMaxOccurs(ParticleSG pController) { return maxOccurs; } 108 public boolean isMultiple(ParticleSG pController) { return maxOccurs == -1 || maxOccurs > 1; } 109 public boolean isGroup(ParticleSG pController) { return type.equals(XSParticle.GROUP); } 110 public boolean isElement(ParticleSG pController) { return type.equals(XSParticle.ELEMENT); } 111 public boolean isWildcard(ParticleSG pController) { return type.equals(XSParticle.WILDCARD); } 112 113 public PropertySG getPropertySG(ParticleSG pController) throws SAXException { 114 if (propertySG == null) { 115 if (element != null || wildcard != null) { 116 PropertySGChain chain = (PropertySGChain) pController.newPropertySGChain(); 117 propertySG = new PropertySGImpl(chain); 118 propertySG.init(); 119 } else { 120 throw new IllegalStateException ("This particle has no PropertySG."); 121 } 122 } 123 return propertySG; 124 } 125 126 public ObjectSG getObjectSG(ParticleSG pController) { 127 if (objectSG == null) { 128 throw new IllegalStateException ("This particle is neither an element nor a wildcard."); 129 } 130 return objectSG; 131 } 132 133 public GroupSG getGroupSG(ParticleSG pController) { 134 if (groupSG == null) { 135 throw new IllegalStateException ("This particle is no group."); 136 } 137 return groupSG; 138 } 139 140 public void forAllNonNullValues(ParticleSG pController, JavaMethod pMethod, 141 DirectAccessible pElement, SGlet pSGlet) throws SAXException { 142 if (pController.isElement()) { 143 PropertySG pSG = pController.getPropertySG(); 144 boolean hasIsSetMethod = pSG.hasIsSetMethod(); 145 if (hasIsSetMethod) { 146 pMethod.addIf(pSG.getXMLIsSetMethodName(), "()"); 147 } 148 TypeSG typeSG = pController.getObjectSG().getTypeSG(); 149 Object v = pController.getPropertySG().getValue(pElement); 150 if (typeSG.isComplex()) { 151 pSGlet.generate(pMethod, v); 152 } else { 153 typeSG.getSimpleTypeSG().forAllValues(pMethod, v, pSGlet); 154 } 155 if (hasIsSetMethod) { 156 pMethod.addEndIf(); 157 } 158 } else { 159 throw new IllegalStateException ("Wildcards and subgroups are not yet supported."); 161 } 162 } 163 } 164 | Popular Tags |