Author: roxspring
Date: Thu Jun 23 14:30:40 2005
New Revision: 201497

URL: http://svn.apache.org/viewcvs?rev=201497&view=rev
Log:
Added initial stab at commons-configuration integration

Added:
    
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java
    
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java
    
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java
Modified:
    
jakarta/commons/proper/cli/branches/commons-configuration-integration/project.xml

Modified: 
jakarta/commons/proper/cli/branches/commons-configuration-integration/project.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/commons-configuration-integration/project.xml?rev=201497&r1=201496&r2=201497&view=diff
==============================================================================
--- 
jakarta/commons/proper/cli/branches/commons-configuration-integration/project.xml
 (original)
+++ 
jakarta/commons/proper/cli/branches/commons-configuration-integration/project.xml
 Thu Jun 23 14:30:40 2005
@@ -131,6 +131,16 @@
     </dependency>
 
     <dependency>
+      <id>commons-configuration</id>
+      <version>1.0</version>
+    </dependency>
+
+    <dependency>
+      <id>commons-collections</id>
+      <version>3.1</version>
+    </dependency>
+
+    <dependency>
       <id>junit</id>
       <version>3.8.1</version>
     </dependency>

Added: 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java?rev=201497&view=auto
==============================================================================
--- 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java
 (added)
+++ 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/CommandLineConfiguration.java
 Thu Jun 23 14:30:40 2005
@@ -0,0 +1,75 @@
+/**

+ * Copyright 2005 The Apache Software Foundation

+ *

+ * Licensed under the Apache License, Version 2.0 (the "License");

+ * you may not use this file except in compliance with the License.

+ * You may obtain a copy of the License at

+ *

+ *     http://www.apache.org/licenses/LICENSE-2.0

+ *

+ * Unless required by applicable law or agreed to in writing, software

+ * distributed under the License is distributed on an "AS IS" BASIS,

+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+ * See the License for the specific language governing permissions and

+ * limitations under the License.

+ */

+package org.apache.commons.cli2.commandline;

+

+import java.util.Iterator;

+

+import org.apache.commons.cli2.CommandLine;

+import org.apache.commons.configuration.AbstractConfiguration;

+

+/**

+ * @author roxspring

+ */

