KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > xml > JtXMLMsgReader


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 /**
14   * Read Jt messages from a XML file. This class is being deprecated.
15   */

16
17 public class JtXMLMsgReader extends JtObject
18     implements ContentHandler {
19   private XMLReader reader = null;
20   protected static final String JavaDoc DEFAULT_PARSER_NAME = "org.apache.xerces.parsers.SAXParser";
21   private String JavaDoc uri;
22   private String JavaDoc string;
23   private JtMessage msg = null;
24   private String JavaDoc elemName = null;
25   //String elemValue = null;
26
Object JavaDoc replyTo = null;
27   StringBuffer JavaDoc buffer = null;
28   Object JavaDoc output = null;
29   JtXMLHelper xmlHelper = null;
30   private String JavaDoc parserName = DEFAULT_PARSER_NAME;
31   
32
33   public JtXMLMsgReader() {
34   }
35
36
37  /**
38   * Specifies the parser.
39   * @param parserName parse
40   */

41
42   void setParserName (String JavaDoc parserName) {
43     this.parserName = parserName;
44   
45   }
46
47
48  /**
49    * Returns the parser.
50    */

51
52   String JavaDoc getParserName () {
53     return (parserName);
54   }
55
56
57   // URI
58

59   public void setUri (String JavaDoc uri) {
60      this.uri = uri;
61   }
62
63   public String JavaDoc getUri () {
64      return (uri);
65   }
66
67   // Use a String (XML format) instead of a URI
68

69   public void setString (String JavaDoc string) {
70      this.string = string; // void operation
71
}
72
73   public String JavaDoc getString () {
74      return (string);
75   }
76
77   /** Start element (SAX API). */
78   public void startElement(String JavaDoc uri, String JavaDoc local, String JavaDoc 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       //elemValue = null;
91
buffer = null;
92
93     }else
94       handleTrace ("Start element: {" + uri + "}" + local);
95     
96   }
97
98   Object JavaDoc sendReply (JtMessage msg ) {
99     if (msg != null && replyTo != null )
100       return (sendMessage (replyTo, msg));
101     return (null);
102   }
103
104   public void endElement(String JavaDoc uri,
105                         String JavaDoc name,
106                         String JavaDoc qName)
107                 throws SAXException {
108
109
110     
111 // elemName = null;
112
// elemValue = null;
113
// buffer = null;
114

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   /** Start document. */
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 JavaDoc tmp;
152
153     //handleTrace ("Characters: \"");
154
if (buffer == null)
155       buffer = new StringBuffer JavaDoc ();
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       //handleTrace("\\n");
167
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     //handleTrace("\"\n");
181
//elemValue = buffer.toString ();
182
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 JavaDoc 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 JavaDoc target,
205                                     String JavaDoc data)
206                            throws SAXException {
207
208   }
209
210   public void setDocumentLocator(Locator locator) {
211
212   }
213
214   public void skippedEntity(java.lang.String JavaDoc name)
215                    throws SAXException {
216
217   }
218
219   public void startPrefixMapping(String JavaDoc prefix,
220                                  String JavaDoc uri)
221                         throws SAXException
222   {
223
224   }
225
226   // realize
227
public void realize () {
228
229      if (reader != null)
230        return;
231
232      try{
233        reader = XMLReaderFactory.createXMLReader (parserName);
234
235        reader.setContentHandler (this);
236        //reader.setErrorHandler (this);
237
} catch (Exception JavaDoc ex) {
238
239        handleException (ex);
240      }
241
242   }
243
244   private Object JavaDoc 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 JavaDoc 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     //System.out.println ("output:" + output);
280
//return output;
281
}
282
283   // Process object messages
284

285   public Object JavaDoc processMessage (Object JavaDoc event) {
286
287    String JavaDoc msgid = null;
288    JtMessage e = (JtMessage) event;
289    Object JavaDoc content;
290
291      if (e == null)
292     return null;
293
294      msgid = (String JavaDoc) e.getMsgId ();
295
296      if (msgid == null)
297     return null;
298
299 // content = e.getMsgContent();
300

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   // Test program
318

319   public static void main(String JavaDoc[] args) {
320
321     JtObject main = new JtObject ();
322     JtMessage msg, msg1;
323     Integer JavaDoc count;
324     String JavaDoc str;
325
326     main.setObjTrace (1);
327     //main.setLogFile ("log.txt");
328

329
330     if (args.length < 1) {
331       System.err.println ("Usage: java JtXMLMsgReader uri");
332       System.exit (1);
333     }
334
335     // Create message reader
336

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 JavaDoc) 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