Search in sources :

Example 21 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class SoapArrayType method readCollection.

protected List<Object> readCollection(MessageReader reader, Context context, ArrayTypeInfo arrayTypeInfo, int maxSize) throws DatabindingException {
    List<Object> values = new ArrayList<>();
    Boolean sparse = null;
    while (reader.hasMoreElementReaders()) {
        MessageReader creader = reader.getNextElementReader();
        // if the first element contains a position attribute, this is a sparse array
        // and all subsequent elements must contain the position attribute
        String position = readAttributeValue(creader, SOAP_ARRAY_POSITION);
        if (sparse == null) {
            sparse = position != null;
        }
        // nested element names can specify a type
        AegisType compType = getTypeMapping().getType(creader.getName());
        if (compType == null) {
            // use the type declared in the arrayType attribute
            compType = arrayTypeInfo.getType();
        }
        // check for an xsi:type override
        compType = TypeUtil.getReadType(creader.getXMLStreamReader(), context.getGlobalContext(), compType);
        // wrap type with soap ref to handle hrefs
        compType = new SoapRefType(compType);
        // read the value
        Object value;
        if (creader.isXsiNil()) {
            value = null;
            creader.readToEnd();
        } else {
            value = compType.readObject(creader, context);
        }
        // add the value
        if (!sparse) {
            if (values.size() + 1 > maxSize) {
                throw new DatabindingException("The number of elements in " + getSchemaType() + " exceeds the maximum size of " + maxSize);
            }
            values.add(value);
        } else {
            int valuesPosition = readValuesPosition(position, arrayTypeInfo.getDimensions());
            if (valuesPosition > maxSize) {
                throw new DatabindingException("Array position " + valuesPosition + " in " + getSchemaType() + " exceeds the maximum size of " + maxSize);
            }
            if (values.size() <= valuesPosition) {
                values.addAll(Collections.nCopies(valuesPosition - values.size() + 1, null));
            }
            Object oldValue = values.set(valuesPosition, value);
            if (oldValue != null) {
                throw new DatabindingException("Array position " + valuesPosition + " in " + getSchemaType() + " is already assigned to value " + oldValue);
            }
        }
    }
    return values;
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) ArrayList(java.util.ArrayList) MessageReader(org.apache.cxf.aegis.xml.MessageReader)

Example 22 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class MapType method writeSchema.

@Override
public void writeSchema(XmlSchema root) {
    XmlSchemaComplexType complex = new XmlSchemaComplexType(root, true);
    complex.setName(getSchemaType().getLocalPart());
    XmlSchemaSequence sequence = new XmlSchemaSequence();
    complex.setParticle(sequence);
    AegisType kType = getKeyType();
    AegisType vType = getValueType();
    XmlSchemaElement element = new XmlSchemaElement(root, false);
    sequence.getItems().add(element);
    element.setName(getEntryName().getLocalPart());
    element.setMinOccurs(0);
    element.setMaxOccurs(Long.MAX_VALUE);
    XmlSchemaComplexType evType = new XmlSchemaComplexType(root, false);
    element.setType(evType);
    XmlSchemaSequence evSequence = new XmlSchemaSequence();
    evType.setParticle(evSequence);
    createElement(root, evSequence, getKeyName(), kType, false);
    createElement(root, evSequence, getValueName(), vType, true);
}
Also used : XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) AegisType(org.apache.cxf.aegis.type.AegisType) XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType)

Example 23 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class MapType method writeObject.

