KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.axis.message;
17
18 import org.apache.axis.encoding.DeserializationContext;
19 import org.xml.sax.ContentHandler JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21 import org.xml.sax.ext.LexicalHandler JavaDoc;
22
23 /**
24  * This class records SAX2 Events and allows
25  * the events to be replayed by start and stop index
26  */

27 public class SAX2EventRecorder {
28     
29     private static final Integer JavaDoc Z = new Integer JavaDoc(0);
30
31     private static final Integer JavaDoc STATE_SET_DOCUMENT_LOCATOR = new Integer JavaDoc(0);
32     private static final Integer JavaDoc STATE_START_DOCUMENT = new Integer JavaDoc(1);
33     private static final Integer JavaDoc STATE_END_DOCUMENT = new Integer JavaDoc(2);
34     private static final Integer JavaDoc STATE_START_PREFIX_MAPPING = new Integer JavaDoc(3);
35     private static final Integer JavaDoc STATE_END_PREFIX_MAPPING = new Integer JavaDoc(4);
36     private static final Integer JavaDoc STATE_START_ELEMENT = new Integer JavaDoc(5);
37     private static final Integer JavaDoc STATE_END_ELEMENT = new Integer JavaDoc(6);
38     private static final Integer JavaDoc STATE_CHARACTERS = new Integer JavaDoc(7);
39     private static final Integer JavaDoc STATE_IGNORABLE_WHITESPACE = new Integer JavaDoc(8);
40     private static final Integer JavaDoc STATE_PROCESSING_INSTRUCTION = new Integer JavaDoc(9);
41     private static final Integer JavaDoc STATE_SKIPPED_ENTITY = new Integer JavaDoc(10);
42     
43     // This is a "custom" event which tells DeserializationContexts
44
// that the current element is moving down the stack...
45
private static final Integer JavaDoc STATE_NEWELEMENT = new Integer JavaDoc(11);
46
47     // Lexical handler events...
48
private static final Integer JavaDoc STATE_START_DTD = new Integer JavaDoc(12);
49     private static final Integer JavaDoc STATE_END_DTD = new Integer JavaDoc(13);
50     private static final Integer JavaDoc STATE_START_ENTITY = new Integer JavaDoc(14);
51     private static final Integer JavaDoc STATE_END_ENTITY = new Integer JavaDoc(15);
52     private static final Integer JavaDoc STATE_START_CDATA = new Integer JavaDoc(16);
53     private static final Integer JavaDoc STATE_END_CDATA = new Integer JavaDoc(17);
54     private static final Integer JavaDoc STATE_COMMENT = new Integer JavaDoc(18);
55     
56     org.xml.sax.Locator JavaDoc locator;
57     objArrayVector events = new objArrayVector();
58     
59     public void clear() {
60         locator = null;
61         events = new objArrayVector();
62     }
63     public int getLength()
64     {
65         return events.getLength();
66     }
67     
68     public int setDocumentLocator(org.xml.sax.Locator JavaDoc p1) {
69         locator = p1;
70         return events.add(STATE_SET_DOCUMENT_LOCATOR, Z,Z,Z,Z);
71     }
72     public int startDocument() {
73         return events.add(STATE_START_DOCUMENT, Z,Z,Z,Z);
74     }
75     public int endDocument() {
76         return events.add(STATE_END_DOCUMENT, Z,Z,Z,Z);
77     }
78     public int startPrefixMapping(String JavaDoc p1, String JavaDoc p2) {
79         return events.add(STATE_START_PREFIX_MAPPING, p1, p2, Z,Z);
80     }
81     public int endPrefixMapping(String JavaDoc p1) {
82         return events.add(STATE_END_PREFIX_MAPPING, p1,Z,Z,Z);
83     }
84     public int startElement(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3, org.xml.sax.Attributes JavaDoc p4) {
85         return events.add(STATE_START_ELEMENT, p1, p2, p3, p4);
86     }
87     public int endElement(String JavaDoc p1, String JavaDoc p2, String JavaDoc p3) {
88         return events.add(STATE_END_ELEMENT, p1, p2, p3, Z);
89     }
90     public int characters(char[] p1, int p2, int p3) {
91         return events.add(STATE_CHARACTERS,
92                           (Object JavaDoc)clone(p1, p2, p3),
93                           Z, Z, Z);
94     }
95     public int ignorableWhitespace(char[] p1, int p2, int p3) {
96         return events.add(STATE_IGNORABLE_WHITESPACE,
97                           (Object JavaDoc)clone(p1, p2, p3),
98                           Z, Z, Z);
99     }
100     public int processingInstruction(String JavaDoc p1, String JavaDoc p2) {
101         return events.add(STATE_PROCESSING_INSTRUCTION, p1, p2, Z,Z);
102     }
103     public int skippedEntity(String JavaDoc p1) {
104         return events.add(STATE_SKIPPED_ENTITY, p1, Z,Z,Z);
105     }
106     
107     public void startDTD(java.lang.String JavaDoc name,
108                      java.lang.String JavaDoc publicId,
109                      java.lang.String JavaDoc systemId) {
110         events.add(STATE_START_DTD, name, publicId, systemId, Z);
111     }
112     public void endDTD() {
113         events.add(STATE_END_DTD, Z, Z, Z, Z);
114     }
115     public void startEntity(java.lang.String JavaDoc name) {
116         events.add(STATE_START_ENTITY, name, Z, Z, Z);
117     }
118     public void endEntity(java.lang.String JavaDoc name) {
119         events.add(STATE_END_ENTITY, name, Z, Z, Z);
120     }
121     public void startCDATA() {
122         events.add(STATE_START_CDATA, Z, Z, Z, Z);
123     }
124     public void endCDATA() {
125         events.add(STATE_END_CDATA, Z, Z, Z, Z);
126     }
127     public void comment(char[] ch,
128                     int start,
129                     int length) {
130         events.add(STATE_COMMENT,
131                    (Object JavaDoc)clone(ch, start, length),
132                    Z, Z, Z);
133     }
134     
135     public int newElement(MessageElement elem) {
136         return events.add(STATE_NEWELEMENT, elem, Z,Z,Z);
137     }
138     
139     public void replay(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
140         if (events.getLength() > 0) {
141             replay(0, events.getLength() - 1, handler);
142         }
143     }
144     
145     public void replay(int start, int stop, ContentHandler JavaDoc handler) throws SAXException JavaDoc {
146         // Special case : play the whole thing for [0, -1]
147
if ((start == 0) && (stop == -1)) {
148             replay(handler);
149             return;
150         }
151         
152         if (stop + 1 > events.getLength() ||
153             stop < start) {
154             return; // should throw an error here
155
}
156         
157         LexicalHandler JavaDoc lexicalHandler = null;
158         if (handler instanceof LexicalHandler JavaDoc) {
159             lexicalHandler = (LexicalHandler JavaDoc) handler;
160         }
161         
162         for (int n = start; n <= stop; n++) {
163             Object JavaDoc event = events.get(n,0);
164             if (event == STATE_START_ELEMENT) {
165                 handler.startElement((String JavaDoc)events.get(n,1),
166                                      (String JavaDoc)events.get(n,2),
167                                      (String JavaDoc)events.get(n,3),
168                                      (org.xml.sax.Attributes JavaDoc)events.get(n,4));
169                 
170             } else if (event == STATE_END_ELEMENT) {
171                 handler.endElement((String JavaDoc)events.get(n,1),
172                                    (String JavaDoc)events.get(n,2),
173                                    (String JavaDoc)events.get(n,3));
174                 
175             } else if (event == STATE_CHARACTERS) {
176                 char[] data = (char[])events.get(n,1);
177                 handler.characters(data, 0, data.length);
178                 
179             } else if (event == STATE_IGNORABLE_WHITESPACE) {
180                 char[] data = (char[])events.get(n,1);
181                 handler.ignorableWhitespace(data, 0, data.length);
182                 
183             } else if (event == STATE_PROCESSING_INSTRUCTION) {
184                 handler.processingInstruction((String JavaDoc)events.get(n,1),
185                                               (String JavaDoc)events.get(n,2));
186                 
187             } else if (event == STATE_SKIPPED_ENTITY) {
188                 handler.skippedEntity((String JavaDoc)events.get(n,1));
189                 
190             } else if (event == STATE_SET_DOCUMENT_LOCATOR) {
191                 handler.setDocumentLocator(locator);
192                 
193             } else if (event == STATE_START_DOCUMENT) {
194                 handler.startDocument();
195                 
196             } else if (event == STATE_END_DOCUMENT) {
197                 handler.endDocument();
198                 
199             } else if (event == STATE_START_PREFIX_MAPPING) {
200                 handler.startPrefixMapping((String JavaDoc)events.get(n, 1),
201                                            (String JavaDoc)events.get(n, 2));
202                 
203             } else if (event == STATE_END_PREFIX_MAPPING) {
204                 handler.endPrefixMapping((String JavaDoc)events.get(n, 1));
205                 
206             } else if (event == STATE_START_DTD && lexicalHandler != null) {
207                 lexicalHandler.startDTD((String JavaDoc)events.get(n,1),
208                                    (String JavaDoc)events.get(n,2),
209                                    (String JavaDoc)events.get(n,3));
210             } else if (event == STATE_END_DTD && lexicalHandler != null) {
211                 lexicalHandler.endDTD();
212             
213             } else if (event == STATE_START_ENTITY && lexicalHandler != null) {
214                 lexicalHandler.startEntity((String JavaDoc)events.get(n,1));
215             
216             } else if (event == STATE_END_ENTITY && lexicalHandler != null) {
217                 lexicalHandler.endEntity((String JavaDoc)events.get(n,1));
218             
219             } else if (event == STATE_START_CDATA && lexicalHandler != null) {
220                 lexicalHandler.startCDATA();
221             
222             } else if (event == STATE_END_CDATA && lexicalHandler != null) {
223                 lexicalHandler.endCDATA();
224             
225             } else if (event == STATE_COMMENT && lexicalHandler != null) {
226                 char[] data = (char[])events.get(n,1);
227                 lexicalHandler.comment(data, 0, data.length);
228             
229             } else if (event == STATE_NEWELEMENT) {
230                 if (handler instanceof DeserializationContext) {
231                     DeserializationContext context =
232                               (DeserializationContext)handler;
233                     context.setCurElement(
234                               (MessageElement)(events.get(n,1)));
235                 }
236             }
237         }
238     }
239
240     private static char[] clone(char[] in, int off, int len) {
241         char[] out = new char[len];
242         System.arraycopy(in, off, out, 0, len);
243         return out;
244     }
245     
246 /////////////////////////////////////////////
247
class objArrayVector {
248         private int RECORD_SIZE = 5;
249         private int currentSize = 0;
250         private Object JavaDoc[] objarray = new Object JavaDoc[50 * RECORD_SIZE]; // default to 50 5 field records
251

252         public int add(Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3, Object JavaDoc p4, Object JavaDoc p5) {
253             if (currentSize == objarray.length) {
254                 Object JavaDoc[] newarray = new Object JavaDoc[currentSize * 2];
255                 System.arraycopy(objarray, 0, newarray, 0, currentSize);
256                 objarray = newarray;
257             }
258             int pos = currentSize / RECORD_SIZE;
259             objarray[currentSize++] = p1;
260             objarray[currentSize++] = p2;
261             objarray[currentSize++] = p3;
262             objarray[currentSize++] = p4;
263             objarray[currentSize++] = p5;
264             return pos;
265         }
266
267         public Object JavaDoc get(int pos, int fld) {
268             return objarray[(pos * RECORD_SIZE) + fld];
269         }
270     
271         public int getLength() {
272             return (currentSize / RECORD_SIZE);
273         }
274     }
275 /////////////////////////////////////////////
276

277 }
278
Popular Tags