Author: ebourg Date: Sun Jun 21 13:03:27 2009 New Revision: 787006 URL: http://svn.apache.org/viewvc?rev=787006&view=rev Log: Fixed empty dictionaries <dict/> (CONFIGURATION-362)
Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.plist.xml Modified: commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java?rev=787006&r1=787005&r2=787006&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/main/java/org/apache/commons/configuration2/plist/XMLPropertyListConfiguration.java Sun Jun 21 13:03:27 2009 @@ -278,35 +278,51 @@ } /** + * Returns the sequence of space characters to indent at the specified level. + */ + private String getPadding(int level) + { + return StringUtils.repeat(" ", level * INDENT_SIZE); + } + + /** * Append a node to the writer, indented according to a specific level. */ private void printNode(PrintWriter out, int indentLevel, ConfigurationNode node) { - String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); + String padding = getPadding(indentLevel); if (node.getName() != null) { out.println(padding + "<key>" + StringEscapeUtils.escapeXml(node.getName()) + "</key>"); } - List<ConfigurationNode> children = node.getChildren(); - if (!children.isEmpty()) + if (node.getValue() == null) { - out.println(padding + "<dict>"); - - Iterator<ConfigurationNode> it = children.iterator(); - while (it.hasNext()) + List<ConfigurationNode> children = node.getChildren(); + + if (children.isEmpty()) + { + out.println(padding + "<dict/>"); + } + else { - ConfigurationNode child = it.next(); - printNode(out, indentLevel + 1, child); + out.println(padding + "<dict>"); - if (it.hasNext()) + Iterator<ConfigurationNode> it = children.iterator(); + while (it.hasNext()) { - out.println(); + ConfigurationNode child = it.next(); + printNode(out, indentLevel + 1, child); + + if (it.hasNext()) + { + out.println(); + } } - } - out.println(padding + "</dict>"); + out.println(padding + "</dict>"); + } } else { @@ -320,7 +336,7 @@ */ private void printValue(PrintWriter out, int indentLevel, Object value) { - String padding = StringUtils.repeat(" ", indentLevel * INDENT_SIZE); + String padding = getPadding(indentLevel); if (value instanceof Date) { @@ -397,13 +413,17 @@ } else if (value instanceof byte[]) { - String base64 = new String(Base64.encodeBase64((byte[]) value)); + String base64 = Base64.encodeBase64((byte[]) value); out.println(padding + "<data>" + StringEscapeUtils.escapeXml(base64) + "</data>"); } - else + else if (value != null) { out.println(padding + "<string>" + StringEscapeUtils.escapeXml(String.valueOf(value)) + "</string>"); } + else + { + out.println(padding + "<string/>"); + } } /** @@ -502,6 +522,7 @@ } else { + PListNode node = (PListNode) peek(); if ("string".equals(qName)) { ((PListNode) peek()).addValue(buffer.toString()); Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java?rev=787006&r1=787005&r2=787006&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/java/org/apache/commons/configuration2/plist/TestXMLPropertyListConfiguration.java Sun Jun 21 13:03:27 2009 @@ -271,8 +271,30 @@ { assertEquals("Value of the '" + key + "' property", config.getProperty(key), checkConfig.getProperty(key)); } + } + } + + public void testSaveEmptyDictionary() throws Exception + { + File savedFile = new File("target/testsave.plist.xml"); + // remove the file previously saved if necessary + if (savedFile.exists()) + { + assertTrue(savedFile.delete()); } + + // save the configuration + String filename = savedFile.getAbsolutePath(); + config.save(filename); + + assertTrue("The saved file doesn't exist", savedFile.exists()); + + // read the configuration and compare the properties + Configuration checkConfig = new XMLPropertyListConfiguration(new File(filename)); + + assertEquals(null, config.getProperty("empty-dictionary")); + assertEquals(null, checkConfig.getProperty("empty-dictionary")); } /** Modified: commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.plist.xml URL: http://svn.apache.org/viewvc/commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.plist.xml?rev=787006&r1=787005&r2=787006&view=diff ============================================================================== --- commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.plist.xml (original) +++ commons/proper/configuration/branches/configuration2_experimental/src/test/resources/test.plist.xml Sun Jun 21 13:03:27 2009 @@ -80,5 +80,8 @@ </dict> </dict> + <key>empty-dictionary</key> + <dict/> + </dict> </plist>