Update of 
/var/cvs/applications/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache
In directory 
james.mmbase.org:/tmp/cvs-serv16247/applications/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache

Modified Files:
        FlushNameTemplateBean.java 
Removed Files:
        PrefixSuffixModifier.java 
Log Message:
lots of changes, lots of tests that all pass


See also: 
http://cvs.mmbase.org/viewcvs/applications/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache


Index: FlushNameTemplateBean.java
===================================================================
RCS file: 
/var/cvs/applications/vpro-wizards/src/org/mmbase/applications/vprowizards/spring/cache/FlushNameTemplateBean.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- FlushNameTemplateBean.java  28 Nov 2008 20:20:48 -0000      1.2
+++ FlushNameTemplateBean.java  6 Dec 2008 17:58:51 -0000       1.3
@@ -9,9 +9,8 @@
  */
 package org.mmbase.applications.vprowizards.spring.cache;
 
-import java.util.StringTokenizer;
-
 import org.apache.commons.lang.StringUtils;
+import org.mmbase.applications.vprowizards.spring.cache.template.*;
 import org.mmbase.bridge.Cloud;
 import org.mmbase.bridge.Node;
 import org.mmbase.bridge.NodeList;
@@ -22,10 +21,7 @@
  * 
  * <pre>
  * This bean is used in the templates of the vpro-wizards. it is used to add 
node numbers to templates inside flush names.
- * It also has a method for cleaning the templates out of the flush names. For 
this reason it implements the Modifier 
- * interface.
- * 
- * As arguments it takes a template string, a 
+ * It is actually a facade for the classes in [EMAIL PROTECTED] 
org.mmbase.applications.vprowizards.spring.cache.template}. 
  * 
  * So, what are templates?
  * 
@@ -52,8 +48,7 @@
  * @author ebunders
  * 
  */
