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;
}
use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.
the class ElementWriter method getPrefixForNamespace.
public String getPrefixForNamespace(String ns, String hint) {
try {
String pfx = writer.getPrefix(ns);
String contextPfx = writer.getNamespaceContext().getPrefix(ns);
if (pfx == null) {
String ns2 = writer.getNamespaceContext().getNamespaceURI(hint);
// if the hint is "" (the default) and the context does
if (ns2 == null && !"".equals(hint)) {
pfx = hint;
} else if (ns.equals(ns2)) {
// just because it's in the context, doesn't mean it has been written.
pfx = hint;
} else if (contextPfx != null) {
pfx = contextPfx;
} else {
pfx = StaxUtils.getUniquePrefix(writer);
}
writer.setPrefix(pfx, ns);
writer.writeNamespace(pfx, ns);
}
return pfx;
} catch (XMLStreamException e) {
throw new DatabindingException("Error writing document.", e);
}
}
use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.
the class NoDefaultConstructorBeanType method readObject.
@Override
public Object readObject(MessageReader reader, Context context) throws DatabindingException {
BeanTypeInfo inf = getTypeInfo();
try {
String id = null;
String name = null;
// Read child elements
while (reader.hasMoreElementReaders()) {
MessageReader childReader = reader.getNextElementReader();
if (childReader.isXsiNil()) {
childReader.readToEnd();
continue;
}
QName qName = childReader.getName();
AegisType defaultType = inf.getType(qName);
AegisType type = TypeUtil.getReadType(childReader.getXMLStreamReader(), context.getGlobalContext(), defaultType);
if (type != null) {
String value = (String) type.readObject(childReader, context);
if ("id".equals(qName.getLocalPart())) {
id = value;
} else if ("name".equals(qName.getLocalPart())) {
name = value;
}
} else {
childReader.readToEnd();
}
}
return new NoDefaultConstructorBeanImpl(id, name);
} catch (IllegalArgumentException e) {
throw new DatabindingException("Illegal argument. " + e.getMessage(), e);
}
}
use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.
the class ElementDataWriter method write.
public void write(Object obj, MessagePartInfo part, Element output) {
AegisType type = databinding.getType(part);
if (type == null) {
throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG));
}
Context context = new Context(databinding.getAegisContext());
context.setAttachments(attachments);
type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
try {
W3CDOMStreamWriter domWriter = new W3CDOMStreamWriter(output);
ElementWriter writer = new ElementWriter(domWriter);
MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
if (type.isNillable() && type.isWriteOuter() && obj == null) {
w2.writeXsiNil();
w2.close();
return;
}
type.writeObject(obj, w2, context);
w2.close();
} catch (DatabindingException e) {
throw new RuntimeException(e);
}
}
use of org.apache.cxf.aegis.DatabindingException in project cxf by apache.
the class XMLStreamDataWriter method write.
public void write(Object obj, MessagePartInfo part, XMLStreamWriter output) {
AegisType type = databinding.getType(part);
if (type == null) {
type = databinding.getTypeFromClass(obj.getClass());
}
if (type == null) {
throw new Fault(new Message("NO_MESSAGE_FOR_PART", LOG, part));
}
Context context = new Context(databinding.getAegisContext());
context.setAttachments(attachments);
type = TypeUtil.getWriteType(databinding.getAegisContext(), obj, type);
/*
* We arrive here with a 'type' of the inner type if isWriteOuter is null.
* However, in that case, the original type is available.
*/
AegisType outerType = null;
if (part != null) {
outerType = part.getProperty("org.apache.cxf.aegis.outerType", AegisType.class);
}
try {
if (obj == null) {
if (part.getXmlSchema() instanceof XmlSchemaElement && ((XmlSchemaElement) part.getXmlSchema()).getMinOccurs() == 0) {
// skip writing minOccurs=0 stuff if obj is null
return;
} else if (type.isNillable()) {
ElementWriter writer = new ElementWriter(output);
MessageWriter w2 = writer.getElementWriter(part.getConcreteName());
w2.writeXsiNil();
w2.close();
return;
}
}
ElementWriter writer = new ElementWriter(output);
// outerType is only != null for a flat array.
if (outerType == null) {
MessageWriter w2 = writer.getElementWriter(part != null ? part.getConcreteName() : type.getSchemaType());
type.writeObject(obj, w2, context);
w2.close();
} else {
// it has better be an array (!)
ArrayType aType = (ArrayType) outerType;
// the part has to have a name or we can't do this.
aType.writeObject(obj, writer, context, part.getConcreteName());
}
} catch (DatabindingException e) {
throw new RuntimeException(e);
}
}
Aggregations