hlship 2003/10/14 08:33:18
Modified: hivemind/framework/src/test/hivemind/test/rules
TestNumericTranslators.java
TestObjectTranslator.java
hivemind/framework/src/java/org/apache/commons/hivemind/schema/rules
ObjectTranslator.java
hivemind/framework/src/java/org/apache/commons/hivemind
HiveMindMessages.properties
hivemind/xdocs rules.xml
hivemind/framework/src/java/org/apache/commons/hivemind/parse
DescriptorParser.java
Added: hivemind/framework/src/test/hivemind/test/rules
PrivateObject.java
hivemind/framework/src/java/org/apache/commons/hivemind/schema/rules
DoubleTranslator.java ClassTranslator.java
Log:
Add two new translators: double and class.
Revision Changes Path
1.3 +57 -1
jakarta-commons-sandbox/hivemind/framework/src/test/hivemind/test/rules/TestNumericTranslators.java
Index: TestNumericTranslators.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/hivemind/framework/src/test/hivemind/test/rules/TestNumericTranslators.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestNumericTranslators.java 12 Oct 2003 09:55:31 -0000 1.2
+++ TestNumericTranslators.java 14 Oct 2003 15:33:17 -0000 1.3
@@ -62,6 +62,7 @@
import org.apache.commons.hivemind.Element;
import org.apache.commons.hivemind.impl.ElementImpl;
import org.apache.commons.hivemind.schema.SchemaProcessor;
+import org.apache.commons.hivemind.schema.rules.DoubleTranslator;
import org.apache.commons.hivemind.schema.rules.IntTranslator;
import org.apache.commons.hivemind.schema.rules.LongTranslator;
@@ -182,6 +183,61 @@
t.translate(new MockSchemaProcessor(), new ElementImpl(), "qbert"));
assertLoggedMessage("'qbert' (in element foo/bar/baz at null) is not a long
value.");
+ }
+
+ /**
+ * Tests [EMAIL PROTECTED]
org.apache.commons.hivemind.schema.rules.IntTranslator} with
+ * the default constructor.
+ */
+ public void testDoubleTranslator()
+ {
+ DoubleTranslator t = new DoubleTranslator();
+
+ assertEquals(new Double(10.7), t.translate(null, null, "10.7"));
+ }
+
+ public void testDoubleDefault()
+ {
+ DoubleTranslator t = new DoubleTranslator();
+
+ assertEquals(new Double(0), t.translate(null, null, null));
+ }
+
+ public void testDoubleRange()
+ {
+ DoubleTranslator t = new DoubleTranslator("min=5.25,max=200");
+ SchemaProcessor p = new MockSchemaProcessor();
+ Element e = new ElementImpl();
+
+ interceptLogging();
+
+ assertEquals(new Double(5.25), t.translate(p, e, "3"));
+
+ assertLoggedMessage("Value 3 (at null) is less than minimum value 5.25.");
+
+ assertEquals(new Double(200), t.translate(p, e, "50900.30"));
+
+ assertLoggedMessage("Value 50900.30 (at null) is greater than maximum value
200.");
+ }
+
+ public void testDoubleDefaultValue()
+ {
+ DoubleTranslator t = new DoubleTranslator("default=7.77");
+
+ assertEquals(new Double(7.77), t.translate(null, null, null));
+ }
+
+ public void testDoubleInvalid()
+ {
+ DoubleTranslator t = new DoubleTranslator("default=13");
+
+ interceptLogging();
+
+ assertEquals(
+ new Double(13),
+ t.translate(new MockSchemaProcessor(), new ElementImpl(), "qbert"));
+
+ assertLoggedMessage("'qbert' (in element foo/bar/baz at null) is not a
double value.");
}
}
1.3 +39 -2
jakarta-commons-sandbox/hivemind/framework/src/test/hivemind/test/rules/TestObjectTranslator.java
Index: TestObjectTranslator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/hivemind/framework/src/test/hivemind/test/rules/TestObjectTranslator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- TestObjectTranslator.java 12 Oct 2003 09:55:31 -0000 1.2
+++ TestObjectTranslator.java 14 Oct 2003 15:33:17 -0000 1.3
@@ -61,17 +61,20 @@
import java.util.List;
+import org.apache.commons.hivemind.ApplicationRuntimeException;
import org.apache.commons.hivemind.Location;
import org.apache.commons.hivemind.Registry;
import org.apache.commons.hivemind.impl.BaseLocatable;
import org.apache.commons.hivemind.impl.ElementImpl;
import org.apache.commons.hivemind.impl.LocationImpl;
import org.apache.commons.hivemind.schema.SchemaProcessor;
+import org.apache.commons.hivemind.schema.rules.ClassTranslator;
import org.apache.commons.hivemind.schema.rules.ObjectTranslator;
/**
* Fill in some gaps in
- * [EMAIL PROTECTED] org.apache.commons.hivemind.schema.rules.ObjectTranslator}.
+ * [EMAIL PROTECTED] org.apache.commons.hivemind.schema.rules.ObjectTranslator} and
+ * [EMAIL PROTECTED] org.apache.commons.hivemind.schema.rules.ClassTranslator}.
*
* @author Howard Lewis Ship
* @version $Id$
@@ -109,6 +112,29 @@
assertLoggedMessagePattern("'bad.class.Name' is not a valid class name
\\(at .*\\): Could not load class bad.class.Name");
}
+ public void testPrivateObject() throws Exception
+ {
+ ObjectTranslator t = new ObjectTranslator();
+ ElementImpl e = new ElementImpl();
+ Location l = new LocationImpl(getResource("TestObjectTranslator.class"),
50);
+ e.setLocation(l);
+
+ SchemaProcessor p = new MockSchemaProcessor();
+
+ try
+ {
+ t.translate(p, e, PrivateObject.class.getName());
+ unreachable();
+ }
+ catch (ApplicationRuntimeException ex)
+ {
+ assertExceptionSubstring(
+ ex,
+ "Class org.apache.commons.hivemind.schema.rules.ObjectTranslator
can not access a member "
+ + "of class hivemind.test.rules.PrivateObject");
+ }
+ }
+
public void testLocatable() throws Exception
{
ObjectTranslator t = new ObjectTranslator();
@@ -140,5 +166,16 @@
l.size();
assertLoggedMessagePattern("Unable to set property value of [EMAIL
PROTECTED] to {} \\(at datum/value\\): ");
+ }
+
+ public void testClassTranslator() throws Exception
+ {
+ ClassTranslator t = new ClassTranslator();
+ SchemaProcessor p = new MockSchemaProcessor();
+
+ Class c = (Class)t.translate(p, null, getClass().getName());
+
+ assertEquals(getClass(), c);
+
}
}
1.1
jakarta-commons-sandbox/hivemind/framework/src/test/hivemind/test/rules/PrivateObject.java
Index: PrivateObject.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package hivemind.test.rules;
/**
* Used to test failure when creating an object
* (by [EMAIL PROTECTED] org.apache.commons.hivemind.schema.rules.ObjectTranslator}.
*
* @author Howard Lewis Ship
* @version $Id: PrivateObject.java,v 1.1 2003/10/14 15:33:17 hlship Exp $
*/
public class PrivateObject
{
private PrivateObject()
{
}
}
1.3 +7 -27
jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/schema/rules/ObjectTranslator.java
Index: ObjectTranslator.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/schema/rules/ObjectTranslator.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- ObjectTranslator.java 12 Oct 2003 10:13:55 -0000 1.2
+++ ObjectTranslator.java 14 Oct 2003 15:33:18 -0000 1.3
@@ -57,15 +57,10 @@
package org.apache.commons.hivemind.schema.rules;
+import org.apache.commons.hivemind.ApplicationRuntimeException;
import org.apache.commons.hivemind.Element;
-import org.apache.commons.hivemind.HiveMind;
-import org.apache.commons.hivemind.ClassResolver;
import org.apache.commons.hivemind.LocationHolder;
import org.apache.commons.hivemind.schema.SchemaProcessor;
-import org.apache.commons.hivemind.schema.Translator;
-import org.apache.commons.lang.StringUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
/**
* Used to translate from a class name to an instance of the class.
@@ -73,24 +68,17 @@
* @author Howard Lewis Ship
* @version $Id$
*/
-public class ObjectTranslator implements Translator
+public class ObjectTranslator extends ClassTranslator
{
- private static final Log LOG = LogFactory.getLog(ObjectTranslator.class);
-
- /**
- * Converts the value from a class name to an instance of the class. Null input
- * returns null. If an exception is thrown, then it is logged and null is
returned.
- */
public Object translate(SchemaProcessor processor, Element element, String
inputValue)
{
- if (StringUtils.isBlank(inputValue))
+ Class objectClass = extractClass(processor, element, inputValue);
+
+ if (objectClass == null)
return null;
try
{
- ClassResolver resolver =
processor.getContributingModule().getClassResolver();
-
- Class objectClass = resolver.findClass(inputValue);
Object result = objectClass.newInstance();
@@ -104,15 +92,7 @@
}
catch (Exception ex)
{
- LOG.error(
- HiveMind.format(
- "ClassTranslator.bad-class-name",
- inputValue,
- element.getLocation(),
- ex.getMessage()),
- ex);
-
- return null;
+ throw new ApplicationRuntimeException(ex.getMessage(),
element.getLocation(), ex);
}
}
1.1
jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/schema/rules/DoubleTranslator.java
Index: DoubleTranslator.java
===================================================================
/*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.commons.hivemind.schema.rules;
import java.util.Map;
import org.apache.commons.hivemind.Element;
import org.apache.commons.hivemind.HiveMind;
import org.apache.commons.hivemind.schema.SchemaProcessor;
import org.apache.commons.hivemind.schema.Translator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Translates strings to integer values.
*
* @author Howard Lewis Ship
* @version $Id: DoubleTranslator.java,v 1.1 2003/10/14 15:33:18 hlship Exp $
*/
public class DoubleTranslator implements Translator
{
private static final Log LOG = LogFactory.getLog(DoubleTranslator.class);
private double _minValue;
private boolean _isMinValue;
private double _maxValue;
private boolean _isMaxValue;
private double _defaultValue = 0;
public DoubleTranslator()
{
}
/**
* Initializers:
* <ul>
* <li>default: default value for empty or invalid input
* <li>min: minimum acceptible value
* <li>max: maximum acceptible value
*/
public DoubleTranslator(String initializer)
{
Map m = RuleUtils.convertInitializer(initializer);
String defaultInit = (String) m.get("default");
if (defaultInit != null)
_defaultValue = Double.parseDouble(defaultInit);
String minInit = (String) m.get("min");
if (minInit != null)
{
_isMinValue = true;
_minValue = Double.parseDouble(minInit);
}
String maxInit = (String) m.get("max");
if (maxInit != null)
{
_isMaxValue = true;
_maxValue = Double.parseDouble(maxInit);
}
}
/**
* Converts the string to an Double. The empty string is returned as zero.
* On failure, an error is logged and the method returns zero.
*/
public Object translate(SchemaProcessor processor, Element element, String
inputValue)
{
if (StringUtils.isBlank(inputValue))
return new Double(_defaultValue);
double value;
try
{
value = Double.parseDouble(inputValue);
if (_isMinValue && value < _minValue)
{
LOG.error(
HiveMind.format(
"DoubleTranslator.min-value",
inputValue,
element.getLocation(),
Double.toString(_minValue)));
value = _minValue;
}
if (_isMaxValue && value > _maxValue)
{
LOG.error(
HiveMind.format(
"DoubleTranslator.max-value",
inputValue,
element.getLocation(),
Double.toString(_maxValue)));
value = _maxValue;
}
}
catch (Exception ex)
{
LOG.error(
HiveMind.format(
"DoubleTranslator.invalid-value",
inputValue,
processor.getElementPath(),
element.getLocation()));
value = _defaultValue;
}
return new Double(value);
}
}
1.3 +14 -20
jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/schema/rules/ClassTranslator.java
1.6 +5 -1
jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/HiveMindMessages.properties
Index: HiveMindMessages.properties
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/HiveMindMessages.properties,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- HiveMindMessages.properties 2 Oct 2003 18:42:06 -0000 1.5
+++ HiveMindMessages.properties 14 Oct 2003 15:33:18 -0000 1.6
@@ -117,6 +117,10 @@
LongTranslator.min-value=Value {0} (at {1}) is less than minimum value {2}.
LongTranslator.max-value=Value {0} (at {1}) is greater than maximum value {2}.
+DoubleTranslator.invalid-value=''{0}'' (in element {1} at {2}) is not a double
value.
+DoubleTranslator.min-value=Value {0} (at {1}) is less than minimum value {2}.
+DoubleTranslator.max-value=Value {0} (at {1}) is greater than maximum value {2}.
+
ServiceTranslator.invalid-value=Error resolving service {0} (in element {1} at
{2}): {3}
EnumerationTranslator.not-recognized=''{0}'' (at {1}) is not a recognized
enumerated value.
1.12 +55 -19 jakarta-commons-sandbox/hivemind/xdocs/rules.xml
Index: rules.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/hivemind/xdocs/rules.xml,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- rules.xml 10 Oct 2003 19:25:41 -0000 1.11
+++ rules.xml 14 Oct 2003 15:33:18 -0000 1.12
@@ -327,6 +327,49 @@
</subsection>
+<subsection name="class">
+
+<p>
+The class translator converts a class name into a Class object. The value must be
a fully
+qualified class name. A null input value returns null.
+</p>
+
+
+</subsection>
+
+
+<subsection name="configuration">
+
+<p>
+The configuration translator converts an input value into an configuration point
id, then obtains
+the elements for that configuration point as the value. The id may be fully
qualified, or a local id within
+the contributing module.
+</p>
+
+<p>
+A null input value returns null. Invalid input, such as an unknown configuration
point id, will be logged as an error and
+null will be returned.
+</p>
+
+</subsection>
+
+<subsection name="double">
+
+<p>
+The double translator converts the input into an double precision floating point
value.
+It recognizes three initializer values:
+<ul>
+<li>default: the default value (normally 0) to use when the input is null or
invalid</li>
+<li>min: a minimum acceptible value</li>
+<li>max: a maximum acceptible value</li>
+</ul>
+</p>
+
+<p>
+If the value is outside of the range defined by the min and max initializer keys,
it is constrained into the range.
+</p>
+
+</subsection>
<subsection name="enumeration">
@@ -357,21 +400,6 @@
</subsection>
-<subsection name="configuration">
-
-<p>
-The configuration translator converts an input value into an configuration point
id, then obtains
-the elements for that configuration point as the value. The id may be fully
qualified, or a local id within
-the contributing module.
-</p>
-
-<p>
-A null input value returns null. Invalid input, such as an unknown configuration
point id, will be logged as an error and
-null will be returned.
-</p>
-
-</subsection>
-
<subsection name="int">
<p>
@@ -392,10 +420,18 @@
<subsection name="long">
<p>
-The long translator is identitical to the int translator, except that the input is
converted to a long (64 bit) integer.
+The long translator converts the input into an long integer (64 bit) value. It
recognizes three initializer values:
+<ul>
+<li>default: the default value (normally 0) to use when the input is null or
invalid</li>
+<li>min: a minimum acceptible value</li>
+<li>max: a maximum acceptible value</li>
+</ul>
+</p>
+
+<p>
+If the value is outside of the range defined by the min and max initializer keys,
it is constrained into the range.
</p>
-
-
+
</subsection>
<subsection name="object">
1.11 +5 -1
jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/parse/DescriptorParser.java
Index: DescriptorParser.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/hivemind/framework/src/java/org/apache/commons/hivemind/parse/DescriptorParser.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- DescriptorParser.java 12 Oct 2003 10:13:54 -0000 1.10
+++ DescriptorParser.java 14 Oct 2003 15:33:18 -0000 1.11
@@ -89,6 +89,8 @@
import org.apache.commons.hivemind.schema.impl.ElementModelImpl;
import org.apache.commons.hivemind.schema.impl.SchemaImpl;
import org.apache.commons.hivemind.schema.rules.BooleanTranslator;
+import org.apache.commons.hivemind.schema.rules.ClassTranslator;
+import org.apache.commons.hivemind.schema.rules.DoubleTranslator;
import org.apache.commons.hivemind.schema.rules.ObjectTranslator;
import org.apache.commons.hivemind.schema.rules.ConfigurationTranslator;
import org.apache.commons.hivemind.schema.rules.CreateObjectRule;
@@ -358,6 +360,8 @@
_translatorClasses.put("configuration", ConfigurationTranslator.class);
_translatorClasses.put("resource", ResourceTranslator.class);
_translatorClasses.put("long", LongTranslator.class);
+ _translatorClasses.put("double", DoubleTranslator.class);
+ _translatorClasses.put("class", ClassTranslator.class);
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]