1 16 package org.apache.axis.message; 17 18 import org.apache.axis.encoding.DeserializationContext; 19 import org.xml.sax.ContentHandler ; 20 import org.xml.sax.SAXException ; 21 import org.xml.sax.ext.LexicalHandler ; 22 23 27 public class SAX2EventRecorder { 28 29 private static final Integer Z = new Integer (0); 30 31 private static final Integer STATE_SET_DOCUMENT_LOCATOR = new Integer (0); 32 private static final Integer STATE_START_DOCUMENT = new Integer (1); 33 private static final Integer STATE_END_DOCUMENT = new Integer (2); 34 private static final Integer STATE_START_PREFIX_MAPPING = new Integer (3); 35 private static final Integer STATE_END_PREFIX_MAPPING = new Integer (4); 36 private static final Integer STATE_START_ELEMENT = new Integer (5); 37 private static final Integer STATE_END_ELEMENT = new Integer (6); 38 private static final Integer STATE_CHARACTERS = new Integer (7); 39 private static final Integer STATE_IGNORABLE_WHITESPACE = new Integer (8); 40 private static final Integer STATE_PROCESSING_INSTRUCTION = new Integer (9); 41 private static final Integer STATE_SKIPPED_ENTITY = new Integer (10); 42 43 private static final Integer STATE_NEWELEMENT = new Integer (11); 46 47 private static final Integer STATE_START_DTD = new Integer (12); 49 private static final Integer STATE_END_DTD = new Integer (13); 50 private static final Integer STATE_START_ENTITY = new Integer (14); 51 private static final Integer STATE_END_ENTITY = new Integer (15); 52 private static final Integer STATE_START_CDATA = new Integer (16); 53 private static final Integer STATE_END_CDATA = new Integer (17); 54 private static final Integer STATE_COMMENT = new Integer (18); 55 56 org.xml.sax.Locator 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 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 p1, String p2) { 79 return events.add(STATE_START_PREFIX_MAPPING, p1, p2, Z,Z); 80 } 81 public int endPrefixMapping(String p1) { 82 return events.add(STATE_END_PREFIX_MAPPING, p1,Z,Z,Z); 83 } 84 public int startElement(String p1, String p2, String p3, org.xml.sax.Attributes p4) { 85 return events.add(STATE_START_ELEMENT, p1, p2, p3, p4); 86 } 87 public int endElement(String p1, String p2, String 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 )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 )clone(p1, p2, p3), 98 Z, Z, Z); 99 } 100 public int processingInstruction(String p1, String p2) { 101 return events.add(STATE_PROCESSING_INSTRUCTION, p1, p2, Z,Z); 102 } 103 public int skippedEntity(String p1) { 104 return events.add(STATE_SKIPPED_ENTITY, p1, Z,Z,Z); 105 } 106 107 public void startDTD(java.lang.String name, 108 java.lang.String publicId, 109 java.lang.String 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 name) { 116 events.add(STATE_START_ENTITY, name, Z, Z, Z); 117 } 118 public void endEntity(java.lang.String 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 )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 handler) throws SAXException { 140 if (events.getLength() > 0) { 141 replay(0, events.getLength() - 1, handler); 142 } 143 } 144 145 public void replay(int start, int stop, ContentHandler handler) throws SAXException { 146 if ((start == 0) && (stop == -1)) { 148 replay(handler); 149 return; 150 } 151 152 if (stop + 1 > events.getLength() || 153 stop < start) { 154 return; } 156 157 LexicalHandler lexicalHandler = null; 158 if (handler instanceof LexicalHandler ) { 159 lexicalHandler = (LexicalHandler ) handler; 160 } 161 162 for (int n = start; n <= stop; n++) { 163 Object event = events.get(n,0); 164 if (event == STATE_START_ELEMENT) { 165 handler.startElement((String )events.get(n,1), 166 (String )events.get(n,2), 167 (String )events.get(n,3), 168 (org.xml.sax.Attributes )events.get(n,4)); 169 170 } else if (event == STATE_END_ELEMENT) { 171 handler.endElement((String )events.get(n,1), 172 (String )events.get(n,2), 173 (String )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 )events.get(n,1), 185 (String )events.get(n,2)); 186 187 } else if (event == STATE_SKIPPED_ENTITY) { 188 handler.skippedEntity((String )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 )events.get(n, 1), 201 (String )events.get(n, 2)); 202 203 } else if (event == STATE_END_PREFIX_MAPPING) { 204 handler.endPrefixMapping((String )events.get(n, 1)); 205 206 } else if (event == STATE_START_DTD && lexicalHandler != null) { 207 lexicalHandler.startDTD((String )events.get(n,1), 208 (String )events.get(n,2), 209 (String )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 )events.get(n,1)); 215 216 } else if (event == STATE_END_ENTITY && lexicalHandler != null) { 217 lexicalHandler.endEntity((String )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 class objArrayVector { 248 private int RECORD_SIZE = 5; 249 private int currentSize = 0; 250 private Object [] objarray = new Object [50 * RECORD_SIZE]; 252 public int add(Object p1, Object p2, Object p3, Object p4, Object p5) { 253 if (currentSize == objarray.length) { 254 Object [] newarray = new Object [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 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 277 } 278 | Popular Tags |