incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From i..@apache.org
Subject svn commit: r1405242 - in /sling/trunk/bundles/commons/osgi: pom.xml src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
Date Sat, 03 Nov 2012 00:31:36 GMT
Author: ieb
Date: Sat Nov  3 00:31:35 2012
New Revision: 1405242

URL: http://svn.apache.org/viewvc?rev=1405242&view=rev
Log:
 SLING-2625 Patch from Chetan Mehrotra to include utility method to convert config values
to maps, applied. Thank you. Incremented version of the o.a.s.commons.osgi to 2.2.0 due to
addition of new method.

Added:
    sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
  (with props)
Modified:
    sling/trunk/bundles/commons/osgi/pom.xml
    sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java

Modified: sling/trunk/bundles/commons/osgi/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/pom.xml?rev=1405242&r1=1405241&r2=1405242&view=diff
==============================================================================
--- sling/trunk/bundles/commons/osgi/pom.xml (original)
+++ sling/trunk/bundles/commons/osgi/pom.xml Sat Nov  3 00:31:35 2012
@@ -53,7 +53,7 @@
                 <configuration>
                     <instructions>
                         <Export-Package>
-                            org.apache.sling.commons.osgi;version=2.1.0
+                            org.apache.sling.commons.osgi;version=2.2.0
                         </Export-Package>
                     </instructions>
                 </configuration>

Modified: sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java?rev=1405242&r1=1405241&r2=1405242&view=diff
==============================================================================
--- sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
(original)
+++ sling/trunk/bundles/commons/osgi/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
Sat Nov  3 00:31:35 2012
@@ -20,7 +20,9 @@ package org.apache.sling.commons.osgi;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * The <code>PropertiesUtil</code> is a utility class providing some
@@ -216,4 +218,58 @@ public class PropertiesUtil {
 
         return defaultArray;
     }
+
+    /**
+     * Returns the parameter as a map with string keys and string values.
+     *
+     * The parameter is considered as a collection whose entries are of the form
+     * key=value. The conversion has following rules
+     * <ul>
+     *     <li>Entries are of the form key=value</li>
+     *     <li>key is trimmed</li>
+     *     <li>value is trimmed. If a trimmed value results in an empty string it is
treated as null</li>
+     *     <li>Malformed entries like 'foo','foo=' are ignored</li>
+     *     <li>Map entries maintain the input order</li>
+     * </ul>
+     *
+     * Otherwise (if the property is <code>null</code>) a provided default value
is
+     * returned.
+     * @param propValue The object to convert.
+     * @param defaultArray The default array converted to map.
+     */
+    public static Map<String, String> toMap(Object propValue, String[] defaultArray)
{
+        String[] arrayValue = toStringArray(propValue, defaultArray);
+
+        if (arrayValue == null) {
+            return null;
+        }
+
+        //in property values
+        Map<String, String> result = new LinkedHashMap<String, String>();
+        for (String kv : arrayValue) {
+            int indexOfEqual = kv.indexOf('=');
+            if (indexOfEqual > 0) {
+                String key = trimToNull(kv.substring(0, indexOfEqual));
+                String value = trimToNull(kv.substring(indexOfEqual + 1));
+                if (key != null) {
+                    result.put(key, value);
+                }
+            }
+        }
+        return result;
+    }
+
+    private static String trimToNull(String str)    {
+        String ts = trim(str);
+        return isEmpty(ts) ? null : ts;
+    }
+
+    private static String trim(String str){
+        return str == null ? null : str.trim();
+    }
+
+    private static boolean isEmpty(String str){
+        return str == null || str.length() == 0;
+    }
+
 }

Added: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java?rev=1405242&view=auto
==============================================================================
--- sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
(added)
+++ sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
Sat Nov  3 00:31:35 2012
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sling.commons.osgi;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+public class PropertiesUtilTest extends TestCase{
+
+    public void testToMap() {
+        final String[] defaultValue = new String[] {"a1=b1","a2=b2"};
+        Map<String,String> expected = asMap("a1","b1","a2","b2");
+        assertEquals(expected,PropertiesUtil.toMap(new String[] {"a1=b1","a2=b2"},null));
+        assertEquals(null,PropertiesUtil.toMap(null,null));
+        assertEquals(expected,PropertiesUtil.toMap(null,defaultValue));
+
+        //Trimming
+        assertEquals(expected,PropertiesUtil.toMap(new String[] {"a1 = b1 "," a2 = b2 "},null));
+
+        //Malformed handling
+        assertEquals(expected,PropertiesUtil.toMap(new String[] {"a1 = b1 "," a2 = b2 ","a3"},null));
+        assertEquals(asMap("a1","b1","a2","b2","a4",null),
+                PropertiesUtil.toMap(new String[] {"a1 = b1 "," a2 = b2 ","a3","a4="},null));
+    }
+
+
+    private static Map<String,String> asMap(String ... entries){
+        Map<String,String> m = new LinkedHashMap<String, String>();
+        for(int i = 0; i < entries.length; i+=2){
+            m.put(entries[i],entries[i+1]);
+        }
+        return m;
+    }
+}

Propchange: sling/trunk/bundles/commons/osgi/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message