⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 beanserializer.java

📁 Java有关XML编程需要用到axis 的源代码 把里面bin下的包导入相应的Java工程 进行使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            } else {                superPd = BeanUtils.getPd(superClass, null);            }        } else {            e = complexType;        }        // Add fields under sequence element.        // 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        // an all element.  This is the reason why a sequence        // element is used.        Element all = types.createElement("sequence");        e.appendChild(all);        if (Modifier.isAbstract(javaType.getModifiers())) {            complexType.setAttribute("abstract", "true");        }        // Serialize each property        for (int i=0; i<propertyDescriptor.length; i++) {            String propName = propertyDescriptor[i].getName();            // Don't serializer properties named class            boolean writeProperty = true;            if (propName.equals("class")) {                writeProperty = false;            }            // Don't serialize the property if it is present            // in the super class property list            if (superPd != null && writeProperty) {                for (int j=0; j<superPd.length && writeProperty; j++) {                    if (propName.equals(superPd[j].getName())) {                        writeProperty = false;                    }                }            }            if (!writeProperty) {                continue;            }            // 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) {                Class fieldType = propertyDescriptor[i].getType();                FieldDesc field = typeDesc.getFieldByName(propName);                if (field != null) {                    QName qname = field.getXmlName();                    QName fieldXmlType = field.getXmlType();                    boolean isAnonymous = fieldXmlType != null && fieldXmlType.getLocalPart().startsWith(">");                    if (qname != null) {                        // FIXME!                        // Check to see if this is in the right namespace -                        // if it's not, we need to use an <element ref="">                        // to represent it!!!                        // Use the default...                        propName = qname.getLocalPart();                    }                    if (!field.isElement()) {                        writeAttribute(types,                                       propName,                                       fieldType,                                       fieldXmlType,                                       complexType);                    } else {                        writeField(types,                                   propName,                                   fieldXmlType,                                   fieldType,                                   propertyDescriptor[i].isIndexed(),                                   field.isMinOccursZero(),                                   all, isAnonymous,                                   ((ElementDesc)field).getItemQName());                    }                } else {                    writeField(types,                               propName,                               null,                               fieldType,                               propertyDescriptor[i].isIndexed(), false, all, false, null);                }            } else {                boolean done = false;                if (propertyDescriptor[i] instanceof FieldPropertyDescriptor){                    FieldPropertyDescriptor fpd = (FieldPropertyDescriptor) propertyDescriptor[i];                    Class clazz = fpd.getField().getType();                    if(types.getTypeQName(clazz)!=null) {                        writeField(types,                                   propName,                                   null,                                   clazz,                                   false, false, all, false, null);                                           done = true;                    }                }                if(!done) {                    writeField(types,                               propName,                               null,                               propertyDescriptor[i].getType(),                               propertyDescriptor[i].isIndexed(), false, all, false, null);                }                                                }        }        // done        return complexType;    }    /**     * 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 xmlType the schema type of the field     * @param fieldType type of the field     * @param isUnbounded causes maxOccurs="unbounded" if set     * @param where location for the generated schema node     * @param itemQName     * @throws Exception     */    protected void writeField(Types types,                              String fieldName,                              QName xmlType,                              Class fieldType,                              boolean isUnbounded,                              boolean isOmittable,                              Element where,                              boolean isAnonymous,                              QName itemQName) throws Exception {        Element elem;        String elementType = null;        if (isAnonymous) {            elem = types.                    createElementWithAnonymousType(fieldName,                                                   fieldType,                                                   isOmittable,                                                   where.getOwnerDocument());        } else {            if (!SchemaUtils.isSimpleSchemaType(xmlType) &&                    Types.isArray(fieldType)) {                xmlType = null;            }            if (itemQName != null &&                    SchemaUtils.isSimpleSchemaType(xmlType) &&                    Types.isArray(fieldType)) {                xmlType = null;            }            QName typeQName = types.writeTypeAndSubTypeForPart(fieldType, xmlType);            elementType = types.getQNameString(typeQName);            if (elementType == null) {                // If writeType returns null, then emit an anytype.                QName anyQN = Constants.XSD_ANYTYPE;                String prefix = types.getNamespaces().                        getCreatePrefix(anyQN.getNamespaceURI());                elementType = prefix + ":" + anyQN.getLocalPart();            }            // isNillable default value depends on the field type            boolean isNillable = Types.isNullable(fieldType);            if (typeDesc != null) {                FieldDesc field = typeDesc.getFieldByName(fieldName);                if (field != null && field.isElement()) {                    isNillable = ((ElementDesc)field).isNillable();                }            }            elem = types.createElement(fieldName,                    elementType,                    isNillable,                    isOmittable,                    where.getOwnerDocument());        }        if (isUnbounded) {            elem.setAttribute("maxOccurs", "unbounded");        }        where.appendChild(elem);    }    /**     * write aa attribute element and append it to the 'where' Node     * @param fieldName name of the field     * @param fieldType type of the field     * @param where location for the generated schema node     * @throws Exception     */    protected void writeAttribute(Types types,                                String fieldName,                                Class fieldType,                                QName fieldXmlType,                                Element where) throws Exception {        // Attribute must be a simple type.        if (!types.isAcceptableAsAttribute(fieldType)) {            throw new AxisFault(Messages.getMessage("AttrNotSimpleType00",                                                     fieldName,                                                     fieldType.getName()));        }        Element elem = types.createAttributeElement(fieldName,                                           fieldType, fieldXmlType,                                           false,                                           where.getOwnerDocument());        where.appendChild(elem);    }    /**     * 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     * @return attributes for this element, null if none     */    protected Attributes getObjectAttributes(Object value,                                           Attributes attributes,                                           SerializationContext context) {        if (typeDesc == null || !typeDesc.hasAttributes())            return attributes;        AttributesImpl attrs;        if (attributes == null) {            attrs = new AttributesImpl();        } else if (attributes instanceof AttributesImpl) {            attrs = (AttributesImpl)attributes;        } else {            attrs = new AttributesImpl(attributes);        }        try {            // 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("", propName);                }                if (propertyDescriptor[i].isReadable() &&                    !propertyDescriptor[i].isIndexed()) {                    // add to our attributes                    Object propValue = propertyDescriptor[i].get(value);                    // Convert true/false to 1/0 in case of soapenv:mustUnderstand                    if (qname.equals(MUST_UNDERSTAND_QNAME)) {                    	if (propValue.equals(Boolean.TRUE)) {                                propValue = "1";                    	} else if (propValue.equals(Boolean.FALSE)) {                    		propValue = "0";                    	}                    }                    // 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                    // serialize if the attribute matches the default value.                    if (propValue != null) {                        setAttributeProperty(propValue,                                             qname,                                             field.getXmlType(),                                              field.getJavaType(),                                             attrs,                                             context);                    }                }            }        } catch (Exception e) {            // no attributes            return attrs;        }        return attrs;    }    private void setAttributeProperty(Object propValue,                                      QName qname,                                      QName xmlType,                                      Class javaType,                                      AttributesImpl attrs,                                      SerializationContext context) throws Exception {        String namespace = qname.getNamespaceURI();        String localName = qname.getLocalPart();        // org.xml.sax.helpers.AttributesImpl JavaDoc says: "For the        // sake of speed, this method does no checking to see if the        // attribute is already in the list: that is the        // responsibility of the application." check for the existence        // of the attribute to avoid adding it more than once.        if (attrs.getIndex(namespace, localName) != -1) {            return;        }        String propString = context.getValueAsString(propValue, xmlType, javaType);        attrs.addAttribute(namespace,                           localName,                           context.attributeQName2String(qname),                           "CDATA",                           propString);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -