1 17 package org.apache.ws.jaxme.generator.sg.impl; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.xml.parsers.ParserConfigurationException ; 25 26 import org.apache.ws.jaxme.generator.Generator; 27 import org.apache.ws.jaxme.generator.sg.Context; 28 import org.apache.ws.jaxme.generator.sg.Facet; 29 import org.apache.ws.jaxme.generator.sg.GroupSG; 30 import org.apache.ws.jaxme.generator.sg.ObjectSG; 31 import org.apache.ws.jaxme.generator.sg.ObjectSGChain; 32 import org.apache.ws.jaxme.generator.sg.SGFactory; 33 import org.apache.ws.jaxme.generator.sg.SGFactoryChain; 34 import org.apache.ws.jaxme.generator.sg.SchemaSG; 35 import org.apache.ws.jaxme.generator.sg.SchemaSGChain; 36 import org.apache.ws.jaxme.generator.sg.TypeSG; 37 import org.apache.ws.jaxme.generator.sg.TypeSGChain; 38 import org.apache.ws.jaxme.logging.Logger; 39 import org.apache.ws.jaxme.logging.LoggerAccess; 40 import org.apache.ws.jaxme.xs.XSAny; 41 import org.apache.ws.jaxme.xs.XSElement; 42 import org.apache.ws.jaxme.xs.XSEnumeration; 43 import org.apache.ws.jaxme.xs.XSGroup; 44 import org.apache.ws.jaxme.xs.XSObjectFactory; 45 import org.apache.ws.jaxme.xs.XSParser; 46 import org.apache.ws.jaxme.xs.XSSchema; 47 import org.apache.ws.jaxme.xs.XSType; 48 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser; 49 import org.apache.ws.jaxme.xs.jaxb.impl.JAXBXsObjectFactoryImpl; 50 import org.apache.ws.jaxme.xs.parser.XSContext; 51 import org.apache.ws.jaxme.xs.xml.XsObjectFactory; 52 import org.apache.ws.jaxme.xs.xml.XsQName; 53 import org.xml.sax.EntityResolver ; 54 import org.xml.sax.SAXException ; 55 import org.xml.sax.XMLReader ; 56 57 58 59 62 public class JAXBSGFactory implements SGFactoryChain { 63 private final static Logger log = LoggerAccess.getLogger(JAXBSGFactory.class); 64 private final Generator generator; 65 private SchemaSG schemaSG; 66 private Map groups = new HashMap (); 67 private Map objects = new HashMap (); 68 private Map types = new HashMap (); 69 private List groupsByOrder = new ArrayList (); 70 private List objectsByOrder = new ArrayList (); 71 private List typesByOrder = new ArrayList (); 72 73 75 public JAXBSGFactory(Generator pGenerator) { 76 generator = pGenerator; 77 } 78 79 public void init(SGFactory pController) { 80 } 81 82 public Generator getGenerator(SGFactory pController) { 83 return generator; 84 } 85 86 public GroupSG getGroupSG(SGFactory pController, XSGroup pGroup) throws SAXException { 87 GroupSG result = (GroupSG) groups.get(pGroup); 88 if (result == null) { 89 result = pController.newGroupSG(pGroup); 90 groups.put(pGroup, result); 91 groupsByOrder.add(result); 92 result.init(); 93 } 94 return result; 95 } 96 97 public GroupSG newGroupSG(SGFactory pController, XSGroup pGroup) throws SAXException { 98 if (schemaSG == null) { 99 throw new IllegalStateException ("A schema has not yet been created"); 100 } 101 return new GroupSGImpl(new JAXBGroupSG(pController, schemaSG, pGroup)); 102 } 103 104 public GroupSG getGroupSG(SGFactory pController, XSGroup pGroup, Context pClassContext) throws SAXException { 105 GroupSG result = pController.newGroupSG(pGroup, pClassContext); 106 result.init(); 107 groupsByOrder.add(result); 108 return result; 109 } 110 111 public GroupSG newGroupSG(SGFactory pController, XSGroup pGroup, Context pClassContext) throws SAXException { 112 if (schemaSG == null) { 113 throw new IllegalStateException ("A schema has not yet been created"); 114 } 115 return new GroupSGImpl(new JAXBGroupSG(pController, schemaSG, pGroup, pClassContext)); 116 } 117 118 119 public Object newObjectSG(SGFactory pController, XSElement pElement) throws SAXException { 120 if (schemaSG == null) { 121 throw new IllegalStateException ("A schema has not yet been created."); 122 } 123 return new JAXBObjectSG(pController, schemaSG, pElement); 124 } 125 126 public ObjectSG getObjectSG(SGFactory pController, XSElement pElement) throws SAXException { 127 ObjectSG result = (ObjectSG) objects.get(pElement); 128 if (result == null) { 129 ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pElement); 130 result = new ObjectSGImpl(chain); 131 objects.put(pElement, result); 132 objectsByOrder.add(result); 133 result.init(); 134 } 135 return result; 136 } 137 138 public Object newObjectSG(SGFactory pController, XSAny pAny) { 139 if (schemaSG == null) { 140 throw new IllegalStateException ("A schema has not yet been created."); 141 } 142 return new JAXBObjectSG(pController, schemaSG, pAny); 143 } 144 145 public ObjectSG getObjectSG(SGFactory pController, XSAny pAny, Context pContext) throws SAXException { 146 ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pAny); 147 ObjectSG result = new ObjectSGImpl(chain); 148 result.init(); 149 return result; 150 } 151 152 public Object newObjectSG(SGFactory pController, XSElement pElement, Context pContext) throws SAXException { 153 if (schemaSG == null) { 154 throw new IllegalStateException ("A schema has not yet been created."); 155 } 156 return new JAXBObjectSG(pController, schemaSG, pElement, pContext); 157 } 158 159 public ObjectSG getObjectSG(SGFactory pController, XSElement pElement, Context pContext) throws SAXException { 160 ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pElement, pContext); 161 ObjectSG result = new ObjectSGImpl(chain); 162 result.init(); 163 return result; 164 } 165 166 public SchemaSG getSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException { 167 if (schemaSG == null) { 168 SchemaSGChain chain = (SchemaSGChain) pController.newSchemaSG(pSchema); 169 schemaSG = new SchemaSGImpl(chain); 170 schemaSG.init(); 171 } 172 return schemaSG; 173 } 174 175 public SchemaSG getSchemaSG(SGFactory pController) { 176 if (schemaSG == null) { 177 throw new IllegalStateException ("A factory has not yet been created."); 178 } 179 return schemaSG; 180 } 181 182 public Object newSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException { 183 return new JAXBSchemaSG(pController, pSchema); 184 } 185 186 public TypeSG getTypeSG(SGFactory pController, XSType pType) throws SAXException { 187 final String mName = "getTypeSG(XSType)"; 188 TypeSG result = (TypeSG) types.get(pType); 189 if (result == null) { 190 log.finest(mName, "->", pType.getName()); 191 TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType); 192 result = new TypeSGImpl(chain); 193 types.put(pType, result); 194 typesByOrder.add(result); 195 result.init(); 196 log.finest(mName, "<-", new Object []{chain, result}); 197 } 198 return result; 199 } 200 201 public Object newTypeSG(SGFactory pController, XSType pType) throws SAXException { 202 if (schemaSG == null) { 203 throw new IllegalStateException ("A schema has not yet been created"); 204 } 205 return new JAXBTypeSG(pController, schemaSG, pType); 206 } 207 208 public TypeSG getTypeSG(SGFactory pController, XSType pType, Context pClassContext, XsQName pName) throws SAXException { 209 final String mName = "getTypeSG(XSType,ClassContext)"; 210 log.finest(mName, "->", new Object []{pType, pClassContext}); 211 TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType, pClassContext, pName); 212 TypeSG result = new TypeSGImpl(chain); 213 typesByOrder.add(result); 214 result.init(); 215 log.finest(mName, "<-", new Object []{chain, result}); 216 return result; 217 } 218 219 public Object newTypeSG(SGFactory pController, XSType pType, Context pClassContext, XsQName pName) throws SAXException { 220 if (schemaSG == null) { 221 throw new IllegalStateException ("A schema has not yet been created"); 222 } 223 return new JAXBTypeSG(pController, schemaSG, pType, pClassContext, pName); 224 } 225 226 public Object newTypeSG(SGFactory pController, XSType pType, XsQName pName) throws SAXException { 227 if (schemaSG == null) { 228 throw new IllegalStateException ("A schema has not yet been created"); 229 } 230 return new JAXBTypeSG(pController, schemaSG, pType, pName); 231 } 232 233 public TypeSG getTypeSG(SGFactory pController, XSType pType, XsQName pName) throws SAXException { 234 TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType, pName); 235 TypeSG result = new TypeSGImpl(chain); 236 typesByOrder.add(result); 237 result.init(); 238 return result; 239 } 240 241 public XSParser newXSParser(SGFactory pController) throws SAXException { 242 XSParser parser = new JAXBParser(); 243 XSContext context = parser.getContext(); 244 context.setXsObjectFactory(pController.newXsObjectFactory()); 245 context.setXSObjectFactory(pController.newXSObjectFactory()); 246 return parser; 247 } 248 249 public Facet newFacet(SGFactory pController, XSType pType, XSEnumeration[] pEnumerations) throws SAXException { 250 return new FacetImpl(pType, pEnumerations); 251 } 252 253 public TypeSG[] getTypes(SGFactory pController) { 254 return (TypeSG[]) typesByOrder.toArray(new TypeSG[typesByOrder.size()]); 255 } 256 257 public GroupSG[] getGroups(SGFactory pController) { 258 return (GroupSG[]) groupsByOrder.toArray(new GroupSG[groupsByOrder.size()]); 259 } 260 261 public ObjectSG[] getObjects(SGFactory pController) { 262 return (ObjectSG[]) objectsByOrder.toArray(new ObjectSG[objectsByOrder.size()]); 263 } 264 265 public XsObjectFactory newXsObjectFactory(SGFactory pController) throws SAXException { 266 return new JAXBXsObjectFactoryImpl(){ 267 public XMLReader newXMLReader(boolean pValidating) throws ParserConfigurationException , SAXException { 268 XMLReader xr = super.newXMLReader(pValidating); 269 EntityResolver entityResolver = generator.getEntityResolver(); 270 if (entityResolver != null) { 271 xr.setEntityResolver(entityResolver); 272 } 273 return xr; 274 } 275 }; 276 } 277 278 public XSObjectFactory newXSObjectFactory(SGFactory pController) throws SAXException { 279 return JAXBParser.JAXB_OBJECT_FACTORY; 280 } 281 } 282 | Popular Tags |