commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ep...@apache.org
Subject cvs commit: jakarta-commons/configuration/src/java/org/apache/commons/configuration BasePropertiesConfiguration.java
Date Fri, 30 Jan 2004 14:05:01 GMT
epugh       2004/01/30 06:05:01

  Modified:    configuration/xdocs changes.xml examples.xml overview.xml
               configuration/src/test/org/apache/commons/configuration
                        TestPropertiesConfiguration.java
               configuration/conf test.properties
               configuration/src/java/org/apache/commons/configuration
                        BasePropertiesConfiguration.java
  Log:
  Escape and Unescape crlf type characters when working with properties file.
  
  Revision  Changes    Path
  1.6       +4 -0      jakarta-commons/configuration/xdocs/changes.xml
  
  Index: changes.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/changes.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- changes.xml	23 Jan 2004 11:52:36 -0000	1.5
  +++ changes.xml	30 Jan 2004 14:05:00 -0000	1.6
  @@ -7,6 +7,10 @@
   
     <body>
       <release version="1.0-dev-4" date="">
  +	 <action dev="ebourg" type="add">
  +      	For configuration based on properties files, allow characters like \n etc
  +      	to be escaped and unescaped.
  +     </action>      
   	 <action dev="epugh" type="add">
         	ConfigurationFactory now throws ConfigurationLoadException.
        </action>       	
  
  
  
  1.4       +10 -0     jakarta-commons/configuration/xdocs/examples.xml
  
  Index: examples.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/examples.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- examples.xml	16 Jan 2004 14:56:45 -0000	1.3
  +++ examples.xml	30 Jan 2004 14:05:00 -0000	1.4
  @@ -33,6 +33,16 @@
   				Note that this file complies to the typical format of
   				properties files.
   			</p>
  +			<p>
  +				You can also have in a simple properties file various characters
  +				that are typically escaped like \n, \t etc:
  +			</p>
  +    			<source>
  +<![CDATA[
  +#Unescaped string that will be escaped
  +test.unescape = This \n string \t contains \" escaped \\ characters
  +]]>
  +				</source>			
   		</subsection>
   		
   		<subsection name="The configuration definition file">
  
  
  
  1.3       +28 -23    jakarta-commons/configuration/xdocs/overview.xml
  
  Index: overview.xml
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/xdocs/overview.xml,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- overview.xml	16 Jan 2004 14:56:45 -0000	1.2
  +++ overview.xml	30 Jan 2004 14:05:00 -0000	1.3
  @@ -88,27 +88,27 @@
         </subsection>
       </section>
       <section name="Configuration Details">
  -    	<p>
  -    	Configuration is done by taking the configuration XML file and using included Digester
rules,
  -    	parsing the individual configurations.  Make sure to include the various dependencies
required for
  -    	each type of configuration!  If you have an XML file, you'll need dom4j.jar for instance!
  -    	</p>
  -    	<subsection name="Classic Properties File">
  +      <p>
  +      Configuration is done by taking the configuration XML file and using included Digester
rules,
  +      parsing the individual configurations.  Make sure to include the various dependencies
required for
  +      each type of configuration!  If you have an XML file, you'll need dom4j.jar for instance!
  +      </p>
  +      <subsection name="Classic Properties File">
           <source>
   <![CDATA[       
     <properties className="org.apache.commons.configuration.PropertiesConfiguration" fileName="conf/test.properties"/>
   ]]>   
           </source>
           <p>
  -    	This configuration file is very simple.  You just need to specify the path to the
property file.
  -    	</p>
  -    	</subsection>
  -    	<subsection name="XML Properties File">
  +      This configuration file is very simple.  You just need to specify the path to the
property file.
  +      </p>
  +      </subsection>
  +      <subsection name="XML Properties File">
           <source>
   <![CDATA[       
     <dom4j className="org.apache.commons.configuration.DOM4JConfiguration" fileName="conf/test.xml"/>
   ]]>   
  -        </source>    	
  +        </source>     
           <p>
           The configuration is very similar to the classic properties file.  However, the
xml file must be in a specific
           format.  Currently there is no DTD.
  @@ -123,7 +123,7 @@
       </subelement>
     </element2>
     <test>
  -  	<short>8</short>
  +    <short>8</short>
     </test>
   </baseElement>
   ]]>   
  @@ -132,35 +132,40 @@
           In the above example, the root element is ignored.  So to get the value "8", you
would request from your 
           Configuration object the key "test.short".  The root element can be called anything.
           </p>
  -    	</subsection>    	
  -    	<subsection name="JNDI Properties File">
  +      </subsection>     
  +      <subsection name="JNDI Properties File">
           <source>
   <![CDATA[       
     <jndi className="org.apache.commons.configuration.JNDIConfiguration" prefix="java:comp/env"/>
   ]]>   
  -        </source>    	
  +        </source>     
           <p>
           This configuration is very useful for setting environment specific settings like
mail servers! The
  -        prefix tells the ConfigurationFactory what the root will be to look up your configuration
settings. To 
  -        enter a value under the key "test.short" you would replcace the '.' with '/' in
your web.xml.
  +        prefix tells the ConfigurationFactory what the root will be to look up your configuration
