geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From djen...@apache.org
Subject svn commit: r906237 - in /geronimo/xbean/trunk/xbean-blueprint/src: main/java/org/apache/xbean/blueprint/context/impl/ test/java/org/apache/xbean/blueprint/context/
Date Wed, 03 Feb 2010 20:55:03 GMT
Author: djencks
Date: Wed Feb  3 20:55:03 2010
New Revision: 906237

URL: http://svn.apache.org/viewvc?rev=906237&view=rev
Log:
XBEAN-142 add some property editor support

Modified:
    geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java
    geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/BlueprintTestSupport.java
    geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/FlatMapTest.java
    geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanAndPropertiesTest.java
    geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanTest.java

Modified: geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java?rev=906237&r1=906236&r2=906237&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java
(original)
+++ geronimo/xbean/trunk/xbean-blueprint/src/main/java/org/apache/xbean/blueprint/context/impl/XBeanNamespaceHandler.java
Wed Feb  3 20:55:03 2010
@@ -24,6 +24,7 @@
 import java.beans.IntrospectionException;
 import java.beans.Introspector;
 import java.beans.PropertyDescriptor;
+import java.beans.PropertyEditor;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
@@ -72,17 +73,19 @@
     private final Set<Class> managedClasses;
     private final MappingMetaData mappingMetaData;
     private final Map<String, Class> managedClassesByName;
+    private final Map<String, Class<? extends PropertyEditor>> propertyEditors;
     private final NamedConstructorArgs namedConstructorArgs = new NamedConstructorArgs();
 
