1 package net.sf.tomp.xtcl.command;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.FileInputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.util.Properties;
8
9 import net.sf.tomp.xtcl.Context;
10
11 /***
12 * Creates java.util.Properties and puts it into Context.
13 *
14 * @author tomp
15 */
16 public class PropertiesCommand extends VarCommand {
17
18 /*** Properties body */
19 private String body;
20
21 /*** Properties file */
22 private String fileNameOrRef;
23
24 /***
25 * if fileNameOrRef == null, create Properties from String 'body'
26 * otherwise from File referenced by 'fileNameOrRef'
27 *
28 */
29 public int execute(Context context) throws Exception {
30 Properties props = new Properties();
31 InputStream is = null;
32
33 if (fileNameOrRef == null) {
34
35 is = new ByteArrayInputStream(body.getBytes());
36 } else {
37 is = new FileInputStream(FileCommand
38 .getFile(context, fileNameOrRef));
39 }
40 try {
41 props.load(is);
42 } catch (IOException ioe) {
43 return done(context, 1);
44 }
45 context.put(var, props);
46 return done(context, 0);
47 }
48
49 /***
50 * @return the body of the text from which the Properties will be created.
51 */
52 public String getBody() {
53 return body;
54 }
55
56 /***
57 * @return Returns the fileNameOrRef.
58 */
59 public String getFileNameOrRef() {
60 return fileNameOrRef;
61 }
62
63 /***
64 * Set the command (sequence) found in this template body
65 *
66 * @param c
67 * the command from the body
68 */
69 public void setBody(String b) {
70 body = b;
71 }
72
73 public void setFileNameOrRef(String b) {
74 fileNameOrRef = b;
75 }
76
77 /***
78 * @return "PROPERTIES " + var + " <- "
79 + (fileNameOrRef == null ? "{...}" : fileNameOrRef);
80 */
81 public String toString() {
82 return "PROPERTIES " + var + " <- "
83 + (fileNameOrRef == null ? "{...}" : fileNameOrRef);
84 }
85 }
86
87
88
89
90
91
92
93
94
95
96