Author: adamw Date: 2006-05-09 12:29:16 -0400 (Tue, 09 May 2006) New Revision: 4158 Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/test/ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/test/VariablesTest.java labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/MultiValueVariableResolver.java labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/Substitution.java labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/TypeVariableResolver.java labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/VariableResolver.java Modified: labs/shotoku/trunk/shotoku-feeds/shotoku-feeds.iml labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java Log: http://jira.jboss.com/jira/browse/JBSHOTOKU-36 Modified: labs/shotoku/trunk/shotoku-feeds/shotoku-feeds.iml =================================================================== --- labs/shotoku/trunk/shotoku-feeds/shotoku-feeds.iml 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/shotoku-feeds.iml 2006-05-09 16:29:16 UTC (rev 4158) @@ -16,6 +16,7 @@ <orderEntry type="library" name="jboss" level="application" /> <orderEntry type="library" name="commons" level="application" /> <orderEntry type="library" name="velocity" level="application" /> + <orderEntry type="library" name="junit" level="application" /> <orderEntryProperties /> </component> </module> Modified: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java =================================================================== --- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/FeedsDescriptor.java 2006-05-09 16:29:16 UTC (rev 4158) @@ -42,9 +42,7 @@ import org.apache.velocity.VelocityContext; import org.apache.xerces.parsers.DOMParser; -import org.jboss.shotoku.feeds.FeedsHelper; import org.jboss.shotoku.tools.XmlTools; -import org.jboss.shotoku.feeds.SiteHelper; import org.jboss.logging.Logger; import org.jboss.shotoku.ContentManager; import org.jboss.shotoku.tools.Pair; @@ -103,8 +101,8 @@ @CacheItem private static FeedsCache feedsCache; - static synchronized FeedsDescriptor getInstance(final String baseServerAddress) { - return feedsCache.get(baseServerAddress); + static synchronized FeedsDescriptor getInstance(String id, String baseServerAddress) { + return feedsCache.get(new Pair<String, String>(id, baseServerAddress)); } /** Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/test/VariablesTest.java =================================================================== --- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/test/VariablesTest.java 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/test/VariablesTest.java 2006-05-09 16:29:16 UTC (rev 4158) @@ -0,0 +1,64 @@ +package org.jboss.shotoku.feeds.test; + +import junit.framework.TestCase; +import org.jboss.shotoku.feeds.variables.Substitution; +import org.jboss.shotoku.feeds.variables.VariableResolver; +import org.jboss.shotoku.feeds.variables.MultiValueVariableResolver; + +import java.util.Set; +import java.util.HashSet; + +/** + * @author Adam Warski (ad...@as...) + */ +public class VariablesTest extends TestCase { + public void testSubstitution1() { + Substitution sub = new Substitution(); + sub.add("xxx", "bbb"); + sub.add("ff", "m78"); + + assertEquals(sub.make("__xxx__ff__xxx"), "__bbb__m78__bbb"); + } + + public void testSubstitution2() { + Substitution sub = new Substitution(); + sub.add("${var1}", "213kf*@"); + sub.add("${var2}", "://&&.,"); + + assertEquals(sub.make("${var1}${var2}var1var2${var2}${var3}${var1}"), + "213kf*@://&&.,var1var2://&&.,${var3}213kf*@"); + + sub.add("${var3}", "#14';[]"); + + assertEquals(sub.make("${var1}${var2}var1var2${var2}${var3}${var1}"), + "213kf*@://&&.,var1var2://&&.,#14';[]213kf*@"); + } + + public void testMultiValResover() { + VariableResolver vr = new MultiValueVariableResolver() { + protected String getVarName() { + return "${var}"; + } + + protected String[] getValues() { + return new String[] { "a", "bb", "ccc" }; + } + }; + + Set<Substitution> subs = Substitution.newEmptySubstitionsSet(); + Set<String> vars = new HashSet<String>(); + vars.add("${var}"); + vr.expandSubstitutionsSet(vars, subs); + + Set<String> result = new HashSet<String>(); + + for (Substitution s : subs) { + result.add(s.make("xx${var}xx")); + } + + assertEquals(result.size(), 3); + assertTrue(result.contains("xxaxx")); + assertTrue(result.contains("xxbbxx")); + assertTrue(result.contains("xxcccxx")); + } +} Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/MultiValueVariableResolver.java =================================================================== --- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/MultiValueVariableResolver.java 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/MultiValueVariableResolver.java 2006-05-09 16:29:16 UTC (rev 4158) @@ -0,0 +1,27 @@ +package org.jboss.shotoku.feeds.variables; + +import java.util.Set; +import java.util.HashSet; + +/** + * @author Adam Warski (ad...@as...) + */ +abstract public class MultiValueVariableResolver implements VariableResolver { + abstract protected String getVarName(); + abstract protected String[] getValues(); + + public void expandSubstitutionsSet(Set<String> variables, Set<Substitution> substitutions) { + Set<Substitution> newSubs = new HashSet<Substitution>(); + + for (Substitution s : substitutions) { + for (String value : getValues()) { + Substitution newSub = s.clone(); + newSub.add(getVarName(), value); + newSubs.add(newSub); + } + } + + substitutions.clear(); + substitutions.addAll(newSubs); + } +} Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/Substitution.java =================================================================== --- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/Substitution.java 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/Substitution.java 2006-05-09 16:29:16 UTC (rev 4158) @@ -0,0 +1,66 @@ +package org.jboss.shotoku.feeds.variables; + +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.HashSet; + +/** + * Not thread-safe. + * @author Adam Warski (ad...@as...) + */ +public class Substitution implements Cloneable { + private Map<Pattern, String> substitutions; + + public Substitution() { + substitutions = new HashMap<Pattern, String>(); + } + + public void add(String pattern, String substitution) { + substitutions.put(Pattern.compile(Pattern.quote(pattern)), substitution); + } + + public String make(String original) { + StringBuffer result = new StringBuffer(original); + StringBuffer work = new StringBuffer(); + for (Map.Entry<Pattern, String> e : substitutions.entrySet()) { + Matcher m = e.getKey().matcher(result); + + work.setLength(0); + String replacement = e.getValue(); + while (m.find()) { + m.appendReplacement(work, replacement); + } + + m.appendTail(work); + + result.setLength(0); + result.append(work); + } + + return result.toString(); + } + + public Substitution clone() { + Substitution ret = null; + try { + ret = (Substitution) super.clone(); + } catch (CloneNotSupportedException e) { + // We know we support cloning. + } + ret.substitutions = new HashMap<Pattern, String>(substitutions); + + return ret; + } + + // --- + + public static Set<Substitution> newEmptySubstitionsSet() { + Set<Substitution> ret = new HashSet<Substitution>(); + ret.add(new Substitution()); + + return ret; + } +} Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/TypeVariableResolver.java =================================================================== --- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/TypeVariableResolver.java 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/TypeVariableResolver.java 2006-05-09 16:29:16 UTC (rev 4158) @@ -0,0 +1,16 @@ +package org.jboss.shotoku.feeds.variables; + +/** + * @author Adam Warski (ad...@as...) + */ +public class TypeVariableResolver extends MultiValueVariableResolver { + private static final String[] values = { "rss2", "atom", "rdf" }; + + protected String getVarName() { + return "${type}"; + } + + protected String[] getValues() { + return values; + } +} Added: labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/VariableResolver.java =================================================================== --- labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/VariableResolver.java 2006-05-09 14:51:59 UTC (rev 4157) +++ labs/shotoku/trunk/shotoku-feeds/src/java/org/jboss/shotoku/feeds/variables/VariableResolver.java 2006-05-09 16:29:16 UTC (rev 4158) @@ -0,0 +1,20 @@ +package org.jboss.shotoku.feeds.variables; + +import java.util.Set; + +/** + * @author Adam Warski (ad...@as...) + */ +public interface VariableResolver { + /** + * Expands each substitution in the given substitutions set to expand + * some variables that are (1) contained in the variables set and + * (2) handled by this variable resolver. Expanded variables should be + * removed from the variables set. Only variables that are in the variables + * set should be expanded. + * @param variables All variables that should be expanded. + * @param substitutions Current substitutions, which are to be expanded. + */ + public void expandSubstitutionsSet(Set<String> variables, + Set<Substitution> substitutions); +} |