You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by si...@apache.org on 2011/01/27 18:28:19 UTC

svn commit: r1064217 - /commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java

Author: simonetripodi
Date: Thu Jan 27 17:28:18 2011
New Revision: 1064217

URL: http://svn.apache.org/viewvc?rev=1064217&view=rev
Log:
first checkin of MultiVariableExpander class, a tidy version of the proper one on /trunk

Added:
    commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java   (with props)

Added: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java?rev=1064217&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java (added)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java Thu Jan 27 17:28:18 2011
@@ -0,0 +1,102 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.digester3.substitution;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Expands variable references from multiple sources.
+ */
+public class MultiVariableExpander implements VariableExpander {
+
+    private final List<String> markers = new ArrayList<String>(2);
+
+    private final List<Map<String, Object>> sources = new ArrayList<Map<String, Object>>(2);
+
+    private int nEntries = 0;
+
+    public void addSource(String marker, Map<String, Object> source) {
+        ++this.nEntries;
+        this.markers.add(marker);
+        this.sources.add(source);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String expand(String param) {
+        for(int i = 0; i < this.nEntries; ++i) {
+            param = expand(
+                param, 
+                this.markers.get(i), 
+                this.sources.get(i));
+        }
+        return param;
+    }
+
+    /**
+     * Replace any occurrences within the string of the form
+     * "marker{key}" with the value from source[key].
+     * <p>
+     * Commonly, the variable marker is "$", in which case variables
+     * are indicated by ${key} in the string.
+     * <p>
+     * Returns the string after performing all substitutions.
+     * <p>
+     * If no substitutions were made, the input string object is
+     * returned (not a copy).
+     *
+     * @throws IllegalArgumentException if the input param references
+     * a variable which is not known to the specified source.
+     */
+    public String expand(String str, String marker, Map<String, Object> source) {
+        String startMark = marker + "{";
+        int markLen = startMark.length();
+
+        int index = 0;
+        for(;;) {
+            index = str.indexOf(startMark, index);
+            if (index == -1) {
+                return str;
+            }
+
+            int startIndex = index + markLen;
+            if (startIndex > str.length()) {
+                throw new IllegalArgumentException("var expression starts at end of string");
+            }
+
+            int endIndex = str.indexOf("}", index + markLen);
+            if (endIndex == -1) {
+                throw new IllegalArgumentException("var expression starts but does not end");
+            }
+
+            String key = str.substring(index+markLen, endIndex);
+            Object value =  source.get(key);
+            if (value == null) {
+                throw new IllegalArgumentException("parameter [" + key + "] is not defined.");
+            }
+            String varValue = value.toString();
+
+            str = str.substring(0, index) + varValue + str.substring(endIndex+1);
+            index += varValue.length();
+        }
+    }
+
+}

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/MultiVariableExpander.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain