Search in sources :

Example 1 with MessageReader

use of org.apache.cxf.aegis.xml.MessageReader in project qi4j-sdk by Qi4j.

the class ValueCompositeCxfType method readObject.

@Override
public Object readObject(final MessageReader reader, final Context context) throws DatabindingException {
    QName qname = getSchemaType();
    final String className = (qname.getNamespaceURI() + "." + qname.getLocalPart()).substring(20);
    // Read attributes
    ValueDescriptor descriptor = module.valueDescriptor(className);
    StateDescriptor stateDescriptor = descriptor.state();
    final Map<QualifiedName, Object> values = new HashMap<>();
    while (reader.hasMoreElementReaders()) {
        MessageReader childReader = reader.getNextElementReader();
        QName childName = childReader.getName();
        QualifiedName childQualifiedName = QualifiedName.fromClass((Class) typeClass, childName.getLocalPart());
        PropertyDescriptor propertyDescriptor = stateDescriptor.findPropertyModelByQualifiedName(childQualifiedName);
        Type propertyType = propertyDescriptor.type();
        AegisType type = getTypeMapping().getType(propertyType);
        Object value = type.readObject(childReader, context);
        values.put(childQualifiedName, value);
    }
    ValueBuilder<?> builder = module.newValueBuilderWithState((Class<?>) typeClass, new Function<PropertyDescriptor, Object>() {

        @Override
        public Object map(PropertyDescriptor descriptor1) {
            return values.get(descriptor1.qualifiedName());
        }
    }, new Function<AssociationDescriptor, EntityReference>() {

        @Override
        public EntityReference map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return null;
            }
            return EntityReference.parseEntityReference(value.toString());
        }
    }, new Function<AssociationDescriptor, Iterable<EntityReference>>() {

        @Override
        public Iterable<EntityReference> map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return Iterables.empty();
            }
            String[] ids = value.toString().split(",");
            List<EntityReference> references = new ArrayList<>(ids.length);
            for (String id : ids) {
                references.add(EntityReference.parseEntityReference(id));
            }
            return references;
        }
    }, new Function<AssociationDescriptor, Map<String, EntityReference>>() {

        @Override
        public Map<String, EntityReference> map(AssociationDescriptor descriptor) {
            Object value = values.get(descriptor.qualifiedName());
            if (value == null) {
                return Collections.emptyMap();
            }
            String[] namedRefs = value.toString().split(",");
            Map<String, EntityReference> references = new HashMap<>(namedRefs.length);
            for (String namedRef : namedRefs) {
                String[] splitted = namedRef.split(":");
                references.put(splitted[0], EntityReference.parseEntityReference(splitted[1]));
            }
            return references;
        }
    });
    return builder.newInstance();
}
Also used : HashMap(java.util.HashMap) ValueDescriptor(org.qi4j.api.value.ValueDescriptor) MessageReader(org.apache.cxf.aegis.xml.MessageReader) AssociationDescriptor(org.qi4j.api.association.AssociationDescriptor) EntityReference(org.qi4j.api.entity.EntityReference) ArrayList(java.util.ArrayList) List(java.util.List) PropertyDescriptor(org.qi4j.api.property.PropertyDescriptor) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) QualifiedName(org.qi4j.api.common.QualifiedName) CollectionType(org.apache.cxf.aegis.type.collection.CollectionType) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) MapType(org.apache.cxf.aegis.type.collection.MapType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AegisType(org.apache.cxf.aegis.type.AegisType) StateDescriptor(org.qi4j.api.composite.StateDescriptor) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 QName (javax.xml.namespace.QName)1 AegisType (org.apache.cxf.aegis.type.AegisType)1 CollectionType (org.apache.cxf.aegis.type.collection.CollectionType)1 MapType (org.apache.cxf.aegis.type.collection.MapType)1 MessageReader (org.apache.cxf.aegis.xml.MessageReader)1 XmlSchemaComplexType (org.apache.ws.commons.schema.XmlSchemaComplexType)1 AssociationDescriptor (org.qi4j.api.association.AssociationDescriptor)1 QualifiedName (org.qi4j.api.common.QualifiedName)1 StateDescriptor (org.qi4j.api.composite.StateDescriptor)1 EntityReference (org.qi4j.api.entity.EntityReference)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 ValueDescriptor (org.qi4j.api.value.ValueDescriptor)1