FELIX-4917 : FilePersistenceManager doesn't support comments
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1686141 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java b/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java
index d1a54a9..17534c3 100644
--- a/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java
+++ b/configadmin/src/main/java/org/apache/felix/cm/file/ConfigurationHandler.java
@@ -75,6 +75,8 @@
protected static final int TOKEN_VAL_OPEN = '"'; // '{';
protected static final int TOKEN_VAL_CLOS = '"'; // '}';
+ protected static final int TOKEN_COMMENT = '#';
+
// simple types (string & primitive wrappers)
protected static final int TOKEN_SIMPLE_STRING = 'T';
protected static final int TOKEN_SIMPLE_INTEGER = 'I';
@@ -290,12 +292,12 @@
Hashtable configuration = new Hashtable();
token = 0;
- while ( nextToken( pr ) == TOKEN_NAME )
+ while ( nextToken( pr, true ) == TOKEN_NAME )
{
String key = tokenValue;
// expect equal sign
- if ( nextToken( pr ) != TOKEN_EQ )
+ if ( nextToken( pr, false ) != TOKEN_EQ )
{
throw readFailure( token, TOKEN_EQ );
}
@@ -566,8 +568,7 @@
}
}
-
- private int nextToken( PushbackReader pr ) throws IOException
+ private int nextToken( PushbackReader pr, final boolean newLine ) throws IOException
{
int c = ignorableWhiteSpace( pr );
@@ -577,6 +578,22 @@
return ( token = c );
}
+ // check for comment
+ if ( newLine && c == TOKEN_COMMENT )
+ {
+ // skip everything until end of line
+ do
+ {
+ c = read( pr );
+ } while ( c != -1 && c != '\n' );
+ if ( c == -1 )
+ {
+ return ( token = c);
+ }
+ // and start over
+ return nextToken( pr, true );
+ }
+
// check whether there is a name
if ( NAME_CHARS.get( c ) || !TOKEN_CHARS.get( c ) )
{
diff --git a/configadmin/src/test/java/org/apache/felix/cm/file/ConfigurationHandlerTest.java b/configadmin/src/test/java/org/apache/felix/cm/file/ConfigurationHandlerTest.java
new file mode 100644
index 0000000..00d062d
--- /dev/null
+++ b/configadmin/src/test/java/org/apache/felix/cm/file/ConfigurationHandlerTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.felix.cm.file;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Dictionary;
+
+import junit.framework.TestCase;
+
+public class ConfigurationHandlerTest extends TestCase
+{
+
+ private static final String PAR_1 = "mongouri";
+ private static final String VAL_1 = "127.0.0.1:27017";
+ private static final String PAR_2 = "customBlobStore";
+ private static final String VAL_2 = "true";
+
+ private static final String CONFIG =
+ "#mongodb URI\n" +
+ PAR_1 + "=\"" + VAL_1 + "\"\n" +
+ "\n" +
+ " # custom datastore\n" +
+ PAR_2 + "=B\"" + VAL_2 + "\"\n";
+
+ public void testComments() throws IOException
+ {
+ final Dictionary dict = ConfigurationHandler.read(new ByteArrayInputStream(CONFIG.getBytes("UTF-8")));
+ assertEquals(2, dict.size());
+ assertEquals(VAL_1, dict.get(PAR_1));
+ assertEquals(VAL_2, dict.get(PAR_2).toString());
+ }
+}