From jackrabbit-commits-return-1757-apmail-incubator-jackrabbit-commits-archive=www.apache.org@incubator.apache.org Thu Jan 26 10:44:29 2006 Return-Path: Delivered-To: apmail-incubator-jackrabbit-commits-archive@www.apache.org Received: (qmail 9502 invoked from network); 26 Jan 2006 10:44:29 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 26 Jan 2006 10:44:29 -0000 Received: (qmail 42202 invoked by uid 500); 26 Jan 2006 10:44:28 -0000 Mailing-List: contact jackrabbit-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: jackrabbit-dev@incubator.apache.org Delivered-To: mailing list jackrabbit-commits@incubator.apache.org Received: (qmail 42191 invoked by uid 500); 26 Jan 2006 10:44:28 -0000 Delivered-To: apmail-incubator-jackrabbit-cvs@incubator.apache.org Received: (qmail 42187 invoked by uid 99); 26 Jan 2006 10:44:28 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 26 Jan 2006 02:44:27 -0800 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Thu, 26 Jan 2006 02:44:27 -0800 Received: (qmail 9315 invoked by uid 65534); 26 Jan 2006 10:44:07 -0000 Message-ID: <20060126104407.9314.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r372500 - in /incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit: core/config/ConfigurationParser.java util/Text.java Date: Thu, 26 Jan 2006 10:44:06 -0000 To: jackrabbit-cvs@incubator.apache.org From: tripod@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Author: tripod Date: Thu Jan 26 02:44:01 2006 New Revision: 372500 URL: http://svn.apache.org/viewcvs?rev=372500&view=rev Log: - moving convenience method to commons. Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/util/Text.java Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java?rev=372500&r1=372499&r2=372500&view=diff ============================================================================== --- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java (original) +++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/core/config/ConfigurationParser.java Thu Jan 26 02:44:01 2006 @@ -23,6 +23,7 @@ import org.w3c.dom.NodeList; import org.xml.sax.InputSource; import org.xml.sax.SAXException; +import org.apache.jackrabbit.util.Text; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -515,32 +516,11 @@ */ protected String replaceVariables(String value) throws ConfigurationException { - StringBuffer result = new StringBuffer(); - - // Value: - // +--+-+--------+-+-----------------+ - // | |p|--> |q|--> | - // +--+-+--------+-+-----------------+ - int p = 0, q = value.indexOf("${"); // Find first ${ - while (q != -1) { - result.append(value.substring(p, q)); // Text before ${ - p = q; - q = value.indexOf("}", q + 2); // Find } - if (q != -1) { - String variable = value.substring(p + 2, q); - String replacement = variables.getProperty(variable); - if (replacement == null) { - throw new ConfigurationException( - "Replacement not found for ${" + variable + "}."); - } - result.append(replacement); - p = q + 1; - q = value.indexOf("${", p); // Find next ${ - } + try { + return Text.replaceVariables(variables, value, false); + } catch (IllegalArgumentException e) { + throw new ConfigurationException(e.getMessage()); } - result.append(value.substring(p, value.length())); // Trailing text - - return result.toString(); } /** Modified: incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/util/Text.java URL: http://svn.apache.org/viewcvs/incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/util/Text.java?rev=372500&r1=372499&r2=372500&view=diff ============================================================================== --- incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/util/Text.java (original) +++ incubator/jackrabbit/trunk/jackrabbit/src/main/java/org/apache/jackrabbit/util/Text.java Thu Jan 26 02:44:01 2006 @@ -22,6 +22,7 @@ import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.BitSet; +import java.util.Properties; /** * This Class provides some text related utilities @@ -164,7 +165,7 @@ * Concatenates all strings in the string array using the specified delimiter. * @param arr * @param delim - * @return + * @return the concatenated string */ public static String implode(String[] arr, String delim) { StringBuffer buf = new StringBuffer(); @@ -586,6 +587,56 @@ level--; } return level >= 0 ? "" : path.substring(0, idx); + } + + /** + * Performs variable replacement on the given string value. + * Each ${...} sequence within the given value is replaced + * with the value of the named parser variable. If a variable is not found + * in the properties an IllegalArgumentException is thrown unless + * ignoreMissing is true. In the later case, the + * missing variable is replaced by the empty string. + * + * @param value the original value + * @param ignoreMissing if true, missing variables are replaced + * by the empty string. + * @return value after variable replacements + * @throws IllegalArgumentException if the replacement of a referenced + * variable is not found + */ + public static String replaceVariables(Properties variables, String value, + boolean ignoreMissing) + throws IllegalArgumentException { + StringBuffer result = new StringBuffer(); + + // Value: + // +--+-+--------+-+-----------------+ + // | |p|--> |q|--> | + // +--+-+--------+-+-----------------+ + int p = 0, q = value.indexOf("${"); // Find first ${ + while (q != -1) { + result.append(value.substring(p, q)); // Text before ${ + p = q; + q = value.indexOf("}", q + 2); // Find } + if (q != -1) { + String variable = value.substring(p + 2, q); + String replacement = variables.getProperty(variable); + if (replacement == null) { + if (ignoreMissing) { + replacement = ""; + } else { + throw new IllegalArgumentException( + "Replacement not found for ${" + variable + "}."); + } + } + result.append(replacement); + p = q + 1; + q = value.indexOf("${", p); // Find next ${ + } + } + result.append(value.substring(p, value.length())); // Trailing text + + return result.toString(); } }