View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Context;
4   import net.sf.tomp.xtcl.ParametrizedCommand;
5   
6   import javax.xml.transform.Result;
7   import javax.xml.transform.Source;
8   import javax.xml.transform.Templates;
9   import javax.xml.transform.Transformer;
10  import javax.xml.transform.TransformerException;
11  import javax.xml.transform.TransformerFactory;
12  import javax.xml.transform.stream.StreamResult;
13  import javax.xml.transform.stream.StreamSource;
14  
15  import java.util.HashMap;
16  import java.util.HashSet;
17  import java.util.Iterator;
18  import java.util.Map;
19  import java.util.Set;
20  
21  /***
22   * Performs a transformation - similar as in Chain but only single-step.
23   * 
24   * @author tomp
25   */
26  public class Transform extends AbstractCommand implements ParametrizedCommand  {
27  	
28      /*** DOCUMENT ME! */
29      protected String sourceRef;
30  
31      /*** DOCUMENT ME! */
32      protected String styleRef;
33  
34      /*** DOCUMENT ME! */
35      protected String resultRef;
36  
37      /*** DOCUMENT ME! */
38      protected Map params = new HashMap();
39  
40      /*** DOCUMENT ME! */
41      protected Map realParams = new HashMap();
42  
43      /*** DOCUMENT ME! */
44      protected Source source;
45  
46      /*** DOCUMENT ME! */
47      protected Templates templates;
48  
49      /*** DOCUMENT ME! */
50      protected Result result;
51  
52      /*** set of Properties references */
53      private Set propsReferences = new HashSet();
54  
55      /*** adds a reference to Properties that will be used as parameters */
56      public void addParameterPropertiesReference(String ref) {
57          propsReferences.add(ref); 
58      }
59  
60      /***
61       * DOCUMENT ME!
62       * 
63       * @param s DOCUMENT ME!
64       */
65      public void setSourceRef(String s) {
66          sourceRef = s;
67      }
68  
69      /***
70       * DOCUMENT ME!
71       * 
72       * @param s DOCUMENT ME!
73       */
74      public void setStyleRef(String s) {
75          styleRef = s;
76      }
77  
78      /***
79       * DOCUMENT ME!
80       * 
81       * @param s DOCUMENT ME!
82       */
83      public void setResultRef(String s) {
84          //System.err.println("*** Transform setting resultref="+s);
85          resultRef = s;
86      }
87  
88      /***
89       * DOCUMENT ME!
90       * 
91       * @param context DOCUMENT ME!
92       * @return DOCUMENT ME!
93       * @throws Exception DOCUMENT ME!
94       */
95      public int execute(Context context) throws Exception {
96  
97          TransformerFactory tf = context.getTransformerFactory();
98  
99          try {
100             source = sourceRef.startsWith("$") ? context.refToSource(sourceRef)
101                     : new StreamSource(context.getFile(sourceRef));
102 
103             templates = styleRef.startsWith("$") ? context.refToStyle(styleRef)
104                     : tf.newTemplates(new StreamSource(context.getFile(styleRef)));
105 
106             result = resultRef.startsWith("$") ? context.refToResult(resultRef)
107                     : new StreamResult(context.getFile(resultRef));
108 
109             Transformer t = (templates == null) ? tf
110                     .newTransformer(new StreamSource(context.getFile(styleRef)))
111                     : templates.newTransformer();
112 
113             initTransformer(t, context);
114 
115             //out.print("EXECUTING TRANSF "+this);
116             //String sid = source.getSystemId();
117             //System.out.println("TRANSF: srcsysid="+sid);
118             //result.setSystemId(sid);
119             t.transform(source, result);
120 
121 
122             return done(context, 0);
123             
124         } catch (TransformerException te) {
125 
126         	return done(context, 1);
127         }
128     }
129 
130     /***
131      * DOCUMENT ME!
132      * 
133      * @return DOCUMENT ME!
134      */
135     public String toString() {
136         return "TRANSFORM " + sourceRef + " STYLE " + styleRef + " -> "
137                 + resultRef + " (" + listMap(realParams) + ")";
138     }
139 
140     /***
141      * DOCUMENT ME!
142      * 
143      * @param k DOCUMENT ME!
144      * @param v DOCUMENT ME!
145      */
146     public void setParameter(String k, Object v) {
147         params.put(k, v);
148 
149         //System.out.println("Setting param "+k+"="+v);
150     }
151 
152     /***
153      * DOCUMENT ME!
154      * 
155      * @param t DOCUMENT ME!
156      * @param context DOCUMENT ME!
157      */
158     protected void initTransformer(Transformer t, Context context) {
159         for (Iterator i = params.keySet().iterator(); i.hasNext();) {
160             String k = (String) i.next();
161             Object v = params.get(k);
162 
163             if (v instanceof String && ((String) v).startsWith("$")) {
164                 //v = refToSource(v, context);
165                 v = context.get(v);
166             }
167 
168             t.setParameter(k, v);
169             realParams.put(k, v);
170         }
171     }
172 
173     /* (non-Javadoc)
174      * @see net.sf.tomp.xtcl.ParametrizedCommand#setParameterPropertyReference(java.lang.String)
175      */
176     public void setParameterPropertyReference(String ref) {
177         // TODO Auto-generated method stub
178         
179     }
180 }
181 
182 /*
183  * The contents of this file are subject to the Mozilla Public License Version
184  * 1.1 (the "License"); you may not use this file except in compliance with the
185  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
186  * Software distributed under the License is distributed on an "AS IS" basis,
187  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
188  * the specific language governing rights and limitations under the License. The
189  * Original Code is: all this file. The Initial Developer of the Original Code
190  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
191  */