Search in sources :

Example 61 with MutableBeanMetadata

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;
}
Also used : MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) QName(javax.xml.namespace.QName)

Example 62 with MutableBeanMetadata

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);
            }
        }
    }
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) List(java.util.List) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) BeanProperty(org.osgi.service.blueprint.reflect.BeanProperty)

Example 63 with MutableBeanMetadata

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);
            }
        }
    }
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) PropertyDescriptor(java.beans.PropertyDescriptor) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) BeanProperty(org.osgi.service.blueprint.reflect.BeanProperty)

Example 64 with MutableBeanMetadata

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;
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Example 65 with MutableBeanMetadata

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);
}
Also used : CollectionMetadata(org.osgi.service.blueprint.reflect.CollectionMetadata) ValueMetadata(org.osgi.service.blueprint.reflect.ValueMetadata) Metadata(org.osgi.service.blueprint.reflect.Metadata) NonNullMetadata(org.osgi.service.blueprint.reflect.NonNullMetadata) RefMetadata(org.osgi.service.blueprint.reflect.RefMetadata) ComponentMetadata(org.osgi.service.blueprint.reflect.ComponentMetadata) MutableRefMetadata(org.apache.aries.blueprint.mutable.MutableRefMetadata) ServiceReferenceMetadata(org.osgi.service.blueprint.reflect.ServiceReferenceMetadata) ReferenceMetadata(org.osgi.service.blueprint.reflect.ReferenceMetadata) BeanMetadata(org.osgi.service.blueprint.reflect.BeanMetadata) NullMetadata(org.osgi.service.blueprint.reflect.NullMetadata) MutableCollectionMetadata(org.apache.aries.blueprint.mutable.MutableCollectionMetadata) MutableBeanMetadata(org.apache.aries.blueprint.mutable.MutableBeanMetadata) MutableValueMetadata(org.apache.aries.blueprint.mutable.MutableValueMetadata) MutableMapMetadata(org.apache.aries.blueprint.mutable.MutableMapMetadata)

Aggregations

MutableBeanMetadata (org.apache.aries.blueprint.mutable.MutableBeanMetadata)96 Element (org.w3c.dom.Element)23 Node (org.w3c.dom.Node)20 MutablePassThroughMetadata (org.apache.aries.blueprint.mutable.MutablePassThroughMetadata)19 Metadata (org.osgi.service.blueprint.reflect.Metadata)19 MutableRefMetadata (org.apache.aries.blueprint.mutable.MutableRefMetadata)18 ComponentDefinitionException (org.osgi.service.blueprint.container.ComponentDefinitionException)18 MutableCollectionMetadata (org.apache.aries.blueprint.mutable.MutableCollectionMetadata)17 BeanMetadata (org.osgi.service.blueprint.reflect.BeanMetadata)16 ComponentMetadata (org.osgi.service.blueprint.reflect.ComponentMetadata)15 MutableValueMetadata (org.apache.aries.blueprint.mutable.MutableValueMetadata)13 RefMetadata (org.osgi.service.blueprint.reflect.RefMetadata)12 NodeList (org.w3c.dom.NodeList)11 MutableMapMetadata (org.apache.aries.blueprint.mutable.MutableMapMetadata)10 PassThroughMetadata (org.apache.aries.blueprint.PassThroughMetadata)9 JAXBException (javax.xml.bind.JAXBException)8 CollectionMetadata (org.osgi.service.blueprint.reflect.CollectionMetadata)8 ValueMetadata (org.osgi.service.blueprint.reflect.ValueMetadata)8 ExpressionNode (org.apache.camel.model.ExpressionNode)7 Attr (org.w3c.dom.Attr)7