gdaniels 02/03/09 11:29:49 Modified: java/src/org/apache/axis/encoding/ser BeanDeserializer.java BeanSerializer.java BeanSerializerFactory.java SimpleDeserializer.java java/src/org/apache/axis/wsdl/toJava JavaComplexTypeWriter.java java/src/org/apache/axis/description TypeDesc.java Log: Correctly deal with mangled element names when writing types in WSDL2Java. Remove "elementFormat" code and constants. If we do any name- mangling that causes the XML and Java names to differ, we'll now add metadata to the generated class to account for it exactly, rather than trying to rely on essentially one-way algorithms. Revision Changes Path 1.11 +2 -19 xml-axis/java/src/org/apache/axis/encoding/ser/BeanDeserializer.java Index: BeanDeserializer.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/ser/BeanDeserializer.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- BeanDeserializer.java 8 Mar 2002 17:22:13 -0000 1.10 +++ BeanDeserializer.java 9 Mar 2002 19:29:48 -0000 1.11 @@ -163,22 +163,8 @@ if (propDesc == null) { // look for a field by this name. Assumes the the number of // properties in a bean is (relatively) small, so uses a linear - // search. Accept a property if it differs only by the - // capitalization of the first character. - String localNameUp = - BeanSerializer.format(localName, - BeanSerializer.FORCE_UPPER); - String localNameLo = - BeanSerializer.format(localName, - BeanSerializer.FORCE_LOWER); - String mangledName = JavaUtils.xmlNameToJava(localName); + // search. propDesc = (BeanPropertyDescriptor) propertyMap.get(localName); - if(propDesc == null) - propDesc = (BeanPropertyDescriptor) propertyMap.get(localNameUp); - if(propDesc == null) - propDesc = (BeanPropertyDescriptor) propertyMap.get(localNameLo); - if(propDesc == null) - propDesc = (BeanPropertyDescriptor) propertyMap.get(mangledName); } if (propDesc == null) { @@ -250,10 +236,7 @@ continue; String attrName = attributes.getLocalName(i); - String attrNameUp = BeanSerializer.format(attrName, BeanSerializer.FORCE_UPPER); - String attrNameLo = BeanSerializer.format(attrName, BeanSerializer.FORCE_LOWER); - String mangledName = JavaUtils.xmlNameToJava(attrName); - + // look for the attribute property BeanPropertyDescriptor bpd = (BeanPropertyDescriptor) propertyMap.get(fieldName); 1.18 +44 -95 xml-axis/java/src/org/apache/axis/encoding/ser/BeanSerializer.java Index: BeanSerializer.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/ser/BeanSerializer.java,v retrieving revision 1.17 retrieving revision 1.18 diff -u -r1.17 -r1.18 --- BeanSerializer.java 8 Mar 2002 22:26:46 -0000 1.17 +++ BeanSerializer.java 9 Mar 2002 19:29:48 -0000 1.18 @@ -113,23 +113,12 @@ public static final Object[] noArgs = new Object[] {}; // For convenience - // When serializing, the property element names passed over the wire - // are the names of the properties (format=PROPERTY_NAME). - // Setting the format to FORCE_UPPER will cause the - // serializer to uppercase the first letter of the property element name. - // Setting the format to FORCE_LOWER will cause the - // serializer to uppercase the first letter of the property element name. - private short elementPropertyFormat = PROPERTY_NAME; - public static short PROPERTY_NAME = 0; - public static short FORCE_UPPER = 1; - public static short FORCE_LOWER = 2; - QName xmlType; Class javaType; private BeanPropertyDescriptor[] propertyDescriptor = null; private TypeDesc typeDesc = null; - + // Construct BeanSerializer for the indicated class/qname public BeanSerializer(Class javaType, QName xmlType) { @@ -140,12 +129,6 @@ typeDesc = TypeDesc.getTypeDescForClass(javaType); } - // Construct BeanSerializer for the indicated class/qname and format - public BeanSerializer(Class javaType, QName xmlType, short format) { - this(javaType, xmlType); - setElementPropertyFormat(format); - } - /** * Serialize a bean. Done simply by serializing each bean property. * @param name is the element name @@ -159,7 +142,7 @@ { boolean isSOAP_ENC = Constants. isSOAP_ENC(context.getMessageContext().getEncodingStyle()); - + // Check for meta-data in the bean that will tell us if any of the // properties are actually attributes, add those to the element // attribute list @@ -170,32 +153,32 @@ // Serialize each property for (int i=0; i<propertyDescriptor.length; i++) { String propName = propertyDescriptor[i].getName(); - if (propName.equals("class")) + if (propName.equals("class")) continue; QName qname = null; - + // If we have type metadata, check to see what we're doing // with this field. If it's an attribute, skip it. If it's // an element, use whatever qname is in there. If we can't // find any of this info, use the default. - + if (typeDesc != null) { FieldDesc field = typeDesc.getFieldByName(propName); if (field != null) { if (!field.isElement()) continue; - + qname = field.getXmlName(); } } - + if (qname == null) { // Use the default... - propName = format(propName, elementPropertyFormat); + propName = propName; qname = new QName("", propName); } - + Method readMethod = propertyDescriptor[i].getReadMethod(); if (readMethod != null && readMethod.getParameterTypes().length == 0) { // Normal case: serialize the value @@ -246,25 +229,25 @@ } return pd; } - + /** * Return a list of properties in the bean which should be attributes - */ + */ static Vector getBeanAttributes(Class javaType, TypeDesc typeDesc) { Vector ret = new Vector(); - + if (typeDesc == null) { // !!! Support old-style beanAttributeNames for now - + // See if this object defined the 'getAttributeElements' function // which returns a Vector of property names that are attributes try { - Method getAttributeElements = + Method getAttributeElements = javaType.getMethod("getAttributeElements", new Class [] {}); // get string array String[] array = (String[])getAttributeElements.invoke(null, noArgs); - + // convert it to a Vector ret = new Vector(array.length); for (int i = 0; i < array.length; i++) { @@ -284,43 +267,10 @@ } } } - + return ret; } - - - /** - * Get the format of the elements for the properties - */ - public short getElementPropertyFormat() { - return elementPropertyFormat; - } - /** - * Set the format of the elements for the properties - */ - public void setElementPropertyFormat(short format) { - if (format > FORCE_LOWER || - format < PROPERTY_NAME) - format = PROPERTY_NAME; - elementPropertyFormat = format; - } - /** - * Returns the property name string formatted in the specified manner - * @param name to format - * @param fmt (PROPERTY_NAME, FORCE_LOWER, FORCE_UPPER) - * @return formatted name - */ - static String format(String name, short fmt) { - if (fmt == PROPERTY_NAME) - return name; - String theRest = ""; - if (name.length() > 1) - theRest = name.substring(1); - if (fmt == FORCE_UPPER) - return Character.toUpperCase(name.charAt(0)) + theRest; - else - return Character.toLowerCase(name.charAt(0)) + theRest; - } + public String getMechanismType() { return Constants.AXIS_SAX; } @@ -348,7 +298,7 @@ Vector stopClasses = types.getStopClasses(); if (superClass != null && superClass != java.lang.Object.class && - (stopClasses == null || + (stopClasses == null || !(stopClasses.contains(superClass.getName()))) ) { // Write out the super class String base = types.writeType(superClass); @@ -363,7 +313,7 @@ } // Add fields under sequence element. - // Note: In most situations it would be okay + // Note: In most situations it would be okay // to put the fields under an all element. // However it is illegal schema to put an // element with minOccurs=0 or maxOccurs>1 underneath @@ -390,7 +340,7 @@ QName attrName = typeDesc.getAttributeNameForField( field.getName()); writeAttribute(types, attrName.getLocalPart(), - field.getType(), + field.getType(), complexType); } else { QName xmlName = typeDesc.getElementNameForField( @@ -414,7 +364,7 @@ } /** - * write a schema representation of the given Class field and append it to + * write a schema representation of the given Class field and append it to * the where Node, recurse on complex types * @param fieldName name of the field * @param fieldType type of the field @@ -436,7 +386,7 @@ } where.appendChild(elem); } - + /** * write aa attribute element and append it to the 'where' Node * @param fieldName name of the field @@ -444,17 +394,17 @@ * @param where location for the generated schema node * @throws Exception */ - private void writeAttribute(Types types, + private void writeAttribute(Types types, String fieldName, Class fieldType, Element where) throws Exception { - + // Attribute must be a simple type. if (!types.isSimpleSchemaType(fieldType)) - throw new AxisFault(JavaUtils.getMessage("AttrNotSimpleType00", + throw new AxisFault(JavaUtils.getMessage("AttrNotSimpleType00", fieldName, fieldType.getName())); - + String elementType = types.writeType(fieldType); Element elem = types.createAttributeElement(fieldName, elementType, @@ -467,15 +417,15 @@ * Check for meta-data in the bean that will tell us if any of the * properties are actually attributes, add those to the element * attribute list - * + * * @param value the object we are serializing * @param pd the properties of this class * @return attributes for this element, null if none - */ + */ private Attributes getObjectAttributes(Object value, Attributes attributes, SerializationContext context) { - + if (typeDesc == null || !typeDesc.hasAttributes()) return attributes; @@ -484,46 +434,45 @@ attrs = new AttributesImpl(attributes); else attrs = new AttributesImpl(); - + try { - // Find each property that is an attribute + // Find each property that is an attribute // and add it to our attribute list for (int i=0; i<propertyDescriptor.length; i++) { String propName = propertyDescriptor[i].getName(); if (propName.equals("class")) continue; - + FieldDesc field = typeDesc.getFieldByName(propName); // skip it if its not an attribute if (field == null || field.isElement()) continue; - + QName qname = field.getXmlName(); if (qname == null) { - qname = new QName("", - format(propName, elementPropertyFormat)); + qname = new QName("", propName); } - + Method readMethod = propertyDescriptor[i].getReadMethod(); - if (readMethod != null && + if (readMethod != null && readMethod.getParameterTypes().length == 0) { // add to our attributes Object propValue = propertyDescriptor[i]. getReadMethod().invoke(value,noArgs); // If the property value does not exist, don't serialize // the attribute. In the future, the decision to serializer - // the attribute may be more sophisticated. For example, don't + // the attribute may be more sophisticated. For example, don't // serialize if the attribute matches the default value. if (propValue != null) { String propString = propValue != null ? propValue.toString() : ""; - + String namespace = qname.getNamespaceURI(); String localName = qname.getLocalPart(); - - attrs.addAttribute(namespace, - localName, - context.qName2String(qname), - "CDATA", + + attrs.addAttribute(namespace, + localName, + context.qName2String(qname), + "CDATA", propString); } } @@ -532,7 +481,7 @@ // no attributes return attrs; } - + return attrs; } } 1.2 +1 -17 xml-axis/java/src/org/apache/axis/encoding/ser/BeanSerializerFactory.java Index: BeanSerializerFactory.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/ser/BeanSerializerFactory.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- BeanSerializerFactory.java 26 Jan 2002 02:40:34 -0000 1.1 +++ BeanSerializerFactory.java 9 Mar 2002 19:29:48 -0000 1.2 @@ -76,8 +76,6 @@ * @author Rich Scheuerle <[EMAIL PROTECTED]> */ public class BeanSerializerFactory extends BaseSerializerFactory { - short format = BeanSerializer.PROPERTY_NAME; - public BeanSerializerFactory(Class javaType, QName xmlType) { super(BeanSerializer.class, false, xmlType, javaType); // Sometimes an Enumeration class is registered as a Bean. @@ -86,23 +84,9 @@ serClass = EnumSerializer.class; } } - // Alternate constructor to set format flag - public BeanSerializerFactory(Class javaType, QName xmlType, short format) { - super(BeanSerializer.class, false, xmlType, javaType); - // Sometimes an Enumeration class is registered as a Bean. - // If this is the case, silently switch to the EnumSerializer - if (JavaUtils.isEnumClass(javaType)) { - serClass = EnumSerializer.class; - } - this.format = format; - } public javax.xml.rpc.encoding.Serializer getSerializerAs(String mechanismType) throws JAXRPCException { - Serializer ser = (Serializer) super.getSerializerAs(mechanismType); - if (ser != null && ser instanceof BeanSerializer) { - ((BeanSerializer)ser).setElementPropertyFormat(format); - } - return ser; + return (Serializer) super.getSerializerAs(mechanismType); } } 1.7 +0 -3 xml-axis/java/src/org/apache/axis/encoding/ser/SimpleDeserializer.java Index: SimpleDeserializer.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/encoding/ser/SimpleDeserializer.java,v retrieving revision 1.6 retrieving revision 1.7 diff -u -r1.6 -r1.7 --- SimpleDeserializer.java 8 Mar 2002 14:12:13 -0000 1.6 +++ SimpleDeserializer.java 9 Mar 2002 19:29:48 -0000 1.7 @@ -271,9 +271,6 @@ continue; String attrName = attributes.getLocalName(i); - String attrNameUp = BeanSerializer.format(attrName, BeanSerializer.FORCE_UPPER); - String attrNameLo = BeanSerializer.format(attrName, BeanSerializer.FORCE_LOWER); - String mangledName = JavaUtils.xmlNameToJava(attrName); // look for the attribute property BeanPropertyDescriptor bpd = 1.16 +54 -20 xml-axis/java/src/org/apache/axis/wsdl/toJava/JavaComplexTypeWriter.java Index: JavaComplexTypeWriter.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/wsdl/toJava/JavaComplexTypeWriter.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- JavaComplexTypeWriter.java 8 Mar 2002 05:04:54 -0000 1.15 +++ JavaComplexTypeWriter.java 9 Mar 2002 19:29:49 -0000 1.16 @@ -57,8 +57,11 @@ import org.apache.axis.utils.JavaUtils; import org.w3c.dom.Node; +import javax.xml.rpc.namespace.QName; import java.io.IOException; import java.util.Vector; +import java.util.HashMap; +import java.util.Iterator; /** * This is Wsdl2java's Complex Type Writer. It writes the <typeName>.java file. @@ -68,6 +71,7 @@ private Vector elements; private Vector attributes; private TypeEntry extendType; + private HashMap elementMappings = null; /** * Constructor. @@ -108,8 +112,20 @@ // We are only interested in the java names of the types, so create a names list Vector names = new Vector(); for (int i = 0; i < elements.size(); i += 2) { - names.add(((TypeEntry) elements.get(i)).getName()); - names.add(Utils.xmlNameToJava((String) elements.get(i + 1))); + TypeEntry type = (TypeEntry) elements.get(i); + String elemName = (String) elements.get(i + 1); + String javaName = Utils.xmlNameToJava(elemName); + if (false && !javaName.equals(elemName)) { + // If we did some mangling, make sure we'll write out the XML + // the correct way. + if (elementMappings == null) + elementMappings = new HashMap(); + + elementMappings.put(Utils.capitalizeFirstChar(javaName), + new QName("", elemName)); + } + names.add(type.getName()); + names.add(javaName); } // add the attributes to the names list (which will be bean elements too) if (attributes != null) { @@ -123,15 +139,6 @@ if (type.isSimpleType()) implementsText = ", org.apache.axis.encoding.SimpleType"; - // For now, do the imports only if we have attributes. This will - // need to happen for any mapping later. - if (attributes != null) { - pw.println("import org.apache.axis.description.FieldDesc;"); - pw.println("import org.apache.axis.description.TypeDesc;"); - pw.println("import org.apache.axis.description.AttributeDesc;"); - pw.println(); - } - pw.println("public class " + className + extendsText + " implements java.io.Serializable" + implementsText + " {"); @@ -239,21 +246,48 @@ // mappings as well, but right now this is just to keep the attribute // mechanism working. - if (attributes != null) { + if (attributes != null || elementMappings != null) { + boolean wroteFieldType = false; pw.println(" // Type metadata"); pw.println(" private static org.apache.axis.description.TypeDesc typeDesc ="); pw.println(" new org.apache.axis.description.TypeDesc();"); pw.println(); pw.println(" static {"); - for (int i = 0; i < attributes.size(); i += 2) { - String fieldName = - Utils.xmlNameToJava((String) attributes.get(i + 1)); - pw.print(" "); - if (i == 0) pw.print("org.apache.axis.description.FieldDesc "); - pw.println("field = new org.apache.axis.description.AttributeDesc();"); - pw.println(" field.setFieldName(\"" + fieldName + "\");"); - pw.println(" typeDesc.addFieldDesc(field);"); + + if (attributes != null) { + for (int i = 0; i < attributes.size(); i += 2) { + String fieldName = + Utils.xmlNameToJava((String) attributes.get(i + 1)); + pw.print(" "); + if (!wroteFieldType) { + pw.print("org.apache.axis.description.FieldDesc "); + wroteFieldType = true; + } + pw.println("field = new org.apache.axis.description.AttributeDesc();"); + pw.println(" field.setFieldName(\"" + fieldName + "\");"); + pw.println(" typeDesc.addFieldDesc(field);"); + } } + + if (elementMappings != null) { + Iterator i = elementMappings.keySet().iterator(); + while (i.hasNext()) { + String fieldName = (String)i.next(); + QName xmlName = (QName)elementMappings.get(fieldName); + pw.print(" "); + if (!wroteFieldType) { + pw.print("org.apache.axis.description.FieldDesc "); + wroteFieldType = true; + } + pw.println("field = new org.apache.axis.description.ElementDesc();"); + pw.println(" field.setFieldName(\"" + fieldName + "\");"); + pw.print( " field.setXmlName(new javax.xml.rpc.namespace.QName(\""); + pw.println(xmlName.getNamespaceURI() + "\", \"" + + xmlName.getLocalPart() + "\"));"); + pw.println(" typeDesc.addFieldDesc(field);"); + } + } + pw.println(" };"); pw.println(); 1.5 +6 -0 xml-axis/java/src/org/apache/axis/description/TypeDesc.java Index: TypeDesc.java =================================================================== RCS file: /home/cvs/xml-axis/java/src/org/apache/axis/description/TypeDesc.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- TypeDesc.java 8 Mar 2002 20:04:45 -0000 1.4 +++ TypeDesc.java 9 Mar 2002 19:29:49 -0000 1.5 @@ -190,6 +190,9 @@ */ public String getFieldNameForElement(QName qname) { + if (fields == null) + return null; + for (int i = 0; i < fields.length; i++) { FieldDesc field = fields[i]; if (field.isElement() && qname.equals(field.getXmlName())) @@ -205,6 +208,9 @@ */ public String getFieldNameForAttribute(QName qname) { + if (fields == null) + return null; + String possibleMatch = null; for (int i = 0; i < fields.length; i++) {