settings. 
           </p>
           <source>
  -<![CDATA[       
  +<![CDATA[      
  +    <env-entry>
  +        <env-entry-name>smtp</env-entry-name>
  +        <env-entry-value>127.0.0.1</env-entry-value>
  +        <env-entry-type>java.lang.String</env-entry-type>
  +    </env-entry>
  +    
       <env-entry>
           <env-entry-name>test/short</env-entry-name>
           <env-entry-value>80</env-entry-value>
  -        <env-entry-type>java.lang.String</env-entry-type>
  +        <env-entry-type>java.lang.Short</env-entry-type>
       </env-entry>
   
   ]]>   
  -        </source>    	
  +        </source>     
           <p>
  -        Note!  If you have a property called "file.name" with spaces in it, then it will
be translated
  -        as the key "file/name".  Therefore, you should NOT use spaces in the name of properties
that
  +        <strong>Note!</strong>  If you have a property called "test.short"
with spaces in it, then it will be translated
  +        as the key "test/short".  Therefore, you should NOT use spaces in the name of properties
that
           are loaded from JNDI!  If you do want to use them, then make sure to convert in
your web.xml the
           "." characters to "/" characters, like in the test.short example above.
           </p>
  -    	</subsection>    	
  +      </subsection>     
       </section>
   
   </body>
  
  
  
  1.4       +24 -8     jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java
  
  Index: TestPropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/test/org/apache/commons/configuration/TestPropertiesConfiguration.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestPropertiesConfiguration.java	16 Jan 2004 14:23:39 -0000	1.3
  +++ TestPropertiesConfiguration.java	30 Jan 2004 14:05:00 -0000	1.4
  @@ -56,6 +56,7 @@
   
   import java.io.File;
   import java.util.ArrayList;
  +import java.util.Iterator;
   import java.util.List;
   
   /**
  @@ -73,28 +74,38 @@
   
       private String testBasePath = new File("conf").getAbsolutePath();
       private String testBasePath2 = new File("conf").getAbsoluteFile().getParentFile().getAbsolutePath();
  +    private File testSavePropertiesFile = new File("target/testsave.properties");
   
       protected void setUp() throws Exception
       {
           conf = new PropertiesConfiguration(testProperties);
  + 
       }
   
  -    public void atestSave() throws Exception
  +    public void testSave() throws Exception
       {
  -        PropertiesConfiguration toSave = new PropertiesConfiguration();
  -        toSave.addProperty("string", "value1");
  +    	if(testSavePropertiesFile.exists()){
  +    		assertTrue(testSavePropertiesFile.delete());
  +    	}
  +    	conf.addProperty("string", "value1");
           List list = new ArrayList();
           for (int i = 1; i < 5; i++)
           {
               list.add("value" + i);
           }
  -        toSave.addProperty("array", list);
  -        String filename = "STRING0";
  +        conf.addProperty("array", list);
   
  -        toSave.save(filename);
  +        conf.save(testSavePropertiesFile.getAbsolutePath());
  +        
  +        PropertiesConfiguration checkConfig = new PropertiesConfiguration(testSavePropertiesFile.getAbsolutePath());
  +        for (Iterator i = conf.getKeys();i.hasNext();){
  +        	String key = (String)i.next();
  +        	assertTrue(checkConfig.containsKey(key));
  +        	assertEquals(conf.getString(key),checkConfig.getString(key));
  +        }
       }
   
  -    public void atestLoadViaProperty() throws Exception
  +    public void testLoadViaProperty() throws Exception
       {
           PropertiesConfiguration pc = new PropertiesConfiguration();
           pc.setFileName(testProperties);
  @@ -130,5 +141,10 @@
           pc.load();
   
           assertTrue("Make sure we have multiple keys", pc.getBoolean("test.boolean"));
  +    }
  +
  +    public void testGetStringWithEscapedChars()
  +    {
  +        assertEquals("String with escaped characters", "This \n string \t contains \" escaped
\\ characters", conf.getString("test.unescape"));
       }
   }
  
  
  
  1.2       +2 -0      jakarta-commons/configuration/conf/test.properties
  
  Index: test.properties
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/conf/test.properties,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- test.properties	23 Dec 2003 15:09:05 -0000	1.1
  +++ test.properties	30 Jan 2004 14:05:01 -0000	1.2
  @@ -4,6 +4,8 @@
   
   include = include.properties
   
  +test.unescape = This \n string \t contains \" escaped \\ characters
  +
   #
   # Other test properties
   #
  
  
  
  1.2       +5 -3      jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java
  
  Index: BasePropertiesConfiguration.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/configuration/src/java/org/apache/commons/configuration/BasePropertiesConfiguration.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- BasePropertiesConfiguration.java	23 Dec 2003 15:09:05 -0000	1.1
  +++ BasePropertiesConfiguration.java	30 Jan 2004 14:05:01 -0000	1.2
  @@ -66,6 +66,7 @@
   import java.util.Date;
   import java.util.Iterator;
   
  +import org.apache.commons.lang.StringEscapeUtils;
   import org.apache.commons.lang.StringUtils;
   
   /**
  @@ -145,6 +146,7 @@
    *      second.prop = ${first.prop}/second
    * </pre>
    *
  + * @author <a href="mailto:e.bourg@cross-systems.com">Emmanuel Bourg</a>
    * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
    * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
    * @author <a href="mailto:daveb@miceda-data">Dave Bryson</a>
  @@ -257,7 +259,7 @@
                   }
                   else
                   {
  -                    addProperty(key, value);
  +                    addProperty(key, StringEscapeUtils.unescapeJava(value));
                   }
               }
           }
  @@ -428,7 +430,7 @@
           {
               write(key);
               write(" = ");
  -            write(value != null ? value : "");
  +            write(value != null ? StringEscapeUtils.escapeJava(value) : "");          
  
               write('\n');
           }
   
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message