Package org.apache.ws.jaxme.generator.sg.impl

Source Code of org.apache.ws.jaxme.generator.sg.impl.JAXBObjectSG

/*
* 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 javax.xml.bind.Element;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

import org.apache.ws.jaxme.JMElement;
import org.apache.ws.jaxme.JMHandler;
import org.apache.ws.jaxme.JMXmlSerializer;
import org.apache.ws.jaxme.generator.sg.ComplexTypeSG;
import org.apache.ws.jaxme.generator.sg.Context;
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.SchemaSG;
import org.apache.ws.jaxme.generator.sg.TypeSG;
import org.apache.ws.jaxme.js.DirectAccessible;
import org.apache.ws.jaxme.js.JavaField;
import org.apache.ws.jaxme.js.JavaMethod;
import org.apache.ws.jaxme.js.JavaQName;
import org.apache.ws.jaxme.js.JavaSource;
import org.apache.ws.jaxme.js.JavaSourceFactory;
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.XSAttribute;
import org.apache.ws.jaxme.xs.XSElement;
import org.apache.ws.jaxme.xs.XSObject;
import org.apache.ws.jaxme.xs.XSSimpleContentType;
import org.apache.ws.jaxme.xs.XSType;
import org.apache.ws.jaxme.xs.xml.XsQName;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;


/**
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class JAXBObjectSG extends JAXBSGItem implements ObjectSGChain {
  private final static Logger log = LoggerAccess.getLogger(JAXBObjectSG.class);
  private final XSType type;
  private final TypeSG typeSG;
  private final XsQName name;
  private final Context classContext;

  /** <p>Creates a new, local instance of JAXBObjectSG, generating
   * the given attribute within the given {@link Context}.</p>
   */
  public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSAttribute pAttribute,
                       Context pClassContext) throws SAXException {
    this(pFactory, pSchema, (XSObject) pAttribute, pClassContext);
  }

  /** <p>Creates a new, global instance of JAXBObjectSG, generating
   * the given element.</p>
   */
  public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSElement pElement) throws SAXException {
    this(pFactory, pSchema, (XSObject) pElement, null);
  }

  /** <p>Creates a new, local instance of JAXBObjectSG, generating
   * the given element within the given {@link Context}.</p>
   */
  public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSElement pElement,
                       Context pClassContext) throws SAXException {
    this(pFactory, pSchema, (XSObject) pElement, pClassContext);
  }

  /** <p>Creates a new, local instance of JAXBObjectSG, which
   * is generated within the given {@link Context}.</p>
   */
  private JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSObject pObject,
                        Context pClassContext) throws SAXException {
    super(pFactory, pSchema, pObject);
    final String mName = "<init>(XSObject,Context)";
    boolean isClassGlobal;
    if (pObject instanceof XSElement) {
      XSElement element = (XSElement) pObject;
      log.finest(mName, "->", new Object[]{element.getName(), pClassContext});
      type = element.getType();
      name = element.getName();
      isClassGlobal = !type.isSimple()  &&  (type.isGlobal()  ||  element.isGlobal());
    } else {
      throw new IllegalStateException("Unknown object type: " + pObject.getClass().getName());
    }

    Context myClassContext;
    final boolean useTypesContext = pClassContext != null;
    if (useTypesContext) {
      myClassContext = pClassContext;
    } else {
      myClassContext = new GlobalContext(name, pObject, null, null, pSchema);
    }

    if (isClassGlobal) {
      if (type.isGlobal()) {
        typeSG = pFactory.getTypeSG(type);
      } else {
        typeSG = pFactory.getTypeSG(type, name);
      }
    } else {
      typeSG = pFactory.getTypeSG(type, myClassContext, name);
    }

    if (useTypesContext) {
      if (typeSG.isComplex()) {
        classContext = typeSG.getComplexTypeSG().getClassContext();
      } else {
        classContext = pClassContext;
      }
    } else if (typeSG.isComplex()) {
      classContext = myClassContext;
      Context tctx = typeSG.getComplexTypeSG().getClassContext();
      AbstractContext ctx = (AbstractContext) classContext;
      ctx.setPMName(tctx.getPMName());
      ctx.setXMLSerializerName(tctx.getXMLSerializerName());
      ctx.setXMLValidatorName(tctx.getXMLValidatorName());
    } else {
      classContext = null;
    }
    log.finest(mName, "<-", new Object[]{typeSG, classContext});
  }

  /** <p>Creates a new instance of JAXBObjectSG generating the given simple
   * content <code>pContent</code> of the given complex type
   * <code>pComplexType</code>.</p>
   */
  public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, TypeSG pComplexType,
                       XSSimpleContentType pContainer, XSType pType) throws SAXException {
    super(pFactory, pSchema, pType);

    type = pType;
    name = null;
    classContext = pComplexType.getComplexTypeSG().getClassContext();
    typeSG = pFactory.getTypeSG(type, classContext, null);
  }

  /** <p>Creates a new instance of JAXBObjectSG generating the given
   * wildcard object.</p>
   */
  public JAXBObjectSG(SGFactory pFactory, SchemaSG pSchema, XSAny pAny) {
      super(pFactory, pSchema, pAny);
      type = null;
      name = null;
      classContext = null;
      typeSG = null;
  }

  public void init(ObjectSG pController) throws SAXException {
  }

  public TypeSG getTypeSG(ObjectSG pController) {
    if (typeSG == null) {
      throw new NullPointerException("ObjectSG not initialized");
    }
    return typeSG;
  }

  public Locator getLocator(ObjectSG pController) { return getLocator(); }
  public SGFactory getFactory(ObjectSG pController) { return getFactory(); }
  public SchemaSG getSchema(ObjectSG pController) { return getSchema(); }
  public Context getClassContext(ObjectSG pController) { return classContext; }
  public XsQName getName(ObjectSG pController) {
    if (name == null) {
      throw new IllegalStateException("The content object of a complex type with simple content doesn't have an XML Schema name.");
    }
    return name;
  }

  public JavaSource getXMLInterface(ObjectSG pController) throws SAXException {
    final String mName = "getXMLInterface";
    log.finest(mName, "->", pController.getName());
    if (!pController.getTypeSG().isComplex()) {
      log.finest(mName, "<-", "null");
      return null;
    }

    JavaQName xmlInterfaceName = pController.getClassContext().getXMLInterfaceName();
    JavaSourceFactory jsf = getSchema().getJavaSourceFactory();
    JavaSource js = jsf.newJavaSource(xmlInterfaceName, JavaSource.PUBLIC);
    js.setType(JavaSource.INTERFACE);
    js.addExtends(Element.class);

    TypeSG myTypeSG = pController.getTypeSG();
    ComplexTypeSG complexTypeSG = myTypeSG.getComplexTypeSG();
    if (myTypeSG.isGlobalClass()) {
      js.addExtends(complexTypeSG.getClassContext().getXMLInterfaceName());
      // No need to generate the types XML interface; this is done by the schema
    } else {
      complexTypeSG.generateXMLInterfaceMethods(js);
    }
    log.finest(mName, "<-", xmlInterfaceName);
    return js;
  }

  public JavaSource getXMLImplementation(ObjectSG pController) throws SAXException {
    final String mName = "getXMLImplementation";
    log.finest(mName, "->", pController.getName());
    if (!pController.getTypeSG().isComplex()) {
      log.finest(mName, "<-", "null");
      return null;
    }

    JavaQName xmlImplementationName = pController.getClassContext().getXMLImplementationName();
    JavaSourceFactory jsf = getSchema().getJavaSourceFactory();
    JavaSource js = jsf.newJavaSource(xmlImplementationName, JavaSource.PUBLIC);
    js.addImplements(pController.getClassContext().getXMLInterfaceName());
    js.addImplements(JMElement.class);

    TypeSG myTypeSG = pController.getTypeSG();
    ComplexTypeSG complexTypeSG = myTypeSG.getComplexTypeSG();
    if (myTypeSG.isGlobalClass()) {
      js.addExtends(complexTypeSG.getClassContext().getXMLImplementationName());
      // No need to generate the types XML implementation; this is done by the schema.
    } else {
      complexTypeSG.generateXMLImplementationMethods(js);
    }

    JavaField myName = js.newJavaField("__qName", QName.class, JavaSource.PRIVATE);
    myName.setStatic(true);
    myName.setFinal(true);
    XsQName qName = pController.getName();
    myName.addLine("new ", QName.class, "(", JavaSource.getQuoted(qName.getNamespaceURI()),
                   ", ", JavaSource.getQuoted(qName.getLocalName()), ")");

    JavaMethod getQName = js.newJavaMethod("getQName", QName.class, JavaSource.PUBLIC);
    getQName.addLine("return ", myName, ";");

    return js;
  }

  public JavaSource getXMLSerializer(ObjectSG pController) throws SAXException {
    final String mName = "getXMLSerializer";
    log.finest(mName, "->", pController.getName());
    TypeSG myTypeSG = pController.getTypeSG();
    if (!myTypeSG.isComplex()  ||  myTypeSG.isGlobalClass()) {
      log.finest(mName, "<-", "null");
      return null;
    }

    JavaQName xmlSerializerName = pController.getClassContext().getXMLSerializerName();
    JavaSourceFactory jsf = getSchema().getJavaSourceFactory();
    JavaSource js = jsf.newJavaSource(xmlSerializerName, JavaSource.PUBLIC);
    js.addImplements(JMXmlSerializer.class);
    myTypeSG.getComplexTypeSG().generateXMLSerializerMethods(js);
    return js;
  }

  public JavaSource getXMLHandler(ObjectSG pController) throws SAXException {
    final String mName = "getXMLHandler";
    log.finest(mName, "->", pController.getName());
    TypeSG myTypeSG = pController.getTypeSG();
    if (!myTypeSG.isComplex()) {
      log.finest(mName, "<-", null);
      return null;
    } else {
      JavaQName xmlHandlerName = pController.getClassContext().getXMLHandlerName();
      JavaSourceFactory jsf = getSchema().getJavaSourceFactory();
      JavaSource js = jsf.newJavaSource(xmlHandlerName, JavaSource.PUBLIC);
      js.addImplements(JMHandler.class);
      if (myTypeSG.isGlobalClass()) {
        Context typeContext = myTypeSG.getComplexTypeSG().getClassContext();
        js.addExtends(typeContext.getXMLHandlerName());
        JavaQName xmlElementInterface = pController.getClassContext().getXMLInterfaceName();
        JavaQName resultInterface = typeContext.getXMLInterfaceName();
        JavaMethod jm = js.newJavaMethod("newResult", resultInterface, JavaSource.PROTECTED);
        jm.addThrows(SAXException.class);
        jm.addTry();
        jm.addLine("return (", resultInterface, ") getData().getFactory().getElement(",
                   xmlElementInterface, ".class);");
        DirectAccessible e = jm.addCatch(JAXBException.class);
        jm.addThrowNew(SAXException.class, e);
        jm.addEndTry();
      } else {
        myTypeSG.getComplexTypeSG().generateXMLHandlerMethods(js);
      }
      return js;
    }
  }

  public void generate(ObjectSG pController) throws SAXException {
    final String mName = "generate";
    log.finest(mName, "->", pController.getName());
    pController.getXMLInterface();
    pController.getXMLImplementation();
    pController.getXMLSerializer();
    pController.getXMLHandler();

    TypeSG myTypeSG = pController.getTypeSG();
    if (myTypeSG.isGlobalClass()  &&  !myTypeSG.isGlobalType()) {
      myTypeSG.generate();
    }
    log.finest(mName, "<-");
  }
}
TOP

Related Classes of org.apache.ws.jaxme.generator.sg.impl.JAXBObjectSG

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.