geronimo-xbean-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r517175 - in /geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring: context/impl/ context/v2c/ generator/
Date Mon, 12 Mar 2007 11:15:29 GMT
Author: gnodet
Date: Mon Mar 12 04:15:28 2007
New Revision: 517175

URL: http://svn.apache.org/viewvc?view=rev&rev=517175
Log:
XBEAN-82: Support flat maps, dups policies and default keys

Modified:
    geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/impl/MappingMetaData.java
    geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/v2c/XBeanNamespaceHandler.java
    geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/MapMapping.java
    geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/QdoxMappingLoader.java
    geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XmlMetadataGenerator.java

Modified: geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/impl/MappingMetaData.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/impl/MappingMetaData.java?view=diff&rev=517175&r1=517174&r2=517175
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/impl/MappingMetaData.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/impl/MappingMetaData.java
Mon Mar 12 04:15:28 2007
@@ -190,6 +190,18 @@
         return properties.getProperty(elementName + "." + property + ".map.keyName");
     }
 
+    public boolean isFlatMap(String elementName, String property) {
+        return properties.getProperty(elementName + "." + property + ".map.flat") != null;
+    }
+    
+    public String getMapDupsMode(String elementName, String property) {
+        return properties.getProperty(elementName + "." + property + ".map.dups");
+    }
+    
+    public String getMapDefaultKey(String elementName, String property) {
+        return properties.getProperty(elementName + "." + property + ".map.defaultKey");
+    }
+    
     public String getFlatCollectionProperty(String elementName, String property)
     {
         return properties.getProperty(elementName + "." + property + ".flatCollection");

Modified: geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/v2c/XBeanNamespaceHandler.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/v2c/XBeanNamespaceHandler.java?view=diff&rev=517175&r1=517174&r2=517175
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/v2c/XBeanNamespaceHandler.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/context/v2c/XBeanNamespaceHandler.java
Mon Mar 12 04:15:28 2007
@@ -28,8 +28,8 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
@@ -596,9 +596,13 @@
         Element parent = (Element) element.getParentNode();
         String entryName = metadata.getMapEntryName(getLocalName(parent), name);
         String keyName = metadata.getMapKeyName(getLocalName(parent), name);
+        String dups = metadata.getMapDupsMode(getLocalName(parent), name);
+        boolean flat = metadata.isFlatMap(getLocalName(parent), name);
+        String defaultKey = metadata.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";
@@ -618,9 +622,14 @@
 
                 // we could use namespaced attributes to differentiate real spring
                 // attributes from namespace-specific attributes
-                if (!isEmpty(uri) && localName.equals(entryName)) {
+                if (!flat && !isEmpty(uri) && localName.equals(entryName))
{
                     String key = childElement.getAttribute(keyName);
-                    if (key == null) throw new RuntimeException("No key defined for map "
+ entryName);
+                    if (key == null || key.length() == 0) {
+                        key = defaultKey;
+                    }
+                    if (key == null) {
+                        throw new RuntimeException("No key defined for map " + entryName);
+                    }
 
                     Object keyValue = getValue(key, null);
 
@@ -645,11 +654,52 @@
                         value = getElementText(childElement);
                     }
 
-                    map.put(keyValue, value);
+                    addValueToMap(map, keyValue, value, dups);
+                } else if (flat && !isEmpty(uri)) {
+                    String key = childElement.getAttribute(keyName);
+                    if (key == null || key.length() == 0) {
+                        key = defaultKey;
+                    }
+                    if (key == null) {
+                        throw new RuntimeException("No key defined for map " + entryName);
+                    }
+                    Object keyValue = getValue(key, null);
+                    childElement.removeAttribute(keyName);
+                    BeanDefinitionHolder bdh = parseBeanFromExtensionElement(childElement);
+                    addValueToMap(map, keyValue, bdh, dups);
                 }
             }
         }
         return map;
