*/
private void createDescriptor(final DescriptorJClass classDesc, final FieldInfo member,
final String localClassName, final String nsURI,
final JSourceCode jsc) {
XMLInfoNature xmlNature = new XMLInfoNature(member);
XSType xsType = xmlNature.getSchemaType();
XSType xsCollectionType = null;
boolean any = false;
NodeType nodeType = xmlNature.getNodeType();
boolean isElement = (nodeType == NodeType.ELEMENT);
boolean isAttribute = (nodeType == NodeType.ATTRIBUTE);
boolean isText = (nodeType == NodeType.TEXT);
jsc.add("//-- ");
jsc.append(member.getName());
//-- a hack, I know, I will change later (kv)
if (member.getName().equals("_anyObject")) {
any = true;
}
if (xsType.isCollection()) {
//Attributes can handle COLLECTION type for NMTOKENS or IDREFS for instance
xsCollectionType = xsType;
xsType = new XMLInfoNature(((CollectionInfo) member).getContent()).getSchemaType();
}
// Resolve how the node name parameter to the XMLFieldDescriptorImpl constructor is supplied
String nodeName = xmlNature.getNodeName();
String nodeNameParam = null;
if ((nodeName != null) && (!isText)) {
//-- By default the node name parameter is a literal string
nodeNameParam = "\"" + nodeName + "\"";
if (_config.classDescFieldNames()) {
//-- The node name parameter is a reference to a public static final
nodeNameParam = nodeName.toUpperCase();
//-- Expose node name as public static final (reused by XMLFieldDescriptorImpl)
JConstant constant = new JConstant(SGTypes.STRING, nodeNameParam);
constant.setInitString("\"" + nodeName + "\"");
classDesc.addMember(constant);
}
}
//-- Generate code to new XMLFieldDescriptorImpl instance
jsc.add("desc = new org.exolab.castor.xml.util.XMLFieldDescriptorImpl(");
jsc.append(classType(xsType.getJType()));
jsc.append(", \"");
jsc.append(member.getName());
jsc.append("\", ");
if (nodeNameParam != null) {
jsc.append(nodeNameParam);
} else if (isText) {
jsc.append("\"PCDATA\"");
} else {
jsc.append("(java.lang.String) null");
}
if (isElement) {
jsc.append(", org.exolab.castor.xml.NodeType.Element);");
} else if (isAttribute) {
jsc.append(", org.exolab.castor.xml.NodeType.Attribute);");
} else if (isText) {
jsc.append(", org.exolab.castor.xml.NodeType.Text);");
}
switch (xsType.getType()) {
case XSType.STRING_TYPE :
jsc.add("desc.setImmutable(true);");
break;
//only for attributes
case XSType.IDREF_TYPE :
jsc.add("desc.setReference(true);");
break;
case XSType.ID_TYPE :
jsc.add("this._identity = desc;");
break;
case XSType.QNAME_TYPE :
jsc.add("desc.setSchemaType(\"QName\");");
break;
default :
break;
} //switch
//-- handler access methods
if (member.getXMLFieldHandler() != null) {
String handler = member.getXMLFieldHandler();
jsc.add("handler = new " + handler + "();");
jsc.add("//-- test for generalized field handler");
jsc.add("if (handler instanceof ");
jsc.append(GENERALIZED_FIELD_HANDLER_CLASS.getName());
jsc.append(")");
jsc.add("{");
jsc.indent();
jsc.add("//-- save reference to user-specified handler");
jsc.add(GENERALIZED_FIELD_HANDLER_CLASS.getName());
jsc.append(" gfh = (");
jsc.append(GENERALIZED_FIELD_HANDLER_CLASS.getName());
jsc.append(") handler;");
_xmlFieldHandlerFactory.createXMLFieldHandler(
member, xsType, localClassName, jsc, true);
jsc.add("gfh.setFieldHandler(handler);");
jsc.add("handler = gfh;");
jsc.unindent();
jsc.add("}");
} else {
_xmlFieldHandlerFactory.createXMLFieldHandler(
member, xsType, localClassName, jsc, false);
addSpecialHandlerLogic(member, xsType, jsc);
}
// Add the schema type as defined in the schema
if (xsCollectionType == null) {
jsc.add("desc.setSchemaType(\"" + xsType.getName() + "\");");
} else {
jsc.add("desc.setSchemaType(\"list\");");
jsc.add("desc.setComponentType(\"" + xsType.getName() + "\");");
if (xsCollectionType instanceof XSList && ((XSList) xsCollectionType).isDerivedFromXSList()) {
jsc.add("desc.setDerivedFromXSList(true);");
}
}
jsc.add("desc.setHandler(handler);");
//-- container
if (member.isContainer()) {
jsc.add("desc.setContainer(true);");
String className = xsType.getName(); //set the class descriptor
//Try to prevent endless loop. Note: we only compare the localClassName.
//If the packages are different an error can happen here
if (className.equals(localClassName)) {
jsc.add("desc.setClassDescriptor(this);");
} else {
String descriptorClassName = getQualifiedDescriptorClassName(className);
jsc.add("desc.setClassDescriptor(new " + descriptorClassName + "());");
}
}
//-- Handle namespaces
//-- FieldInfo namespace has higher priority than ClassInfo namespace.
if (xmlNature.getNamespaceURI() != null) {
jsc.add("desc.setNameSpaceURI(\"");
jsc.append(xmlNature.getNamespaceURI());
jsc.append("\");");
}
//-- required
if (xmlNature.isRequired()) {
jsc.add("desc.setRequired(true);");
}
//-- nillable
if (member.isNillable()) {
jsc.add("desc.setNillable(true);");
}
//-- if any it can match all the names
if (any) {
jsc.add("desc.setMatches(\"*\");");
}
//-- mark as multi or single valued for elements
if (isElement || isAttribute) {
jsc.add("desc.setMultivalued(" + xmlNature.isMultivalued());
jsc.append(");");
}
jsc.add("addFieldDescriptor(desc);");
if (isElement) {