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:30:52 UTC

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

Author: simonetripodi
Date: Thu Jan 27 17:30:51 2011
New Revision: 1064220

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

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

Added: commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/VariableSubstitutor.java
URL: http://svn.apache.org/viewvc/commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/VariableSubstitutor.java?rev=1064220&view=auto
==============================================================================
--- commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/VariableSubstitutor.java (added)
+++ commons/sandbox/digester3/trunk/src/main/java/org/apache/commons/digester3/substitution/VariableSubstitutor.java Thu Jan 27 17:30:51 2011
@@ -0,0 +1,100 @@
+/* $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 org.apache.commons.digester3.spi.Substitutor;
+import org.xml.sax.Attributes;
+
+/**
+ * Substitutor implementation that support variable replacement
+ * for both attributes and body text.
+ * The actual expansion of variables into text is delegated to {@link VariableExpander}
+ * implementations.
+ * Supports setting an expander just for body text or just for attributes.
+ * Also supported is setting no expanders for body text and for attributes.
+ */
+public class VariableSubstitutor implements Substitutor {
+
+    /**
+     * The expander to be used to expand variables in the attributes.
+     * Null when no expansion should be performed.
+     */
+    private VariableExpander attributesExpander;
+
+    /** 
+     * Attributes implementation that (lazily) performs variable substitution.
+     * Will be lazily created when needed then reused.
+     */
+    private VariableAttributes variableAttributes;
+
+    /**
+     * The expander to be used to expand variables in the body text.
+     * Null when no expansion should be performed.
+     */
+    private VariableExpander bodyTextExpander;
+
+    /**
+     * Constructs a Substitutor which uses the same VariableExpander for both
+     * body text and attibutes.
+     *
+     * @param expander VariableExpander implementation,
+     *                 null if no substitutions are to be performed
+     */
+    public VariableSubstitutor(VariableExpander expander) {
+        this(expander, expander);
+    }
+
+    /**
+     * Constructs a Substitutor.
+     *
+     * @param attributesExpander VariableExpander implementation to be used for attributes,
+     *                           null if no attribute substitutions are to be performed
+     * @param bodyTextExpander VariableExpander implementation to be used for bodyTextExpander,
+     *                         null if no attribute substitutions are to be performed
+     */
+    public VariableSubstitutor(VariableExpander attributesExpander, VariableExpander bodyTextExpander) {
+        this.attributesExpander = attributesExpander;
+        this.bodyTextExpander = bodyTextExpander;
+        this.variableAttributes = new VariableAttributes();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public Attributes substitute(Attributes attributes) {
+        Attributes results = attributes;
+        if (this.attributesExpander != null) {
+            this.variableAttributes.init(attributes, this.attributesExpander);
+            results = this.variableAttributes;
+        }
+        return results;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public String substitute(String bodyText) {
+        String result = bodyText;
+        if (bodyTextExpander != null) {
+            result = bodyTextExpander.expand(bodyText);
+        }
+        return result;
+    }
+
+}

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

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

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