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);
}
}
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);
}
}
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;
}
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);
}
}
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);
}
}
Aggregations