commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From thc...@gmail.com
Subject Re: [configuration] Problem with XMLConfiguration.setProperty(String, Object) and setDelimiterParsingDisabled
Date Sun, 22 Apr 2012 11:38:09 GMT
Hi.

Sorry for the code not properly indented (content of DelimiterExample.java).

Here it goes again:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class DelimiterExample
{
  // Configuration file that will have the
  // setDelimiterParsingDisabled as _false_.
  private static final String CONF_DELIMITER_ENABLED_XML =
    "ConfDelimiterEnabled.xml";

  // Configuration file that will have the
  // setDelimiterParsingDisabled as_true_.
  private static final String CONF_DELIMITER_DISABLED_XML =
    "ConfDelimiterDisabled.xml";

  // Key of the data used in the configuration files.
  private static final String LIST_ELEMENT_KEY = "list.element";

  public static void main(String[] args) throws ConfigurationException
  {
    // Data that will be saved in the configuration files.
    List<String> data = new ArrayList<String>(3);
    data.add("value 1");
    data.add("value 2");
    data.add("value 3");


    // First using setDelimiterParsingDisabled as false.

    XMLConfiguration confDelimiterEnabledWrite =
      new XMLConfiguration(CONF_DELIMITER_ENABLED_XML);

    confDelimiterEnabledWrite.setProperty(LIST_ELEMENT_KEY, data);
    // Writes:
    //  <list>
    //  <element>value 1</element>
    //  <element>value 2</element>
    //  <element>value 3</element>
    //  </list>

    confDelimiterEnabledWrite.save();

    XMLConfiguration confDelimiterEnabledRead =
      new XMLConfiguration(CONF_DELIMITER_ENABLED_XML);
    String[] values =
      confDelimiterEnabledRead.getStringArray(LIST_ELEMENT_KEY);

    // Doesn't output anything as they are equals. Expected behaviour.
    if (!Arrays.equals(values, data.toArray()))
    {
      System.out.println(
        "setDelimiterParsingDisabled(false): Data not equal!");
    }


    // Now using setDelimiterParsingDisabled as true.

    XMLConfiguration confDelimiterDisabledWrite = new XMLConfiguration();
    confDelimiterDisabledWrite.setDelimiterParsingDisabled(true);
    confDelimiterDisabledWrite.setFileName(CONF_DELIMITER_DISABLED_XML);
    confDelimiterDisabledWrite.load();

    confDelimiterDisabledWrite.setProperty(LIST_ELEMENT_KEY, data);
    // Writes:
    //  <list>
    //  <element>[value 1, value 2, value 3]</element>
    //  </list>

    // Unexpected behaviour!
    // Shouldn't it write the same as with
    // setDelimiterParsingDisabled as false? as it is a list.

    confDelimiterDisabledWrite.save();

    XMLConfiguration confDelimiterDisabledRead = new XMLConfiguration();
    confDelimiterDisabledRead.setDelimiterParsingDisabled(true);
    confDelimiterDisabledRead.setFileName(CONF_DELIMITER_DISABLED_XML);
    confDelimiterDisabledRead.load();

    values = confDelimiterDisabledRead.getStringArray(LIST_ELEMENT_KEY);

    // As it writes other thing this will output that they are not equals.
    if (!Arrays.equals(values, data.toArray()))
    {
      System.out.println(
        "setDelimiterParsingDisabled(true): Data not equal!");
    }
  }

}


Best regards.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message