public void populateProperties()

in impl/src/main/java/org/apache/tuscany/sdo/util/resource/DataObjectXMLStreamReader.java [261:334]


    public void populateProperties() {
        /*declaredNamespaceMap.put("xml", "http://www.w3.org/XML/1998/namespace");
        declaredNamespaceMap.put("xmlns", "http://www.w3.org/2000/xmlns/");
        declaredNamespaceMap.put("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        */
        
        if (properties != null)
            return;
        if (elementQName == null)
            elementQName = namespaceContext.createQName(this.rootElementURI, this.rootElementName);
        else
            elementQName = namespaceContext.createQName(elementQName.getNamespaceURI(), elementQName.getLocalPart());

        List elementList = new ArrayList();
        List attributeList = new ArrayList();
        if (dataObject == null) {
            return;
        }
        Type type = dataObject.getType();
        
        // Add xsi:type if rootElement doesn't exist or the type is different
        if (rootElement == null || (rootElement != null && rootElement.getType() != type)) {
            // FIXME: XSDHelper.getLocalName() for annoymous type returns null?
            String typeName = xsdHelper.getLocalName(type);
            if (typeName != null) {
                QName realTypeName = namespaceContext.createQName(type.getURI(), typeName);
                String typeQName = realTypeName.getPrefix() + ":" + realTypeName.getLocalPart();
                registerNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                registerNamespace(realTypeName.getPrefix(), realTypeName.getNamespaceURI());
                attributeList.add(new NameValuePair(XSI_TYPE_QNAME, typeQName));
            }
        }
        
        if (type.isSequenced()) {
            Sequence sequence = dataObject.getSequence();
            for (int i = 0; i < sequence.size(); i++) {
                Property property = sequence.getProperty(i);
                Object value = sequence.getValue(i);
                if (property == null) {
                    // property == null for text in mixed content
                    elementList.add(new NameValuePair(ELEMENT_TEXT, value));
                } else {
                    addProperty(property, value, elementList);
                }
            }
            // Attributes are not in the sequence
            List properties = dataObject.getInstanceProperties();
            for (Iterator i = properties.iterator(); i.hasNext();) {
                Property property = (Property) i.next();
                if (xsdHelper.isAttribute(property)) {
                    // FIXME: How to handle nilable=true?
                    if (!dataObject.isSet(property))
                        continue;
                    Object value = dataObject.get(property);
                    addProperty(attributeList, property, value, type);
                }
            }
        } else {
            List properties = dataObject.getInstanceProperties();
            for (Iterator i = properties.iterator(); i.hasNext();) {
                Property property = (Property) i.next();
                // FIXME: How to handle nilable=true?
                if (!dataObject.isSet(property))
                    continue;
                Object value = dataObject.get(property);
                if (xsdHelper.isAttribute(property))
                    addProperty(attributeList, property, value, type);
                else
                    addProperty(elementList, property, value, type);
            }
        }
        properties = (Map.Entry[]) elementList.toArray(new Map.Entry[0]);
        attributes = (Map.Entry[]) attributeList.toArray(new Map.Entry[0]);
    }