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 13 16 17 public class JtXMLMsgReader extends JtObject 18 implements ContentHandler { 19 private XMLReader reader = null; 20 protected static final String DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser"; 21 private String uri; 22 private String string; 23 private JtMessage msg = null; 24 private String elemName = null; 25 Object replyTo = null; 27 StringBuffer buffer = null; 28 Object output = null; 29 JtXMLHelper xmlHelper = null; 30 private String parserName = DEFAULT_PARSER_NAME; 31 32 33 public JtXMLMsgReader() { 34 } 35 36 37 41 42 void setParserName (String parserName) { 43 this.parserName = parserName; 44 45 } 46 47 48 51 52 String getParserName () { 53 return (parserName); 54 } 55 56 57 59 public void setUri (String uri) { 60 this.uri = uri; 61 } 62 63 public String getUri () { 64 return (uri); 65 } 66 67 69 public void setString (String string) { 70 this.string = string; } 72 73 public String getString () { 74 return (string); 75 } 76 77 78 public void startElement(String uri, String local, String raw, 79 Attributes attrs) throws SAXException { 80 81 if ("".equals (uri)) { 82 handleTrace ("Start element: " + raw); 83 84 if ("JtMessage".equals (raw)) { 85 msg = new JtMessage (); 86 return; 87 } 88 89 elemName = raw; 90 buffer = null; 92 93 }else 94 handleTrace ("Start element: {" + uri + "}" + local); 95 96 } 97 98 Object sendReply (JtMessage msg ) { 99 if (msg != null && replyTo != null ) 100 return (sendMessage (replyTo, msg)); 101 return (null); 102 } 103 104 public void endElement(String uri, 105 String name, 106 String qName) 107 throws SAXException { 108 109 110 111 115 if ("".equals (uri)) { 116 handleTrace ("End element: " + qName); 117 118 if (msg != null && "JtMessage".equals (qName)) { 119 output = sendReply (msg); 120 msg = null; 121 buffer = null; 122 return; 123 } 124 125 if (msg != null && buffer != null && 126 qName != null) { 127 128 if (!qName.equals ("JtMessage")) 129 setValue (msg, qName, buffer.toString ()); 130 buffer = null; 131 } 132 133 } else 134 handleTrace ("End element: {" + uri + "}" + name); 135 } 136 137 138 public void startDocument() throws SAXException { 139 140 } 141 142 public void endDocument() 143 throws SAXException { 144 145 } 146 147 public void characters(char[] ch, 148 int start, 149 int length) 150 throws SAXException { 151 String tmp; 152 153 if (buffer == null) 155 buffer = new StringBuffer (); 156 157 for (int i = start; i < start + length; i++) { 158 switch (ch[i]) { 159 case '\\': 160 handleTrace("\\\\"); 161 break; 162 case '"': 163 handleTrace("\\\""); 164 break; 165 case '\n': 166 break; 168 case '\r': 169 handleTrace ("\\r"); 170 break; 171 case '\t': 172 handleTrace ("\\t"); 173 break; 174 default: 175 buffer.append (ch[i]); 176 break; 177 } 178 } 179 180 tmp = buffer.toString().trim (); 183 184 if (!tmp.equals ("")) { 185 handleTrace("JtXMLMsgReader.characters:" + 186 buffer); 187 } 188 189 } 190 191 192 public void endPrefixMapping(String prefix) 193 throws SAXException { 194 195 } 196 197 public void ignorableWhitespace(char[] ch, 198 int start, 199 int length) 200 throws SAXException { 201 202 } 203 204 public void processingInstruction(String target, 205 String data) 206 throws SAXException { 207 208 } 209 210 public void setDocumentLocator(Locator locator) { 211 212 } 213 214 public void skippedEntity(java.lang.String name) 215 throws SAXException { 216 217 } 218 219 public void startPrefixMapping(String prefix, 220 String uri) 221 throws SAXException 222 { 223 224 } 225 226 public void realize () { 228 229 if (reader != null) 230 return; 231 232 try{ 233 reader = XMLReaderFactory.createXMLReader (parserName); 234 235 reader.setContentHandler (this); 236 } catch (Exception ex) { 238 239 handleException (ex); 240 } 241 242 } 243 244 private Object parse () { 245 246 ByteArrayInputStream bstream; 247 JtMessage msg = new JtMessage ("JtCONVERT_OBJECT_TO_XML"); 248 249 if (reader == null) 250 realize (); 251 252 if (uri == null && string == null) { 253 handleError ("JtXMLMsgReader.parse: both uri and string are null"); 254 return null; 255 } 256 257 if (uri != null && string != null) { 258 handleError ("JtXMLMsgReader.parse: set uri or string (only one)"); 259 return null; 260 } 261 262 try { 263 if (uri != null) 264 reader.parse (uri); 265 else { 266 bstream = new ByteArrayInputStream (string.getBytes()); 267 reader.parse (new InputSource ((InputStream) bstream)); 268 269 } 270 } catch (Exception ex) { 271 handleException (ex); 272 } 273 274 if (xmlHelper == null) 275 xmlHelper = new JtXMLHelper (); 276 277 msg.setMsgContent (output); 278 return (sendMessage (xmlHelper, msg)); 279 } 282 283 285 public Object processMessage (Object event) { 286 287 String msgid = null; 288 JtMessage e = (JtMessage) event; 289 Object content; 290 291 if (e == null) 292 return null; 293 294 msgid = (String ) e.getMsgId (); 295 296 if (msgid == null) 297 return null; 298 299 301 replyTo = e.getMsgReplyTo (); 302 303 if (msgid.equals ("JtPARSE")) { 304 return (parse ()); 305 } 306 307 if (msgid.equals ("JtREMOVE")) { 308 return (null); 309 } 310 311 handleError ("JtXMLMsgReader.processMessage: invalid message id:" + msgid); 312 return (null); 313 314 } 315 316 317 319 public static void main(String [] args) { 320 321 JtObject main = new JtObject (); 322 JtMessage msg, msg1; 323 Integer count; 324 String str; 325 326 main.setObjTrace (1); 327 329 330 if (args.length < 1) { 331 System.err.println ("Usage: java JtXMLMsgReader uri"); 332 System.exit (1); 333 } 334 335 337 main.createObject ("Jt.xml.JtXMLMsgReader", "reader"); 338 339 msg = (JtMessage) main.createObject ("Jt.JtMessage", "message"); 340 main.setValue ("message", "msgId", "JtPARSE"); 341 main.setValue ("reader", "uri", args[0]); 342 343 main.sendMessage ("reader", "message"); 344 345 main.createObject ("Jt.JtFile", "file"); 346 main.setValue ("file", "name", args[0]); 347 main.setValue ("message", "msgId", "JtCONVERT_TO_STRING"); 348 349 str = (String ) main.sendMessage ("file", "message"); 350 351 main.setValue ("message", "msgId", "JtPARSE"); 352 main.setValue ("reader", "uri", null); 353 main.setValue ("reader", "string", str); 354 355 main.sendMessage ("reader", "message"); 356 357 358 } 359 360 } 361 362 363 | Popular Tags |