Spring Framework

org.springframework.util
Class DefaultPropertiesPersister

java.lang.Object
  extended by org.springframework.util.DefaultPropertiesPersister
All Implemented Interfaces:
PropertiesPersister

public class DefaultPropertiesPersister
extends java.lang.Object
implements PropertiesPersister

Default implementation of the PropertiesPersister interface. Follows the native parsing of java.util.Properties.

Allows for reading from any Reader and writing to any Writer, for example to specify a charset for a properties file. This is a capability that standard java.util.Properties unfortunately lacks up until JDK 1.5: You can only load files using the ISO-8859-1 charset there.

Loading from and storing to a stream delegates to Properties.load and Properties.store, respectively, to be fully compatible with the Unicode conversion as implemented by the JDK Properties class. On JDK 1.6, Properties.load/store will also be used for readers/writers, effectively turning this class into a plain backwards compatibility adapter.

The persistence code that works with Reader/Writer follows the JDK's parsing strategy but does not implement Unicode conversion, because the Reader/Writer should already apply proper decoding/encoding of characters. If you use prefer to escape unicode characters in your properties files, do not specify an encoding for a Reader/Writer (like ReloadableResourceBundleMessageSource's "defaultEncoding" and "fileEncodings" properties).

As of Spring 1.2.2, this implementation also supports properties XML files, through the loadFromXml and storeToXml methods. The default implementations delegate to JDK 1.5's corresponding methods, throwing an exception if running on an older JDK. Those implementations could be subclassed to apply custom XML handling on JDK 1.4, for example.

Since:
10.03.2004
Author:
Juergen Hoeller
See Also:
Properties, Properties.load(java.io.Reader), Properties.store(java.io.Writer, java.lang.String), ReloadableResourceBundleMessageSource.setPropertiesPersister(org.springframework.util.PropertiesPersister), ReloadableResourceBundleMessageSource.setDefaultEncoding(java.lang.String), ReloadableResourceBundleMessageSource.setFileEncodings(java.util.Properties)

Constructor Summary
DefaultPropertiesPersister()
           
 
Method Summary
protected  void doLoad(java.util.Properties props, java.io.Reader reader)
           
protected  void doStore(java.util.Properties props, java.io.Writer writer, java.lang.String header)
           
protected  boolean endsWithContinuationMarker(java.lang.String line)
           
protected  java.lang.String escape(java.lang.String str, boolean isKey)
           
 void load(java.util.Properties props, java.io.InputStream is)
          Load properties from the given InputStream into the given Properties object.
 void load(java.util.Properties props, java.io.Reader reader)
          Load properties from the given Reader into the given Properties object.
 void loadFromXml(java.util.Properties props, java.io.InputStream is)
          Load properties from the given XML InputStream into the given Properties object.
 void store(java.util.Properties props, java.io.OutputStream os, java.lang.String header)
          Write the contents of the given Properties object to the given OutputStream.
 void store(java.util.Properties props, java.io.Writer writer, java.lang.String header)
          Write the contents of the given Properties object to the given Writer.
 void storeToXml(java.util.Properties props, java.io.OutputStream os, java.lang.String header)
          Write the contents of the given Properties object to the given XML OutputStream.
 void storeToXml(java.util.Properties props, java.io.OutputStream os, java.lang.String header, java.lang.String encoding)
          Write the contents of the given Properties object to the given XML OutputStream.
protected  java.lang.String unescape(java.lang.String str)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultPropertiesPersister

public DefaultPropertiesPersister()
Method Detail

load

public void load(java.util.Properties props,
                 java.io.InputStream is)
          throws java.io.IOException
Description copied from interface: PropertiesPersister
Load properties from the given InputStream into the given Properties object.

Specified by:
load in interface PropertiesPersister
Parameters:
props - the Properties object to load into
is - the InputStream to load from
Throws:
java.io.IOException - in case of I/O errors
See Also:
Properties.load(java.io.Reader)

load

public void load(java.util.Properties props,
                 java.io.Reader reader)
          throws java.io.IOException
Description copied from interface: PropertiesPersister
Load properties from the given Reader into the given Properties object.

Specified by:
load in interface PropertiesPersister
Parameters:
props - the Properties object to load into
reader - the Reader to load from
Throws:
java.io.IOException - in case of I/O errors

doLoad

protected void doLoad(java.util.Properties props,
                      java.io.Reader reader)
               throws java.io.IOException
Throws:
java.io.IOException

endsWithContinuationMarker

protected boolean endsWithContinuationMarker(java.lang.String line)

unescape

protected java.lang.String unescape(java.lang.String str)

store

public void store(java.util.Properties props,
                  java.io.OutputStream os,
                  java.lang.String header)
           throws java.io.IOException
Description copied from interface: PropertiesPersister
Write the contents of the given Properties object to the given OutputStream.

Specified by:
store in interface PropertiesPersister
Parameters:
props - the Properties object to store
os - the OutputStream to write to
header - the description of the property list
Throws:
java.io.IOException - in case of I/O errors
See Also:
Properties.store(java.io.Writer, java.lang.String)

store

public void store(java.util.Properties props,
                  java.io.Writer writer,
                  java.lang.String header)
           throws java.io.IOException
Description copied from interface: PropertiesPersister
Write the contents of the given Properties object to the given Writer.

Specified by:
store in interface PropertiesPersister
Parameters:
props - the Properties object to store
writer - the Writer to write to
header - the description of the property list
Throws:
java.io.IOException - in case of I/O errors

doStore

protected void doStore(java.util.Properties props,
                       java.io.Writer writer,
                       java.lang.String header)
                throws java.io.IOException
Throws:
java.io.IOException

escape

protected java.lang.String escape(java.lang.String str,
                                  boolean isKey)

loadFromXml

public void loadFromXml(java.util.Properties props,
                        java.io.InputStream is)
                 throws java.io.IOException
Description copied from interface: PropertiesPersister
Load properties from the given XML InputStream into the given Properties object.

Specified by:
loadFromXml in interface PropertiesPersister
Parameters:
props - the Properties object to load into
is - the InputStream to load from
Throws:
java.io.IOException - in case of I/O errors
See Also:
Properties.loadFromXML(java.io.InputStream)

storeToXml

public void storeToXml(java.util.Properties props,
                       java.io.OutputStream os,
                       java.lang.String header)
                throws java.io.IOException
Description copied from interface: PropertiesPersister
Write the contents of the given Properties object to the given XML OutputStream.

Specified by:
storeToXml in interface PropertiesPersister
Parameters:
props - the Properties object to store
os - the OutputStream to write to
header - the description of the property list
Throws:
java.io.IOException - in case of I/O errors
See Also:
Properties.storeToXML(java.io.OutputStream, String)

storeToXml

public void storeToXml(java.util.Properties props,
                       java.io.OutputStream os,
                       java.lang.String header,
                       java.lang.String encoding)
                throws java.io.IOException
Description copied from interface: PropertiesPersister
Write the contents of the given Properties object to the given XML OutputStream.

Specified by:
storeToXml in interface PropertiesPersister
Parameters:
props - the Properties object to store
os - the OutputStream to write to
header - the description of the property list
encoding - the encoding to use
Throws:
java.io.IOException - in case of I/O errors
See Also:
Properties.storeToXML(java.io.OutputStream, String, String)

Spring Framework