felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1723557 - in /felix/trunk/utils/src: main/java/org/apache/felix/utils/properties/Properties.java test/java/org/apache/felix/utils/properties/PropertiesTest.java
Date Thu, 07 Jan 2016 14:24:47 GMT
Author: gnodet
Date: Thu Jan  7 14:24:47 2016
New Revision: 1723557

URL: http://svn.apache.org/viewvc?rev=1723557&view=rev
Log:
[FELIX-5156] Felix Properties doesn't handle multi value properties correctly

Modified:
    felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
    felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java

Modified: felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java?rev=1723557&r1=1723556&r2=1723557&view=diff
==============================================================================
--- felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java (original)
+++ felix/trunk/utils/src/main/java/org/apache/felix/utils/properties/Properties.java Thu
Jan  7 14:24:47 2016
@@ -27,6 +27,7 @@ import java.io.LineNumberReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
+import java.io.StringReader;
 import java.io.Writer;
 import java.net.URL;
 import java.security.AccessController;
@@ -284,21 +285,32 @@ public class Properties extends Abstract
         commentLines = new ArrayList<String>(commentLines);
         valueLines = new ArrayList<String>(valueLines);
         String escapedKey = escapeKey(key);
+        StringBuilder sb = new StringBuilder();
         int lastLine = valueLines.size() - 1;
         if (valueLines.isEmpty()) {
             valueLines.add(escapedKey + "=");
-        } else if (!valueLines.get(0).trim().startsWith(escapedKey)) {
-            valueLines.set(0, escapedKey + " = " + escapeJava(valueLines.get(0)) + (0 <
lastLine? "\\": ""));
+            sb.append(escapedKey).append("=");
+        } else {
+            String val0 = valueLines.get(0);
+            if (!val0.trim().startsWith(escapedKey)) {
+                valueLines.set(0, escapedKey + " = " + escapeJava(val0) /*+ (0 < lastLine?
"\\": "")*/);
+                sb.append(escapedKey).append(" = ").append(escapeJava(val0));
+            } else {
+                valueLines.set(0, escapeJava(val0) /*+ (0 < lastLine? "\\": "")*/);
+                sb.append(escapeJava(val0));
+            }
         }
         for (int i = 1; i < valueLines.size(); i++) {
-            valueLines.set(i, escapeJava(valueLines.get(i)) + (i < lastLine? "\\": ""));
-        }
-        StringBuilder value = new StringBuilder();
-        for (String line: valueLines) {
-            value.append(line);
+            String val = valueLines.get(i);
+            valueLines.set(i, escapeJava(val) /*+ (i < lastLine? "\\": "")*/);
+            while (!val.isEmpty() && Character.isWhitespace(val.charAt(0))) {
+                val = val.substring(1);
+            }
+            sb.append(val);
         }
+        String[] property = PropertiesReader.parseProperty(sb.toString());
         this.layout.put(key, new Layout(commentLines, valueLines));
-        return storage.put(key, unescapeJava(value.toString()));
+        return storage.put(key, property[1]);
     }
 
     public String put(String key, List<String> commentLines, String value) {
@@ -470,9 +482,17 @@ public class Properties extends Abstract
             }
             if (l != null && l.getValueLines() != null)
             {
-                for (String s : l.getValueLines())
+                for (int i = 0; i < l.getValueLines().size(); i++)
                 {
-                    writer.writeln(s);
+                    String s = l.getValueLines().get(i);
+                    if (i < l.getValueLines().size() - 1)
+                    {
+                        writer.writeln(s + "\\");
+                    }
+                    else
+                    {
+                        writer.writeln(s);
+                    }
                 }
             }
             else
@@ -838,20 +858,19 @@ public class Properties extends Abstract
                     continue;
                 }
 
+                boolean combine = checkCombineLines(line);
+                if (combine)
+                {
+                    line = line.substring(0, line.length() - 1);
+                }
                 valueLines.add(line);
                 while (line.length() > 0 && contains(WHITE_SPACE, line.charAt(0)))
                 {
                     line = line.substring(1, line.length());
                 }
-
-                if (checkCombineLines(line))
-                {
-                    line = line.substring(0, line.length() - 1);
-                    buffer.append(line);
-                }
-                else
+                buffer.append(line);
+                if (!combine)
                 {
-                    buffer.append(line);
                     break;
                 }
             }

Modified: felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java?rev=1723557&r1=1723556&r2=1723557&view=diff
==============================================================================
--- felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
(original)
+++ felix/trunk/utils/src/test/java/org/apache/felix/utils/properties/PropertiesTest.java
Thu Jan  7 14:24:47 2016
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringReader;
 import java.io.StringWriter;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -237,7 +238,7 @@ public class PropertiesTest extends Test
         assertTrue(sw.toString(), sw.toString().endsWith(RESULT3));
         List<String> rawValue = properties.getRaw(KEY1);
         assertEquals(2, rawValue.size());
-        assertEquals(KEY1A + " = " + VALUE1 + "\\", rawValue.get(0));
+        assertEquals(KEY1A + " = " + VALUE1, rawValue.get(0));
         assertEquals(VALUE1, rawValue.get(1));
     }
 
@@ -263,4 +264,56 @@ public class PropertiesTest extends Test
         assertEquals(VALUE1 + "x", properties.get(KEY1));
     }
 
+    public void testMultiValueEscaping() throws IOException {
+        StringWriter sw = new StringWriter();
+        PrintWriter pw = new PrintWriter(sw);
+        pw.println("fruits                           apple, banana, pear, \\");
+        pw.println("                                 cantaloupe, watermelon, \\");
+        pw.println("                                 kiwi, mango");
+
+        java.util.Properties p = new java.util.Properties();
+        p.load(new StringReader(sw.toString()));
+        assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", p.getProperty("fruits"));
+
+        Properties props = new Properties();
+        props.load(new StringReader(sw.toString()));
+        assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", props.getProperty("fruits"));
+        List<String> raw = props.getRaw("fruits");
+        assertNotNull(raw);
+        assertEquals(3, raw.size());
+        assertEquals("fruits                           apple, banana, pear, ", raw.get(0));
+
+        props = new Properties();
+        props.put("fruits", props.getComments("fruits"), Arrays.asList(
+                "fruits                           apple, banana, pear, ",
+                "                                 cantaloupe, watermelon, ",
+                "                                 kiwi, mango"));
+        assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", props.getProperty("fruits"));
+        raw = props.getRaw("fruits");
+        assertNotNull(raw);
+        assertEquals(3, raw.size());
+        assertEquals("fruits                           apple, banana, pear, ", raw.get(0));
+
+        sw = new StringWriter();
+        props.save(sw);
+        props = new Properties();
+        props.load(new StringReader(sw.toString()));
+        assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", props.getProperty("fruits"));
+        raw = props.getRaw("fruits");
+        assertNotNull(raw);
+        assertEquals(3, raw.size());
+        assertEquals("fruits                           apple, banana, pear, ", raw.get(0));
+
+        props = new Properties();
+        props.put("fruits", props.getComments("fruits"), Arrays.asList(
+                "                           apple, banana, pear, ",
+                "                                 cantaloupe, watermelon, ",
+                "                                 kiwi, mango"));
+        assertEquals("apple, banana, pear, cantaloupe, watermelon, kiwi, mango", props.getProperty("fruits"));
+        raw = props.getRaw("fruits");
+        assertNotNull(raw);
+        assertEquals(3, raw.size());
+        assertEquals("fruits =                            apple, banana, pear, ", raw.get(0));
+    }
+
 }



Mime
View raw message