You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/11/07 09:24:58 UTC

[sling-org-apache-sling-commons-osgi] 09/11: 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.

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.2.0
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git

commit f47b5ff9a12f7af46ed3bb9a8e17c7ad65419388
Author: Ian Boston <ie...@apache.org>
AuthorDate: Sat Nov 3 00:31:35 2012 +0000

     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.
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@1405242 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  2 +-
 .../apache/sling/commons/osgi/PropertiesUtil.java  | 56 ++++++++++++++++++++++
 .../sling/commons/osgi/PropertiesUtilTest.java     | 53 ++++++++++++++++++++
 3 files changed, 110 insertions(+), 1 deletion(-)

diff --git a/pom.xml b/pom.xml
index fba536e..409ab56 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>
diff --git a/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java b/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
index 9a69a68..5335ef5 100644
--- a/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
+++ b/src/main/java/org/apache/sling/commons/osgi/PropertiesUtil.java
@@ -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;
+    }
+
 }
diff --git a/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java b/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
new file mode 100644
index 0000000..8c5c3d8
--- /dev/null
+++ b/src/test/java/org/apache/sling/commons/osgi/PropertiesUtilTest.java
@@ -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;
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.