4

My application has many properties/text configuration files. Is there a common way to validate these kind of files? May be some tools or something like xsd? The files are large (more 1000 rows), so I frequently make mistake. Information in file in majority of cases - is path to different directories. So it will be good if they exist and are consistent. E.g. if I have

mydata.root="c:\data"

and after I have:

myreports=${mydata.root}/reports

that will be good check that c:\data and c:\data\report exist

and not written (some hundreds rows down) e.g.

myreports=${mdata.root}/reports
gprathour
  • 14,813
  • 5
  • 66
  • 90
user710818
  • 23,228
  • 58
  • 149
  • 207
  • This `${}` thing only works in Ant process; that is, it doesn't work from a simple `.properties` file if it is outside of Ant process. It won't work using `load()` method of Java `Properties` class. Anyway, you can check the existence of a file/folder in a Ant process as follows [http://stackoverflow.com/questions/1163998/do-i-have-any-way-to-check-the-existence-of-a-directory-in-ant-not-a-file](http://stackoverflow.com/questions/1163998/do-i-have-any-way-to-check-the-existence-of-a-directory-in-ant-not-a-file) – ecle Dec 12 '11 at 09:01

2 Answers2

5

You could do this validation in your build file.

For example, the following build file defines a macrodef validate-file-property which validates that a specified property is defined and that it exists as a file or dir in the file system.

<project default="init">

  <property file="test.properties"/>

  <target name="init">
    <validate-file-property property="program.files" type="dir"/>
    <validate-file-property property="mydata.root" type="dir"/>
    <validate-file-property property="foo"/>
  </target>

  <macrodef name="validate-file-property">
    <attribute name="property"/>
    <attribute name="type" default="file"/>
    <sequential>
      <fail unless="@{property}" message="The property '@{property}' is undefinded."/>
      <available file="${@{property}}" property="@{property}.exists" type="@{type}"/>
      <fail unless="@{property}.exists" message="The @{type} '@{property}' with value '${@{property}}' does not exist."/>
    </sequential>
  </macrodef>

</project>

You need to decide when to validate the properties, and you need to validate them explicitly - as shown in the init target in this example.

BTW, if you used a standardized pattern to name properties which refer to files or directories - e.g. my.special.file, my.build.dir - then you could use a shell script to discover all relevant properties and write all your validate-file-property elements . Something like this:

awk -F= '/\.(file|dir)/{ printf "<validate-file-property property=\"%s\" type=\"%s\"/>\n", $1, gensub(/.*\.(file|dir)/, "\\1", "g", $1) }' *.properties

You could paste the output into your build file.

ewan.chalmers
  • 16,145
  • 43
  • 60
1

I once offered a bounty for information on configuration languages other than XML that provide schema validation. Unfortunately, such configuration languages are few and far between.

Here is a link to the bountied question, in case you want to see the sparseness of the responses.

Community
  • 1
  • 1
Ciaran McHale
  • 2,126
  • 14
  • 21