Reformatting and Reordering

Since version 2.0.0 Apron provides a de.poiu.apron.reformatting.Reformatter class that allows reformatting and reordering the content of .properties files.

The specific behaviour when reformatting and reordering can be specified via a de.poiu.apron.reformatting.ReformatOptions object.

For convenience the de.poiu.apron.PropertyFile class provides some methods to reformat or reorder the entries in that PropertyFile.

Reformatting

When reformatting a format string can be given to specify how to format leading whitespace, separators and line endings. The default format string is <key> = <value>\n for

  • no leading whitespace

  • an equals sign surrounded by a single whitespace on each side as separator

  • a \n (line feed) character as new line character

By default the keys and values of the reformatted files are not modified. That means any special formatting (like insignificant whitespace, newlines and escape characters) remain after reformatting.

This can be changed via the reformatKeyAndValue option in which case these will be modified as well.

This is an example for reformatting a PropertyFile:

 1// Create the ReformatOptions to:
 2//   - read and write with UTF-8 (which is the default anyway),
 3//   - reformat via a custom format string and 
 4//   - also reformat the keys and values.
 5final ReformatOptions reformatOptions= ReformatOptions.create()
 6    .with(UTF_8)
 7    .withFormat("<key>: <value>\r\n")
 8    .withReformatKeyAndValue(true)
 9    ;
10
11// Create a Reformatter with the specified ReformatOptions
12final Reformatter reformatter= new Reformatter(reformatOptions);
13
14// Reformat a single .properties file according to the specified ReformatOptions
15reformatter.reformat(new File("myproperties.properties"));

Reordering

Reordering the content of .properties files can be done either by alphabetically sorting the keys of the key-value pairs or by referring to a template file in which case the keys are ordered in the same order as in the template file.

Apron allows specifying how to handle non-property lines (comments and empty lines) when reordering. It is possible to move them along with the key-value pair that follows them or the key-value pair that precedes them or be just left at the same position as they are.

This is an example for reordering a PropertyFile:

 1// Create the ReformatOptions to use that does not reorder empty lines and comments
 2final ReformatOptions reorderOptions= ReformatOptions.create()
 3  .with(AttachCommentsTo.ORIG_LINE)
 4  ;
 5
 6// Create a Reformatter with the specified ReformatOptions
 7final Reformatter reformatter= new Reformatter(reorderOptions);
 8
 9// Reorder a single .properties file alphabetically 
10// according to the specified ReformatOptions
11reformatter.reorderByKey(new File("myproperties.properties"));
12
13// Reorder a single .properties file according to the order in another .properties file
14// This time we want to reorder comments and empty lines along with the 
15// key-value pair that follows them. This is possible by specifying a ReformatOptions 
16// object when calling the corresponding reorder method.
17reformatter.reorderByTemplate(
18  new File("template.properties"),
19  new File("someOther.properties"),
20  reorderOptions.with(AttachCommentsTo.NEXT_PROPERTY)
21);