View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Command;
4   import net.sf.tomp.xtcl.Compiler;
5   import net.sf.tomp.xtcl.CompilerBase;
6   import net.sf.tomp.xtcl.Context;
7   import org.apache.commons.logging.Log;
8   import org.apache.commons.logging.LogFactory;
9   
10  import java.util.HashMap;
11  import java.util.Map;
12  import java.util.regex.Matcher;
13  import java.util.regex.Pattern;
14  
15  /***
16   * Do a template.
17   * 
18   * @author tomp
19   */
20  public class Do extends AbstractParametrizedVarCommand {
21  
22      private static Log log = LogFactory.getLog(Do.class);
23  
24      /*** Template name */
25      private String templateName;
26  
27      /*** Template name-parameter assignments */
28      private Map params = new HashMap();
29      
30      private static Pattern varPattern = Pattern.compile("//%([^//%]*)//%");
31  
32      /***
33       * @return Returns the name.
34       */
35      public String getTemplateName() {
36          return templateName;
37      }
38  
39      /***
40       * Do a template.
41       * If templatename starts with @, template is loaded from file 'templatename'
42       * otherwise, template is taken from the Context
43       * 
44       * @param context Context to get, compile and execute  
45       * the commands in the template
46       *  
47       * @return result of the execution
48       * @throws Exception 
49       */
50      public int execute(Context context) throws Exception {
51          // transfer props to params
52          propertiesToParameters(context);
53          
54          Command c = null;
55          Compiler compiler = context.getCompiler();
56          Template template = null;
57          
58          if(templateName == null) {
59              // cannot execute null-named template
60              return done(context, 1);
61  
62          } else  if(templateName.startsWith("@")) {
63              // take template from file 
64              // FIXME: no %param% substitution!
65              c = ((CompilerBase)compiler).compile(context.getFile(templateName.substring(1)));
66          
67          } else { 
68              // take template from context
69              template = context.refToTemplate(templateName);
70              String body = template.getBody();
71              String finished = removeNonsubstituted(substitute(body));
72  
73              c = compiler.compile(finished);
74          }
75          return done(context, context.execute(c));
76      }
77  
78      /***
79       * Find and Replace param references in the template body.
80       *  
81       * @param body original template body
82       * @return substituted template body
83       */
84      protected String substitute(String body) {
85          Matcher m = varPattern.matcher(body);
86          StringBuffer replaced = new StringBuffer();
87  
88          while (m.find()) {
89              String paramName = m.group(1);
90  
91              //			System.out.println("param.name="+paramName);
92              String paramValue = (String) params.get(paramName);
93  
94              //			System.out.println("real.param="+paramValue);
95              if (paramValue != null) {
96                  paramValue = paramValue.replaceAll("//$", "//////$");
97  
98                  //				System.out.println("real.param.$escaped="+paramValue);
99                  m.appendReplacement(replaced, paramValue);
100             }
101         }
102 
103         m.appendTail(replaced);
104 
105         return replaced.toString();
106     }
107 
108     /***
109      * Removes parameter references that were not replaced by new values.
110      * 
111      * @param body
112      * @return body w/removed refs
113      */
114     protected String removeNonsubstituted(String body) {
115         // deletes unused %params% 
116         // example: "%jedna%neco jinyho %tri%deset%pet%" gives "neco jinyho deset"
117         String  removed = body.replaceAll("//%([//w//.//-]+)//%", "");
118 //        System.out.println("***"+ body+" -> "+removed);
119         return removed;
120     }
121 
122     /***
123      * Sets the (formal, real) parameter pair
124      * 
125      * @param k Parameter name (key) - formal param. name
126      * @param v Parameter value - real param. name
127      */
128     public void setParameter(String k, Object v) {
129         params.put(k, v); 
130 //        System.out.println("Setting param "+k+"="+v);
131     }
132 
133     /***
134      * Set the template name
135      * 
136      * @param p template name
137      */
138     public void setTemplateName(String p) {
139         templateName = p;
140     }
141 
142     /***
143      * String repre of DO-TEMPLATE
144      * 
145      * @return "DO-TEMPLATE " + templateName + " {" + listMap(params) + "}";
146      */
147     public String toString() {
148         return "DO-TEMPLATE " + templateName + " {" + listMap(params) + "}";
149     }
150 }
151 
152 /*
153  * The contents of this file are subject to the Mozilla Public License Version
154  * 1.1 (the "License"); you may not use this file except in compliance with the
155  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
156  * Software distributed under the License is distributed on an "AS IS" basis,
157  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
158  * the specific language governing rights and limitations under the License. The
159  * Original Code is: all this file. The Initial Developer of the Original Code
160  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
161  */