KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > message > SAX2EventRecorder


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.message;
56
57 import org.jboss.axis.encoding.DeserializationContext;
58 import org.xml.sax.ContentHandler JavaDoc;
59 import org.xml.sax.SAXException JavaDoc;
60 import org.xml.sax.ext.LexicalHandler JavaDoc;
61
62 /**
63  * This class records SAX2 Events and allows
64  * the events to be replayed by start and stop index
65  */

66 public class SAX2EventRecorder
67 {
68
69    private static final Integer JavaDoc Z = new Integer JavaDoc(0);
70
71    private static final Integer JavaDoc STATE_SET_DOCUMENT_LOCATOR = new Integer JavaDoc(0);
72    private static final Integer JavaDoc STATE_START_DOCUMENT = new Integer JavaDoc(1);
73    private static final Integer JavaDoc STATE_END_DOCUMENT = new Integer JavaDoc(2);
74    private static final Integer JavaDoc STATE_START_PREFIX_MAPPING = new Integer JavaDoc(3);
75    private static final Integer JavaDoc STATE_END_PREFIX_MAPPING = new Integer JavaDoc(4);
76    private static final Integer JavaDoc STATE_START_ELEMENT = new Integer JavaDoc(5);
77    private static final Integer JavaDoc STATE_END_ELEMENT = new Integer JavaDoc(6);
78    private static final Integer JavaDoc STATE_CHARACTERS = new Integer JavaDoc(7);
79    private static final Integer JavaDoc STATE_IGNORABLE_WHITESPACE = new Integer JavaDoc(8);
80    private static final Integer JavaDoc STATE_PROCESSING_INSTRUCTION = new Integer JavaDoc(9);
81    private static final Integer JavaDoc STATE_SKIPPED_ENTITY = new Integer JavaDoc(10);
82
83    // This is a "custom" event which tells DeserializationContexts
84
// that the current element is moving down the stack...
85
private static final Integer JavaDoc STATE_NEWELEMENT = new Integer JavaDoc(11);
86
87    // Lexical handler events...
88
private static final Integer JavaDoc STATE_START_DTD = new Integer JavaDoc(12);
89    private static final Integer JavaDoc STATE_END_DTD = new Integer JavaDoc(13);
90    private static final Integer JavaDoc STATE_START_ENTITY = new Integer JavaDoc(14);
91    private static final Integer JavaDoc STATE_END_ENTITY = new Integer JavaDoc(15);
92    private static final Integer JavaDoc STATE_START_CDATA = new Integer JavaDoc(16);
93    private static final Integer JavaDoc STATE_END_CDATA = new Integer JavaDoc(17);
94    private static final Integer JavaDoc STATE_COMMENT = new Integer JavaDoc(18);
95
96    org.xml.sax.Locator JavaDoc locator;
97    objArrayVector events = new objArrayVector();
98
99    public void clear()
100    {
101       locator = null;
102       events = new objArrayVector();
103    }
104
105    public int getLength()
106    {
107       return events.getLength();
108    }
109
110    public int setDocumentLocator(org.xml.sax.Locator JavaDoc p1)
111    {
112       locator = p1;
113       return events.add(STATE_SET_DOCUMENT_LOCATOR, Z, Z, Z, Z);
114    }
115
116    public int startDocument()
117    {
118       return events.add(STATE_START_DOCUMENT, Z, Z, Z, Z);
119    }
120
121    public int endDocument()
122    {
123       return events.add(STATE_END_DOCUMENT, Z, Z, Z, Z);
124    }
125
126    public int startPrefixMapping(String JavaDoc p1, String JavaDoc p2)
127    {
128       return events.add(STATE_START_PREFIX_MAPPING, p1, p2, Z, Z);
129    }
130
131    public int endPrefixMapping(String JavaDoc p1)
132    {
133       return events.add(STATE_END_PREFIX_MAPPING, p1, Z, Z, Z);
134    }
135
136    public int startElement(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3, org.xml.sax.Attributes JavaDoc p4)
137    {
138       return events.add(STATE_START_ELEMENT, p1, p2, p3, p4);
139    }
140
141    public int endElement(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3)
142    {
143       return events.add(STATE_END_ELEMENT, p1, p2, p3, Z);
144    }
145
146    public int characters(char[] p1, int p2, int p3)
147    {
148       return events.add(STATE_CHARACTERS, new String JavaDoc(p1, p2, p3), Z, Z, Z);
149    }
150
151    public int ignorableWhitespace(char[] p1, int p2, int p3)
152    {
153       return events.add(STATE_IGNORABLE_WHITESPACE, new String JavaDoc(p1, p2, p3), Z, Z, Z);
154    }
155
156    public int processingInstruction(String JavaDoc p1, String JavaDoc p2)
157    {
158       return events.add(STATE_PROCESSING_INSTRUCTION, p1, p2, Z, Z);
159    }
160
161    public int skippedEntity(String JavaDoc p1)
162    {
163       return events.add(STATE_SKIPPED_ENTITY, p1, Z, Z, Z);
164    }
165
166    public void startDTD(java.lang.String JavaDoc name,
167                         java.lang.String JavaDoc publicId,
168                         java.lang.String JavaDoc systemId)
169    {
170       events.add(STATE_START_DTD, name, publicId, systemId, Z);
171    }
172
173    public void endDTD()
174    {
175       events.add(STATE_END_DTD, Z, Z, Z, Z);
176    }
177
178    public void startEntity(java.lang.String JavaDoc name)
179    {
180       events.add(STATE_START_ENTITY, name, Z, Z, Z);
181    }
182
183    public void endEntity(java.lang.String JavaDoc name)
184    {
185       events.add(STATE_END_ENTITY, name, Z, Z, Z);
186    }
187
188    public void startCDATA()
189    {
190       events.add(STATE_START_CDATA, Z, Z, Z, Z);
191    }
192
193    public void endCDATA()
194    {
195       events.add(STATE_END_CDATA, Z, Z, Z, Z);
196    }
197
198    public void comment(char[] ch,
199                        int start,
200                        int length)
201    {
202       events.add(STATE_COMMENT, new String JavaDoc(ch, start, length), Z, Z, Z);
203    }
204
205    public int newElement(SOAPElementAxisImpl elem)
206    {
207       return events.add(STATE_NEWELEMENT, elem, Z, Z, Z);
208    }
209
210    public void replay(ContentHandler JavaDoc handler) throws SAXException JavaDoc
211    {
212       if (events.getLength() > 0)
213       {
214          replay(0, events.getLength() - 1, handler);
215       }
216    }
217
218    public void replay(int start, int stop, ContentHandler JavaDoc handler) throws SAXException JavaDoc
219    {
220       // Special case : play the whole thing for [0, -1]
221
if ((start == 0) && (stop == -1))
222       {
223          replay(handler);
224          return;
225       }
226
227       if (stop + 1 > events.getLength() ||
228               stop < start)
229       {
230          return; // should throw an error here
231
}
232
233       LexicalHandler JavaDoc lexicalHandler = null;
234       if (handler instanceof LexicalHandler JavaDoc)
235       {
236          lexicalHandler = (LexicalHandler JavaDoc)handler;
237       }
238
239       for (int n = start; n <= stop; n++)
240       {
241          Object JavaDoc event = events.get(n, 0);
242          if (event == STATE_START_ELEMENT)
243          {
244             handler.startElement((String JavaDoc)events.get(n, 1),
245                     (String JavaDoc)events.get(n, 2),
246                     (String JavaDoc)events.get(n, 3),
247                     (org.xml.sax.Attributes JavaDoc)events.get(n, 4));
248
249          }
250          else if (event == STATE_END_ELEMENT)
251          {
252             handler.endElement((String JavaDoc)events.get(n, 1),
253                     (String JavaDoc)events.get(n, 2),
254                     (String JavaDoc)events.get(n, 3));
255
256          }
257          else if (event == STATE_CHARACTERS)
258          {
259             char chars[] = ((String JavaDoc)(events.get(n, 1))).toCharArray();
260             handler.characters(chars, 0, chars.length);
261
262          }
263          else if (event == STATE_IGNORABLE_WHITESPACE)
264          {
265             char chars[] = ((String JavaDoc)(events.get(n, 1))).toCharArray();
266             handler.ignorableWhitespace(chars, 0, chars.length);
267
268          }
269          else if (event == STATE_PROCESSING_INSTRUCTION)
270          {
271             handler.processingInstruction((String JavaDoc)events.get(n, 1),
272                     (String JavaDoc)events.get(n, 2));
273
274          }
275          else if (event == STATE_SKIPPED_ENTITY)
276          {
277             handler.skippedEntity((String JavaDoc)events.get(n, 1));
278
279          }
280          else if (event == STATE_SET_DOCUMENT_LOCATOR)
281          {
282             handler.setDocumentLocator(locator);
283
284          }
285          else if (event == STATE_START_DOCUMENT)
286          {
287             handler.startDocument();
288
289          }
290          else if (event == STATE_END_DOCUMENT)
291          {
292             handler.endDocument();
293
294          }
295          else if (event == STATE_START_PREFIX_MAPPING)
296          {
297             handler.startPrefixMapping((String JavaDoc)events.get(n, 1),
298                     (String JavaDoc)events.get(n, 2));
299
300          }
301          else if (event == STATE_END_PREFIX_MAPPING)
302          {
303             handler.endPrefixMapping((String JavaDoc)events.get(n, 1));
304
305          }
306          else if (event == STATE_START_DTD && lexicalHandler != null)
307          {
308             lexicalHandler.startDTD((String JavaDoc)events.get(n, 1),
309                     (String JavaDoc)events.get(n, 2),
310                     (String JavaDoc)events.get(n, 3));
311          }
312          else if (event == STATE_END_DTD && lexicalHandler != null)
313          {
314             lexicalHandler.endDTD();
315
316          }
317          else if (event == STATE_START_ENTITY && lexicalHandler != null)
318          {
319             lexicalHandler.startEntity((String JavaDoc)events.get(n, 1));
320
321          }
322          else if (event == STATE_END_ENTITY && lexicalHandler != null)
323          {
324             lexicalHandler.endEntity((String JavaDoc)events.get(n, 1));
325
326          }
327          else if (event == STATE_START_CDATA && lexicalHandler != null)
328          {
329             lexicalHandler.startCDATA();
330
331          }
332          else if (event == STATE_END_CDATA && lexicalHandler != null)
333          {
334             lexicalHandler.endCDATA();
335
336          }
337          else if (event == STATE_COMMENT && lexicalHandler != null)
338          {
339             char chars[] = ((String JavaDoc)(events.get(n, 1))).toCharArray();
340             lexicalHandler.comment(chars, 0, chars.length);
341
342          }
343          else if (event == STATE_NEWELEMENT)
344          {
345             if (handler instanceof DeserializationContext)
346             {
347                DeserializationContext context =
348                        (DeserializationContext)handler;
349                context.setCurElement((SOAPElementAxisImpl)(events.get(n, 1)));
350             }
351          }
352       }
353    }
354     
355 /////////////////////////////////////////////
356
class objArrayVector
357    {
358       private int RECORD_SIZE = 5;
359       private int currentSize = 0;
360       private Object JavaDoc[] objarray = new Object JavaDoc[50 * RECORD_SIZE]; // default to 50 5 field records
361

362       public int add(Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3, Object JavaDoc p4, Object JavaDoc p5)
363       {
364          if (currentSize == objarray.length)
365          {
366             Object JavaDoc[] newarray = new Object JavaDoc[currentSize * 2];
367             System.arraycopy(objarray, 0, newarray, 0, currentSize);
368             objarray = newarray;
369          }
370          int pos = currentSize / RECORD_SIZE;
371          objarray[currentSize++] = p1;
372          objarray[currentSize++] = p2;
373          objarray[currentSize++] = p3;
374          objarray[currentSize++] = p4;
375          objarray[currentSize++] = p5;
376          return pos;
377       }
378
379       public Object JavaDoc get(int pos, int fld)
380       {
381          return objarray[(pos * RECORD_SIZE) + fld];
382       }
383
384       public int getLength()
385       {
386          return (currentSize / RECORD_SIZE);
387       }
388    }
389 /////////////////////////////////////////////
390
}
391
Popular Tags