public void serialize()

in axis-rt-core/src/main/java/org/apache/axis/encoding/ser/BeanSerializer.java [103:285]


    public void serialize(QName name, Attributes attributes,
                          Object value, SerializationContext context)
        throws IOException
    {
        // 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
        Attributes beanAttrs = getObjectAttributes(value, attributes, context);

        // Get the encoding style
        boolean isEncoded = context.isEncoded();

        if (log.isDebugEnabled()) {
            log.debug("Start serializing bean; xmlType=" + xmlType + "; javaType=" + javaType
                    + "; name=" + name + "; isEncoded=" + isEncoded);
        }
        
        // check whether we have and xsd:any namespace="##any" type
        boolean suppressElement = !isEncoded &&
                                  name.getNamespaceURI().equals("") &&
                                  name.getLocalPart().equals("any");

        if (!suppressElement)
            context.startElement(name, beanAttrs);

        // check whether the array is converted to ArrayOfT shema type    
        if (value != null && value.getClass().isArray()) {
           Object newVal = JavaUtils.convert(value, javaType); 
           if (newVal != null && javaType.isAssignableFrom(newVal.getClass())) {
               value = newVal; 
           }
        }
        try {
            // Serialize each property
            for (int i=0; i<propertyDescriptor.length; i++) {
                String propName = propertyDescriptor[i].getName();
                if (propName.equals("class"))
                    continue;
                QName qname = null;
                QName xmlType = null;
                Class javaType = propertyDescriptor[i].getType();

                boolean isOmittable = false;
                // isNillable default value depends on the field type
                boolean isNillable = Types.isNullable(javaType);
                // isArray
                boolean isArray = false;
                QName itemQName = 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;
                        }

                        ElementDesc element = (ElementDesc)field;

                        // If we're SOAP encoded, just use the local part,
                        // not the namespace.  Otherwise use the whole
                        // QName.
                        if (isEncoded) {
                            qname = new QName(element.getXmlName().getLocalPart());
                        } else {
                            qname = element.getXmlName();
                        }
                        isOmittable = element.isMinOccursZero();
                        isNillable = element.isNillable();
                        isArray = element.isMaxOccursUnbounded();
                        xmlType = element.getXmlType();
                        itemQName = element.getItemQName();
                        context.setItemQName(itemQName);
                    }
                }

                if (qname == null) {
                    qname = new QName(isEncoded ? "" : name.getNamespaceURI(),
                                      propName);
                }

                if (xmlType == null) {
                    // look up the type QName using the class
                    xmlType = context.getQNameForClass(javaType);
                }

                // Read the value from the property
                if (propertyDescriptor[i].isReadable()) {
                    if (itemQName != null ||
                            (!propertyDescriptor[i].isIndexed() && !isArray)) {
                        // Normal case: serialize the value
                        Object propValue =
                            propertyDescriptor[i].get(value);


                        if (propValue == null) {
                            // an element cannot be null if nillable property is set to
                            // "false" and the element cannot be omitted
                            if (!isNillable && !isOmittable) {
                                if (Number.class.isAssignableFrom(javaType)) {
                                    // If we have a null and it's a number, though,
                                    // we might turn it into the appropriate kind of 0.
                                    // TODO : Should be caching these constructors?
                                    try {
                                        Constructor constructor =
                                                javaType.getConstructor(
                                                        SimpleDeserializer.STRING_CLASS);
                                        propValue = constructor.newInstance(ZERO_ARGS);
                                    } catch (Exception e) {
                                        // If anything goes wrong here, oh well we tried.
                                    }
                                }

                                if (propValue == null) {
                                    throw new IOException(
                                            Messages.getMessage(
                                                    "nullNonNillableElement",
                                                    propName));
                                }
                            }

                            // if meta data says minOccurs=0, then we can skip
                            // it if its value is null and we aren't doing SOAP
                            // encoding.
                            if (isOmittable && !isEncoded) {
                                continue;
                            }
                        }

                        context.serialize(qname,
                                          null,
                                          propValue,
                                          xmlType, javaType);
                    } else {
                        // Collection of properties: serialize each one
                        int j=0;
                        while(j >= 0) {
                            Object propValue = null;
                            try {
                                propValue =
                                    propertyDescriptor[i].get(value, j);
                                j++;
                            } catch (Exception e) {
                                j = -1;
                            }
                            if (j >= 0) {
                                context.serialize(qname, null,
                                                  propValue, xmlType, propertyDescriptor[i].getType());
                            }
                        }
                    }
                }
            }

            BeanPropertyDescriptor anyDesc = typeDesc == null ? null :
                    typeDesc.getAnyDesc();
            if (anyDesc != null) {
                // If we have "extra" content here, it'll be an array
                // of MessageElements.  Serialize each one.
                Object anyVal = anyDesc.get(value);
                if (anyVal != null && anyVal instanceof MessageElement[]) {
                    MessageElement [] anyContent = (MessageElement[])anyVal;
                    for (int i = 0; i < anyContent.length; i++) {
                        MessageElement element = anyContent[i];
                        element.output(context);
                    }
                }
            }
        } catch (InvocationTargetException ite) {
            Throwable target = ite.getTargetException();
            log.error(Messages.getMessage("exception00"), target);
            throw new IOException(target.toString());
        } catch (Exception e) {
            log.error(Messages.getMessage("exception00"), e);
            throw new IOException(e.toString());
        }

        if (!suppressElement)
            context.endElement();
    }