jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tri...@apache.org
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 GMT
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 <code>${...}</code> 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
+     * <code>ignoreMissing</code> is <code>true</code>. In the later
case, the
+     * missing variable is replaced by the empty string.
+     *
+     * @param value         the original value
+     * @param ignoreMissing if <code>true</code>, 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();
     }
 
 }



Mime
View raw message