Author: niclas Date: Fri Sep 10 23:59:23 2004 New Revision: 45869 Added: avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/PropertyResolver.java (contents, props changed) avalon/trunk/tools/magic/src/test/org/apache/avalon/tools/model/test/PropertyResolverTestCase.java (contents, props changed) Modified: avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/ElementHelper.java Log: Adding support for system property resolution in xml files.
Modified: avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/ElementHelper.java ============================================================================== --- avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/ElementHelper.java (original) +++ avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/ElementHelper.java Fri Sep 10 23:59:23 2004 @@ -18,19 +18,24 @@ package org.apache.avalon.tools.model; import org.apache.tools.ant.BuildException; + import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; + import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; + import java.util.ArrayList; +import java.util.Properties; /** * Utility class supporting the translation of DOM content into local child, children, @@ -279,11 +284,6 @@ static String normalize( String value, Properties props ) { - if( value.indexOf( '$' ) < 0 ) - return value; - int pos = value.indexOf( "$" ); - if( pos < 0 ) - return value; - return value; + return PropertyResolver.resolve( props, value ); } } Added: avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/PropertyResolver.java ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/src/main/org/apache/avalon/tools/model/PropertyResolver.java Fri Sep 10 23:59:23 2004 @@ -0,0 +1,101 @@ +/* +Copyright 2004 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.avalon.tools.model; + +import java.util.Properties; +import java.util.Stack; +import java.util.StringTokenizer; + + +public class PropertyResolver +{ + static public String resolve( Properties props, String value ) + { + if( value == null ) + return null; + + // optimization for common case. + if( value.indexOf( '$' ) < 0 ) + return value; + int pos1 = value.indexOf( "${" ); + if( pos1 < 0 ) + return value; + + Stack stack = new Stack(); + StringTokenizer st = new StringTokenizer( value, "${}", true ); + + while( st.hasMoreTokens() ) + { + String token = st.nextToken(); + if( token.equals( "}" ) ) + { + String name = (String) stack.pop(); + String open = (String) stack.pop(); + if( open.equals( "${" ) ) + { + String propValue = props.getProperty( name ); + if( propValue == null ) + push( stack, "${" + name + "}" ); + else + push( stack, propValue ); + } + else + { + push( stack, "${" + name + "}" ); + } + } + else + { + if( token.equals( "$" ) ) + stack.push( "$" ); + else + { + push( stack, token ); + } + } + } + String result = ""; + while( stack.size() > 0 ) + { + result = (String) stack.pop() + result; + } + return result; + } + + static private void push( Stack stack , String value ) + { + if( stack.size() > 0 ) + { + String data = (String) stack.pop(); + if( data.equals( "${" ) ) + { + stack.push( data ); + stack.push( value ); + } + else + { + stack.push( data + value ); + } + } + else + { + stack.push( value ); + } + } +} + Added: avalon/trunk/tools/magic/src/test/org/apache/avalon/tools/model/test/PropertyResolverTestCase.java ============================================================================== --- (empty file) +++ avalon/trunk/tools/magic/src/test/org/apache/avalon/tools/model/test/PropertyResolverTestCase.java Fri Sep 10 23:59:23 2004 @@ -0,0 +1,91 @@ +/* + * Copyright 2004 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.avalon.tools.model.test; + +import java.util.Properties; + +import junit.framework.TestCase; +import org.apache.avalon.tools.model.PropertyResolver; + + +/** + * Testcases for the PropertyResolver + * + * @author <a href="mailto:[EMAIL PROTECTED]">Avalon Development Team</a> + * @version CVS $ Revision: 1.1 $ + */ +public class PropertyResolverTestCase extends TestCase +{ + private Properties m_Properties; + + public void setUp() + { + m_Properties = new Properties(); + m_Properties.put( "abc", "def" ); + m_Properties.put( "def", "Hi" ); + m_Properties.put( "mama", "abc" ); + m_Properties.put( "papa", "def" ); + m_Properties.put( "child", "ghi" ); + m_Properties.put( "some.abc.def.ghi.value", "All that." ); + } + + public PropertyResolverTestCase( String name ) + { + super( name ); + } + + public void testSimple1() throws Exception + { + String src = "${abc}"; + String result = PropertyResolver.resolve( m_Properties, src ); + String expected = "def"; + assertEquals( expected, result ); + } + + public void testSimple2() throws Exception + { + String src = "Def = ${abc} is it."; + String result = PropertyResolver.resolve( m_Properties, src ); + String expected = "Def = def is it."; + assertEquals( expected, result ); + } + + public void testSimple3() throws Exception + { + String src = "def = ${abc} = ${def}"; + String result = PropertyResolver.resolve( m_Properties, src ); + String expected = "def = def = Hi"; + assertEquals( expected, result ); + } + + public void testComplex1() throws Exception + { + String src = "${${abc}}"; + String result = PropertyResolver.resolve( m_Properties, src ); + String expected = "Hi"; + assertEquals( expected, result ); + } + + public void testComplex2() throws Exception + { + String src = "${some.${mama}.${papa}.${child}.value}"; + String result = PropertyResolver.resolve( m_Properties, src ); + String expected = "All that."; + assertEquals( expected, result ); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]