1 38 39 40 package com.sun.xml.fastinfoset.tools; 41 42 import java.io.IOException ; 43 import java.io.OutputStream ; 44 import java.io.OutputStreamWriter ; 45 import java.io.Writer ; 46 import java.util.ArrayList ; 47 import java.util.List ; 48 import java.util.Stack ; 49 50 import org.xml.sax.Attributes ; 51 import org.xml.sax.SAXException ; 52 import org.xml.sax.ext.LexicalHandler ; 53 import org.xml.sax.helpers.DefaultHandler ; 54 import com.sun.xml.fastinfoset.CommonResourceBundle; 55 56 public class SAXEventSerializer extends DefaultHandler 57 implements LexicalHandler { 58 59 private Writer _writer; 60 private boolean _charactersAreCDATA; 61 private StringBuffer _characters; 62 63 private Stack _namespaceStack = new Stack (); 64 protected List _namespaceAttributes; 65 66 public SAXEventSerializer(OutputStream s) throws IOException { 67 _writer = new OutputStreamWriter (s); 68 _charactersAreCDATA = false; 69 } 70 71 73 public void startDocument() throws SAXException { 74 try { 75 _writer.write("<sax xmlns=\"http://www.sun.com/xml/sax-events\">\n"); 76 _writer.write("<startDocument/>\n"); 77 _writer.flush(); 78 } 79 catch (IOException e) { 80 throw new SAXException (e); 81 } 82 } 83 84 public void endDocument() throws SAXException { 85 try { 86 _writer.write("<endDocument/>\n"); 87 _writer.write("</sax>"); 88 _writer.flush(); 89 _writer.close(); 90 } 91 catch (IOException e) { 92 throw new SAXException (e); 93 } 94 } 95 96 97 public void startPrefixMapping(String prefix, String uri) 98 throws SAXException 99 { 100 if (_namespaceAttributes == null) { 101 _namespaceAttributes = new ArrayList (); 102 } 103 104 String qName = (prefix == "") ? "xmlns" : "xmlns" + prefix; 105 AttributeValueHolder attribute = new AttributeValueHolder( 106 qName, 107 prefix, 108 uri, 109 null, 110 null); 111 _namespaceAttributes.add(attribute); 112 } 113 114 public void endPrefixMapping(String prefix) 115 throws SAXException 116 { 117 129 } 130 131 public void startElement(String uri, String localName, 132 String qName, Attributes attributes) 133 throws SAXException 134 { 135 try { 136 outputCharacters(); 137 138 if (_namespaceAttributes != null) { 139 140 AttributeValueHolder[] attrsHolder = new AttributeValueHolder[0]; 141 attrsHolder = (AttributeValueHolder[])_namespaceAttributes.toArray(attrsHolder); 142 143 quicksort(attrsHolder, 0, attrsHolder.length - 1); 145 146 for (int i = 0; i < attrsHolder.length; i++) { 147 _writer.write("<startPrefixMapping prefix=\"" + 148 attrsHolder[i].localName + "\" uri=\"" + attrsHolder[i].uri + "\"/>\n"); 149 _writer.flush(); 150 } 151 152 _namespaceStack.push(attrsHolder); 153 _namespaceAttributes = null; 154 } else { 155 _namespaceStack.push(null); 156 } 157 158 AttributeValueHolder[] attrsHolder = 159 new AttributeValueHolder[attributes.getLength()]; 160 for (int i = 0; i < attributes.getLength(); i++) { 161 attrsHolder[i] = new AttributeValueHolder( 162 attributes.getQName(i), 163 attributes.getLocalName(i), 164 attributes.getURI(i), 165 attributes.getType(i), 166 attributes.getValue(i)); 167 } 168 169 quicksort(attrsHolder, 0, attrsHolder.length - 1); 171 172 int attributeCount = 0; 173 for (int i = 0; i < attrsHolder.length; i++) { 174 if (attrsHolder[i].uri.equals("http://www.w3.org/2000/xmlns/")) { 175 continue; 177 } 178 attributeCount++; 179 } 180 181 if (attributeCount == 0) { 182 _writer.write("<startElement uri=\"" + uri 183 + "\" localName=\"" + localName + "\" qName=\"" 184 + qName + "\"/>\n"); 185 return; 186 } 187 188 _writer.write("<startElement uri=\"" + uri 189 + "\" localName=\"" + localName + "\" qName=\"" 190 + qName + "\">\n"); 191 192 for (int i = 0; i < attrsHolder.length; i++) { 194 if (attrsHolder[i].uri.equals("http://www.w3.org/2000/xmlns/")) { 195 continue; 197 } 198 _writer.write( 199 " <attribute qName=\"" + attrsHolder[i].qName + 200 "\" localName=\"" + attrsHolder[i].localName + 201 "\" uri=\"" + attrsHolder[i].uri + 202 "\" value=\"" + attrsHolder[i].value + 204 "\"/>\n"); 205 } 206 207 _writer.write("</startElement>\n"); 208 _writer.flush(); 209 } 210 catch (IOException e) { 211 throw new SAXException (e); 212 } 213 } 214 215 public void endElement(String uri, String localName, String qName) 216 throws SAXException 217 { 218 try { 219 outputCharacters(); 220 221 _writer.write("<endElement uri=\"" + uri 222 + "\" localName=\"" + localName + "\" qName=\"" 223 + qName + "\"/>\n"); 224 _writer.flush(); 225 226 AttributeValueHolder[] attrsHolder = (AttributeValueHolder[])_namespaceStack.pop(); 229 if (attrsHolder != null) { 230 for (int i = 0; i < attrsHolder.length; i++) { 231 _writer.write("<endPrefixMapping prefix=\"" + 232 attrsHolder[i].localName + "\"/>\n"); 233 _writer.flush(); 234 } 235 } 236 237 } 238 catch (IOException e) { 239 throw new SAXException (e); 240 } 241 } 242 243 public void characters(char[] ch, int start, int length) 244 throws SAXException 245 { 246 if (length == 0) { 247 return; 248 } 249 250 if (_characters == null) { 251 _characters = new StringBuffer (); 252 } 253 254 _characters.append(ch, start, length); 256 257 270 } 271 272 private void outputCharacters() throws SAXException { 273 if (_characters == null) { 274 return; 275 } 276 277 try { 278 _writer.write("<characters>" + 279 (_charactersAreCDATA ? "<![CDATA[" : "") + 280 _characters + 281 (_charactersAreCDATA ? "]]>" : "") + 282 "</characters>\n"); 283 _writer.flush(); 284 285 _characters = null; 286 } catch (IOException e) { 287 throw new SAXException (e); 288 } 289 } 290 291 public void ignorableWhitespace(char[] ch, int start, int length) 292 throws SAXException 293 { 294 characters(ch, start, length); 296 } 297 298 public void processingInstruction(String target, String data) 299 throws SAXException 300 { 301 try { 302 outputCharacters(); 303 304 _writer.write("<processingInstruction target=\"" + target 305 + "\" data=\"" + data + "\"/>\n"); 306 _writer.flush(); 307 } 308 catch (IOException e) { 309 throw new SAXException (e); 310 } 311 } 312 313 315 public void startDTD(String name, String publicId, String systemId) 316 throws SAXException { 317 } 319 320 public void endDTD() 321 throws SAXException { 322 } 324 325 public void startEntity(String name) 326 throws SAXException { 327 } 329 330 public void endEntity(String name) 331 throws SAXException { 332 } 334 335 public void startCDATA() 336 throws SAXException { 337 _charactersAreCDATA = true; 338 } 339 340 public void endCDATA() 341 throws SAXException { 342 _charactersAreCDATA = false; 343 } 344 345 public void comment(char[] ch, int start, int length) 346 throws SAXException 347 { 348 try { 349 outputCharacters(); 350 351 _writer.write("<comment>" + 352 new String (ch, start, length) + 353 "</comment>\n"); 354 _writer.flush(); 355 } 356 catch (IOException e) { 357 throw new SAXException (e); 358 } 359 } 360 361 363 private void quicksort(AttributeValueHolder[] attrs, int p, int r) { 364 while (p < r) { 365 final int q = partition(attrs, p, r); 366 quicksort(attrs, p, q); 367 p = q + 1; 368 } 369 } 370 371 private int partition(AttributeValueHolder[] attrs, int p, int r) { 372 AttributeValueHolder x = attrs[(p + r) >>> 1]; 373 int i = p - 1; 374 int j = r + 1; 375 while (true) { 376 while (x.compareTo(attrs[--j]) < 0); 377 while (x.compareTo(attrs[++i]) > 0); 378 if (i < j) { 379 final AttributeValueHolder t = attrs[i]; 380 attrs[i] = attrs[j]; 381 attrs[j] = t; 382 } 383 else { 384 return j; 385 } 386 } 387 } 388 389 public static class AttributeValueHolder implements Comparable { 390 public final String qName; 391 public final String localName; 392 public final String uri; 393 public final String type; 394 public final String value; 395 396 public AttributeValueHolder(String qName, 397 String localName, 398 String uri, 399 String type, 400 String value) 401 { 402 this.qName = qName; 403 this.localName = localName; 404 this.uri = uri; 405 this.type = type; 406 this.value = value; 407 } 408 409 public int compareTo(Object o) { 410 try { 411 return qName.compareTo(((AttributeValueHolder) o).qName); 412 } 413 catch (Exception e) { 414 throw new RuntimeException (CommonResourceBundle.getInstance().getString("message.AttributeValueHolderExpected")); 415 } 416 } 417 } 418 419 } 420 421 | Popular Tags |