| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
package org.webmacro.engine; |
| 25 | |
|
| 26 | |
import java.io.IOException; |
| 27 | |
import java.io.InputStream; |
| 28 | |
import java.io.InputStreamReader; |
| 29 | |
import java.io.Reader; |
| 30 | |
import java.util.Date; |
| 31 | |
|
| 32 | |
import org.webmacro.Broker; |
| 33 | |
import org.webmacro.Context; |
| 34 | |
import org.webmacro.Template; |
| 35 | |
import org.webmacro.WM; |
| 36 | |
import org.webmacro.WMConstants; |
| 37 | |
import org.webmacro.WebMacro; |
| 38 | |
import org.webmacro.util.SelectList; |
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public class StreamTemplate extends WMTemplate |
| 49 | |
{ |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
private Reader _in; |
| 55 | 0 | private String _name = null; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public StreamTemplate (Broker broker, Reader inStream) |
| 61 | |
{ |
| 62 | 0 | super(broker); |
| 63 | 0 | _in = inStream; |
| 64 | 0 | } |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
public StreamTemplate (Broker broker, InputStream in) |
| 74 | |
throws IOException |
| 75 | |
{ |
| 76 | 0 | this(broker, in, null); |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public StreamTemplate (Broker broker, InputStream in, String encoding) |
| 89 | |
throws IOException |
| 90 | |
{ |
| 91 | 0 | super(broker); |
| 92 | 0 | if (encoding == null) |
| 93 | 0 | encoding = getDefaultEncoding(); |
| 94 | 0 | _in = new InputStreamReader(in, encoding); |
| 95 | 0 | } |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
protected Reader getReader () throws IOException |
| 102 | |
{ |
| 103 | 0 | if (_in != null) |
| 104 | |
{ |
| 105 | 0 | Reader ret = _in; |
| 106 | 0 | _in = null; |
| 107 | 0 | return ret; |
| 108 | |
} |
| 109 | |
else |
| 110 | |
{ |
| 111 | 0 | throw new IOException("Already read stream."); |
| 112 | |
} |
| 113 | |
} |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
public String toString () |
| 121 | |
{ |
| 122 | 0 | String s = null; |
| 123 | 0 | if (_in != null) |
| 124 | 0 | s = _in.toString(); |
| 125 | 0 | return (_name != null) |
| 126 | |
? "StreamTemplate:" + _name |
| 127 | |
: (s != null) ? "StreamTemplate(" + _in + ")" : "(stream)"; |
| 128 | |
} |
| 129 | |
|
| 130 | |
public String getName () |
| 131 | |
{ |
| 132 | 0 | return (_name == null) ? toString() : _name; |
| 133 | |
} |
| 134 | |
|
| 135 | |
public void setName (String name) |
| 136 | |
{ |
| 137 | 0 | _name = name; |
| 138 | 0 | } |
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
public static void main (String arg[]) |
| 144 | |
{ |
| 145 | |
|
| 146 | |
|
| 147 | 0 | WebMacro wm = null; |
| 148 | 0 | Context context = null; |
| 149 | 0 | String encoding = null; |
| 150 | |
|
| 151 | |
try |
| 152 | |
{ |
| 153 | 0 | wm = new WM(); |
| 154 | 0 | context = wm.getContext(); |
| 155 | 0 | Object names[] = {"prop"}; |
| 156 | 0 | context.setProperty(names, "Example property"); |
| 157 | 0 | encoding = wm.getConfig(WMConstants.TEMPLATE_INPUT_ENCODING); |
| 158 | |
} |
| 159 | 0 | catch (Exception e) |
| 160 | |
{ |
| 161 | 0 | e.printStackTrace(); |
| 162 | 0 | return; |
| 163 | 0 | } |
| 164 | |
|
| 165 | |
try |
| 166 | |
{ |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
|
| 173 | |
|
| 174 | 0 | context.put("helloworld", "Hello World"); |
| 175 | 0 | context.put("hello", "Hello"); |
| 176 | 0 | context.put("file", "include.txt"); |
| 177 | 0 | context.put("today", new Date()); |
| 178 | 0 | TestObject[] fruits = {new TestObject("apple", false), |
| 179 | |
new TestObject("lemon", true), |
| 180 | |
new TestObject("pear", false), |
| 181 | |
new TestObject("orange", true), |
| 182 | |
new TestObject("watermelon", false), |
| 183 | |
new TestObject("peach", false), |
| 184 | |
new TestObject("lime", true)}; |
| 185 | |
|
| 186 | 0 | SelectList sl = new SelectList(fruits, 3); |
| 187 | 0 | context.put("sl-fruits", sl); |
| 188 | |
|
| 189 | 0 | context.put("fruits", fruits); |
| 190 | 0 | context.put("flipper", new TestObject("flip", false)); |
| 191 | |
|
| 192 | 0 | System.out.println("- - - - - - - - - - - - - - - - - - - -"); |
| 193 | 0 | System.out.println("Context contains: helloWorld, " + |
| 194 | |
"hello, file, TestObject[] fruits, " + |
| 195 | |
"SelectList sl(fruits, 3), TestObject flipper"); |
| 196 | 0 | System.out.println("- - - - - - - - - - - - - - - - - - - -"); |
| 197 | |
|
| 198 | 0 | Template t1 = new StreamTemplate(wm.getBroker(), |
| 199 | |
new InputStreamReader(System.in)); |
| 200 | 0 | t1.parse(); |
| 201 | |
|
| 202 | 0 | System.out.println("*** RESULT ***"); |
| 203 | 0 | t1.write(System.out, encoding, context); |
| 204 | 0 | System.out.println("*** DONE ***"); |
| 205 | 0 | context.clear(); |
| 206 | |
|
| 207 | |
} |
| 208 | 0 | catch (Exception e) |
| 209 | |
{ |
| 210 | 0 | e.printStackTrace(); |
| 211 | 0 | } |
| 212 | |
|
| 213 | 0 | } |
| 214 | |
|
| 215 | |
} |