use of org.apache.aries.blueprint.mutable.MutableBeanMetadata in project geronimo-xbean by apache.
the class QNameHelper method createQNameMetadata.
public static Metadata createQNameMetadata(Element element, String qualifiedName, ParserContext parserContext) {
MutableBeanMetadata beanMetadata = parserContext.createMetadata(MutableBeanMetadata.class);
beanMetadata.setClassName(QName.class.getName());
int index = qualifiedName.indexOf(':');
if (index >= 0) {
String prefix = qualifiedName.substring(0, index);
String localName = qualifiedName.substring(index + 1);
String uri = recursiveGetAttributeValue(element, "xmlns:" + prefix);
beanMetadata.addArgument(valueMetadata(uri, parserContext), String.class.getName(), 0);
beanMetadata.addArgument(valueMetadata(localName, parserContext), String.class.getName(), 1);
beanMetadata.addArgument(valueMetadata(prefix, parserContext), String.class.getName(), 2);
} else {
String uri = recursiveGetAttributeValue(element, "xmlns");
if (uri != null) {
beanMetadata.addArgument(valueMetadata(uri, parserContext), String.class.getName(), 0);
beanMetadata.addArgument(valueMetadata(qualifiedName, parserContext), String.class.getName(), 1);
} else {
beanMetadata.addArgument(valueMetadata(qualifiedName, parserContext), String.class.getName(), 0);
}
}
return beanMetadata;
}
use of org.apache.aries.blueprint.mutable.MutableBeanMetadata in project geronimo-xbean by apache.
the class QNameHelper method coerceNamespaceAwarePropertyValues.
public static void coerceNamespaceAwarePropertyValues(MutableBeanMetadata bd, Element element, PropertyDescriptor descriptor, ParserContext parserContext) {
// When the property is an indexed property, the getPropertyType can return null.
if (descriptor.getPropertyType() == null) {
return;
}
if (descriptor.getPropertyType().isAssignableFrom(QName.class)) {
String name = descriptor.getName();
BeanProperty propertyValue = XBeanNamespaceHandler.propertyByName(name, bd);
if (propertyValue != null) {
Metadata value = propertyValue.getValue();
if (value instanceof ValueMetadata) {
bd.removeProperty(propertyValue);
Metadata valueMetadata = createQNameMetadata(element, ((ValueMetadata) value).getStringValue(), parserContext);
bd.addProperty(name, valueMetadata);
}
// else??
}
} else if (descriptor.getPropertyType().isAssignableFrom(QName[].class)) {
String name = descriptor.getName();
BeanProperty propertyValue = XBeanNamespaceHandler.propertyByName(name, bd);
if (propertyValue != null) {
Object value = propertyValue.getValue();
if (value instanceof CollectionMetadata) {
List<Metadata> values = ((CollectionMetadata) value).getValues();
MutableCollectionMetadata newValue = parserContext.createMetadata(MutableCollectionMetadata.class);
for (Metadata v : values) {
if (v instanceof ValueMetadata) {
newValue.addValue(createQNameMetadata(element, ((ValueMetadata) v).getStringValue(), parserContext));
} else {
newValue.addValue(v);
}
}
bd.removeProperty(propertyValue);
bd.addProperty(name, newValue);
}
}
}
}
use of org.apache.aries.blueprint.mutable.MutableBeanMetadata in project geronimo-xbean by apache.
the class XBeanNamespaceHandler method nestedProperties.
private void nestedProperties(MutableBeanMetadata beanMetadata, Element element, String beanTypeName, String className, ParserContext parserContext) {
NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node instanceof Element) {
Element child = (Element) node;
String childName = child.getLocalName();
String namespace = child.getNamespaceURI();
if (!this.namespace.equals(namespace)) {
BeanProperty prop = parserContext.parseElement(BeanProperty.class, beanMetadata, child);
beanMetadata.addProperty(prop);
continue;
}
Metadata childMetadata = null;
PropertyDescriptor pd = getPropertyDescriptor(mappingMetaData.getClassName(beanTypeName), childName);
Class propertyType = pd == null ? null : pd.getPropertyType();
String propertyName = mappingMetaData.getNestedListProperty(beanTypeName, childName);
boolean isList = false;
// explicit list
if (propertyName != null || isCollectionType(propertyType)) {
propertyName = propertyName == null ? childName : propertyName;
childMetadata = parserContext.parseElement(CollectionMetadata.class, beanMetadata, child);
} else if ((propertyName = mappingMetaData.getFlatCollectionProperty(beanTypeName, childName)) != null) {
// flat collection
Metadata elementMetadata = parse(child, parserContext);
BeanProperty list = propertyByName(propertyName, beanMetadata);
MutableCollectionMetadata listMeta;
if (list == null) {
listMeta = parserContext.createMetadata(MutableCollectionMetadata.class);
childMetadata = listMeta;
} else {
listMeta = (MutableCollectionMetadata) list.getValue();
}
isList = true;
listMeta.addValue(elementMetadata);
} else if ((propertyName = mappingMetaData.getNestedProperty(beanTypeName, childName)) != null) {
// lets find the first child bean that parses fine
childMetadata = parseChildExtensionBean(child, beanMetadata, parserContext);
} else if (mappingMetaData.isFlatProperty(beanTypeName, childName)) {
propertyName = childName;
String flatClassName = getPropertyDescriptor(mappingMetaData.getClassName(beanTypeName), childName).getPropertyType().getName();
childMetadata = parseInternal(child, parserContext, childName, flatClassName);
} else {
childMetadata = tryParseNestedPropertyViaIntrospection(beanMetadata, className, child, parserContext);
propertyName = childName;
}
if (childMetadata == null && !isList) {
String text = getElementText(child);
if (text != null) {
MutableValueMetadata m = parserContext.createMetadata(MutableValueMetadata.class);
m.setStringValue(text.trim());
childMetadata = m;
}
// propertyName = mappingMetaData.getPropertyName(beanTypeName, childName);
// NodeList childNodes = child.getChildNodes();
// StringBuilder buf = new StringBuilder();
// for (int j = 0; j < childNodes.getLength(); j++) {
// Node childNode = childNodes.item(j);
// if (childNode instanceof Element) {
// Element childElement = (Element) childNode;
// if (namespace.equals(childElement.getNamespaceURI())) {
// childMetadata = parse(childElement, parserContext);
// } else {
// try {
// childMetadata = parserContext.parseElement(BeanMetadata.class, beanMetaData, childElement);
// } catch (Exception e) {
// childMetadata = parserContext.parseElement(ValueMetadata.class, beanMetaData, childElement);
// }
// }
//
// break;
// } else if (childNode instanceof Text) {
// String value = childNode.getNodeValue();
// buf.append(value);
// }
// }
// if (childMetadata == null) {
// MutableValueMetadata m = parserContext.createMetadata(MutableValueMetadata.class);
// m.setStringValue(buf.toString().trim());
// childMetadata = m;
// }
}
if (childMetadata != null) {
beanMetadata.addProperty(propertyName, childMetadata);
}
}
}
}
use of org.apache.aries.blueprint.mutable.MutableBeanMetadata in project geronimo-xbean by apache.
the class XBeanNamespaceHandler method parseCustomMapElement.
protected Metadata parseCustomMapElement(MutableBeanMetadata beanMetadata, Element element, String name, ParserContext parserContext) {
MutableMapMetadata map = parserContext.createMetadata(MutableMapMetadata.class);
Element parent = (Element) element.getParentNode();
String entryName = mappingMetaData.getMapEntryName(getLocalName(parent), name);
String keyName = mappingMetaData.getMapKeyName(getLocalName(parent), name);
String dups = mappingMetaData.getMapDupsMode(getLocalName(parent), name);
boolean flat = mappingMetaData.isFlatMap(getLocalName(parent), name);
String defaultKey = mappingMetaData.getMapDefaultKey(getLocalName(parent), name);
if (entryName == null)
entryName = "property";
if (keyName == null)
keyName = "key";
if (dups == null)
dups = "replace";
// TODO : support further customizations
// String valueName = "value";
// boolean keyIsAttr = true;
// boolean valueIsAttr = false;
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element childElement = (Element) node;
String localName = childElement.getLocalName();
String uri = childElement.getNamespaceURI();
if (localName == null || localName.equals("xmlns") || localName.startsWith("xmlns:")) {
continue;
}
// attributes from namespace-specific attributes
if (!flat && !isEmpty(uri) && localName.equals(entryName)) {
String key = childElement.getAttributeNS(uri, keyName);
if (key == null || key.length() == 0) {
key = defaultKey;
}
if (key == null) {
throw new RuntimeException("No key defined for map " + entryName);
}
NonNullMetadata keyValue = (NonNullMetadata) getValue(key, mappingMetaData.getPropertyEditor(localName, key), parserContext);
Element valueElement = getFirstChildElement(childElement);
Metadata value;
if (valueElement != null) {
value = parserContext.parseElement(Metadata.class, beanMetadata, valueElement);
// String valueElUri = valueElement.getNamespaceURI();
// String valueElLocalName = valueElement.getLocalName();
// if (valueElUri == null ||
// valueElUri.equals(BLUEPRINT_NAMESPACE)) {
// if ("bean".equals(valueElLocalName)) {
// value = parserContext.parseElement(BeanMetadata.class, beanMetadata, valueElement);
// } else {
// value = parserContext.parseElement(BeanProperty.class, beanMetadata, valueElement).getValue();
// }
// } else {
// value = parserContext.parseElement(ValueMetadata.class, beanMetadata, valueElement);
// }
} else {
value = getValue(getElementText(childElement), mappingMetaData.getPropertyEditor(localName, key), parserContext);
}
addValueToMap(map, keyValue, value, dups, parserContext);
} else if (flat && !isEmpty(uri)) {
String key = childElement.getAttributeNS(uri, keyName);
if (key == null || key.length() == 0) {
key = defaultKey;
}
if (key == null) {
throw new RuntimeException("No key defined for map entry " + entryName);
}
NonNullMetadata keyValue = (NonNullMetadata) getValue(key, mappingMetaData.getPropertyEditor(localName, key), parserContext);
childElement = cloneElement(childElement);
childElement.removeAttributeNS(uri, keyName);
Metadata bdh = parse(childElement, parserContext);
addValueToMap(map, keyValue, bdh, dups, parserContext);
}
}
}
return map;
}
use of org.apache.aries.blueprint.mutable.MutableBeanMetadata in project geronimo-xbean by apache.
the class XBeanNamespaceHandler method addProperty.
private void addProperty(String name, String value, String propertyEditor, MutableBeanMetadata definition, ParserContext parserContext) {
Metadata m = getValue(value, propertyEditor, parserContext);
definition.addProperty(name, m);
}
Aggregations