Author: cziegeler Date: Tue Jan 4 03:06:06 2005 New Revision: 124095 URL: http://svn.apache.org/viewcvs?view=rev&rev=124095 Log: Add property helper Added: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/util/ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/util/PropertyHelper.java (contents, props changed) cocoon/trunk/src/core/test/org/apache/cocoon/core/container/util/ cocoon/trunk/src/core/test/org/apache/cocoon/core/container/util/PropertyHelperTestCase.java (contents, props changed)
Added: cocoon/trunk/src/core/java/org/apache/cocoon/core/container/util/PropertyHelper.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/java/org/apache/cocoon/core/container/util/PropertyHelper.java?view=auto&rev=124095 ============================================================================== --- (empty file) +++ cocoon/trunk/src/core/java/org/apache/cocoon/core/container/util/PropertyHelper.java Tue Jan 4 03:06:06 2005 @@ -0,0 +1,92 @@ +/* + * Copyright 2002-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.cocoon.core.container.util; + +/** + * Helper class for replacing property references with the value of the + * property + * + * @version CVS $Id: AbstractComponentHandler.java 123887 2005-01-02 15:12:01Z sylvain $ + */ + +public class PropertyHelper { + + /** + * Replace all property references in the string with the current value + * and return it. + */ + static String replace(String value) { + // quick test for null or no references + if ( value == null || value.indexOf("${") == -1 ) { + return value; + } + final StringBuffer buffer = new StringBuffer(); + int prev = 0; + int pos; + + // search for the next instance of $ from the 'prev' position + while ((pos = value.indexOf("$", prev)) >= 0) { + + // if there was any text before this, add it + if (pos > prev) { + buffer.append(value.substring(prev, pos)); + } + + // if we are at the end of the string, end + if (pos == (value.length() - 1)) { + buffer.append("$"); + prev = pos + 1; + } else if (value.charAt(pos + 1) != '{') { + // peek ahead to see if the next char is a property or not + // not a property: insert the char as a literal + buffer.append(value.substring(pos, pos + 2)); + prev = pos + 2; + + } else { + // start token found, check for end token + int endName = value.indexOf('}', pos); + if (endName == -1) { + // no end token found, just append the rest + buffer.append(value.substring(pos)); + prev = value.length(); + } else { + final String propertyName = value.substring(pos + 2, endName); + String propertyValue = getProperty(propertyName); + // compatibility fallback - if the value is null, just readd token + if (propertyValue == null) { + buffer.append("${"); + buffer.append(propertyName); + buffer.append('}'); + } else { + buffer.append(propertyValue); + } + prev = endName + 1; + } + } + } + // no more tokens found + // append the rest + if (prev < value.length()) { + buffer.append(value.substring(prev)); + } + return buffer.toString(); + } + + static String getProperty(String name) { + return System.getProperty(name); + } +} Added: cocoon/trunk/src/core/test/org/apache/cocoon/core/container/util/PropertyHelperTestCase.java Url: http://svn.apache.org/viewcvs/cocoon/trunk/src/core/test/org/apache/cocoon/core/container/util/PropertyHelperTestCase.java?view=auto&rev=124095 ============================================================================== --- (empty file) +++ cocoon/trunk/src/core/test/org/apache/cocoon/core/container/util/PropertyHelperTestCase.java Tue Jan 4 03:06:06 2005 @@ -0,0 +1,41 @@ +/* + * Copyright 2002-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.cocoon.core.container.util; + +import junit.framework.TestCase; + +/** + * Test cases for the [EMAIL PROTECTED] PropertyHelper} class. + * + * @version CVS $Id: AbstractComponentHandler.java 123887 2005-01-02 15:12:01Z sylvain $ + */ +public class PropertyHelperTestCase extends TestCase { + + public void testReplace() { + final String testA = "a simple string"; + final String testB = "a simple string with a start token ${ somewhere"; + final String testC = "and this is the } end token"; + final String testD = "${this.does.not.exists}"; + // some tests for not! replacing + assertEquals(PropertyHelper.replace(testA), testA); + assertEquals(PropertyHelper.replace(testB), testB); + assertEquals(PropertyHelper.replace(testC), testC); + assertEquals(PropertyHelper.replace(testD), testD); + // and finally we have something to replace + assertEquals(PropertyHelper.replace("${java.home}"), System.getProperty("java.home")); + } +}