use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.
the class BeanType method getDependencies.
/**
* {@inheritDoc}
*/
@Override
public Set<AegisType> getDependencies() {
Set<AegisType> deps = new HashSet<>();
BeanTypeInfo inf = getTypeInfo();
for (QName name : inf.getAttributes()) {
if (isInheritedProperty(inf, name)) {
continue;
}
deps.add(inf.getType(name));
}
for (QName name : inf.getElements()) {
if (isInheritedProperty(inf, name)) {
continue;
}
deps.add(inf.getType(name));
}
/*
* Automagically add chain of superclasses if this is an an extension.
*/
AegisType sooperType = getSuperType();
if (sooperType != null) {
deps.add(sooperType);
}
return deps;
}
use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.
the class BeanType method writeSchema.
@Override
public void writeSchema(XmlSchema root) {
BeanTypeInfo inf = getTypeInfo();
XmlSchemaComplexType complex = new XmlSchemaComplexType(root, true);
complex.setName(getSchemaType().getLocalPart());
AegisType sooperType = getSuperType();
/*
* See Java Virtual Machine specification:
* http://java.sun.com/docs/books/vmspec/2nd-edition/html/ClassFile.doc.html#75734
*/
if (((inf.getTypeClass().getModifiers() & Modifier.ABSTRACT) != 0) && !inf.getTypeClass().isInterface()) {
complex.setAbstract(true);
}
XmlSchemaSequence sequence = new XmlSchemaSequence();
/*
* Decide if we're going to extend another type. If we are going to defer, then make sure that we
* extend the type for our superclass.
*/
boolean isExtension = inf.isExtension();
if (isExtension && sooperType != null) {
// if sooperType is null, things are confused.
XmlSchemaComplexContent content = new XmlSchemaComplexContent();
complex.setContentModel(content);
XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension();
content.setContent(extension);
extension.setBaseTypeName(sooperType.getSchemaType());
extension.setParticle(sequence);
} else {
complex.setParticle(sequence);
}
boolean needXmime = false;
boolean needUtilityTypes = false;
// Write out schema for elements
for (QName name : inf.getElements()) {
if (isInheritedProperty(inf, name)) {
continue;
}
XmlSchemaElement element = new XmlSchemaElement(root, false);
element.setName(name.getLocalPart());
sequence.getItems().add(element);
AegisType type = getType(inf, name);
if (type.isFlatArray()) {
// ok, we need some tricks here
element.setMinOccurs(type.getMinOccurs());
element.setMaxOccurs(type.getMaxOccurs());
// for now, assume ArrayType. Look at lists or more general solutions later.
ArrayType aType = (ArrayType) type;
type = aType.getComponentType();
element.setNillable(type.isNillable());
} else {
if (AbstractTypeCreator.HTTP_CXF_APACHE_ORG_ARRAYS.equals(type.getSchemaType().getNamespaceURI())) {
XmlSchemaUtils.addImportIfNeeded(root, AbstractTypeCreator.HTTP_CXF_APACHE_ORG_ARRAYS);
}
}
writeTypeReference(name, element, type, root);
needXmime |= type.usesXmime();
needUtilityTypes |= type.usesUtilityTypes();
}
if (needXmime) {
addXmimeToSchema(root);
}
if (needUtilityTypes) {
AegisContext.addUtilityTypesToSchema(root);
}
/**
* if future proof then add <xsd:any/> element
*/
if (inf.isExtensibleElements()) {
XmlSchemaAny any = new XmlSchemaAny();
any.setMinOccurs(0);
any.setMaxOccurs(Long.MAX_VALUE);
sequence.getItems().add(any);
}
// Write out schema for attributes
for (QName name : inf.getAttributes()) {
if (isInheritedProperty(inf, name)) {
continue;
}
XmlSchemaAttribute attribute = new XmlSchemaAttribute(root, false);
complex.getAttributes().add(attribute);
attribute.setName(name.getLocalPart());
AegisType type = getType(inf, name);
attribute.setSchemaTypeName(type.getSchemaType());
String ns = name.getNamespaceURI();
if (!ns.equals(root.getTargetNamespace())) {
XmlSchemaUtils.addImportIfNeeded(root, ns);
}
}
/**
* If extensible attributes then add <xsd:anyAttribute/>
*/
if (inf.isExtensibleAttributes()) {
complex.setAnyAttribute(new XmlSchemaAnyAttribute());
}
}
use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.
the class BeanType method writeObjectInternal.
private void writeObjectInternal(Object object, MessageWriter writer, Context context, boolean wroteXsiType) throws DatabindingException {
if (object == null) {
return;
}
BeanTypeInfo inf = getTypeInfo();
if (!wroteXsiType && object.getClass() == getTypeClass() && context.isWriteXsiTypes()) {
writer.writeXsiType(getSchemaType());
}
for (QName name : inf.getAttributes()) {
if (isInheritedProperty(inf, name)) {
continue;
}
Object value = readProperty(object, name);
if (value != null) {
AegisType type = getType(inf, name);
if (type == null) {
throw new DatabindingException("Couldn't find type for " + value.getClass() + " for property " + name);
}
MessageWriter cwriter = writer.getAttributeWriter(name);
type.writeObject(value, cwriter, context);
cwriter.close();
}
}
if (inf.isExtension()) {
AegisType t = getSuperType();
if (t != null) {
t.writeObject(object, writer, context);
}
}
for (QName name : inf.getElements()) {
if (isInheritedProperty(inf, name)) {
continue;
}
Object value = readProperty(object, name);
AegisType defaultType = getType(inf, name);
AegisType type = TypeUtil.getWriteType(context.getGlobalContext(), value, defaultType);
// Write the value if it is not null.
if (value != null) {
if (type == null) {
throw new DatabindingException("Couldn't find type for " + value.getClass() + " for property " + name);
}
writeElement(name, value, type, writer, context);
} else if (inf.isNillable(name)) {
MessageWriter cwriter = getWriter(writer, name, type);
// Write the xsi:nil if it is null.
cwriter.writeXsiNil();
cwriter.close();
}
}
}
use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.
the class BeanType method getElementType.
protected AegisType getElementType(QName name, BeanTypeInfo beanTypeInfo, MessageReader reader, Context context) {
AegisType type = beanTypeInfo.getType(name);
// AegisType can be overriden with a xsi:type attribute
type = TypeUtil.getReadType(reader.getXMLStreamReader(), context.getGlobalContext(), type);
return type;
}
use of org.apache.cxf.aegis.type.AegisType in project cxf by apache.
the class BeanTypeInfo method getType.
/**
* Get the type class for the field with the specified QName.
*/
public AegisType getType(QName name) {
// 1. Try a prexisting mapped type
AegisType type = mappedName2type.get(name);
// 2. Try to get the type by its name, if there is one
if (type == null) {
QName typeName = getMappedTypeName(name);
if (typeName != null) {
type = getTypeMapping().getType(typeName);
if (type != null) {
mapType(name, type);
}
}
}
// 3. Create the type from the property descriptor and map it
if (type == null) {
PropertyDescriptor desc;
try {
desc = getPropertyDescriptorFromMappedName(name);
} catch (DatabindingException e) {
throw e;
} catch (Exception e) {
throw new DatabindingException("Couldn't get properties.", e);
}
if (desc == null) {
return null;
}
try {
TypeMapping tm = getTypeMapping();
TypeCreator tc = tm.getTypeCreator();
type = tc.createType(desc);
} catch (DatabindingException e) {
e.prepend("Couldn't create type for property " + desc.getName() + " on " + getTypeClass());
throw e;
}
// second part is possible workaround for XFIRE-586
if (registerType(desc)) {
getTypeMapping().register(type);
}
mapType(name, type);
}
if (type == null) {
throw new DatabindingException("Couldn't find type for property " + name);
}
return type;
}
Aggregations