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.TransformerException;
8 import javax.xml.transform.stream.StreamSource;
9
10 /***
11 * DOCUMENT ME!
12 *
13 * @author tomp
14 */
15 public class Style extends VarCommand {
16 /*** DOCUMENT ME! */
17 protected String styleRef;
18
19 /*** DOCUMENT ME! */
20 protected Templates templates;
21
22 /***
23 * DOCUMENT ME!
24 *
25 * @param s
26 * DOCUMENT ME!
27 */
28 public void setStyleRef(String s) {
29 styleRef = s;
30 }
31
32 /***
33 * creates Templates and puts it to Context
34 * styleRef may start with $ (lookup of var in Context)
35 * or otherwise it means a filename
36 *
37 * @param context
38 * DOCUMENT ME!
39 * @return DOCUMENT ME!
40 * @throws Exception
41 * DOCUMENT ME!
42 */
43 public int execute(Context context) throws Exception {
44
45 if (styleRef == null || styleRef.length() == 0) {
46 return done(context, 1);
47 }
48
49 try {
50
51 Source templatesSource = styleRef.startsWith("$") ? context
52 .refToSource(styleRef) : new StreamSource(context
53 .getFile(styleRef));
54
55 templates = context.getTransformerFactory().newTemplates(
56 templatesSource);
57
58 context.put(var, templates);
59
60 return done(context, 0);
61 } catch (TransformerException te) {
62 return done(context, 1);
63 }
64 }
65
66 /***
67 * @return "STYLE " + var + " <- " + styleRef;
68 */
69 public String toString() {
70 return "STYLE " + var + " <- " + styleRef;
71 }
72 }
73
74
75
76
77
78
79
80
81
82
83