-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,
I've written a method, which transforms Arguments of a CLArgsParser into a 
Configuration. In my test environment it seems to work good, so now, I'm 
asking for some comments. I'd like to get it included into Avalon Framework 
or Excalibur. Thats one of the option questions. Should it be a 
toConfiguration in CLArgsParser or a fromCLArgsParser in Configuration? The 
other question is the way to parameters are transformed into a Configuration. 
Currently I'm using points (.) as a seperator to generate childs. A double 
point (:) is used as mark for an attribute.
I have attached a test class.

With kind regards
        Torsten Knodt
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9dNQ2vxZktkzSmiwRAug3AJ9zzqYDD0fLbqVXijM3FMFcmPOdBACbB/CL
EGHpWAkH8ZE/iMACPqEXhuM=
=1cM8
-----END PGP SIGNATURE-----
package org.apache.batik.util;

import org.apache.avalon.excalibur.cli.CLOption;
import org.apache.avalon.excalibur.cli.CLArgsParser;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.configuration.DefaultConfiguration;

import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
import org.apache.avalon.excalibur.cli.CLOptionDescriptor;

import java.util.Enumeration;
import java.util.Vector;
import java.util.Hashtable;

import java.io.IOException;

import org.xml.sax.SAXException;

public class MyCLArgsParser {

    private static String[] splitString (String str, char sep) {
        Vector v = new Vector();
        int i;
        while ((i = str.indexOf (sep)) >= 0) {
            v.add (str.substring (0, i));
            str = str.substring (i + 1);
        }
        v.add (str);
        return (String[]) v.toArray(new String[] { });
    }

    public static Configuration toConfiguration (Class appClass, CLOptionDescriptor[] argsDesc, CLArgsParser argsParser) {
        Hashtable optionHash = new Hashtable (argsDesc.length);
        for (int i = 0; i < argsDesc.length; i++)
            optionHash.put (new Integer (argsDesc[i].getId()), argsDesc[i]);
        DefaultConfiguration conf = new DefaultConfiguration(appClass.getName(), "/");
        for (Enumeration e = argsParser.getArguments().elements(); e.hasMoreElements();) {
            CLOption option = (CLOption) e.nextElement();
            int optionID = option.getId();
            if (optionID == 0)
                continue;
            DefaultConfiguration actConf = conf;
            String[] dom = splitString(((CLOptionDescriptor) optionHash.get (new Integer (option.getId()))).getName(),'.');
            String[] att = splitString(dom[dom.length - 1],':');
            dom[dom.length - 1] = att[0];
            for (int i = 0; i < dom.length; i++) {
                DefaultConfiguration myConf = (DefaultConfiguration) actConf.getChild (dom[i], false);
                if (myConf == null) {
                    myConf = new DefaultConfiguration (dom[i],"keine ahnung");
                    actConf.addChild (myConf);
                }
                actConf = myConf;
            }
                String arg = option.getArgument();
                if (arg == null)
                    arg = "true";
            if (att.length == 2)
                    actConf.setAttribute (att[1], arg);
            else
                actConf.setValue (arg);
        }
        conf.makeReadOnly();
        return conf;
    }

    public static void main(String[] args) throws SAXException, IOException, ConfigurationException {
        CLOptionDescriptor[] desc = new CLOptionDescriptor[]
                                    {
                                        new CLOptionDescriptor ("test.versuch.hallo:test1", CLOptionDescriptor.ARGUMENT_OPTIONAL, 'h', "Parameter ohne Argument"),
                                        new CLOptionDescriptor ("test.hallo.versuch", CLOptionDescriptor.ARGUMENT_OPTIONAL, 'v', "Parameter ohne Argument")
                                    };
        CLArgsParser parser = new CLArgsParser(args, desc);
        DefaultConfigurationSerializer ser = new DefaultConfigurationSerializer();
        ser.serialize (System.out, toConfiguration(MyCLArgsParser.class, desc, parser));
    }
}

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to