in ide/schema2beans/src/org/netbeans/modules/schema2beansdev/BeanClass.java [465:1230]
void genAccessors(int out) throws IOException {
int size = this.attrList.size();
boolean defaultKey = true;
ArrayList attrNames = new ArrayList();
select(TRAILER_SECTION);
if (this.beanElement.isRoot) {
//
// Put in some writeObject/readObject serializer/deserializer
// methods, so that this object can implement java.io.Serializable
// correctly.
//
comment("Special serializer: output XML as serialization");
gen(PRIVATE, VOID, "writeObject(java.io.ObjectOutputStream out) throws java.io.IOException");
begin();
gen("out.defaultWriteObject()"); eol();
gen("final int MAX_SIZE = 0XFFFF"); eol();
gen("final ByteArrayOutputStream baos = new ByteArrayOutputStream()"); eol();
gen("try");
begin();
gen("write(baos, SERIALIZATION_HELPER_CHARSET)"); eol();
gen("final byte [] array = baos.toByteArray()"); eol();
gen("final int numStrings = array.length / MAX_SIZE"); eol();
gen("final int leftover = array.length % MAX_SIZE"); eol();
gen("out.writeInt(numStrings + (0 == leftover ? 0 : 1))"); eol();
gen("out.writeInt(MAX_SIZE)"); eol();
gen("int offset = 0"); eol();
gen("for (int i = 0; i < numStrings; i++)");
begin();
gen("out.writeUTF(new String(array, offset, MAX_SIZE, SERIALIZATION_HELPER_CHARSET))"); eol();
gen("offset += MAX_SIZE"); eol();
end();
gen("if (leftover > 0)");
begin();
gen("final int count = array.length - offset"); eol();
gen("out.writeUTF(new String(array, offset, count, SERIALIZATION_HELPER_CHARSET))"); eol();
end();
end();
gen("catch (Schema2BeansException ex)");
begin();
gen("throw new Schema2BeansRuntimeException(ex)"); eol();
end();
end();
comment("Special deserializer: read XML as deserialization");
gen(PRIVATE, VOID, "readObject(java.io.ObjectInputStream in) throws java.io.IOException, ClassNotFoundException");
begin();
gen("try");
begin();
gen("in.defaultReadObject()"); eol();
gen("init(comparators, runtimeVersion)"); eol();
comment("init(comparators, new GenBeans.Version(1, 0, 8))");
gen("final int numStrings = in.readInt()");eol();
gen("final int max_size = in.readInt()");eol();
if (config.isJava5()) {
gen("final StringBuilder sb = new StringBuilder(numStrings * max_size)"); eol();
} else {
gen("final StringBuffer sb = new StringBuffer(numStrings * max_size)"); eol();
}
gen("for (int i = 0; i < numStrings; i++)"); begin();
gen("sb.append(in.readUTF())"); eol();
end();
gen("ByteArrayInputStream bais = new ByteArrayInputStream(sb.toString().getBytes(SERIALIZATION_HELPER_CHARSET))"); eol();
gen("Document doc = GraphManager.createXmlDocument(bais, false)"); eol();
if (config.isSetDefaults()){
gen("initOptions(Common.USE_DEFAULT_VALUES)"); eol();
gen("initFromNode(doc, Common.USE_DEFAULT_VALUES)"); eol();
} else {
gen("initOptions(Common.NO_DEFAULT_VALUES)"); eol();
gen("initFromNode(doc, Common.NO_DEFAULT_VALUES)"); eol();
}
end();
gen("catch (Schema2BeansException e)");
begin();
gen("throw new RuntimeException(e)"); eol();
end();
end();
cr();
jw.beginMethod("_setSchemaLocation", "String location", null, "void", jw.PUBLIC);
jw.beginIf("beanProp().getAttrProp(\"xsi:schemaLocation\", true) == null");
jw.writeEol("createAttribute(\"xmlns:xsi\", \"xmlns:xsi\", AttrProp.CDATA | AttrProp.IMPLIED, null, \"http://www.w3.org/2001/XMLSchema-instance\")");
jw.writeEol("setAttributeValue(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\")");
jw.writeEol("createAttribute(\"xsi:schemaLocation\", \"xsi:schemaLocation\", AttrProp.CDATA | AttrProp.IMPLIED, null, location)");
jw.end();
jw.writeEol("setAttributeValue(\"xsi:schemaLocation\", location)");
jw.end();
jw.cr();
jw.beginMethod("_getSchemaLocation", "", null, "String", jw.PUBLIC);
jw.beginIf("beanProp().getAttrProp(\"xsi:schemaLocation\", true) == null");
jw.writeEol("createAttribute(\"xmlns:xsi\", \"xmlns:xsi\", AttrProp.CDATA | AttrProp.IMPLIED, null, \"http://www.w3.org/2001/XMLSchema-instance\")");
jw.writeEol("setAttributeValue(\"xmlns:xsi\", \"http://www.w3.org/2001/XMLSchema-instance\")");
jw.writeEol("createAttribute(\"xsi:schemaLocation\", \"xsi:schemaLocation\", AttrProp.CDATA | AttrProp.IMPLIED, null, null)");
jw.end();
jw.writeEol("return getAttributeValue(\"xsi:schemaLocation\")");
jw.end();
jw.cr();
}
select(TRAILER_SECTION);
comment("Dump the content of this bean returning it as a String");
gen(PUBLIC, VOID, "dump(StringBuffer str, String indent)");
begin();
gen("String s"); eol();
gen("Object o"); eol();
gen("org.netbeans.modules.schema2beans.BaseBean n"); eol();
select(CONSTRUCTOR_SECTION);
comment("Properties (see root bean comments for the bean graph)");
int propertyCount = 0;
for (int i = 0; i < size; i++) {
Property a = (Property) attrList.get(i);
if (!a.isAttribute()) {
++propertyCount;
}
}
jw.writeEol("initPropertyTables("+propertyCount, ")");
select(DECL_SECTION); cr();
//
// Assume that there is no property interested in veto events.
// We'll adjust later on in the above properties loop.
//
this.genVetoListeners = false;
// Find out if any element has a key
if (this.metaElement != null) {
MetaProperty[] mp = this.metaElement.getMetaProperty();
for (int i=0; i<mp.length; i++) {
if (mp[i].isKey()) {
// At least one key is defined - default to false
defaultKey = false;
break;
}
}
}
for(int i=0; i<size; i++) {
boolean indexed = false;
Property a = (Property)this.attrList.get(i);
String constName = a.constName;
boolean keyedElement = defaultKey;
boolean isWrapper = false;
MetaElement me;
String scalarType = a.getScalarType();
boolean genVetoable = this.isVetoable;
Signatures sigs = getSignatures(a);
boolean isScalar = a.isScalar();
//
// Information about this property defined in the mdd file
// (extra information provided on top of the dtd informations)
//
MetaProperty mp = getMetaProperty(a);
if (mp != null) {
keyedElement = (mp.isKey())?true:defaultKey;
genVetoable = (mp.isVetoable())?true:genVetoable;
}
// Change the class type with the Wrapper info
me = getMetaElement(a);
if (me != null) {
/*
String tmp = me.getWrapperClass();
if (tmp != null && !tmp.equals("") &&
!tmp.equals("String") && !tmp.equals("java.lang.String")) {
int t = Common.wrapperToType(tmp);
if (t != Common.NONE)
a.type = t;
else {
a.classType = tmp;
isWrapper = true;
}
}
*/
/*
if (me.getBeanName() != null) {
if (config.isTraceGen() && !a.name.equals(me.getBeanName()))
config.messageOut.println("Reseting a.name from "+a.name+" to "+me.getBeanName());
a.name = me.getBeanName();
}
*/
genVetoable = (me.isVetoable())?true:genVetoable;
}
if (!isScalar && !a.isBean) {
if (!JavaUtil.isPrimitiveType(a.classType))
isWrapper = true;
}
//System.out.println("a.name="+a.name+" a.type="+a.type+" a.dtdName="+a.dtdName+" isWrapper="+isWrapper+" a.classType="+a.classType+" me="+me+" constName="+constName);
if (genVetoable) {
// At least prop is handling veto events - we'll need to
// generate the listener methods.
this.genVetoListeners = true;
}
// Only final prop can be vetoable
if (a.isBean)
genVetoable = false;
// Generate the constants
select(DECL_SECTION);
gen(STATIC, PUBLIC, FINAL, STRING, (String)constName);
gen(" = \"");
gen(a.name);
gen("\"");
eolNoI18N();
// Keep track of the constant name for this property
select(CONSTRUCTOR_SECTION);
if (!a.isAttribute()) {
gen("this.createProperty(\"", a.dtdName, "\", ");
noI18N(); tabIn();
gen(constName, ", ");
if (a.ored)
gen("Common.SEQUENCE_OR | ");
if (genVetoable)
gen("Common.TYPE_VETOABLE |");
cr(); tabIn();
select(BODY_SECTION);
if (a.elementInstance == Common.TYPE_1) {
//
// The property is mandatory. Check it is properly set.
//
if (!Common.isBoolean(a.type)) {
select(CONSTRUCTOR_SECTION);
gen("Common.TYPE_1");
}
else {
//
// A boolean type cannot be mandatory because the
// value of the tag depends on the existance of the
// empty tag in the document (a mandatory DTD empty
// element doesn't make sense since it would always
// be true).
//
select(CONSTRUCTOR_SECTION);
gen("Common.TYPE_0_1");
}
}
else
if (a.elementInstance == Common.TYPE_0_1) {
select(CONSTRUCTOR_SECTION);
gen("Common.TYPE_0_1");
}
else
if (a.elementInstance == Common.TYPE_1_N) {
//
// Check out that the first element is set and check
// that every element set is valid.
//
indexed = true;
select(CONSTRUCTOR_SECTION);
gen("Common.TYPE_1_N");
}
else
if (a.elementInstance == Common.TYPE_0_N) {
//
// The property is optional. However if
// the property is set and is a bean,
// its content has to be valid.
//
indexed = true;
select(CONSTRUCTOR_SECTION);
gen("Common.TYPE_0_N");
}
}
select(out);
/*
* Set method
*/
Property attributeOwner = null;
if (a.isAttribute()) {
attributeOwner = a.getAttributeOwner();
indexed = attributeOwner.isIndexed();
}
comment("This attribute is ", Common.instanceToString(a.elementInstance));
String type;
if (isScalar)
type = scalarType;
else
type = a.classType;
// Set - signature
String setParameters = "";
if (indexed)
setParameters = "int index, ";
if (a.getPropertyInterface() == null) {
setParameters += type + " value";
} else {
setParameters += a.getPropertyInterface() + " valueInterface";
}
String thrownExceptions = null;
if (genVetoable) {
thrownExceptions = "java.beans.PropertyVetoException";
}
jw.beginMethod(a.getWriteMethod(), setParameters, thrownExceptions,
"void", jw.PUBLIC | jw.BEANINFO);
if (a.getPropertyInterface() != null) {
jw.writeEol(type+" value = ("+type+") valueInterface");
}
if (genVetoable)
genVetoBegin();
SchemaRep.WhiteSpace ws = (SchemaRep.WhiteSpace) a.searchExtraData(SchemaRep.WhiteSpace.class);
if (ws != null)
genWhiteSpaceRestriction(ws, "value", type);
if (a.isAttribute()) {
AttrProp attrProp = a.getAttrProp();
if (attributeOwner != a) {
jw.comment("Make sure we've got a place to put this attribute.");
jw.beginIf("size("+attributeOwner.constName+") == 0");
String valueToSetTo = "\"\""; // The empty string.
if (Common.isBoolean(attributeOwner.type) && attributeOwner.getCanBeEmpty())
valueToSetTo = "java.lang.Boolean.TRUE";
if (attributeOwner.isIndexed()) {
jw.writeEol("addValue("+attributeOwner.constName+", ",
valueToSetTo, ")");
} else {
jw.writeEol("setValue("+attributeOwner.constName+", ",
valueToSetTo, ")");
}
jw.end();
if (Common.isBoolean(attributeOwner.type) && attributeOwner.getCanBeEmpty() && attributeOwner.isIndexed()) {
jw.write("setValue(", attributeOwner.constName);
jw.write(", index");
jw.writeEol(", java.lang.Boolean.TRUE)");
}
jw.write("setAttributeValue(");
jw.write(attributeOwner.constName+", ");
if (attributeOwner.isIndexed())
gen("index, ");
jw.write("\""+attrProp.getName()+"\"");
} else {
gen("setAttributeValue(");
gen(a.constName);
}
gen(", ");
if (isScalar)
gen("\"\"+value");
else
gen(JavaUtil.typeToString(a.getType(), "value"));
geneol(")");
} else {
gen("this.setValue(", constName, ", ");
if (indexed) gen("index, ");
genSetValue(isScalar, a.getType());
eol();
}
if (genVetoable)
genVetoEnd();
if (!indexed)
genResetMutuallyExclusive(a, true);
end(); cr();
/*
* Get method
*/
comment();
// Signature
String getParameters = "";
if (indexed)
getParameters = "int index";
jw.beginMethod(a.getReadMethod(indexed), getParameters, null,
a.getPropertyInterface() == null ? type :a.getPropertyInterface(),
jw.PUBLIC|jw.BEANINFO);
// Body
if (a.isAttribute()) {
List exceps = JavaUtil.exceptionsFromParsingText(a.getType());
if (!exceps.isEmpty()) {
gen("try ");
begin();
}
if (attributeOwner != a) {
AttrProp attrProp = a.getAttrProp();
jw.comment("If our element does not exist, then the attribute does not exist.");
jw.beginIf("size("+attributeOwner.constName+") == 0");
jw.writeEol("return " + JavaUtil.nullValueForType(a.getType()));
jw.endElseBegin();
jw.write("return ");
jw.writeEol(JavaUtil.genParseText(a.getType(), "getAttributeValue("+attributeOwner.constName+ ((attributeOwner.isIndexed()) ? ", index" : "") +", \""+attrProp.getName()+"\")", config.isForME()));
jw.end();
} else {
String nullValue = JavaUtil.nullValueForType(a.getType());
gen("return ");
if (!"null".equals(nullValue))
gen("(getAttributeValue("+a.constName+") == null) ? "+nullValue+" : ");
gen(JavaUtil.genParseText(a.getType(), "getAttributeValue("+a.constName+")", config.isForME()));
eol();
}
if (!exceps.isEmpty()) {
end();
genRethrowExceptions(exceps);
}
} else if (isScalar) {
gen(a.classType, " ret = ");
gen("(", a.classType, ")");
gen("this.getValue(", constName);
if (indexed) gen(", index");
PC(); eol();
gencr("if (ret == null)");
if (this.config.isScalarException() && !Common.isBoolean(a.type)) {
if (this.config.isStandalone()) {
tabIn();
gen("throw new NoSuchElementException(\"The element ",
a.name, " of type ");
gen(scalarType, " has no value.\")"); eol();
} else {
tabIn();
gencr("throw new RuntimeException(Common.getMessage(");
tabIn(); tabIn();
gencr("\"NoValueForElt_msg\",");
tabIn(); tabIn();
gen("new Object[] {\"");
gen(constName);
gen("\", ");
gen("\"", scalarType);
gen("\"}));");
cr();
}
}
else {
tabIn();
gen("ret = ");
gen("(", a.classType, ")");
gen("Common.defaultScalarValue(Common.");
gen(Common.typeToString(a.type), ")"); eol();
}
gen("return "+JavaUtil.fromObject(a.getType(), "ret"));
eol();
} else {
gen("return (", a.classType, ")");
gen("this.getValue(", constName);
if (indexed) gen(", index"); PC();
eol();
}
end(); cr();
//
// If we are an indexed property, we have to generate another
// kind of setter and getter method for the whole array.
//
if (indexed) {
jw.comment("Return the number of properties");
// Signature
gen(sigs.findSignature(SIZE));
sp();
begin();
// Body
if (!a.isAttribute())
gen("return this.size(", constName, ")");
else
gen("return this.size(", attributeOwner.constName, ")");
eol(); end(); cr();
if (!a.isAttribute()) {
// Setter method
comment("This attribute is ", Common.instanceToString(a.elementInstance));
// Set - signature
if (a.getPropertyInterface() == null) {
setParameters = type + "[] value";
} else {
setParameters = a.getPropertyInterface() +"[] value";
}
jw.beginMethod(a.getWriteMethod(), setParameters, thrownExceptions, "void", jw.PUBLIC|jw.BEANINFO);
// Body
if (isScalar) {
gen(a.classType, "[] values = null"); eol();
gencr("if (value != null)");
begin();
gen("values = new ", a.classType, "[value.length]"); eol();
gencr("for (int i=0; i<value.length; i++)");
gentab(1);
if (!config.isForME() && Common.isBoolean(a.type))
gen("values[i] = (value[i] ? Boolean.TRUE : Boolean.FALSE)");
else
gen("values[i] = new ", a.classType, "(value[i])");
eol();
end();
}
if (genVetoable)
genVetoBegin();
gen("this.setValue(", constName, ", ");
if (isScalar)
gen("values)");
else
gen("value)");
eol();
if (genVetoable)
genVetoEnd();
genResetMutuallyExclusive(a, true);
end(); cr();
// Getter method
comment();
// Signature
jw.beginMethod(a.getReadMethod(false), "", null,
(a.getPropertyInterface() == null ? type : a.getPropertyInterface())+"[]",
jw.PUBLIC|jw.BEANINFO);
// Body
if (isScalar) {
gen(scalarType, "[] ret = null"); eol();
gen(a.classType, "[] values = (", a.classType);
gen("[])this.getValues(", constName, ")"); eol();
gencr("if (values != null)");
begin();
gen("ret = new ", scalarType, "[values.length]"); eol();
gencr("for (int i=0; i<values.length; i++)");
gentab(1);
gen("ret[i] = values[i].",
Common.wrapperGetMethod(a.type), "()"); eol();
end();
geneol("return ret");
} else {
gen("return (", a.classType, "[])");
gen("this.getValues(", constName, ")");
eol();
}
end(); cr();
/*
* Remove and Add methods
*/
comment("Add a new element returning its index in the list");
// Signature
gen(sigs.findSignature(ADD));
sp();
if (genVetoable) {
gencr("throws PropertyVetoException ");
}
begin();
// Body
if (a.getPropertyInterface() != null) {
jw.writeEol(a.getType()+" value = ("+a.getType()+") valueInterface");
}
if (genVetoable)
genVetoBegin();
jw.write("int positionOfNewItem = this.addValue(", constName, ", ");
genSetValue(isScalar, a.getType());
jw.eol();
if (isMutuallyExclusive(a)) {
jw.beginIf("positionOfNewItem == 0");
genResetMutuallyExclusive(a, false);
jw.end();
}
jw.writeEol("return positionOfNewItem");
if (genVetoable)
genVetoEnd();
end(); cr();
comment();
comment("Remove an element using its reference");
comment("Returns the index the element had in the list");
comment();
// Signature
gen(sigs.findSignature(REMOVE));
sp();
if (genVetoable) {
gencr("throws PropertyVetoException ");
}
begin();
// Body
if (a.getPropertyInterface() != null) {
jw.writeEol(a.getType()+" value = ("+a.getType()+") valueInterface");
}
if (genVetoable)
genVetoBegin();
gen("return this.removeValue(", constName, ", ");
genSetValue(isScalar, a.getType());
eol();
if (genVetoable)
genVetoEnd();
end(); cr();
if (isScalar && !"int".equals(a.getType())) {
// Don't do "int" because we already have a method
// with that signature.
comment();
comment("Remove an element using its index");
comment();
// Signature
jw.beginMethod(a.getRemoveMethod(), "int index",
thrownExceptions, "void",
jw.PUBLIC|jw.BEANINFO);
// Body
if (genVetoable)
genVetoBegin();
gen("this.removeValue(", constName, ", index)");
eol();
if (genVetoable)
genVetoEnd();
end(); cr();
}
}
}
//
//-----------------
{
// The metaDD might define the class name of the bean
MetaElement e = getMetaElement(a);
String cls = null;
if (e != null)
cls = e.getBeanClass();
if (cls == null)
cls = a.classType;
//System.out.println("cls="+cls);
if (!a.isAttribute()) {
select(CONSTRUCTOR_SECTION);
gen(" | Common.");
if (isScalar && (a.type != Common.TYPE_BOOLEAN)) {
gen(Common.typeToString(Common.TYPE_STRING));
} else {
gen(Common.typeToString(a.type));
if (a.type == Common.TYPE_BOOLEAN && !a.getCanBeEmpty()) {
gen(" | Common.TYPE_SHOULD_NOT_BE_EMPTY");
}
}
if (keyedElement)
gen(" | Common.TYPE_KEY");
gen(", "); cr(); tabIn();
gen( cls, ".class)"); eol();
}
}
// dump()
if (!a.isAttribute()) {
select(TRAILER_SECTION);
if (isScalar && config.isScalarException() && !Common.isBoolean(a.type)) {
gen("if (this.getValue(");
gen(constName);
gen(") != null) ");
begin();
}
gen("str.append(indent)"); eol();
gen("str.append(\"",a.name);
if (indexed) {
gen("[\"+this.size", a.name, "()+\"]");
}
gen("\")" );
eolNoI18N();
if (indexed) {
gen("for(int i=0; i<this.size", a.name, "(); i++)");
cr();
begin();
gen("str.append(indent+\"\\t\")"); eol();
gen("str.append(\"#\"+i+\":\")"); eol();
}
boolean isCharArray = a.classType.equals("char[]");
if (Common.isBoolean(a.type)) {
gen("str.append(indent+\"\\t\")"); eolNoI18N();
gen("str.append((");
gen("this.is", a.name, "(");
if (indexed)
gen("i");
gen(")?\"true\":\"false\"))"); eol();
} else if (Common.isString(a.type) || isScalar || isCharArray) {
gen("str.append(indent+\"\\t\")"); eolNoI18N();
gen("str.append(\"<\")"); eolNoI18N();
if (isWrapper && !isCharArray)
gen("o = ");
else
gen("s = ");
if (isScalar || isCharArray) gen("String.valueOf(");
gen("this.get", a.name, "(");
if (indexed) gen("i");
gen(")");
if (isScalar || isCharArray) gen(")");
eol();
if (isWrapper && !isCharArray) {
gen("str.append((o==null?\"null\":o.toString()");
} else {
if (isScalar && !isCharArray) {
gen("str.append((s");
} else {
gen("str.append((s==null?\"null\":s");
}
}
gen(".trim()))");
eolNoI18N();
gen("str.append(\">\\n\")"); eolNoI18N();
} else if (a.isBean) {
gen("n = (org.netbeans.modules.schema2beans.BaseBean) this.get", a.name, "(");
if (indexed) gen("i");
gencr(");");
gencr("if (n != null)");
gen("\tn.dump(str, indent + \"\\t\")"); eolNoI18N();
gencr("else");
gen("\tstr.append(indent+\"\\tnull\")"); eolNoI18N();
}
if (!a.isAttribute()) {
gen("this.dumpAttributes(", constName, ", ");
if (indexed)
gen("i");
else
gen("0");
gen(", str, indent)"); eol();
}
if (isScalar && config.isScalarException() && !Common.isBoolean(a.type)) {
end();
}
if (indexed) {
//gen("str.append(indent+\"\\t\")"); eol();
//gen("str.append(\"---\")"); eol();
end();
}
cr();
}
genAttributes(CONSTRUCTOR_SECTION, a.attributes, constName);
select(ACCESS_SECTION);
genDefaultsAccessable(a);
if (a.isBean) {
genNewMethod(a.getPropertyInterface(), a.getType());
}
}
// The root might have attributes
if (this.beanElement.isRoot)
genAttributes(CONSTRUCTOR_SECTION,
this.beanElement.node.getAttributes(), null);
select(DECL_SECTION); cr();
select(TRAILER_SECTION);
end(false); cr();
}