-public class FlushNameTemplateBean implements Modifier {
-    private static final String TEMPLATE_REGEXP = 
"^.*\\[[a-zA-Z0-9\\.:]+\\].*$";
+public class FlushNameTemplateBean {
 
     private String template;
 
@@ -85,190 +80,32 @@
     }
 
 
-    public String processAndGetTemplate() {
-        if (StringUtils.isEmpty(template)) {
-            throw new IllegalStateException("template not set");
-        }
-
-        if (StringUtils.isEmpty(nodeNumber)) {
-            throw new IllegalStateException("nodenr not set");
-        }
-        if (StringUtils.isEmpty(nodeType)) {
-            throw new IllegalStateException("type not set");
-        }
-        
-        if (cloud == null) {
-            throw new IllegalStateException("cloud not set");
+    public String processAndGetTemplate(){
+        checkNull(template, "template");
+        checkNull(nodeType, "nodeType");
+        checkNull(nodeNumber, "nodeNumber");
+        checkNull(cloud, "cloud");
+        if(MultiTemplateParser.isTemplate(template)) {
+            TemplateQueryRunner templateQueryRunner = new 
MMBaseTemplateQueryRunner(cloud);
+            TemplateParser parser = new MultiTemplateParser(nodeType, 
nodeNumber, template, templateQueryRunner );
+            parser.insertNumber();
+            return parser.getTemplate();
         }
-        
-        //we create a processor that will first strip the old node numbers, 
and then insert new ones.
-        processTemplate(new SubTemplateProcessor() {
-            @Override
-            void process() {
-                // Perhaps the template has been used before, and 'old' node 
numbers are present
-                // if this is so, they must be removed.
-                _subTemplate = stripNodenumberFromSubtemplate(_subTemplate);
-                
-
-                String templateNodeType = deriveTemplateType(_subTemplate);
-                if (nodeType.equals(templateNodeType)) {
-                    _subTemplate = appendNodenumberToTemplate(_subTemplate);
-                }
-                
-                //even if the node type did not match, perhaps the template 
was cleaned from previous uses.
-                //reinsert it anyway.
-                reinsertSubTemplateIntoTemplate(_subTemplate, _begin, _end);
-            }
-        });
         return template;
     }
 
     
-    /**
-     * This method is from the Modifier interface, and allows this class to 
play as a Modifier instance.
-     */
-    public String modify(String input) {
-            setTemplate(input);
-            processTemplate(new SubTemplateProcessor(){
-                @Override
-                void process() {
-                    _subTemplate = 
stripNodenumberFromSubtemplate(_subTemplate);
-                    reinsertSubTemplateIntoTemplate(_subTemplate, _begin, 
_end);
-                }});
-        return template;
-    }
-
-    private void processTemplate(SubTemplateProcessor processor) {
-        int offset = 0;
-        while (offset < template.length()  && 
template.substring(offset).matches(TEMPLATE_REGEXP) ) {
-            log.debug("evaluating: " + template.substring(offset) + ", from: " 
+ offset);
-            int begin = template.indexOf("[", offset) + 1;
-            int end = template.indexOf("]", offset);
-
-            String subTemplate = template.substring(begin, end);
-            log.debug("begin: " + begin + ", end: " + end + ", template: " + 
subTemplate);
-
-            processor.setSubTemplate(subTemplate);
-            processor.setBegin(begin);
-            processor.setEnd(end);
-            processor.process();
-            offset = offset + begin + processor.getSubTemplate().length() + 1;
-        }
-    }
-
-    
-    
-    private void reinsertSubTemplateIntoTemplate(String subTemplate, int 
begin, int end){
-        String subTemplatePrefix = template.substring(0, begin);
-        String subTemplateSuffix = template.substring(end);
-        template = subTemplatePrefix + subTemplate + subTemplateSuffix;
-    }
-
-    private String stripNodenumberFromSubtemplate(String subTemplate) {
-        //pattern: aaa:89 or aaa.aaa.aaa:00
-        if (subTemplate.matches("(\\w+:\\d+)|((\\w+)(\\.\\w+){2}:\\d+)")) {
-            subTemplate = subTemplate.substring(0, subTemplate.indexOf(":"));
-            log.debug("template reuse. after cleaning: " + subTemplate);
-        }
-        return subTemplate;
-    }
-
-    private boolean subtemplateIsQuery(String subTemplate) {
-        return subTemplate.split("\\.").length == 3;
-    }
-
-    private String resolveNodeNumber(String subTemplate) {
-        if (subtemplateIsQuery(subTemplate)) {
-            return resolveNodeNumberForQuery(subTemplate);
-        } else {
-            return nodeNumber;
-        }
-    }
-    
-    private String appendNodenumberToTemplate(String subTemplate) {
-        return subTemplate + ":" + resolveNodeNumber(subTemplate);
+    private void checkNull(Object obj, String name){
+        if (obj == null) {
+            throw new IllegalStateException(String.format("property %s has not 
been set.", name));
     }
-
-    private String resolveNodeNumberForQuery(String subTemplate) {
-        QueryTemplate queryTemplate = parseQueryTemplate(subTemplate);
-        Node node = cloud.getNode(nodeNumber);
-        NodeList nl = node.getRelatedNodes(queryTemplate.getDestinationType(), 
queryTemplate.getRelationRole(), "both");
-        if (nl.size() > 0) {
-            return "" + nl.getNode(0).getNumber();
-        } else {
-            log.error("could not find 'parent' node with path " + subTemplate 
+ " and root node " + nodeNumber);
-            return "!notfound!";
+        if (String.class.isAssignableFrom(obj.getClass()) && 
StringUtils.isBlank((String)obj)) {
+            throw new IllegalStateException(String.format("property %s is an 
empty string.", name));
         }
     }
 
-    private String deriveTemplateType(String subTemplate) {
-        if (subtemplateIsQuery(subTemplate)) {
-            return parseQueryTemplate(subTemplate).getSourceType();
-        } else {
-            return subTemplate;
-        }
-    }
 
-    private QueryTemplate parseQueryTemplate(String subTemplate) {
-        String[] q = subTemplate.split("\\.");
-        if (q.length == 3) {
-            return new QueryTemplate(q[0], q[1], q[2]);
-        } else {
-            throw new IllegalStateException(String.format(
-                    "template '%s' is not a query template, can not create 
QueryTemplate instance", subTemplate));
-        }
-    }
 
-    public Modifier copy() {
-        // as a Modifier this thing is completely stateless, so just return
-        // this instance
-        return this;
-    }
-
-    private static class QueryTemplate {
-        private String sourceType, relationRole, destinationType;
-
-        public QueryTemplate(String sourceType, String relationRole, String 
destinationType) {
-            this.sourceType = sourceType;
-            this.relationRole = relationRole;
-            this.destinationType = destinationType;
-        }
 
-        public String getSourceType() {
-            return sourceType;
-        }
-
-        public String getRelationRole() {
-            return relationRole;
-        }
 
-        public String getDestinationType() {
-            return destinationType;
-        }
-    }
-
-    private abstract class SubTemplateProcessor {
-        protected int _begin;
-        protected int _end;
-        protected String _subTemplate;
-
-        abstract void process();
-
-        public void setSubTemplate(String subTemplate) {
-            this._subTemplate = subTemplate;
-        }
-        
-        public String getSubTemplate(){
-            return _subTemplate;
-        }
-
-        public void setBegin(int begin) {
-            this._begin = begin;
-        }
-
-        public void setEnd(int end) {
-            this._end = end;
-        }
-
-    }
 }


_______________________________________________
Cvs mailing list
[email protected]
http://lists.mmbase.org/mailman/listinfo/cvs

Reply via email to