@Override
public void writeObject(Object object, MessageWriter writer, Context context) throws DatabindingException {
    if (object == null) {
        return;
    }
    try {
        Map<?, ?> map = (Map<?, ?>) object;
        AegisType kType = getKeyType();
        AegisType vType = getValueType();
        for (Iterator<?> itr = map.entrySet().iterator(); itr.hasNext(); ) {
            Map.Entry<?, ?> entry = (Map.Entry<?, ?>) itr.next();
            writeEntry(writer, context, kType, vType, entry);
        }
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) NavigableMap(java.util.NavigableMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) TreeMap(java.util.TreeMap) Map(java.util.Map) SortedMap(java.util.SortedMap)

Example 24 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class StructType method getElementType.

/**
 * Returns a SoapRefType wrapping the actual type.
 */
@Override
protected AegisType getElementType(QName name, BeanTypeInfo beanTypeInfo, MessageReader reader, Context context) {
    // nested elements use unqualified names
    name = qualifyName(name);
    AegisType type = super.getElementType(name, beanTypeInfo, reader, context);
    if (type != null) {
        type = new SoapRefType(type);
    }
    return type;
}
Also used : AegisType(org.apache.cxf.aegis.type.AegisType)

Example 25 with AegisType

use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.

the class XMLBeanTypeInfo method mapProperty.

@Override
protected void mapProperty(PropertyDescriptor pd) {
    Element e = getPropertyElement(pd.getName());
    String style = null;
    QName mappedName = null;
    if (e != null) {
        String ignore = DOMUtils.getAttributeValueEmptyNull(e, "ignore");
        if ("true".equals(ignore)) {
            return;
        }
        LOG.finest("Found mapping for property " + pd.getName());
        style = DOMUtils.getAttributeValueEmptyNull(e, "style");
    }
    if (style == null) {
        style = "element";
    }
    boolean element = "element".equals(style);
    boolean qualify;
    if (element) {
        qualify = isQualifyElements();
    } else {
        qualify = isQualifyAttributes();
    }
    String namespace = null;
    if (qualify) {
        namespace = getDefaultNamespace();
    }
    if (e != null) {
        mappedName = NamespaceHelper.createQName(e, DOMUtils.getAttributeValueEmptyNull(e, "mappedName"), namespace);
    }
    if (mappedName == null) {
        mappedName = createMappedName(pd, qualify);
    }
    if (e != null) {
        QName mappedType = NamespaceHelper.createQName(e, DOMUtils.getAttributeValueEmptyNull(e, "typeName"), getDefaultNamespace());
        if (mappedType != null) {
            mapTypeName(mappedName, mappedType);
        }
        /*
             * Whenever we create a type object, it has to have a schema type. If we created a custom type
             * object out of thin air here, we've may have a problem. If "typeName" was specified, then then
             * we know the mapping. But if mappedName was not specified, then the typeName will come from the
             * type mapping, so we have to ask it. And if some other type creator has something to say about
             * it, we'll get it wrong.
             */
        String explicitTypeName = DOMUtils.getAttributeValueEmptyNull(e, "type");
        if (explicitTypeName != null) {
            try {
                Class<?> typeClass = ClassLoaderUtils.loadClass(explicitTypeName, XMLBeanTypeInfo.class);
                AegisType customTypeObject = (AegisType) typeClass.newInstance();
                mapType(mappedName, customTypeObject);
                QName schemaType = mappedType;
                if (schemaType == null) {
                    schemaType = getTypeMapping().getTypeQName(pd.getPropertyType());
                }
                customTypeObject.setTypeClass(typeClass);
                customTypeObject.setSchemaType(schemaType);
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e1) {
            // 
            }
        }
        String nillableVal = DOMUtils.getAttributeValueEmptyNull(e, "nillable");
        if (nillableVal != null && nillableVal.length() > 0) {
            ensurePropertyInfo(mappedName).setNillable(Boolean.parseBoolean(nillableVal));
        }
        String minOccurs = DOMUtils.getAttributeValueEmptyNull(e, "minOccurs");
        if (minOccurs != null && minOccurs.length() > 0) {
            ensurePropertyInfo(mappedName).setMinOccurs(Integer.parseInt(minOccurs));
        }
        String maxOccurs = DOMUtils.getAttributeValueEmptyNull(e, "maxOccurs");
        if (maxOccurs != null && maxOccurs.length() > 0) {
            ensurePropertyInfo(mappedName).setMinOccurs(Integer.parseInt(maxOccurs));
        }
    }
    try {
        // name " + mappedName);
        if ("element".equals(style)) {
            mapElement(pd.getName(), mappedName);
        } else if ("attribute".equals(style)) {
            mapAttribute(pd.getName(), mappedName);
        } else {
            throw new DatabindingException("Invalid style: " + style);
        }
    } catch (DatabindingException ex) {
        ex.prepend("Couldn't create type for property " + pd.getName() + " on " + getTypeClass());
        throw ex;
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) AegisType(org.apache.cxf.aegis.type.AegisType) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element)

Aggregations

AegisType (org.apache.cxf.aegis.type.AegisType)97 QName (javax.xml.namespace.QName)43 Test (org.junit.Test)40 AbstractAegisTest (org.apache.cxf.aegis.AbstractAegisTest)35 DatabindingException (org.apache.cxf.aegis.DatabindingException)18 AegisContext (org.apache.cxf.aegis.AegisContext)16 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)11 Element (org.w3c.dom.Element)11 Context (org.apache.cxf.aegis.Context)10 BeanType (org.apache.cxf.aegis.type.basic.BeanType)10 Method (java.lang.reflect.Method)9 MessageReader (org.apache.cxf.aegis.xml.MessageReader)9 TypeCreationOptions (org.apache.cxf.aegis.type.TypeCreationOptions)8 BeanTypeInfo (org.apache.cxf.aegis.type.basic.BeanTypeInfo)8 CollectionType (org.apache.cxf.aegis.type.collection.CollectionType)8 HashSet (java.util.HashSet)7 MapType (org.apache.cxf.aegis.type.collection.MapType)7 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)7 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)7 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)5