Groovy implementation of INIX file format

About to write another extension of Ant hook scripts and discovered that the file format I needed was too complex. So, in this post I present another implementation in Groovy.

Update: changed the URL of this blog post.

In two prior posts I presented a very simple metadata file storage approach. Pretty much using the INI file format as an external “heredoc”. I now call this an INIX format. Btw, this file extension is already being used by the Adobe InDesign product.

In the example below, the section is uniquely identified with a path, and the query string supplies parameters to any client accessing the data.

[>hook/root/compile?when=after,skip=false]
    println " hook: root,{target=${event.target.name},when=post,event=$event}"
[<]

The terminal tag could have also been written as: [<hook/root/compile]

The change from the previous INIX format is in the section header format. It is now more like a URL: [>path#fragment-id?query-string] The query string is not following the full URL spec, it uses ‘&’ to separate entries. Also, commas are used as in Groovy Object Notation (GRON).

Example
In a blog post, “Ant hooks using Groovy, INIX, and XMLTask“, I show how an inix file can be used to store multiple Groovy scripts to be used as Ant hooks.

Update Feb 5, 2014: Just noticed that Git also uses the INI file subsection approach, for example [section “subsection”]. See “CONFIGURATION FILE” section here.
 

Implementation
Source code available at Github: https://gist.github.com/josefbetancourt/7701645

Listing 2, Implementation

Test class
Note that there are not enough tests and the implementation code has not been reviewed.

Listing 3, Test class

The test data is:

Listing 4, data file

Grammar
A possible grammar follows, but has not been ‘checked’ by attempted use of a parser generator like Antlr.

section  : '[>' path ('#' fragment)? ('?' args)? ']' data '[<' path? ']';
path     :  NAME ('/' NAME)*;
fragment :  NAME;
args     :  (NAME=NAME (',' NAME=NAME)*)?;
data     :  (ANYTHING CRLF)*;
NAME     :  ('a'..'z' | 'A'..'Z')('a' .. 'z' | 'A'..'Z'|'0'..'9'|'_');
TODO:

  1. Now that the query string is being used, we can add “import” of sections. A section ID beginning with ‘@’, will reuse the contents of another section. Any content in the destination section will be appended to the imported content of the source section. Import here does not mean a namespace concern as in Java imports.
  2. Allow multiple params per param key: ?measure=21,measure=34,measure=90. Or better yet, just allow array in arg string: measure=[21,34,90],color=red

 

Further Reading

  1. INI file
  2. Data File Metaformats
  3. Here document
  4. JSON configuration file format
  5. Groovy Object Notation (GrON) for Data
    Interchange
  6. Cloanto Implementation of INI File Format
  7. http://groovy.codehaus.org/Tutorial+5+-+Capturing+regex+groups
  8. URI
  9. Designing a simple file format
  10. The Universal Design Pattern

Similar Posts:

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

2 thoughts on “Groovy implementation of INIX file format”

Leave a Reply

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