+    }
+    
+    protected void addValueToMap(Map map, Object keyValue, Object value, String dups) {
+        if (map.containsKey(keyValue)) {
+            if ("discard".equalsIgnoreCase(dups)) {
+                // Do nothing
+            } else if ("replace".equalsIgnoreCase(dups)) {
+                map.put(keyValue, value);
+            } else if ("allow".equalsIgnoreCase(dups)) {
+                List l = new ManagedList();
+                l.add(map.get(keyValue));
+                l.add(value);
+                map.put(keyValue, l);
+            } else if ("always".equalsIgnoreCase(dups)) {
+                List l = (List) map.get(keyValue);
+                l.add(value);
+            }
+        } else {
+            if ("always".equalsIgnoreCase(dups)) {
+                List l = (List) map.get(keyValue);
+                if (l == null) {
+                    l = new ManagedList();
+                    map.put(keyValue, l);
+                }
+                l.add(value);
+            } else {
+                map.put(keyValue, value);
+            }
+        }
     }
 
     protected Element getFirstChildElement(Element element) {

Modified: geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/MapMapping.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/MapMapping.java?view=diff&rev=517175&r1=517174&r2=517175
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/MapMapping.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/MapMapping.java
Mon Mar 12 04:15:28 2007
@@ -19,10 +19,20 @@
 public class MapMapping {
     private String entryName;
     private String keyName;
+    private boolean flat;
+    private String dupsMode;
+    private String defaultKey;
 
-    public MapMapping(String entryName, String keyName) {
+    public MapMapping(String entryName, 
+                      String keyName, 
+                      boolean flat, 
+                      String dupsMode, 
+                      String defaultKey) {
         this.entryName = entryName;
         this.keyName = keyName;
+        this.flat = flat;
+        this.dupsMode = dupsMode;
+        this.defaultKey = defaultKey;
     }
 
     public String getEntryName() {
@@ -31,5 +41,17 @@
 
     public String getKeyName() {
         return keyName;
+    }
+
+    public boolean isFlat() {
+        return flat;
+    }
+
+    public String getDupsMode() {
+        return dupsMode;
+    }
+
+    public String getDefaultKey() {
+        return defaultKey;
     }
 }

Modified: geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/QdoxMappingLoader.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/QdoxMappingLoader.java?view=diff&rev=517175&r1=517174&r2=517175
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/QdoxMappingLoader.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/QdoxMappingLoader.java
Mon Mar 12 04:15:28 2007
@@ -204,8 +204,12 @@
                     if (acc != null) {
                         DocletTag mapTag = acc.getTagByName(MAP_ANNOTATION);
                         if (mapTag != null) {
-                            MapMapping mm = new MapMapping(mapTag.getNamedParameter("entryName"),

-                                    mapTag.getNamedParameter("keyName"));
+                            MapMapping mm = new MapMapping(
+                                    mapTag.getNamedParameter("entryName"), 
+                                    mapTag.getNamedParameter("keyName"),
+                                    Boolean.valueOf(mapTag.getNamedParameter("flat")).booleanValue(),
+                                    mapTag.getNamedParameter("dups"),
+                                    mapTag.getNamedParameter("defaultKey"));
                             mapsByPropertyName.put(beanProperty.getName(), mm);
                         }
                         

Modified: geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XmlMetadataGenerator.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XmlMetadataGenerator.java?view=diff&rev=517175&r1=517174&r2=517175
==============================================================================
--- geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XmlMetadataGenerator.java
(original)
+++ geronimo/xbean/trunk/xbean-spring/src/main/java/org/apache/xbean/spring/generator/XmlMetadataGenerator.java
Mon Mar 12 04:15:28 2007
@@ -136,8 +136,21 @@
         for (Iterator itr = maps.entrySet().iterator(); itr.hasNext();) {
             Map.Entry entry = (Map.Entry) itr.next();
             MapMapping mm = (MapMapping) entry.getValue();
-            out.println(element.getElementName() + "." + entry.getKey() + ".map.entryName
= " + mm.getEntryName());
-            out.println(element.getElementName() + "." + entry.getKey() + ".map.keyName =
" + mm.getKeyName());
+            if (mm.getEntryName() != null) {
+                out.println(element.getElementName() + "." + entry.getKey() + ".map.entryName
= " + mm.getEntryName());
+            }
+            if (mm.getKeyName() != null) {
+                out.println(element.getElementName() + "." + entry.getKey() + ".map.keyName
= " + mm.getKeyName());
+            }
+            if (mm.isFlat()) {
+                out.println(element.getElementName() + "." + entry.getKey() + ".map.flat
= " + Boolean.toString(mm.isFlat()));
+            }
+            if (mm.getDupsMode() != null) {
+                out.println(element.getElementName() + "." + entry.getKey() + ".map.dups
= " + mm.getDupsMode());
+            }
+            if (mm.getDefaultKey() != null) {
+                out.println(element.getElementName() + "." + entry.getKey() + ".map.defaultKey
= " + mm.getDefaultKey());
+            }
         }
 
         Map flatCollections = element.getFlatCollections();



Mime
View raw message