+public class CommandLineConfiguration extends AbstractConfiguration {

+       

+       private final CommandLine cmd;

+

+       public CommandLineConfiguration(final CommandLine cmd) {

+               this.cmd = cmd;

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.configuration.AbstractConfiguration#getPropertyDirect(java.lang.String)

+        */

+       protected Object getPropertyDirect(String key) {

+               return cmd.getValue(key);

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.configuration.AbstractConfiguration#addPropertyDirect(java.lang.String,
 java.lang.Object)

+        */

+       protected void addPropertyDirect(String key, Object value) {

+               throw new 
UnsupportedOperationException("CommandLineConfiguration is currently read 
only");

+       }

+

+       /* (non-Javadoc)

+        * @see org.apache.commons.configuration.AbstractConfiguration#isEmpty()

+        */

+       public boolean isEmpty() {

+               return cmd.getOptions().isEmpty();

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.configuration.AbstractConfiguration#containsKey(java.lang.String)

+        */

+       public boolean containsKey(String key) {

+               return cmd.hasOption(key);

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.configuration.AbstractConfiguration#clearProperty(java.lang.String)

+        */

+       public void clearProperty(String arg0) {

+               throw new 
UnsupportedOperationException("CommandLineConfiguration is currently read 
only");

+       }

+

+       /* (non-Javadoc)

+        * @see org.apache.commons.configuration.AbstractConfiguration#getKeys()

+        */

+       public Iterator getKeys() {

+               return cmd.getOptionTriggers().iterator();

+       }

+}


Added: 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java?rev=201497&view=auto
==============================================================================
--- 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java
 (added)
+++ 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/java/org/apache/commons/cli2/commandline/ConfigurationCommandLine.java
 Thu Jun 23 14:30:40 2005
@@ -0,0 +1,119 @@
+/**

+ * Copyright 2005 The Apache Software Foundation

+ *

+ * Licensed under the Apache License, Version 2.0 (the "License");

+ * you may not use this file except in compliance with the License.

+ * You may obtain a copy of the License at

+ *

+ *     http://www.apache.org/licenses/LICENSE-2.0

+ *

+ * Unless required by applicable law or agreed to in writing, software

+ * distributed under the License is distributed on an "AS IS" BASIS,

+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+ * See the License for the specific language governing permissions and

+ * limitations under the License.

+ */

+package org.apache.commons.cli2.commandline;

+

+import java.util.ArrayList;

+import java.util.Collections;

+import java.util.HashSet;

+import java.util.Iterator;

+import java.util.List;

+import java.util.Set;

+

+import org.apache.commons.cli2.Option;

+import org.apache.commons.configuration.Configuration;

+

+/**

+ * @author roxspring

+ */

+public class ConfigurationCommandLine extends CommandLineImpl {

+       

+       private final Option root;

+       private final Configuration config;

+

+       public ConfigurationCommandLine(final Option root, final Configuration 
config) {

+               this.root = root;

+               this.config = config;

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.cli2.CommandLine#hasOption(org.apache.commons.cli2.Option)

+        */

+       public boolean hasOption(Option option) {

+               if(option==null){

+                       return false;

+               }

+        return config.containsKey(option.getPreferredName());

+       }

+

+       /* (non-Javadoc)

+        * @see org.apache.commons.cli2.CommandLine#getOption(java.lang.String)

+        */

+       public Option getOption(String trigger) {

+               return root.findOption(trigger);

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.cli2.CommandLine#getValues(org.apache.commons.cli2.Option, 
java.util.List)

+        */

+       public List getValues(Option option, List defaultValues) {

+               return config.getList(option.getPreferredName(), defaultValues);

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.cli2.CommandLine#getSwitch(org.apache.commons.cli2.Option, 
java.lang.Boolean)

+        */

+       public Boolean getSwitch(Option option, Boolean defaultValue) {

+               return config.getBoolean(option.getPreferredName(), 
defaultValue);

+       }

+

+       /* (non-Javadoc)

+        * @see 
org.apache.commons.cli2.CommandLine#getProperty(java.lang.String, 
java.lang.String)

+        */

+       public String getProperty(String property, String defaultValue) {

+               return config.getString(property, defaultValue);

+       }

+

+       /* (non-Javadoc)

+        * @see org.apache.commons.cli2.CommandLine#getProperties()

+        */

+       public Set getProperties() {

+               final HashSet properties = new HashSet();

+               final Iterator keys = config.getKeys();

+               while(keys.hasNext()) {

+                       properties.add(keys.next());

+               }

+               return properties;

+       }

+

+       /* (non-Javadoc)

+        * @see org.apache.commons.cli2.CommandLine#getOptions()

+        */

+       public List getOptions() {

+               final List options = new ArrayList();

+               final Iterator keys = config.getKeys();

+               while(keys.hasNext()){

+                       final String trigger = (String)keys.next();

+                       final Option option = root.findOption(trigger);

+                       if(option!=null){

+                               options.add(option);

+                       }

+               }

+               return Collections.unmodifiableList(options);

+       }

+

+       /* (non-Javadoc)

+        * @see org.apache.commons.cli2.CommandLine#getOptionTriggers()

+        */

+       public Set getOptionTriggers() {

+               final Set triggers = new HashSet();

+               final Iterator options = getOptions().iterator();

+               while(options.hasNext()){

+                       final Option option = (Option)options.next();

+                       triggers.addAll(option.getTriggers());

+               }

+               return Collections.unmodifiableSet(triggers);

+       }

+}


Added: 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/cli/branches/commons-configuration-integration/src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java?rev=201497&view=auto
==============================================================================
--- 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java
 (added)
+++ 
jakarta/commons/proper/cli/branches/commons-configuration-integration/src/test/org/apache/commons/cli2/commandline/ConfigurationCommandLineTest.java
 Thu Jun 23 14:30:40 2005
@@ -0,0 +1,47 @@
+/**

+ * Copyright 2005 The Apache Software Foundation

+ *

+ * Licensed under the Apache License, Version 2.0 (the "License");

+ * you may not use this file except in compliance with the License.

+ * You may obtain a copy of the License at

+ *

+ *     http://www.apache.org/licenses/LICENSE-2.0

+ *

+ * Unless required by applicable law or agreed to in writing, software

+ * distributed under the License is distributed on an "AS IS" BASIS,

+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

+ * See the License for the specific language governing permissions and

+ * limitations under the License.

+ */

+package org.apache.commons.cli2.commandline;

+

+import java.util.Arrays;

+

+import org.apache.commons.cli2.CommandLine;

+import org.apache.commons.cli2.CommandLineTestCase;

+import org.apache.commons.configuration.BaseConfiguration;

+import org.apache.commons.configuration.Configuration;

+

+/**

+ * @author Rob Oxspring

+ */

+public class ConfigurationCommandLineTest extends CommandLineTestCase {

+       

+       /* (non-Javadoc)

+        * @see org.apache.commons.cli2.CommandLineTest#createCommandLine()

+        */

+       protected CommandLine createCommandLine() {

+               final Configuration config = new BaseConfiguration();

+               config.addProperty("--present","present value");

+               config.addProperty("--alsopresent","present value");

+               config.addProperty("--multiple",Arrays.asList(new 
String[]{"value 1", "value 2" , "value 3"}));

+               config.addProperty("--bool",Boolean.TRUE);

+               config.setProperty("present","present property");

+               

+               return new ConfigurationCommandLine(root,config);

+       }

+       

+       public void testToMakeEclipseSpotTheTestCase(){

+               // nothing to test

+       }

+}




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

Reply via email to