Search in sources :

Example 16 with DatabindingException

use of org.apache.cxf.aegis.DatabindingException 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 17 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 18 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 19 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 20 with DatabindingException

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

the class SourceType method write.

protected void write(Source object, XMLStreamWriter writer) throws FactoryConfigurationError, XMLStreamException, DatabindingException {
    if (object == null) {
        return;
    }
    if (object instanceof DOMSource) {
        DOMSource ds = (DOMSource) object;
        Element element = null;
        if (ds.getNode() instanceof Element) {
            element = (Element) ds.getNode();
        } else if (ds.getNode() instanceof Document) {
            element = ((Document) ds.getNode()).getDocumentElement();
        } else {
            throw new DatabindingException("Node type " + ds.getNode().getClass() + " was not understood.");
        }
        StaxUtils.writeElement(element, writer, false);
    } else {
        StaxUtils.copy(object, writer);
    }
}
Also used : DatabindingException(org.apache.cxf.aegis.DatabindingException) DOMSource(javax.xml.transform.dom.DOMSource) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

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