[Yanel-commits] rev 27948 - in public/yanel/trunk/src: contributions/resources/pdf/src/java/org/wyona/yanel/impl/resources core/java/org/wyona/yanel/core/util

josias at wyona.com josias at wyona.com
Thu Oct 4 17:08:11 CEST 2007


Author: josias
Date: 2007-10-04 17:08:10 +0200 (Thu, 04 Oct 2007)
New Revision: 27948

Added:
   public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/WildcardReplacerHelper.java
Modified:
   public/yanel/trunk/src/contributions/resources/pdf/src/java/org/wyona/yanel/impl/resources/PDFResource.java
Log:
added WildcardReplacerHelper, see bug #5581, thanks to simon

Modified: public/yanel/trunk/src/contributions/resources/pdf/src/java/org/wyona/yanel/impl/resources/PDFResource.java
===================================================================
--- public/yanel/trunk/src/contributions/resources/pdf/src/java/org/wyona/yanel/impl/resources/PDFResource.java	2007-10-04 14:26:29 UTC (rev 27947)
+++ public/yanel/trunk/src/contributions/resources/pdf/src/java/org/wyona/yanel/impl/resources/PDFResource.java	2007-10-04 15:08:10 UTC (rev 27948)
@@ -26,6 +26,8 @@
 import org.wyona.yarep.core.RepositoryException;
 import org.wyona.yarep.core.Repository;
 
+import org.wyona.yanel.core.util.WildcardReplacerHelper;
+
 import org.apache.log4j.Category;
 
 import org.apache.fop.apps.FopFactory;
@@ -34,6 +36,8 @@
 
 import java.io.File;
 import java.io.InputStream;
+import java.util.Iterator;
+import java.util.Map;
 
 import javax.xml.transform.Transformer;
 import javax.xml.transform.TransformerFactory;
@@ -128,9 +132,8 @@
      *  @return String datapath
      */
     public String getDataPath() throws Exception {
-        String yanelPath = getResourceConfigProperty("yanel-path");
-        if (yanelPath != null) return yanelPath;
-        return getPath();
+        WildcardReplacerHelper dataPath = new WildcardReplacerHelper(getResourceConfigProperty("yanel-path"), getResourceConfigProperty("yanel-path-matcher"));
+        return dataPath.getReplacedString(getPath());
     }
 
     /**

Added: public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/WildcardReplacerHelper.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/WildcardReplacerHelper.java	                        (rev 0)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/WildcardReplacerHelper.java	2007-10-04 15:08:10 UTC (rev 27948)
@@ -0,0 +1,98 @@
+/*
+ * Copyright 2006 Wyona
+ *
+ *  Licensed 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.wyona.org/licenses/APACHE-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.wyona.yanel.core.util;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.log4j.Category;
+import org.wyona.yanel.core.Resource;
+import org.wyona.yanel.core.util.WildcardMatcherHelper;
+
+/**
+ * This class is an utility class that perform wildcard-patterns replacement.
+ * replaces the the {[\d]+} found in String stringWithReplaceTokens with
+ * the wildcard found in String pattern
+ * e.g:
+ * stringWithReplaceTokens = "/{1}/hello/{3}.world"
+ * pattern = "/STAR/STAR/STAR.STAR" (STAR means * which is not possible to use with a slash in javadoc)
+ * origString = "/test1/test2/test3.test4"
+ * getReplacedString() will give back: /test1/hello/test3.world
+ *
+ * {0} will be replaced with the whole origString
+ *
+ * see: org.wyona.yanel.core.util.WildcardMatcherHelper
+ */
+public class WildcardReplacerHelper {
+    private String stringWithReplaceTokens = null;
+    private String pattern = null;
+    private static Category log = Category.getInstance(WildcardReplacerHelper.class);
+
+    public WildcardReplacerHelper () {
+    }
+
+    public WildcardReplacerHelper (String stringWithReplaceTokens, String pattern) {
+        this.stringWithReplaceTokens = stringWithReplaceTokens;
+        this.pattern = pattern;
+    }
+    /**
+     * @param origString e.g. /test1/test2/test3.test4
+     * @return e.g. /test1/hello/test3.world
+     * if the stringWithReplaceTokens == null it returns the origString
+     * if pattern == null it returns the stringWithReplaceTokens
+     * else it replaces the the {[\d]+} found in String stringWithReplaceTokens with
+     * the wildcard found in String pattern
+     * returns null if the pattern doesn't match.
+     * @throws Exception
+     * @see org.wyona.yanel.core.util.WildcardMatcherHelper
+     */
+    public String getReplacedString(String origString) throws Exception {
+        if (stringWithReplaceTokens == null) return origString;
+        if (pattern == null) return stringWithReplaceTokens;
+
+        log.debug("yanel-path property: " + stringWithReplaceTokens);
+        log.debug("yanel-path-matcher property: " + pattern);
+
+        Map map = WildcardMatcherHelper.match(pattern, origString);
+        if (map == null) {
+            return null;
+        }
+        String resultString = stringWithReplaceTokens;
+        Iterator it = map.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry pairs = (Map.Entry)it.next();
+            resultString = resultString.replaceAll("\\{" + pairs.getKey() + "\\}" , (String) pairs.getValue());
+            log.debug(pairs.getKey() + " = " + pairs.getValue());
+        }
+        log.debug("String after wildcard replacement: " + resultString);
+        return resultString;
+    }
+
+    /**
+     * @param stringWithReplaceTokens e.g /{1}/hello/{3}.world
+     */
+    public void setStringWithReplaceTokens(String stringWithReplaceTokens) {
+        this.stringWithReplaceTokens = stringWithReplaceTokens;
+    }
+
+    /**
+     * @param pattern e.g. /STAR/STAR/STAR.STAR (STAR means * which is not possible to use with a slash in javadoc)
+     */
+    public void setPattern(String pattern) {
+        this.pattern = pattern;
+    }
+}
\ No newline at end of file


Property changes on: public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/WildcardReplacerHelper.java
___________________________________________________________________
Name: svn:eol-style
   + native



More information about the Yanel-commits mailing list