You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by cz...@apache.org on 2002/02/05 09:22:08 UTC
cvs commit: jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source SourceParameters.java
cziegeler 02/02/05 00:22:08
Modified: src/scratchpad/org/apache/avalon/excalibur/source
SourceParameters.java
Log:
Minor update to SourceParameters
Revision Changes Path
1.5 +91 -39 jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceParameters.java
Index: SourceParameters.java
===================================================================
RCS file: /home/cvs/jakarta-avalon-excalibur/src/scratchpad/org/apache/avalon/excalibur/source/SourceParameters.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- SourceParameters.java 11 Dec 2001 09:53:36 -0000 1.4
+++ SourceParameters.java 5 Feb 2002 08:22:08 -0000 1.5
@@ -7,6 +7,8 @@
*/
package org.apache.avalon.excalibur.source;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.parameters.Parameters;
import java.io.*;
import java.util.*;
@@ -17,10 +19,10 @@
* more than one value for a parameter.
*
* @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
- * @version $Id: SourceParameters.java,v 1.4 2001/12/11 09:53:36 jefft Exp $
+ * @version $Id: SourceParameters.java,v 1.5 2002/02/05 08:22:08 cziegeler Exp $
*/
public final class SourceParameters
-implements Serializable {
+implements Serializable, Cloneable {
/** The parameter names are the keys and the value is a List object */
private Map names = new HashMap(5);
@@ -60,6 +62,32 @@
}
/**
+ * Create a new parameters object from the
+ * children of the configuration.
+ * If no children are available <code>null</code>
+ * is returned.
+ */
+ public static SourceParameters create(Configuration conf) {
+ Configuration[] children = conf.getChildren();
+ if (children != null && children.length > 0) {
+ SourceParameters pars = new SourceParameters();
+ String name;
+ String value;
+ for(int i=0; i < children.length; i++) {
+ name = children[i].getName();
+ try {
+ value = children[i].getValue();
+ } catch (ConfigurationException local) {
+ value = ""; // ignore exception
+ }
+ pars.setParameter(name, value);
+ }
+ return pars;
+ }
+ return null;
+ }
+
+ /**
* Standard Constructor
*/
public SourceParameters() {
@@ -88,7 +116,8 @@
* @param name The name of the parameter.
* @param value The value of the parameter.
*/
- private void setParameter(String name, String value) {
+ public void setParameter(String name, String value)
+ {
ArrayList list;
if (names.containsKey(name) == true) {
list = (ArrayList)names.get(name);
@@ -105,7 +134,8 @@
* @return The value of the first parameter with the name
* or <CODE>null</CODE>
*/
- public String getParameter(String name) {
+ public String getParameter(String name)
+ {
if (names.containsKey(name) == true) {
return (String)((ArrayList)names.get(name)).get(0);
}
@@ -119,7 +149,8 @@
* @return The value of the first parameter with the name
* or <CODE>defaultValue</CODE>
*/
- public String getParameter(String name, String defaultValue) {
+ public String getParameter(String name, String defaultValue)
+ {
if (names.containsKey(name) == true) {
return (String)((ArrayList)names.get(name)).get(0);
}
@@ -133,7 +164,8 @@
* @return The value of the first parameter with the name
* or <CODE>defaultValue</CODE>
*/
- public int getParameterAsInteger(String name, int defaultValue) {
+ public int getParameterAsInteger(String name, int defaultValue)
+ {
if (names.containsKey(name) == true) {
return new Integer((String)((ArrayList)names.get(name)).get(0)).intValue();
}
@@ -147,7 +179,8 @@
* @return The value of the first parameter with the name
* or <CODE>defaultValue</CODE>
*/
- public boolean getParameterAsBoolean(String name, boolean defaultValue) {
+ public boolean getParameterAsBoolean(String name, boolean defaultValue)
+ {
if (names.containsKey(name) == true) {
return new Boolean((String)((ArrayList)names.get(name)).get(0)).booleanValue();
}
@@ -159,7 +192,8 @@
* @param name The name of the parameter.
* @return <CODE>true</CODE> if a value exists, otherwise <CODE>false</CODE>
*/
- public boolean containsParameter(String name) {
+ public boolean containsParameter(String name)
+ {
return names.containsKey(name);
}
@@ -169,18 +203,21 @@
* @return Iterator for the (String) values or null if the parameter
* is not defined.
*/
- public Iterator getParameterValues(String name) {
+ public Iterator getParameterValues(String name)
+ {
if (names.containsKey(name) == true) {
ArrayList list = (ArrayList)names.get(name);
return list.iterator();
}
return null;
}
+
/**
* Get all parameter names.
* @return Iterator for the (String) parameter names.
*/
- public Iterator getParameterNames() {
+ public Iterator getParameterNames()
+ {
return names.keySet().iterator();
}
@@ -190,7 +227,8 @@
* @return An Parameters object - if no parameters are defined this is an
* empty object.
*/
- public Parameters getFirstParameters() {
+ public Parameters getFirstParameters()
+ {
Parameters result = new Parameters();
Iterator iter = this.getParameterNames();
String parName;
@@ -208,7 +246,8 @@
* a part, like "parameter=value" separated by "&".
* If no parameter is defined <CODE>null</CODE> is returned.
*/
- public String getQueryString() {
+ public String getQueryString()
+ {
StringBuffer result = new StringBuffer();
Iterator iter = this.names.keySet().iterator();
Iterator listIterator;
@@ -235,7 +274,8 @@
* a part, like "parameter=value" separated by "&".
* If no parameter is defined <CODE>null</CODE> is returned.
*/
- public String getEncodedQueryString() {
+ public String getEncodedQueryString()
+ {
StringBuffer result = new StringBuffer();
Iterator iter = this.names.keySet().iterator();
Iterator listIterator;
@@ -258,17 +298,21 @@
/**
* Add all parameters from the incoming parameters object.
*/
- public void add(SourceParameters parameters) {
- Iterator names = parameters.getParameterNames();
- Iterator values;
- String name;
- String value;
- while (names.hasNext() == true) {
- name = (String)names.next();
- values = parameters.getParameterValues(name);
- while (values.hasNext() == true) {
- value = (String)values.next();
- this.setParameter(name, value);
+ public void add(SourceParameters parameters)
+ {
+ if ( null != parameters )
+ {
+ Iterator names = parameters.getParameterNames();
+ Iterator values;
+ String name;
+ String value;
+ while (names.hasNext() == true) {
+ name = (String)names.next();
+ values = parameters.getParameterValues(name);
+ while (values.hasNext() == true) {
+ value = (String)values.next();
+ this.setParameter(name, value);
+ }
}
}
}
@@ -276,7 +320,8 @@
/**
* Overriding toString
*/
- public String toString() {
+ public String toString()
+ {
StringBuffer buffer = new StringBuffer("SourceParameters: {");
Iterator names = this.getParameterNames();
String name;
@@ -284,7 +329,8 @@
Iterator values;
String value;
boolean firstValue;
- while (names.hasNext() == true) {
+ while (names.hasNext() == true)
+ {
name = (String)names.next();
if (firstName == false) {
buffer.append(", ");
@@ -312,15 +358,18 @@
/**
* Returns a copy of the parameters object.
*/
- public Object clone() {
+ public Object clone()
+ {
SourceParameters newObject = new SourceParameters();
Iterator names = this.getParameterNames();
Iterator values;
String name, value;
- while (names.hasNext() == true) {
+ while ( names.hasNext() )
+ {
name = (String)names.next();
- values = this.getParameterValues(name);
- while (values.hasNext() == true) {
+ values = this.getParameterValues( name );
+ while ( values.hasNext() )
+ {
value = (String)values.next();
newObject.setParameter(name, value);
}
@@ -331,26 +380,29 @@
/**
* Test if there are any parameters.
*/
- public boolean hasParameters() {
- return (this.names.size() > 0);
+ public boolean hasParameters()
+ {
+ return ( this.names.size() > 0 );
}
/**
* Set the value of this parameter to the given value.
* Remove all other values for this parameter.
*/
- public void setSingleParameterValue(String name, String value) {
- this.removeParameter(name);
- this.setParameter(name, value);
+ public void setSingleParameterValue(String name, String value)
+ {
+ this.removeParameter( name );
+ this.setParameter( name, value );
}
/**
* Remove all values for this parameter
*/
- public void removeParameter(String name) {
- if (this.names.containsKey(name) == true) {
- this.names.remove(name);
+ public void removeParameter(String name)
+ {
+ if ( this.names.containsKey( name ) )
+ {
+ this.names.remove( name );
}
}
-
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>