Simple Java Data File

In a prior post “A very simple data file metaformat” I presented an idea for an INI type of file and used an example implementation in the Groovy language.

I rewrote it in Java. One change was the use of section terminators. I’m still not sure about specifying of the data type in the section header.

Note the only testing of this is the embedded JUnit tests as shown in the source.
Update: A version of this is being successfully used to store test data for a suite of unit tests. Each file holds several sections with hundreds of lines each of XML and other types.

API

  • load(): Load all sections into a map.
  • load(String id, String subsection): Load a specific section.
  • T loadAs(final Class type, final String id, final String subsection): load a section as the given class type
  • next(): Pull event style reading.
  • void parse(final BufferedReader reader,
    final Callable callable)
    : Push event style reading.
  • void parse(final String filePath, final Runnable runnable): Push event style reading.

Update 1 20121208T1809-5
Java Jar manifest files have a very useful property. Attributes which are in the ‘main’ section are inherited by individual entries unless redefined in the entry. For the application I envisioned this metaformat, that would be ideal. One way of doing this is that any data contained in a section ‘x’ is also applicable or in scope for any section x/y. And, so forth, x/y/z would have the data in x/y available.

Update 2 20130107T2000-5
Added method Map load() throws IOException. This is more easy to use method of accessing inix file section data. This is only in the source Gist repo.

Update 3 20130113T1300-5
Added loadAs(…) to make loading specific types easier.

Update 4 20131028T0939-5
Doh, forgot about mentioning Apache Commons Configuration. It does support hierarchical INI files. However, the sections in that format only support property mappings.

The test data file is:

# Example very simple data file
! Another comment

#[>map:/junk]
#[<]

[>list:credit/tests]
one
two
three
[<]

[>credit/report]
one,two,three
[<]

[>properties:credit/config]
one=alpha
two=beta
three=charlie
[<]
    [>xml:credit/beans]
	<description>
	    <item>one</item>
	    <item>two</item>
	    <item>three</item>
	</description>
	[<]
	
[>notype/sub]
[<]	

[>credit/alerts]
["one","two","three"]
[<]
[>credit]
one
two
three
[<]

[>credit/coverages]
["one","two","three"]
[<]


 

Further Reading

  1. INI file
  2. Java Properties file format
  3. Data File Metaformats
  4. Here document
  5. JSON configuration file format
  6. Groovy Object Notation (GrON) for Data
    Interchange
  7. Cloanto Implementation of INI File Format
  8. http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
  9. URI
  10. RFC822
  11. MIME
Click to expand implementation source

Similar Posts:

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.

5 thoughts on “Simple Java Data File”

Leave a Reply

Your email address will not be published. Required fields are marked *