Search in sources :

Example 1 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class ElementReader method getValue.

public String getValue() {
    if (value == null) {
        try {
            if (isXsiNil()) {
                readToEnd();
                return null;
            }
            value = root.getElementText();
            hasCheckedChildren = true;
            hasChildren = false;
            if (root.hasNext()) {
                root.next();
            }
        } catch (XMLStreamException e) {
            throw new DatabindingException("Could not read XML stream.", e);
        }
        if (value == null) {
            value = "";
        }
    }
    return value;
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 2 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class DocumentType method readObject.

@Override
public Object readObject(MessageReader mreader, Context context) throws DatabindingException {
    try {
        XMLStreamReader reader = ((ElementReader) mreader).getXMLStreamReader();
        // we need to eat the surrounding element.
        reader.nextTag();
        Object tree = StaxUtils.read(null, new FragmentStreamReader(reader), true);
        // eat the end tag.
        reader.nextTag();
        return tree;
    } catch (XMLStreamException e) {
        throw new DatabindingException("Could not parse xml.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) FragmentStreamReader(org.apache.cxf.staxutils.FragmentStreamReader) ElementReader(org.apache.cxf.aegis.xml.stax.ElementReader)

Example 3 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException 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 4 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class SoapArrayType method readObject.

@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
    try {
        // get the encoded array type info
        TypeMapping tm = context.getTypeMapping();
        if (tm == null) {
            tm = getTypeMapping();
        }
        ArrayTypeInfo arrayTypeInfo = new ArrayTypeInfo(reader, tm);
        // verify arrayType dimensions are the same as this type class's array dimensions
        if (getDimensions() != arrayTypeInfo.getTotalDimensions()) {
            throw new DatabindingException("In " + getSchemaType() + " expected array with " + getDimensions() + " dimensions, but arrayType has " + arrayTypeInfo.getTotalDimensions() + " dimensions: " + arrayTypeInfo.toString());
        }
        // calculate max size
        int maxSize = 1;
        for (int dimension : arrayTypeInfo.getDimensions()) {
            maxSize *= dimension;
        }
        // verify offset doesn't exceed maximum size
        if (arrayTypeInfo.getOffset() >= maxSize) {
            throw new DatabindingException("The array offset " + arrayTypeInfo.getOffset() + " in " + getSchemaType() + " exceeds the expecte size of " + maxSize);
        }
        // read the values
        List<Object> values = readCollection(reader, context, arrayTypeInfo, maxSize - arrayTypeInfo.getOffset());
        // if it is a partially transmitted array offset the array values
        if (arrayTypeInfo.getOffset() > 0) {
            List<Object> list = new ArrayList<>(values.size() + arrayTypeInfo.getOffset());
            list.addAll(Collections.nCopies(arrayTypeInfo.getOffset(), null));
            list.addAll(values);
            values = list;
        }
        // check bounds
        if (values.size() > maxSize) {
            throw new DatabindingException("The number of elements " + values.size() + " in " + getSchemaType() + " exceeds the expecte size of " + maxSize);
        }
        if (values.size() < maxSize) {
            values.addAll(Collections.nCopies(maxSize - values.size(), null));
        // todo is this an error?
        // throw new DatabindingException("The number of elements in " + getSchemaType() +
        // " is less then the expected size of " + expectedSize);
        }
        if (values.size() != maxSize) {
            throw new IllegalStateException("Internal error: Expected values collection to contain " + maxSize + " elements but it contains " + values.size() + " elements");
        }
        // create the array
        return makeArray(values, arrayTypeInfo.getDimensions(), getTypeClass().getComponentType());
    } catch (IllegalArgumentException e) {
        throw new DatabindingException("Illegal argument.", e);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) ArrayList(java.util.ArrayList) TypeMapping(org.apache.cxf.aegis.type.TypeMapping)

Example 5 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.

the class CollectionType method createCollection.

@Override
@SuppressWarnings("unchecked")
protected Collection<Object> createCollection() {
    final Collection<Object> values;
    /*
         * getTypeClass returns the type of the object. These 'if's asked if the proposed
         * type can be assigned to the object, not the other way around. Thus List before
         * Vector and Set before SortedSet.
         */
    Class<?> userTypeClass = getTypeClass();
    if (userTypeClass.isAssignableFrom(List.class)) {
        values = new ArrayList<>();
    } else if (userTypeClass.isAssignableFrom(LinkedList.class)) {
        values = new LinkedList<>();
    } else if (userTypeClass.isAssignableFrom(Set.class)) {
        values = new HashSet<>();
    } else if (userTypeClass.isAssignableFrom(SortedSet.class)) {
        values = new TreeSet<>();
    } else if (userTypeClass.isAssignableFrom(Vector.class)) {
        // NOPMD
        // NOPMD
        values = new Vector<>();
    } else if (userTypeClass.isAssignableFrom(Stack.class)) {
        values = new Stack<>();
    } else if (userTypeClass.isInterface()) {
        values = new ArrayList<>();
    } else {
        try {
            values = (Collection<Object>) userTypeClass.newInstance();
        } catch (Exception e) {
            throw new DatabindingException("Could not create map implementation: " + userTypeClass.getName(), e);
        }
    }
    return values;
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) TreeSet(java.util.TreeSet) SortedSet(java.util.SortedSet) LinkedList(java.util.LinkedList) DatabindingException(org.apache.cxf.aegis.DatabindingException) Stack(java.util.Stack)

Aggregations

DatabindingException (org.apache.cxf.aegis.DatabindingException)38 AegisType (org.apache.cxf.aegis.type.AegisType)17 QName (javax.xml.namespace.QName)9 MessageReader (org.apache.cxf.aegis.xml.MessageReader)8 XMLStreamException (javax.xml.stream.XMLStreamException)6 PropertyDescriptor (java.beans.PropertyDescriptor)5 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 Element (org.w3c.dom.Element)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 ArrayList (java.util.ArrayList)3 Context (org.apache.cxf.aegis.Context)3 TypeMapping (org.apache.cxf.aegis.type.TypeMapping)3 BeanTypeInfo (org.apache.cxf.aegis.type.basic.BeanTypeInfo)3 ElementReader (org.apache.cxf.aegis.xml.stax.ElementReader)3 Document (org.w3c.dom.Document)3 IntrospectionException (java.beans.IntrospectionException)2 Method (java.lang.reflect.Method)2 Collection (java.util.Collection)2 AegisContext (org.apache.cxf.aegis.AegisContext)2 MessageWriter (org.apache.cxf.aegis.xml.MessageWriter)2