View Javadoc

1   package net.sf.tomp.xtcl.command;
2   
3   import net.sf.tomp.xtcl.Context;
4   
5   import javax.xml.transform.Source;
6   import javax.xml.transform.Templates;
7   import javax.xml.transform.Transformer;
8   import javax.xml.transform.TransformerException;
9   import javax.xml.transform.stream.StreamResult;
10  
11  import java.io.PrintStream;
12  import java.util.Properties;
13  
14  /***
15   * Prints variable content.
16   * 
17   * @author tomp
18   */
19  public class Dump extends AbstractCommand {
20  
21  	/*** DOCUMENT ME! */
22  	protected String var;
23  
24  	/*** DOCUMENT ME! */
25  	protected boolean deep = false;
26  
27  	/*** DOCUMENT ME! */
28  	protected boolean all = false;
29  
30  	/***
31  	 * set the variable name
32  	 * 
33  	 * @param v
34  	 *            the variable name
35  	 */
36  	public void setVar(String v) {
37  		var = v;
38  	}
39  
40  	/***
41  	 * set whether deep or not
42  	 * 
43  	 * @param v
44  	 *            deeply?
45  	 */
46  	public void setDeep(boolean v) {
47  		deep = v;
48  	}
49  
50  	/***
51  	 * DOCUMENT ME!
52  	 * 
53  	 * @param v
54  	 *            DOCUMENT ME!
55  	 */
56  	public void setAll(boolean v) {
57  		all = v;
58  	}
59  
60  	/***
61  	 * Prints variable content.
62  	 * String is printed directly as 'string',
63  	 * Properties and Templates via toString()
64  	 * otherwise the variable is getAsSource() and
65  	 * transformed via empty transformation to output.
66  	 * 
67  	 * @param context
68  	 *            to get dumped variable from
69  	 * @return 0 if OK, otherwise 1
70  	 * @throws Exception
71  	 *             if output fails
72  	 */
73  	public int execute(Context context) throws Exception {
74  		PrintStream out = context.getOut();
75  
76  		if (out == null || var == null) {
77  			return 0;
78  		}
79  
80  		Object o = context.get(var);
81  		if (o instanceof String) {
82  			out.print("'");
83  			out.print(o);
84  			out.print("' ");
85  		} else if (o instanceof Properties || o instanceof Templates) {
86  			out.print(o + " ");
87  		} else {
88  			Transformer empty = context.getEmptyTransformer();
89  			try {
90  				Source source = context.refToSource(var);
91  				empty.transform(source, new StreamResult(out));
92  			} catch (TransformerException te) {
93  				return done(context, 1);
94  			}
95  		}
96  		return done(context, 0);
97  	}
98  
99  	/***
100 	 * 
101 	 * @return "DUMP "
102 				+ ((var == null) ? ("CONTEXT " + (deep ? "DEEP" : "")) : var);
103 	 */
104 	public String toString() {
105 		return "DUMP "
106 				+ ((var == null) ? ("CONTEXT " + (deep ? "DEEP" : "")) : var);
107 	}
108 }
109 
110 /*
111  * The contents of this file are subject to the Mozilla Public License Version
112  * 1.1 (the "License"); you may not use this file except in compliance with the
113  * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
114  * Software distributed under the License is distributed on an "AS IS" basis,
115  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
116  * the specific language governing rights and limitations under the License. The
117  * Original Code is: all this file. The Initial Developer of the Original Code
118  * is: Tomas Pitner, Masaryk University in Brno, Czech Republic. Contributor(s):
119  */