1 2 3 package Jt.xml; 4 import java.util.*; 5 import java.lang.reflect.*; 6 import java.beans.*; 7 import java.io.*; 8 import org.xml.sax.*; 9 import org.xml.sax.helpers.*; 10 import Jt.*; 11 12 15 16 17 public class JtScriptInterpreter extends JtObject { 18 private XMLReader reader = null; 19 private String filename; 20 private String content; 21 private boolean remoteInvocation = false; 22 JtXMLHelper xmlHelper = null; 24 Object scriptOutput = null; 28 29 30 public JtScriptInterpreter () { 31 } 32 33 34 38 39 public void setFilename (String filename) { 40 this.filename = filename; 41 } 42 43 44 47 48 public String getFilename () { 49 return (filename); 50 } 51 52 56 57 public void setContent (String content) { 58 this.content = content; 59 } 60 61 62 65 public String getContent () { 66 return (content); 67 } 68 69 70 71 72 public void setRemoteInvocation (boolean remoteInvocation) { 73 this.remoteInvocation = remoteInvocation; 74 } 75 76 77 public boolean getRemoteInvocation () { 78 return (remoteInvocation); 79 } 80 81 83 private Exception propagateException (Object obj) 84 { 85 Exception ex; 86 87 if (obj == null) 88 return null; 89 90 ex = (Exception ) 91 getValue (obj, "objException"); 92 93 if (ex != null) 94 setValue (this, "objException", ex); 95 96 return (ex); 97 } 98 99 100 103 private Object interpretMsg (Object event) { 104 105 String msgid; 106 JtMessage msg = (JtMessage) event; 107 Object result = null; 108 String ctype; 109 110 if (msg == null) 111 return (null); 112 113 msgid = (String ) msg.getMsgId (); 114 115 if (msgid == null) 116 return (null); 117 118 if (msgid.equals ("JtSET_VALUE")) { 119 120 if ("this".equals (msg.getMsgSubject ())) 121 setValue (this, 122 msg.getMsgContent (), 123 msg.getMsgData()); 124 else 125 setValue (msg.getMsgSubject (), 126 msg.getMsgContent (), 127 msg.getMsgData()); 128 129 return (null); 131 } 132 133 if (msgid.equals ("JtCREATE_OBJECT")) { 134 137 ctype = (String ) msg.getMsgContent(); 138 139 if (remoteInvocation) { 140 if ("Jt.JtOSCommand".equals (ctype) || "Jt.JtFile".equals (ctype)) { 141 handleError ("Security violation: unable to create a remote instance of " + 142 ctype); 143 144 return (null); 145 } 146 147 } 148 result = createObject (msg.getMsgContent (), 149 msg.getMsgData()); 150 151 if (remoteInvocation) 152 return (null); 153 154 scriptOutput = result; 155 return (result); 156 } 157 158 if (msgid.equals ("JtGET_VALUE")) { 159 160 161 result = getValue (msg.getMsgContent (), 162 msg.getMsgData()); 163 164 scriptOutput = result; 165 return (result); 166 } 167 168 if (msgid.equals ("JtREMOVE_OBJECT")) { 169 removeObject (msg.getMsgContent ()); 170 return (null); 171 } 172 173 174 if (msgid.equals ("JtSEND_MESSAGE")) { 175 176 result = sendMessage (msg.getMsgContent (), 177 msg.getMsgData ()); 178 179 181 scriptOutput = result; 182 return (result); 183 184 } 185 186 if (msgid.equals ("JtREMOVE")) { 187 return (null); 188 } 189 190 handleError ("JtScriptInterpreter.interpretMessage: invalid msg ID:" 191 + msg.getMsgId ()); 192 return (null); 193 } 194 195 196 private Object parse () { 197 198 199 JtMessage msg; 200 JtList col; 201 JtMessage msg1; 202 JtIterator iterator; 203 String str; 204 JtFile file; 205 Object result = null; 206 Exception ex; 207 208 if (filename != null && content != null) { 209 handleError ("JtScriptInterpreter.parse: set uri or string (only one)"); 210 return null; 211 } 212 213 if (filename == null && content == null) { 214 handleError ("JtScriptInterpreter.parse: both uri and string are null"); 215 return null; 216 } 217 218 scriptOutput = null; 219 220 if (filename != null) { 221 222 file = new JtFile (); 223 224 setValue (file, "name", filename); 225 226 str = (String ) sendMessage (file, new JtMessage ("JtCONVERT_TO_STRING")); 227 228 229 } else 230 str = content; 231 232 233 if (str == null) 234 return (null); 235 236 if (xmlHelper == null) 237 xmlHelper = (JtXMLHelper) createObject ("Jt.xml.JtXMLHelper", "xmlHelper"); 238 239 240 msg = new JtMessage ("JtCONVERT_XML_TO_OBJECT"); 241 242 msg.setMsgContent (str); 243 244 col = (JtList) sendMessage (xmlHelper, msg); 245 246 248 if (col == null) 249 return (null); 250 251 msg = new JtMessage ("JtNEXT"); 252 253 iterator = (JtIterator) getValue (col, "iterator"); 254 255 if (iterator == null) 256 return (null); 257 258 for (;;) { 259 msg1 = (JtMessage) iterator.processMessage (new JtMessage ("JtNEXT")); 261 262 if (msg1 == null) 263 break; 264 265 interpretMsg (msg1); 266 267 ex = (Exception ) 268 getValue (this, "objException"); 269 270 if (ex != null) 271 break; } 274 275 return (scriptOutput); 276 } 277 278 279 281 public Object processMessage (Object event) { 282 283 String msgid = null; 284 JtMessage e = (JtMessage) event; 285 Object content; 286 287 if (e == null) 288 return null; 289 290 msgid = (String ) e.getMsgId (); 291 292 if (msgid == null) 293 return null; 294 295 297 if (msgid.equals ("JtPARSE")) { 298 return (parse ()); 299 } 300 301 302 handleError ("JtScriptInterpreter.processMessage: invalid message id:" + msgid); 303 return (null); 304 305 } 306 307 308 309 312 313 public static void main(String [] args) { 314 315 JtObject main = new JtObject (); 316 JtMessage msg, msg1; 317 Integer count; 318 String str; 319 320 main.setObjTrace (1); 321 323 324 if (args.length < 1) { 325 System.err.println ("Usage: java JtScriptInterpreter uri"); 326 System.exit (1); 327 } 328 329 331 main.createObject ("Jt.xml.JtScriptInterpreter", "reader"); 332 333 msg = (JtMessage) main.createObject ("Jt.JtMessage", "message"); 334 main.setValue ("message", "msgId", "JtPARSE"); 335 main.setValue ("reader", "filename", args[0]); 336 337 338 main.setValue ("message", "msgId", "JtPARSE"); 339 340 341 main.sendMessage ("reader", "message"); 342 343 344 } 345 346 } 347 348 349 | Popular Tags |