/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003,2004 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.List;
import org.apache.ws.jaxme.generator.sg.AttributeSG;
import org.apache.ws.jaxme.generator.sg.ObjectSG;
import org.apache.ws.jaxme.generator.sg.PropertySG;
import org.apache.ws.jaxme.generator.sg.PropertySGChain;
import org.apache.ws.jaxme.generator.sg.SGlet;
import org.apache.ws.jaxme.generator.sg.SchemaSG;
import org.apache.ws.jaxme.generator.sg.SimpleTypeSG;
import org.apache.ws.jaxme.generator.sg.TypeSG;
import org.apache.ws.jaxme.generator.util.JavaNamer;
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.LocalJavaField;
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.XSType;
import org.apache.ws.jaxme.xs.jaxb.JAXBProperty;
import org.apache.ws.jaxme.xs.jaxb.JAXBPropertyOwner;
import org.xml.sax.SAXException;
/**
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
* @author <a href="mailto:iasandcb@tmax.co.kr">Ias</a>
*/
public class JAXBPropertySG implements PropertySGChain {
private final String propertyName;
private final String collectionType;
private final boolean generateIsSetMethod;
private final String defaultValue;
private final TypeSG typeSG;
protected JAXBPropertySG(String pDefaultPropertyName, SchemaSG pSchema, XSObject pXSObject,
String pDefaultValue, TypeSG pTypeSG) {
typeSG = pTypeSG;
defaultValue = pDefaultValue;
String myPropertyName = null;
boolean myGeneratedIsSetMethod = pSchema.isGeneratingIsSetMethod();
String myCollectionType = null;
if (pXSObject instanceof JAXBPropertyOwner) {
JAXBPropertyOwner jaxbPropertyOwner = (JAXBPropertyOwner) pXSObject;
JAXBProperty jaxbProperty = jaxbPropertyOwner.getJAXBProperty();
if (jaxbProperty != null) {
myPropertyName = jaxbProperty.getName();
Boolean jaxbGeneratedIsSetMethod = jaxbProperty.isGenerateIsSetMethod();
if (jaxbGeneratedIsSetMethod != null) {
myGeneratedIsSetMethod = jaxbGeneratedIsSetMethod.booleanValue();
myCollectionType = jaxbProperty.getCollectionType();
}
}
}
collectionType = myCollectionType == null ? pSchema.getCollectionType() : myCollectionType;
propertyName = myPropertyName == null ? JavaNamer.convert(pDefaultPropertyName, pSchema) : myPropertyName;
generateIsSetMethod = myGeneratedIsSetMethod;
}
protected JAXBPropertySG(AttributeSG pAttribute, XSAttribute pXSAttribute) {
this(pAttribute.getName().getLocalName(), pAttribute.getSchema(), pXSAttribute, pXSAttribute.getDefault(),
pAttribute.getTypeSG());
}
protected JAXBPropertySG(ObjectSG pElement, XSElement pXSElement) {
this(pElement.getName().getLocalName(), pElement.getSchema(), pXSElement, pXSElement.getDefault(),
pElement.getTypeSG());
}
protected JAXBPropertySG(TypeSG pComplexType, XSType pType) throws SAXException {
this("value", pComplexType.getSchema(), pType, null,
pComplexType.getComplexTypeSG().getSimpleContentSG().getContentTypeSG());
}
public void init(PropertySG pController) throws SAXException {}
public boolean hasIsSetMethod(PropertySG pController) { return generateIsSetMethod; }
public String getCollectionType(PropertySG pController) { return collectionType; }
public String getXMLFieldName(PropertySG pController) throws SAXException {
return propertyName;
}
public String getXMLGetMethodName(PropertySG pController) throws SAXException {
return "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
}
public String getXMLSetMethodName(PropertySG pController) throws SAXException {
return "set" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
}
public String getXMLIsSetMethodName(PropertySG pController) throws SAXException {
return hasIsSetMethod(pController) ?
"isSet" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1) : null;
}
public JavaField getXMLField(PropertySG pController, JavaSource pSource) throws SAXException {
return typeSG.getXMLField(pSource, pController.getXMLFieldName(), defaultValue);
}
public JavaMethod getXMLGetMethod(PropertySG pController, JavaSource pSource) throws SAXException {
return typeSG.getXMLGetMethod(pSource, pController.getXMLFieldName(),
pController.getXMLGetMethodName());
}
public JavaMethod getXMLSetMethod(PropertySG pController, JavaSource pSource) throws SAXException {
return typeSG.getXMLSetMethod(pSource, pController.getXMLFieldName(),
pController.getXMLSetMethodName());
}
public JavaMethod getXMLIsSetMethod(PropertySG pController, JavaSource pSource) throws SAXException {
return typeSG.getXMLIsSetMethod(pSource, pController.getXMLFieldName(),
pController.getXMLIsSetMethodName());
}
public void forAllNonNullValues(PropertySG pController, JavaMethod pMethod,
DirectAccessible pElement, SGlet pSGlet) throws SAXException {
boolean hasIsSetMethod = pController.hasIsSetMethod();
if (hasIsSetMethod) {
if (pElement == null) {
pMethod.addIf(pController.getXMLIsSetMethodName(), "()");
} else {
pMethod.addIf(pElement, ".", pController.getXMLIsSetMethodName(), "()");
}
if (typeSG.isComplex()) {
pSGlet.generate(pMethod, pController.getValue(pElement));
} else {
typeSG.getSimpleTypeSG().forAllValues(pMethod, pController.getValue(pElement), pSGlet);
}
pMethod.addEndIf();
} else {
if (typeSG.isComplex()) {
LocalJavaField f = pMethod.newJavaField(typeSG.getComplexTypeSG().getClassContext().getXMLInterfaceName());
f.addLine(pController.getValue(pElement));
pMethod.addIf(f, " != null");
pSGlet.generate(pMethod, pController.getValue(pElement));
pMethod.addEndIf();
} else {
typeSG.getSimpleTypeSG().forAllNonNullValues(pMethod, pController.getValue(pElement), pSGlet);
}
}
}
public void forAllValues(PropertySG pController, JavaMethod pMethod,
DirectAccessible pElement, SGlet pSGlet) throws SAXException {
SimpleTypeSG simpleTypeSG = typeSG.getSimpleTypeSG();
simpleTypeSG.forAllValues(pMethod, pController.getValue(pElement), pSGlet);
}
public Object getValue(PropertySG pController, DirectAccessible pElement) throws SAXException {
if (pElement == null) {
return new Object[]{pController.getXMLGetMethodName(), "()"};
} else {
return new Object[]{pElement, ".", pController.getXMLGetMethodName(), "()"};
}
}
public void generate(PropertySG pController, JavaSource pSource) throws SAXException {
if (typeSG != null && !typeSG.isGlobalType() && pSource.isInterface()) {
typeSG.generate(pSource);
}
pController.getXMLField(pSource);
pController.getXMLGetMethod(pSource);
pController.getXMLSetMethod(pSource);
if (hasIsSetMethod(pController)) {
pController.getXMLIsSetMethod(pSource);
}
}
public void setValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, Object pValue, JavaQName pType)
throws SAXException {
if (pType != null) {
pValue = new Object[]{"((", pType, ") ", pValue, ")"};
}
if (typeSG.isComplex() || !typeSG.getSimpleTypeSG().isList()) {
if (pElement == null) {
pMethod.addLine(pController.getXMLSetMethodName(), "(", pValue, ");");
} else {
pMethod.addLine(pElement, ".", pController.getXMLSetMethodName(), "(", pValue, ");");
}
} else {
LocalJavaField list = pMethod.newJavaField(List.class);
list.addLine(pController.getValue(pElement));
pMethod.addLine(list, ".clear();");
pMethod.addLine(list, ".addAll(", pValue, ");");
}
}
public void addValue(PropertySG pController, JavaMethod pMethod, DirectAccessible pElement, Object pValue, JavaQName pType) throws SAXException {
pController.setValue(pMethod, pElement, pValue, pType);
}
public String getDefaultValue(PropertySG pController) throws IllegalStateException {
if (typeSG.isComplex()) {
throw new IllegalStateException("Complex type " + typeSG.getName() + " doesn't support default value.");
}
return defaultValue;
}
}