adammurdoch 2002/06/25 07:36:40
Modified: container/src/java/org/apache/myrmidon/components/property
DefaultPropertyResolver.java
ClassicPropertyResolver.java
Added: lib commons-jxpath-1.0.jar
container/src/java/org/apache/myrmidon/components/property
XPathPropertyResolver.java
Log:
Added a property resolver that uses JXPath to evaluate XPath expressions
in property references.
Revision Changes Path
1.1 jakarta-ant-myrmidon/lib/commons-jxpath-1.0.jar
<<Binary file>>
1.11 +20 -16
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/property/DefaultPropertyResolver.java
Index: DefaultPropertyResolver.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/property/DefaultPropertyResolver.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- DefaultPropertyResolver.java 9 Jun 2002 13:22:49 -0000 1.10
+++ DefaultPropertyResolver.java 25 Jun 2002 14:36:40 -0000 1.11
@@ -7,17 +7,16 @@
*/
package org.apache.myrmidon.components.property;
-import org.apache.excalibur.converter.Converter;
-import org.apache.excalibur.converter.ConverterException;
import org.apache.avalon.excalibur.i18n.ResourceManager;
import org.apache.avalon.excalibur.i18n.Resources;
import org.apache.avalon.framework.service.ServiceException;
import org.apache.avalon.framework.service.ServiceManager;
import org.apache.avalon.framework.service.Serviceable;
-import org.apache.myrmidon.api.TaskException;
+import org.apache.excalibur.converter.Converter;
+import org.apache.excalibur.converter.ConverterException;
import org.apache.myrmidon.api.TaskContext;
+import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.interfaces.property.PropertyResolver;
-import org.apache.myrmidon.interfaces.property.PropertyStore;
/**
* Base class for PropertyResolver implementations.
@@ -72,7 +71,7 @@
if( 0 == start && end == ( length - 1 ) )
{
- return getPropertyValue( content.substring( start + 2, end ),
+ return evaluateExpression( content.substring( start + 2, end ),
context );
}
@@ -131,7 +130,7 @@
{
final String propertyName = content.substring( start + 2, end );
final Object key = recursiveResolveProperty( propertyName,
context );
- return getPropertyValue( key.toString(), context );
+ return evaluateExpression( key.toString(), context );
}
final StringBuffer sb = new StringBuffer( length * 2 );
@@ -246,11 +245,17 @@
/**
* Returns a property's value, converted to a String.
*/
- private String getPropertyStringValue( final String propertyName,
+ private String getPropertyStringValue( final String expression,
final TaskContext context )
throws TaskException
{
- final Object value = getPropertyValue( propertyName, context );
+ final Object value = evaluateExpression( expression, context );
+ if( value == null )
+ {
+ final String message = REZ.getString(
"prop.missing-value.error", expression );
+ throw new TaskException( message );
+ }
+
if( value instanceof String )
{
return (String)value;
@@ -266,24 +271,23 @@
}
/**
- * Retrieve a value from the specified context using the specified key.
+ * Evaluates an expression.
*
- * @param propertyName the key of value in context
+ * @param expression the expression to evaluate
* @param context the set of known properties
* @return the object retrieved from context
- * @throws TaskException if the property is undefined
*/
- protected Object getPropertyValue( final String propertyName,
- final TaskContext context )
+ protected Object evaluateExpression( final String expression,
+ final TaskContext context )
throws TaskException
{
- final Object value = context.getProperty( propertyName );
+ final Object value = context.getProperty( expression );
if( value != null )
{
return value;
}
- final String message = REZ.getString( "prop.missing-value.error",
propertyName );
+ final String message = REZ.getString( "prop.missing-value.error",
expression );
throw new TaskException( message );
}
}
1.7 +9 -11
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/property/ClassicPropertyResolver.java
Index: ClassicPropertyResolver.java
===================================================================
RCS file:
/home/cvs/jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/property/ClassicPropertyResolver.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ClassicPropertyResolver.java 9 Apr 2002 02:26:34 -0000 1.6
+++ ClassicPropertyResolver.java 25 Jun 2002 14:36:40 -0000 1.7
@@ -7,10 +7,9 @@
*/
package org.apache.myrmidon.components.property;
-import org.apache.myrmidon.interfaces.property.PropertyResolver;
-import org.apache.myrmidon.interfaces.property.PropertyStore;
-import org.apache.myrmidon.api.TaskException;
import org.apache.myrmidon.api.TaskContext;
+import org.apache.myrmidon.api.TaskException;
+import org.apache.myrmidon.interfaces.property.PropertyResolver;
/**
* A [EMAIL PROTECTED] PropertyResolver} implementation which resolves
properties
@@ -26,24 +25,23 @@
implements PropertyResolver
{
/**
- * Retrieve a value from the specified context using the specified key.
- * If there is no such value, returns the original property reference.
+ * Evaluates an expression.
*
- * @param propertyName the name of the property to retrieve
+ * @param expression the name of the property to retrieve
* @param context the set of known properties
*/
- protected Object getPropertyValue( final String propertyName,
- final TaskContext context )
+ protected Object evaluateExpression( final String expression,
+ final TaskContext context )
throws TaskException
{
- final Object value = context.getProperty( propertyName );
+ final Object value = context.getProperty( expression );
if( value != null )
{
return value;
}
else
{
- return "${" + propertyName + "}";
+ return "${" + expression + "}";
}
}
}
1.1
jakarta-ant-myrmidon/container/src/java/org/apache/myrmidon/components/property/XPathPropertyResolver.java
Index: XPathPropertyResolver.java
===================================================================
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE.txt file.
*/
package org.apache.myrmidon.components.property;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.myrmidon.api.TaskContext;
import org.apache.myrmidon.api.TaskException;
/**
* A property resolver that uses JXPath to resolve values.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a>
* @version $Revision: 1.1 $ $Date: 2002/06/25 14:36:40 $
*/
public class XPathPropertyResolver
extends DefaultPropertyResolver
{
/**
* Evaluates an expression.
*
* @param expression the expression to evaluate
* @param context the set of known properties
* @return the object retrieved from context
*/
protected Object evaluateExpression( final String expression,
final TaskContext context )
throws TaskException
{
final JXPathContext jxContext = JXPathContext.newContext(
context.getProperties() );
return jxContext.getValue( expression );
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>