/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.generator.sg.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.ws.jaxme.generator.Generator;
import org.apache.ws.jaxme.generator.sg.Context;
import org.apache.ws.jaxme.generator.sg.Facet;
import org.apache.ws.jaxme.generator.sg.GroupSG;
import org.apache.ws.jaxme.generator.sg.ObjectSG;
import org.apache.ws.jaxme.generator.sg.ObjectSGChain;
import org.apache.ws.jaxme.generator.sg.SGFactory;
import org.apache.ws.jaxme.generator.sg.SGFactoryChain;
import org.apache.ws.jaxme.generator.sg.SchemaSG;
import org.apache.ws.jaxme.generator.sg.SchemaSGChain;
import org.apache.ws.jaxme.generator.sg.TypeSG;
import org.apache.ws.jaxme.generator.sg.TypeSGChain;
import org.apache.ws.jaxme.logging.Logger;
import org.apache.ws.jaxme.logging.LoggerAccess;
import org.apache.ws.jaxme.xs.XSAny;
import org.apache.ws.jaxme.xs.XSElement;
import org.apache.ws.jaxme.xs.XSEnumeration;
import org.apache.ws.jaxme.xs.XSGroup;
import org.apache.ws.jaxme.xs.XSObjectFactory;
import org.apache.ws.jaxme.xs.XSParser;
import org.apache.ws.jaxme.xs.XSSchema;
import org.apache.ws.jaxme.xs.XSType;
import org.apache.ws.jaxme.xs.jaxb.impl.JAXBParser;
import org.apache.ws.jaxme.xs.jaxb.impl.JAXBXsObjectFactoryImpl;
import org.apache.ws.jaxme.xs.parser.XSContext;
import org.apache.ws.jaxme.xs.xml.XsObjectFactory;
import org.apache.ws.jaxme.xs.xml.XsQName;
import org.xml.sax.EntityResolver;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class JAXBSGFactory implements SGFactoryChain {
private final static Logger log = LoggerAccess.getLogger(JAXBSGFactory.class);
private final Generator generator;
private SchemaSG schemaSG;
private Map groups = new HashMap();
private Map objects = new HashMap();
private Map types = new HashMap();
private List groupsByOrder = new ArrayList();
private List objectsByOrder = new ArrayList();
private List typesByOrder = new ArrayList();
public JAXBSGFactory(Generator pGenerator) {
generator = pGenerator;
}
public void init(SGFactory pController) {
}
public Generator getGenerator(SGFactory pController) {
return generator;
}
public GroupSG getGroupSG(SGFactory pController, XSGroup pGroup) throws SAXException {
GroupSG result = (GroupSG) groups.get(pGroup);
if (result == null) {
result = pController.newGroupSG(pGroup);
groups.put(pGroup, result);
groupsByOrder.add(result);
result.init();
}
return result;
}
public GroupSG newGroupSG(SGFactory pController, XSGroup pGroup) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created");
}
return new GroupSGImpl(new JAXBGroupSG(pController, schemaSG, pGroup));
}
public GroupSG getGroupSG(SGFactory pController, XSGroup pGroup, Context pClassContext) throws SAXException {
GroupSG result = pController.newGroupSG(pGroup, pClassContext);
result.init();
groupsByOrder.add(result);
return result;
}
public GroupSG newGroupSG(SGFactory pController, XSGroup pGroup, Context pClassContext) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created");
}
return new GroupSGImpl(new JAXBGroupSG(pController, schemaSG, pGroup, pClassContext));
}
public Object newObjectSG(SGFactory pController, XSElement pElement) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created.");
}
return new JAXBObjectSG(pController, schemaSG, pElement);
}
public ObjectSG getObjectSG(SGFactory pController, XSElement pElement) throws SAXException {
ObjectSG result = (ObjectSG) objects.get(pElement);
if (result == null) {
ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pElement);
result = new ObjectSGImpl(chain);
objects.put(pElement, result);
objectsByOrder.add(result);
result.init();
}
return result;
}
public Object newObjectSG(SGFactory pController, XSAny pAny) {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created.");
}
return new JAXBObjectSG(pController, schemaSG, pAny);
}
public ObjectSG getObjectSG(SGFactory pController, XSAny pAny, Context pContext) throws SAXException {
ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pAny);
ObjectSG result = new ObjectSGImpl(chain);
result.init();
return result;
}
public Object newObjectSG(SGFactory pController, XSElement pElement, Context pContext) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created.");
}
return new JAXBObjectSG(pController, schemaSG, pElement, pContext);
}
public ObjectSG getObjectSG(SGFactory pController, XSElement pElement, Context pContext) throws SAXException {
ObjectSGChain chain = (ObjectSGChain) pController.newObjectSG(pElement, pContext);
ObjectSG result = new ObjectSGImpl(chain);
result.init();
return result;
}
public SchemaSG getSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException {
if (schemaSG == null) {
SchemaSGChain chain = (SchemaSGChain) pController.newSchemaSG(pSchema);
schemaSG = new SchemaSGImpl(chain);
schemaSG.init();
}
return schemaSG;
}
public SchemaSG getSchemaSG(SGFactory pController) {
if (schemaSG == null) {
throw new IllegalStateException("A factory has not yet been created.");
}
return schemaSG;
}
public Object newSchemaSG(SGFactory pController, XSSchema pSchema) throws SAXException {
return new JAXBSchemaSG(pController, pSchema);
}
public TypeSG getTypeSG(SGFactory pController, XSType pType) throws SAXException {
final String mName = "getTypeSG(XSType)";
TypeSG result = (TypeSG) types.get(pType);
if (result == null) {
log.finest(mName, "->", pType.getName());
TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType);
result = new TypeSGImpl(chain);
types.put(pType, result);
typesByOrder.add(result);
result.init();
log.finest(mName, "<-", new Object[]{chain, result});
}
return result;
}
public Object newTypeSG(SGFactory pController, XSType pType) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created");
}
return new JAXBTypeSG(pController, schemaSG, pType);
}
public TypeSG getTypeSG(SGFactory pController, XSType pType, Context pClassContext, XsQName pName) throws SAXException {
final String mName = "getTypeSG(XSType,ClassContext)";
log.finest(mName, "->", new Object[]{pType, pClassContext});
TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType, pClassContext, pName);
TypeSG result = new TypeSGImpl(chain);
typesByOrder.add(result);
result.init();
log.finest(mName, "<-", new Object[]{chain, result});
return result;
}
public Object newTypeSG(SGFactory pController, XSType pType, Context pClassContext, XsQName pName) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created");
}
return new JAXBTypeSG(pController, schemaSG, pType, pClassContext, pName);
}
public Object newTypeSG(SGFactory pController, XSType pType, XsQName pName) throws SAXException {
if (schemaSG == null) {
throw new IllegalStateException("A schema has not yet been created");
}
return new JAXBTypeSG(pController, schemaSG, pType, pName);
}
public TypeSG getTypeSG(SGFactory pController, XSType pType, XsQName pName) throws SAXException {
TypeSGChain chain = (TypeSGChain) pController.newTypeSG(pType, pName);
TypeSG result = new TypeSGImpl(chain);
typesByOrder.add(result);
result.init();
return result;
}
public XSParser newXSParser(SGFactory pController) throws SAXException {
XSParser parser = new JAXBParser();
XSContext context = parser.getContext();
context.setXsObjectFactory(pController.newXsObjectFactory());
context.setXSObjectFactory(pController.newXSObjectFactory());
return parser;
}
public Facet newFacet(SGFactory pController, XSType pType, XSEnumeration[] pEnumerations) throws SAXException {
return new FacetImpl(pType, pEnumerations);
}
public TypeSG[] getTypes(SGFactory pController) {
return (TypeSG[]) typesByOrder.toArray(new TypeSG[typesByOrder.size()]);
}
public GroupSG[] getGroups(SGFactory pController) {
return (GroupSG[]) groupsByOrder.toArray(new GroupSG[groupsByOrder.size()]);
}
public ObjectSG[] getObjects(SGFactory pController) {
return (ObjectSG[]) objectsByOrder.toArray(new ObjectSG[objectsByOrder.size()]);
}
public XsObjectFactory newXsObjectFactory(SGFactory pController) throws SAXException {
return new JAXBXsObjectFactoryImpl(){
public XMLReader newXMLReader(boolean pValidating) throws ParserConfigurationException, SAXException{
XMLReader xr = super.newXMLReader(pValidating);
EntityResolver entityResolver = generator.getEntityResolver();
if (entityResolver != null) {
xr.setEntityResolver(entityResolver);
}
return xr;
}
};
}
public XSObjectFactory newXSObjectFactory(SGFactory pController) throws SAXException {
return JAXBParser.JAXB_OBJECT_FACTORY;
}
}