You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by eb...@apache.org on 2007/04/08 20:05:18 UTC
svn commit: r526587 - in /jakarta/commons/proper/configuration/trunk:
src/java/org/apache/commons/configuration/INIConfiguration.java
src/test/org/apache/commons/configuration/TestINIConfiguration.java
xdocs/changes.xml
Author: ebourg
Date: Sun Apr 8 11:05:17 2007
New Revision: 526587
URL: http://svn.apache.org/viewvc?view=rev&rev=526587
Log:
Fixed INIConfiguration to handle the quoted values and the lines containing a value and a comment.
Modified:
jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/INIConfiguration.java
jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestINIConfiguration.java
jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
Modified: jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/INIConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/INIConfiguration.java?view=diff&rev=526587&r1=526586&r2=526587
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/INIConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/java/org/apache/commons/configuration/INIConfiguration.java Sun Apr 8 11:05:17 2007
@@ -28,6 +28,8 @@
import java.util.Set;
import java.util.TreeSet;
+import org.apache.commons.lang.StringUtils;
+
/**
* <p>
* An initialization or ini file is a configuration file tpically found on
@@ -242,7 +244,7 @@
String value = values.getString(key);
pw.print(key);
pw.print(" = ");
- pw.print(value);
+ pw.print(formatValue(value));
pw.print(LINE_SEPARATOR);
}
@@ -283,7 +285,7 @@
if (index >= 0)
{
key = section + line.substring(0, index);
- value = line.substring(index + 1);
+ value = parseValue(line.substring(index + 1));
}
else
{
@@ -291,14 +293,14 @@
if (index >= 0)
{
key = section + line.substring(0, index);
- value = line.substring(index + 1);
+ value = parseValue(line.substring(index + 1));
}
else
{
key = section + line;
}
}
- this.addProperty(key.trim(), value.trim());
+ this.addProperty(key.trim(), value);
}
}
line = bufferedReader.readLine();
@@ -307,6 +309,99 @@
catch (IOException ioe)
{
throw new ConfigurationException(ioe.getMessage());
+ }
+ }
+
+ /**
+ * Parse the value to remove the quotes and ignoring the comment.
+ * Example:
+ *
+ * <code>"value" ; comment -> value</code>
+ *
+ * @param value
+ */
+ private String parseValue(String value)
+ {
+ value = value.trim();
+
+ boolean quoted = value.startsWith("\"");
+ boolean stop = false;
+ boolean escape = false;
+
+ int i = quoted ? 1 : 0;
+
+ StringBuffer result = new StringBuffer();
+ while (i < value.length() && !stop)
+ {
+ char c = value.charAt(i);
+
+ if (quoted)
+ {
+ if ('\\' == c && !escape)
+ {
+ escape = true;
+ }
+ else if (!escape && '"' == c)
+ {
+ stop = true;
+ }
+ else if (escape && '"' == c)
+ {
+ escape = false;
+ result.append(c);
+ }
+ else
+ {
+ if (escape)
+ {
+ escape = false;
+ result.append('\\');
+ }
+
+ result.append(c);
+ }
+ }
+ else
+ {
+ if (COMMENT_CHARS.indexOf(c) == -1)
+ {
+ result.append(c);
+ }
+ else
+ {
+ stop = true;
+ }
+ }
+
+ i++;
+ }
+
+ return result.toString().trim();
+ }
+
+ /**
+ * Add quotes around the specified value if it contains a comment character.
+ */
+ private String formatValue(String value)
+ {
+ boolean quoted = false;
+
+ for (int i = 0; i < COMMENT_CHARS.length() && !quoted; i++)
+ {
+ char c = COMMENT_CHARS.charAt(i);
+ if (value.indexOf(c) != -1)
+ {
+ quoted = true;
+ }
+ }
+
+ if (quoted)
+ {
+ return '"' + StringUtils.replace(value, "\"", "\\\"") + '"';
+ }
+ else
+ {
+ return value;
}
}
Modified: jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestINIConfiguration.java
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestINIConfiguration.java?view=diff&rev=526587&r1=526586&r2=526587
==============================================================================
--- jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestINIConfiguration.java (original)
+++ jakarta/commons/proper/configuration/trunk/src/test/org/apache/commons/configuration/TestINIConfiguration.java Sun Apr 8 11:05:17 2007
@@ -37,13 +37,28 @@
public class TestINIConfiguration extends TestCase
{
/** Constant for the content of an ini file. */
- private static final String INI_DATA = "[section1]\r\nvar1 = foo\r\n"
- + "var2 = 451\r\n\r\n[section2]\r\nvar1 = 123.45\r\nvar2 = bar\r\n\r\n"
- + "[section3]\r\nvar1 = true\r\n\r\n";
+ private static final String INI_DATA =
+ "[section1]\r\n"
+ + "var1 = foo\r\n"
+ + "var2 = 451\r\n"
+ + "\r\n"
+ + "[section2]\r\n"
+ + "var1 = 123.45\r\n"
+ + "var2 = bar\r\n"
+ + "\r\n"
+ + "[section3]\r\n"
+ + "var1 = true\r\n"
+ + "\r\n";
+
+ private static final String INI_DATA2 =
+ "[section4]\r\n"
+ + "var1 = \"quoted value\"\r\n"
+ + "var2 = \"quoted value\\nwith \\\"quotes\\\"\"\r\n"
+ + "var3 = 123 ; comment\r\n"
+ + "var4 = \"1;2;3\" ; comment\r\n";
- /**
- * Test of save method, of class
- * org.apache.commons.configuration.INIConfiguration.
+ /**
+ * Test of save method, of class {@link INIConfiguration}.
*/
public void testSave() throws Exception
{
@@ -59,8 +74,7 @@
}
/**
- * Test of load method, of class
- * org.apache.commons.configuration.INIConfiguration.
+ * Test of load method, of class {@link INIConfiguration}.
*/
public void testLoad() throws Exception
{
@@ -82,8 +96,7 @@
*
* @param data the data to load
*/
- private void checkLoad(String data) throws ConfigurationException,
- IOException
+ private void checkLoad(String data) throws ConfigurationException, IOException
{
Reader reader = new StringReader(data);
INIConfiguration instance = new INIConfiguration();
@@ -98,8 +111,7 @@
}
/**
- * Test of isCommentLine method, of class
- * org.apache.commons.configuration.INIConfiguration.
+ * Test of isCommentLine method, of class {@link INIConfiguration}.
*/
public void testIsCommentLine()
{
@@ -111,8 +123,7 @@
}
/**
- * Test of isSectionLine method, of class
- * org.apache.commons.configuration.INIConfiguration.
+ * Test of isSectionLine method, of class {@link INIConfiguration}.
*/
public void testIsSectionLine()
{
@@ -123,8 +134,7 @@
}
/**
- * Test of getSections method, of class
- * org.apache.commons.configuration.INIConfiguration.
+ * Test of getSections method, of class {@link INIConfiguration}.
*/
public void testGetSections()
{
@@ -137,4 +147,50 @@
Set result = instance.getSections();
assertEquals(expResult, result);
}
+
+ public void testQuotedValue() throws Exception
+ {
+ INIConfiguration config = new INIConfiguration();
+ config.load(new StringReader(INI_DATA2));
+
+ assertEquals("value", "quoted value", config.getString("section4.var1"));
+ }
+
+ public void testQuotedValueWithQuotes() throws Exception
+ {
+ INIConfiguration config = new INIConfiguration();
+ config.load(new StringReader(INI_DATA2));
+
+ assertEquals("value", "quoted value\\nwith \"quotes\"", config.getString("section4.var2"));
+ }
+
+ public void testValueWithComment() throws Exception
+ {
+ INIConfiguration config = new INIConfiguration();
+ config.load(new StringReader(INI_DATA2));
+
+ assertEquals("value", "123", config.getString("section4.var3"));
+ }
+
+ public void testQuotedValueWithComment() throws Exception
+ {
+ INIConfiguration config = new INIConfiguration();
+ config.load(new StringReader(INI_DATA2));
+
+ assertEquals("value", "1;2;3", config.getString("section4.var4"));
+ }
+
+ public void testWriteValueWithCommentChar() throws Exception
+ {
+ INIConfiguration config = new INIConfiguration();
+ config.setProperty("section.key1", "1;2;3");
+
+ StringWriter writer = new StringWriter();
+ config.save(writer);
+
+ INIConfiguration config2 = new INIConfiguration();
+ config2.load(new StringReader(writer.toString()));
+
+ assertEquals("value", "1;2;3", config2.getString("section.key1"));
+ }
}
Modified: jakarta/commons/proper/configuration/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jakarta/commons/proper/configuration/trunk/xdocs/changes.xml?view=diff&rev=526587&r1=526586&r2=526587
==============================================================================
--- jakarta/commons/proper/configuration/trunk/xdocs/changes.xml (original)
+++ jakarta/commons/proper/configuration/trunk/xdocs/changes.xml Sun Apr 8 11:05:17 2007
@@ -23,7 +23,12 @@
<body>
<release version="1.5-SNAPSHOT" date="in SVN">
+ <action dev="ebourg" type="update">
+ Fixed INIConfiguration to handle the quoted values and the lines
+ containing a value and a comment.
+ </action>
</release>
+
<release version="1.4" date="2007-04-08">
<action dev="oheger" type="update" issue="CONFIGURATION-256">
MapConfiguration and the web-based configurations now treat strings
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org