-    public XBeanNamespaceHandler(String namespace, URL schemaLocation, Set<Class> managedClasses,
Properties properties) {
+    public XBeanNamespaceHandler(String namespace, URL schemaLocation, Set<Class> managedClasses,
Map<String, Class<? extends PropertyEditor>> propertyEditors, Properties properties)
{
         this.namespace = namespace;
         this.schemaLocation = schemaLocation;
         this.managedClasses = managedClasses;
         managedClassesByName = mapClasses(managedClasses);
+        this.propertyEditors = propertyEditors;
         this.mappingMetaData = new MappingMetaData(properties);
     }
 
-    public XBeanNamespaceHandler(String namespace, String schemaLocation, Bundle bundle,
String propertiesLocation) throws IOException, ClassNotFoundException {
+    public XBeanNamespaceHandler(String namespace, String schemaLocation, Bundle bundle,
String propertiesLocation) throws IOException, ClassNotFoundException, InstantiationException,
IllegalAccessException {
         URL propertiesUrl = bundle.getEntry(propertiesLocation);
         InputStream in = propertiesUrl.openStream();
         Properties properties = new Properties();
@@ -95,11 +98,16 @@
         this.schemaLocation = bundle.getEntry(schemaLocation);
         this.managedClasses = managedClassesFromProperties(bundle, properties);
         managedClassesByName = mapClasses(managedClasses);
+        propertyEditors = propertyEditorsFromProperties(bundle, properties);
         this.mappingMetaData = new MappingMetaData(properties);
     }
-    public XBeanNamespaceHandler(String namespace, String schemaLocation, String propertiesLocation)
throws IOException, ClassNotFoundException {
+
+    public XBeanNamespaceHandler(String namespace, String schemaLocation, String propertiesLocation)
throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException
{
         ClassLoader cl = getClass().getClassLoader();
         URL propertiesUrl = cl.getResource(propertiesLocation);
+        if (propertiesUrl == null) {
+            throw new IOException("Could not locate properties at " + propertiesLocation);
+        }
         InputStream in = propertiesUrl.openStream();
         Properties properties = new Properties();
         try {
@@ -111,6 +119,7 @@
         this.schemaLocation = cl.getResource(schemaLocation);
         this.managedClasses = managedClassesFromProperties(cl, properties);
         managedClassesByName = mapClasses(managedClasses);
+        propertyEditors = propertyEditorsFromProperties(cl, properties);
         this.mappingMetaData = new MappingMetaData(properties);
     }
 
@@ -140,6 +149,31 @@
         return managedClasses;
     }
 
+    private Map<String, Class<? extends PropertyEditor>> propertyEditorsFromProperties(Bundle
bundle, Properties properties) throws ClassNotFoundException, IllegalAccessException, InstantiationException
{
+        Map<String, Class<? extends PropertyEditor>> propertyEditors = new HashMap<String,
Class<? extends PropertyEditor>>();
+        for (Map.Entry entry : properties.entrySet()) {
+            String key = (String) entry.getKey();
+            if (key.endsWith(".propertyEditor") ) {
+                String className = (String) entry.getValue();
+                Class<? extends PropertyEditor> clazz = bundle.loadClass(className).asSubclass(PropertyEditor.class);
+                propertyEditors.put(className, clazz);
+            }
+        }
+        return propertyEditors;
+    }
+    private Map<String, Class<? extends PropertyEditor>> propertyEditorsFromProperties(ClassLoader
classLoader, Properties properties) throws ClassNotFoundException, IllegalAccessException,
InstantiationException {
+        Map<String, Class<? extends PropertyEditor>> propertyEditors = new HashMap<String,
Class<? extends PropertyEditor>>();
+        for (Map.Entry entry : properties.entrySet()) {
+            String key = (String) entry.getKey();
+            if (key.endsWith(".propertyEditor") ) {
+                String className = (String) entry.getValue();
+                Class<? extends PropertyEditor> clazz = classLoader.loadClass(className).asSubclass(PropertyEditor.class);
+                propertyEditors.put(className, clazz);
+            }
+        }
+        return propertyEditors;
+    }
+
     private Map<String, Class> mapClasses(Set<Class> managedClasses) {
         Map<String, Class> map = new HashMap<String, Class>();
         for (Class clazz : managedClasses) {
@@ -213,20 +247,22 @@
         NamedNodeMap attrs = element.getAttributes();
         for (int i = 0; i < attrs.getLength(); i++) {
             Attr attr = (Attr) attrs.item(i);
-            if (namespace.equals(attr.getNamespaceURI())) {
+            if (namespace.equals(attr.getNamespaceURI()) || attr.getNamespaceURI() == null)
{
                 String attrName = attr.getLocalName();
                 String value = attr.getValue().trim();
                 String propertyName = mappingMetaData.getPropertyName(beanTypeName, attrName);
-                addProperty(propertyName, value, beanMetaData, parserContext);
+                String propertyEditor = mappingMetaData.getPropertyEditor(beanTypeName, attrName);
+                addProperty(propertyName, value, propertyEditor, beanMetaData, parserContext);
             }
         }
     }
 
     private void contentProperty(MutableBeanMetadata definition, Element element, ParserContext
parserContext) {
         String name = mappingMetaData.getContentProperty(element.getLocalName());
+        String propertyEditor = mappingMetaData.getPropertyEditor(element.getLocalName(),
name);
         if (name != null) {
             String value = getElementText(element).trim();
-            addProperty(name, value, definition, parserContext);
+            addProperty(name, value, propertyEditor, definition, parserContext);
         } else {
             ByteArrayInputStream in = new ByteArrayInputStream(getElementText(element).getBytes());
             Properties properties = new Properties();
@@ -239,13 +275,13 @@
             for (Map.Entry<Object, Object> entry : properties.entrySet()) {
                 String key = (String) entry.getKey();
                 String value = (String) entry.getValue();
-                addProperty(key, value, definition, parserContext);
+                addProperty(key, value, propertyEditor, definition, parserContext);
             }
         }
     }
 
-    private void addProperty(String name, String value, MutableBeanMetadata definition, ParserContext
parserContext) {
-        Metadata m = getValue(value, null, parserContext);
+    private void addProperty(String name, String value, String propertyEditor, MutableBeanMetadata
definition, ParserContext parserContext) {
+        Metadata m = getValue(value, propertyEditor, parserContext);
         definition.addProperty(name, m);
     }
 
@@ -450,7 +486,7 @@
                         throw new RuntimeException("No key defined for map " + entryName);
                     }
 
-                    NonNullMetadata keyValue = (NonNullMetadata) getValue(key, null, parserContext);
+                    NonNullMetadata keyValue = (NonNullMetadata) getValue(key, mappingMetaData.getPropertyEditor(localName,
key), parserContext);
 
                     Element valueElement = getFirstChildElement(childElement);
                     Metadata value;
@@ -469,7 +505,7 @@
 //                            value = parserContext.parseElement(ValueMetadata.class, beanMetadata,
valueElement);
 //                        }
                     } else {
-                        value = getValue(getElementText(childElement), null, parserContext);
+                        value = getValue(getElementText(childElement), mappingMetaData.getPropertyEditor(localName,
key), parserContext);
                     }
 
                     addValueToMap(map, keyValue, value, dups, parserContext);
@@ -481,7 +517,7 @@
                     if (key == null) {
                         throw new RuntimeException("No key defined for map entry " + entryName);
                     }
-                    NonNullMetadata keyValue = (NonNullMetadata) getValue(key, null, parserContext);
+                    NonNullMetadata keyValue = (NonNullMetadata) getValue(key, mappingMetaData.getPropertyEditor(localName,
key), parserContext);
                     childElement = cloneElement(childElement);
                     childElement.removeAttributeNS(uri, keyName);
                     Metadata bdh = parse(childElement, parserContext);
@@ -553,7 +589,7 @@
         return uri == null || uri.length() == 0;
     }
 
-    protected Metadata getValue(String value, String propertyEditor, ParserContext parserContext)
{
+    protected Metadata getValue(String value, String propertyEditorName, ParserContext parserContext)
{
         if (value == null) return null;
 
         //
@@ -593,6 +629,18 @@
         // Neither null nor a reference
         //
         MutableValueMetadata metadata = parserContext.createMetadata(MutableValueMetadata.class);
+        if (propertyEditorName != null) {
+            PropertyEditor propertyEditor;
+            try {
+                propertyEditor = propertyEditors.get(propertyEditorName).newInstance();
+            } catch (InstantiationException e) {
+                throw new ComponentDefinitionException("Could not create a " + propertyEditorName
+ " to convert value " + value + " for namespace " + namespace);
+            } catch (IllegalAccessException e) {
+                throw new ComponentDefinitionException("Could not create a " + propertyEditorName
+ " to convert value " + value + " for namespace " + namespace);
+            }
+            propertyEditor.setAsText(value);
+            value = propertyEditor.getAsText();
+        }
         metadata.setStringValue(value);
         return metadata;
     }

Modified: geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/BlueprintTestSupport.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/BlueprintTestSupport.java?rev=906237&r1=906236&r2=906237&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/BlueprintTestSupport.java
(original)
+++ geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/BlueprintTestSupport.java
Wed Feb  3 20:55:03 2010
@@ -26,6 +26,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.xbean.blueprint.context.impl.QNameNamespaceHandler;
 import org.apache.xbean.blueprint.context.impl.XBeanNamespaceHandler;
+import org.apache.xbean.blueprint.example.MilliLittersPropertyEditor;
 import org.xml.sax.SAXException;
 import org.osgi.service.blueprint.reflect.BeanProperty;
 import org.osgi.service.blueprint.reflect.ValueMetadata;
@@ -36,6 +37,7 @@
 import javax.xml.validation.Schema;
 import javax.xml.namespace.QName;
 
+import java.beans.PropertyEditor;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URI;
@@ -94,7 +96,9 @@
         }
         classes.add(QName.class);
 
-        final NamespaceHandler xbeanHandler = new XBeanNamespaceHandler(NAMESPACE_URI.toString(),
BlueprintTestSupport.class.getClassLoader().getResource("restaurant.xsd"), classes, properties);
+        Map<String, Class<? extends PropertyEditor>> propertyEditors = new HashMap<String,
Class<? extends PropertyEditor>>();
+        propertyEditors.put(MilliLittersPropertyEditor.class.getName(), MilliLittersPropertyEditor.class);
+        final NamespaceHandler xbeanHandler = new XBeanNamespaceHandler(NAMESPACE_URI.toString(),
BlueprintTestSupport.class.getClassLoader().getResource("restaurant.xsd"), classes, propertyEditors,
properties);
         final NamespaceHandler qnameHandler = new QNameNamespaceHandler();
         NamespaceHandlerRegistry.NamespaceHandlerSet handlers = new NamespaceHandlerRegistry.NamespaceHandlerSet()
{
             public Set<URI> getNamespaces() {

Modified: geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/FlatMapTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/FlatMapTest.java?rev=906237&r1=906236&r2=906237&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/FlatMapTest.java
(original)
+++ geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/FlatMapTest.java
Wed Feb  3 20:55:03 2010
@@ -37,14 +37,14 @@
         assertEquals("key1", ((ValueMetadata) me.getKey()).getStringValue());
         CollectionMetadata l = (CollectionMetadata) me.getValue();
         assertEquals(2, l.getValues().size());
-        checkEntry(l.getValues().get(0), "1000 ml");
-        checkEntry(l.getValues().get(1), "5 pints");
+        checkEntry(l.getValues().get(0), "1000");
+        checkEntry(l.getValues().get(1), "8750");
 
         me = c.getEntries().get(1);
         assertEquals("key2", ((ValueMetadata) me.getKey()).getStringValue());
         l = (CollectionMetadata) me.getValue();
         assertEquals(1, l.getValues().size());
-        checkEntry(l.getValues().get(0), "20 liter");
+        checkEntry(l.getValues().get(0), "20000");
 
         me = c.getEntries().get(2);
         assertEquals("others", ((ValueMetadata) me.getKey()).getStringValue());

Modified: geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanAndPropertiesTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanAndPropertiesTest.java?rev=906237&r1=906236&r2=906237&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanAndPropertiesTest.java
(original)
+++ geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanAndPropertiesTest.java
Wed Feb  3 20:55:03 2010
@@ -30,6 +30,10 @@
         //disabled test
     }
 
+    @Override
+    protected void setUp() throws Exception {
+    }
+
     protected String getPlan() {
         return "org/apache/xbean/blueprint/context/keg-xbean-properties.xml";
     }

Modified: geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanTest.java?rev=906237&r1=906236&r2=906237&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanTest.java
(original)
+++ geronimo/xbean/trunk/xbean-blueprint/src/test/java/org/apache/xbean/blueprint/context/KegXBeanTest.java
Wed Feb  3 20:55:03 2010
@@ -35,10 +35,10 @@
         BeanMetadataImpl pints5 = (BeanMetadataImpl) reg.getComponentDefinition("pints5");
         BeanMetadataImpl liter20 = (BeanMetadataImpl) reg.getComponentDefinition("liter20");
         
-        checkPropertyValue("remaining", "1000 ml", ml1000);
+        checkPropertyValue("remaining", "1000", ml1000);
         checkPropertyValue("remaining", "0", empty);
-        checkPropertyValue("remaining", "5 pints", pints5);
-        checkPropertyValue("remaining", "20 liter", liter20);
+        checkPropertyValue("remaining", "8750", pints5);
+        checkPropertyValue("remaining", "20000", liter20);
 //        checkPropertyValue("remaining", "1000", ml1000);
 //        checkPropertyValue("remaining", "0", empty);
 //        checkPropertyValue("remaining", "8750", pints5);



Mime
View raw message