Oi wheres me config docs Oh bugger I dont have a nsf

Here we go with another example of what to do when cruelly ripped from the
bosom of the notes server

This time its config docs, these as we all know are a happy doddle in notes, we
just store them in what ever db (usually the current one) is suitable and pull
then back with a view (personally i don’t like profile documents as they are a
swine to copy around in comparison to normal docs), when faced this same basic
need in Java, and not wanting to use a relation database (feels like a bit of
an over kill for some things) i went searching and found more wonderful people
who have already done the job, this time at
http://commons.apache.org/configuration/
They have a nice little lib that lets you store stuff in xml docs (as well as
many other things), here is an example (lets call it config.xml)

<?xml version="1.0" encoding="ISO-8859-1" ?>
<project-definition>
    <example1>
     <textexample>im a example</textexample>
    </example1>
    <example2>
     <intexample>15</intexample>
     <list1>
      <name>item1,item2,item</name>
     </list1>
    </example2>
</project-definition>

 

I’m going to save this in the root of my Java project, and now on to the code

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public class EasyConfigDocs{
       public static void main(String[] args) {
          try {
              XMLConfiguration config = new XMLConfiguration("Config.xml");
            String textconfig = config.getString("example1.textexample");
               //Note: to get an indented value you separate with a "."  
              int numberconfig = config.getInt("example2.intexample");
              List buttons = config.getList("example2.list1.name");
               //Note: on multivalue items the default separator is a "," if 
you want to use a "," in a text string you need to either do "," or change the 
default delimiter (easy to do, its on the site) 
          } catch(ConfigurationException cex) {
             System.out.println("config file not found");
          }
       }
}

 

Nearly as simple as Notes (not that i did any work), it also does all the Edit
and Add stuff, but you can get that off the examples at
http://commons.apache.org/configuration/ if you need it, i just want to save
you the pain on the initial search, Oh!, on that note keep a look out at
http://www.benpoole.com hes on the same track as me and has some good blog
tutorials for notes developers fighting with pure Java coming up.

Old Comments
————
##### John(15/07/2011 13:51:29 GDT)
I don’t really like the way that they have write that code
##### Mark Myers(17/07/2011 17:42:12 GDT)
@john really?, i find it a pleasant and easy going way of doing configs, what way do you prefer?

Leave a Reply

Your email address will not be published.