LayoutPreservingProperties (Apache Ant API)
Description: declaration: package: org.apache.tools.ant.util, class: LayoutPreservingProperties

Package org.apache.tools.ant.util

# Class LayoutPreservingProperties

java.lang.Object
java.util.Dictionary<K,V>
java.util.Hashtable<Object,Object>
java.util.Properties
org.apache.tools.ant.util.LayoutPreservingProperties

All Implemented Interfaces:
  `Serializable`, `Cloneable`, `Map<Object,Object>`

public class LayoutPreservingProperties extends Properties

A Properties collection which preserves comments and whitespace present in the input stream from which it was loaded.

The class defers the usual work of the java.util.Properties class to there, but it also keeps track of the contents of the input stream from which it was loaded (if applicable), so that it can write out the properties in as close a form as possible to the input.

If no changes occur to property values, the output should be the same as the input, except for the leading date stamp, as normal for a properties file. Properties added are appended to the file. Properties whose values are changed are changed in place. Properties that are removed are excised. If the `removeComments` flag is set, then the comments immediately preceding the property are also removed.

If a second set of properties is loaded into an existing set, the lines of the second set are added to the end. Note however, that if a property already stored is present in a stream subsequently loaded, then that property is removed before the new value is set. For example, consider the file

     # the first line
     alpha=one

     # the second line
     beta=two

This file is loaded, and then the following is also loaded into the same `LayoutPreservingProperties` object

     # association
     beta=band

     # and finally
     gamma=rays

The resulting collection sequence of logical lines depends on whether or not `removeComments` was set at the time the second stream is loaded. If it is set, then the resulting list of lines is

     # the first line
     alpha=one

     # association
     beta=band

     # and finally
     gamma=rays

If the flag is not set, then the comment "the second line" is retained, although the key-value pair `beta=two` is removed.

See Also:
  - Serialized Form

- ## Field Summary

  ### Fields inherited from class java.util.Properties

  `defaults`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `LayoutPreservingProperties()`
  Create a new, empty, Properties collection, with no defaults.
  `LayoutPreservingProperties(Properties defaults)`
  Create a new, empty, Properties collection, with the specified defaults.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `clear()`
  `Object`
  `clone()`
  `boolean`
  `isRemoveComments()`
  Returns `true` if comments are removed along with properties, or `false` otherwise.
  `void`
  `listLines(PrintStream out)`
  Echo the lines of the properties (including blanks and comments) to the stream.
  `void`
  `load(InputStream inStream)`
  `Object`
  `put(Object key, Object value)`
  `Object`
  `remove(Object key)`
  `void`
  `saveAs(File dest)`
  Save the properties to a file.
  `Object`
  `setProperty(String key, String value)`
  `void`
  `setRemoveComments(boolean val)`
  Sets the behaviour for comments accompanying properties that are being removed.
  `void`
  `store(OutputStream out, String header)`

  ### Methods inherited from class java.util.Properties

  `compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, equals, forEach, get, getOrDefault, getProperty, getProperty, hashCode, isEmpty, keys, keySet, list, list, load, loadFromXML, merge, propertyNames, putAll, putIfAbsent, rehash, remove, replace, replace, replaceAll, save, size, store, storeToXML, storeToXML, storeToXML, stringPropertyNames, toString, values`

  ### Methods inherited from class java.lang.Object

  `finalize, getClass, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### LayoutPreservingProperties

    public LayoutPreservingProperties()
    Create a new, empty, Properties collection, with no defaults.

  - ### LayoutPreservingProperties

    public LayoutPreservingProperties(Properties defaults)
    Create a new, empty, Properties collection, with the specified defaults.
    Parameters:
      `defaults` - the default property values

- ## Method Details

  - ### isRemoveComments

    public boolean isRemoveComments()
    Returns `true` if comments are removed along with properties, or `false` otherwise. If `true`, then when a property is removed, the comment preceding it in the original file is removed also.
    Returns:
      `true` if leading comments are removed when a property is removed; `false` otherwise

  - ### setRemoveComments

    public void setRemoveComments(boolean val)
    Sets the behaviour for comments accompanying properties that are being removed. If `true`, then when a property is removed, the comment preceding it in the original file is removed also.
    Parameters:
      `val` - `true` if leading comments are to be removed when a property is removed; `false` otherwise

  - ### load

    public void load(InputStream inStream) throws IOException
    Overrides:
      `load` in class `Properties`
    Throws:
      `IOException`

  - ### put

    public Object put(Object key, Object value) throws NullPointerException
    Specified by:
      `put` in interface `Map<Object,Object>`
    Overrides:
      `put` in class `Properties`
    Throws:
      `NullPointerException`

  - ### setProperty

    public Object setProperty(String key, String value) throws NullPointerException
    Overrides:
      `setProperty` in class `Properties`
    Throws:
      `NullPointerException`

  - ### clear

    public void clear()
    Specified by:
      `clear` in interface `Map<Object,Object>`
    Overrides:
      `clear` in class `Properties`

  - ### remove

    public Object remove(Object key)
    Specified by:
      `remove` in interface `Map<Object,Object>`
    Overrides:
      `remove` in class `Properties`

  - ### clone

    public Object clone()
    Overrides:
      `clone` in class `Properties`

  - ### listLines

    public void listLines(PrintStream out)
    Echo the lines of the properties (including blanks and comments) to the stream.
    Parameters:
      `out` - the stream to write to

  - ### saveAs

    public void saveAs(File dest) throws IOException
    Save the properties to a file.
    Parameters:
      `dest` - the file to write to
    Throws:
      `IOException` - if save fails

  - ### store

    public void store(OutputStream out, String header) throws IOException
    Overrides:
      `store` in class `Properties`
    Throws:
      `IOException`