1 16 package org.apache.cocoon.forms.transformation; 17 18 import org.apache.cocoon.xml.AbstractXMLPipe; 19 import org.apache.cocoon.xml.SaxBuffer; 20 21 import org.xml.sax.Attributes ; 22 import org.xml.sax.Locator ; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.helpers.LocatorImpl ; 25 26 import java.util.LinkedList ; 27 28 34 public class EffectPipe extends AbstractXMLPipe { 35 36 40 protected interface Handler { 41 public Handler startDocument() 42 throws SAXException ; 43 44 public void endDocument() 45 throws SAXException ; 46 47 public void startPrefixMapping(String prefix, String uri) 48 throws SAXException ; 49 50 public void endPrefixMapping(String prefix) 51 throws SAXException ; 52 53 public Handler startElement(String uri, String loc, String raw, Attributes attrs) 54 throws SAXException ; 55 56 60 public Handler nestedElement(String uri, String loc, String raw, Attributes attrs) 61 throws SAXException ; 62 63 public void endElement(String uri, String loc, String raw) 64 throws SAXException ; 65 66 public Handler characters(char ch[], int start, int length) 67 throws SAXException ; 68 69 public Handler ignorableWhitespace(char ch[], int start, int length) 70 throws SAXException ; 71 72 public Handler processingInstruction(String target, String data) 73 throws SAXException ; 74 75 public Handler skippedEntity(String name) 76 throws SAXException ; 77 78 public Handler startDTD(String name, String publicId, String systemId) 79 throws SAXException ; 80 81 public Handler endDTD() 82 throws SAXException ; 83 84 public Handler startEntity(String name) 85 throws SAXException ; 86 87 public Handler endEntity(String name) 88 throws SAXException ; 89 90 public Handler startCDATA() 91 throws SAXException ; 92 93 public Handler endCDATA() 94 throws SAXException ; 95 96 public Handler comment(char c[], int start, int len) 97 throws SAXException ; 98 } 99 100 103 protected class NullHandler implements Handler { 104 public Handler startDocument() throws SAXException { 105 return this; 106 } 107 108 public void endDocument() throws SAXException { 109 } 110 111 public void startPrefixMapping(String prefix, String uri) throws SAXException { 112 } 113 114 public void endPrefixMapping(String prefix) throws SAXException { 115 } 116 117 public Handler nestedElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 118 return this; 119 } 120 121 public Handler startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 122 return this; 123 } 124 125 public void endElement(String uri, String loc, String raw) throws SAXException { 126 } 127 128 public Handler characters(char ch[], int start, int length) throws SAXException { 129 return this; 130 } 131 132 public Handler ignorableWhitespace(char ch[], int start, int length) throws SAXException { 133 return this; 134 } 135 136 public Handler processingInstruction(String target, String data) throws SAXException { 137 return this; 138 } 139 140 public Handler skippedEntity(String name) throws SAXException { 141 return this; 142 } 143 144 public Handler startDTD(String name, String publicId, String systemId) throws SAXException { 145 return this; 146 } 147 148 public Handler endDTD() throws SAXException { 149 return this; 150 } 151 152 public Handler startEntity(String name) throws SAXException { 153 return this; 154 } 155 156 public Handler endEntity(String name) throws SAXException { 157 return this; 158 } 159 160 public Handler startCDATA() throws SAXException { 161 return this; 162 } 163 164 public Handler endCDATA() throws SAXException { 165 return this; 166 } 167 168 public Handler comment(char c[], int start, int len) throws SAXException { 169 return this; 170 } 171 } 172 173 176 protected class BufferHandler extends NullHandler { 177 public Handler startDocument() throws SAXException { 178 if (buffer != null) buffer.startDocument(); 179 return this; 180 } 181 182 public void setDocumentLocator(Locator paramLocator) { 183 locator = new LocatorImpl (paramLocator); 184 if (buffer != null) buffer.setDocumentLocator(paramLocator); 185 } 186 187 public void endDocument() throws SAXException { 188 if (buffer != null) buffer.endDocument(); 189 } 190 191 public void startPrefixMapping(String prefix, String uri) throws SAXException { 192 if (buffer != null) buffer.startPrefixMapping(prefix, uri); 193 } 194 195 public void endPrefixMapping(String prefix) throws SAXException { 196 if (buffer != null) buffer.endPrefixMapping(prefix); 197 } 198 199 public Handler startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 200 if (buffer != null) buffer.startElement(uri, loc, raw, attrs); 201 return this; 202 } 203 204 public void endElement(String uri, String loc, String raw) throws SAXException { 205 if (buffer != null) buffer.endElement(uri, loc, raw); 206 } 207 208 public Handler characters(char ch[], int start, int length) throws SAXException { 209 if (buffer != null) buffer.characters(ch, start, length); 210 return this; 211 } 212 213 public Handler ignorableWhitespace(char ch[], int start, int length) throws SAXException { 214 if (buffer != null) buffer.ignorableWhitespace(ch, start, length); 215 return this; 216 } 217 218 public Handler processingInstruction(String target, String data) throws SAXException { 219 if (buffer != null) buffer.processingInstruction(target, data); 220 return this; 221 } 222 223 public Handler skippedEntity(String name) throws SAXException { 224 if (buffer != null) buffer.skippedEntity(name); 225 return this; 226 } 227 228 public Handler startDTD(String name, String publicId, String systemId) throws SAXException { 229 if (buffer != null) buffer.startDTD(name, publicId, systemId); 230 return this; 231 } 232 233 public Handler endDTD() throws SAXException { 234 if (buffer != null) buffer.endDTD(); 235 return this; 236 } 237 238 public Handler startEntity(String name) throws SAXException { 239 if (buffer != null) buffer.startEntity(name); 240 return this; 241 } 242 243 public Handler endEntity(String name) throws SAXException { 244 if (buffer != null) buffer.endEntity(name); 245 return this; 246 } 247 248 public Handler startCDATA() throws SAXException { 249 if (buffer != null) buffer.startCDATA(); 250 return this; 251 } 252 253 public Handler endCDATA() throws SAXException { 254 if (buffer != null) buffer.endCDATA(); 255 return this; 256 } 257 258 public Handler comment(char c[], int start, int len) throws SAXException { 259 if (buffer != null) buffer.comment(c, start, len); 260 return this; 261 } 262 } 263 264 267 protected class CopyHandler extends NullHandler { 268 public Handler startDocument() throws SAXException { 269 contentHandler.startDocument(); 270 return this; 271 } 272 273 public void endDocument() throws SAXException { 274 contentHandler.endDocument(); 275 } 276 277 public void startPrefixMapping(String prefix, String uri) throws SAXException { 278 contentHandler.startPrefixMapping(prefix, uri); 279 } 280 281 public void endPrefixMapping(String prefix) throws SAXException { 282 contentHandler.endPrefixMapping(prefix); 283 } 284 285 public Handler startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 286 contentHandler.startElement(uri, loc, raw, attrs); 287 return this; 288 } 289 290 public void endElement(String uri, String loc, String raw) throws SAXException { 291 contentHandler.endElement(uri, loc, raw); 292 } 293 294 public Handler characters(char ch[], int start, int length) throws SAXException { 295 contentHandler.characters(ch, start, length); 296 return this; 297 } 298 299 public Handler ignorableWhitespace(char ch[], int start, int length) throws SAXException { 300 contentHandler.ignorableWhitespace(ch, start, length); 301 return this; 302 } 303 304 public Handler processingInstruction(String target, String data) throws SAXException { 305 contentHandler.processingInstruction(target, data); 306 return this; 307 } 308 309 public Handler skippedEntity(String name) throws SAXException { 310 contentHandler.skippedEntity(name); 311 return this; 312 } 313 314 public Handler startDTD(String name, String publicId, String systemId) throws SAXException { 315 if (lexicalHandler != null) lexicalHandler.startDTD(name, publicId, systemId); 316 return this; 317 } 318 319 public Handler endDTD() throws SAXException { 320 if (lexicalHandler != null) lexicalHandler.endDTD(); 321 return this; 322 } 323 324 public Handler startEntity(String name) throws SAXException { 325 if (lexicalHandler != null) lexicalHandler.startEntity(name); 326 return this; 327 } 328 329 public Handler endEntity(String name) throws SAXException { 330 if (lexicalHandler != null) lexicalHandler.endEntity(name); 331 return this; 332 } 333 334 public Handler startCDATA() throws SAXException { 335 if (lexicalHandler != null) lexicalHandler.startCDATA(); 336 return this; 337 } 338 339 public Handler endCDATA() throws SAXException { 340 if (lexicalHandler != null) lexicalHandler.endCDATA(); 341 return this; 342 } 343 344 public Handler comment(char c[], int start, int len) throws SAXException { 345 if (lexicalHandler != null) lexicalHandler.comment(c, start, len); 346 return this; 347 } 348 } 349 350 353 protected class ErrorHandler extends NullHandler { 354 protected String getName() { 355 return "<unknown>"; 356 } 357 358 public Handler startDocument() throws SAXException { 359 throw new SAXException ("Unexpected startDocument in '" + getName() + "' (" + getLocation() + ")"); 360 } 361 362 public void endDocument() throws SAXException { 363 throw new SAXException ("Unexpected endDocument in '" + getName() + "' (" + getLocation() + ")"); 364 } 365 366 public void startPrefixMapping(String prefix, String uri) throws SAXException { 367 throw new SAXException ("Unexpected startPrefixMapping in '" + getName() + "' (" + getLocation() + ")"); 368 } 369 370 public void endPrefixMapping(String prefix) throws SAXException { 371 throw new SAXException ("Unexpected endPrefixMapping in '" + getName() + "' (" + getLocation() + ")"); 372 } 373 374 public Handler startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 375 throw new SAXException ("Unexpected startElement in '" + getName() + "' (" + getLocation() + ")"); 376 } 377 378 public void endElement(String uri, String loc, String raw) throws SAXException { 379 throw new SAXException ("Unexpected endElement in '" + getName() + "' (" + getLocation() + ")"); 380 } 381 382 public Handler characters(char ch[], int start, int length) throws SAXException { 383 throw new SAXException ("Unexpected characters in '" + getName() + "' (" + getLocation() + ")"); 384 } 385 386 public Handler processingInstruction(String target, String data) throws SAXException { 387 throw new SAXException ("Unexpected processingInstruction in '" + getName() + "' (" + getLocation() + ")"); 388 } 389 390 public Handler skippedEntity(String name) throws SAXException { 391 throw new SAXException ("Unexpected skippedEntity in '" + getName() + "' (" + getLocation() + ")"); 392 } 393 394 public Handler startDTD(String name, String publicId, String systemId) throws SAXException { 395 throw new SAXException ("Unexpected startDTD in '" + getName() + "' (" + getLocation() + ")"); 396 } 397 398 public Handler endDTD() throws SAXException { 399 throw new SAXException ("Unexpected endDTD in '" + getName() + "' (" + getLocation() + ")"); 400 } 401 402 public Handler startEntity(String name) throws SAXException { 403 throw new SAXException ("Unexpected startEntity in '" + getName() + "' (" + getLocation() + ")"); 404 } 405 406 public Handler endEntity(String name) throws SAXException { 407 throw new SAXException ("Unexpected endEntity in '" + getName() + "' (" + getLocation() + ")"); 408 } 409 410 public Handler startCDATA() throws SAXException { 411 throw new SAXException ("Unexpected startCDATA in '" + getName() + "' (" + getLocation() + ")"); 412 } 413 414 public Handler endCDATA() throws SAXException { 415 throw new SAXException ("Unexpected endCDATA in '" + getName() + "' (" + getLocation() + ")"); 416 } 417 418 public Handler comment(char c[], int start, int len) throws SAXException { 419 throw new SAXException ("Unexpected comment in '" + getName() + "' (" + getLocation() + ")"); 420 } 421 } 422 423 protected final Handler hNull = new NullHandler(); 424 protected final Handler hBuffer = new BufferHandler(); 425 426 private Locator locator; 427 private LinkedList handlers; 428 private Handler handler; 429 430 private LinkedList buffers; 431 private LinkedList locators; 432 private SaxBuffer buffer; 433 434 435 438 protected void init(Handler top) { 439 locators = new LinkedList (); 440 handlers = new LinkedList (); 441 handler = top; 442 } 443 444 447 public void recycle() { 448 super.recycle(); 449 handlers = null; 450 handler = null; 451 buffers = null; 452 buffer = null; 453 locator = null; 454 locators = null; 455 } 456 457 460 protected String getLocation() { 461 if (locator == null) { 462 return "unknown"; 463 } 464 465 final String location = " (" + locator.getSystemId() + ":" + 466 locator.getLineNumber() + ":" + 467 locator.getColumnNumber() + ")"; 468 return location; 469 } 470 471 protected void pushHandler(Handler handler) { 472 this.handlers.addFirst(this.handler); 473 this.handler = handler; 474 } 475 476 protected void popHandler() { 477 this.handler = (Handler) this.handlers.removeFirst(); 478 } 479 480 protected void beginBuffer() { 481 if (this.buffer != null) { 482 if (this.buffers == null) { 483 this.buffers = new LinkedList (); 484 } 485 this.buffers.addFirst(this.buffer); 486 } 487 locators.addFirst(locator); 488 locator = new LocatorImpl (locator); 489 this.buffer = new SaxBuffer(); 490 } 491 492 protected SaxBuffer endBuffer() { 493 SaxBuffer buffer = this.buffer; 494 if (this.buffers != null && this.buffers.size() > 0) { 495 this.buffer = (SaxBuffer) this.buffers.removeFirst(); 496 } else { 497 this.buffer = null; 498 } 499 locator = (Locator )locators.removeFirst(); 500 return buffer; 501 } 502 503 507 public void setDocumentLocator(Locator locator) { 508 this.locator = locator; 509 } 510 511 public void startDocument() throws SAXException { 512 pushHandler(handler.startDocument()); 513 } 514 515 public void endDocument() throws SAXException { 516 handler.endDocument(); 517 popHandler(); 518 } 519 520 public void startPrefixMapping(String prefix, String uri) throws SAXException { 521 handler.startPrefixMapping(prefix, uri); 522 } 523 524 public void endPrefixMapping(String prefix) throws SAXException { 525 handler.endPrefixMapping(prefix); 526 } 527 528 public void startElement(String uri, String loc, String raw, Attributes attrs) throws SAXException { 529 pushHandler(handler.nestedElement(uri, loc, raw, attrs)); 530 handler = handler.startElement(uri, loc, raw, attrs); 531 } 532 533 public void endElement(String uri, String loc, String raw) throws SAXException { 534 handler.endElement(uri, loc, raw); 535 popHandler(); 536 } 537 538 public void characters(char ch[], int start, int len) throws SAXException { 539 handler = handler.characters(ch, start, len); 540 } 541 542 public void ignorableWhitespace(char ch[], int start, int len) throws SAXException { 543 handler = handler.ignorableWhitespace(ch, start, len); 544 } 545 546 public void processingInstruction(String target, String data) throws SAXException { 547 handler = handler.processingInstruction(target, data); 548 } 549 550 public void skippedEntity(String name) throws SAXException { 551 handler = handler.skippedEntity(name); 552 } 553 554 558 public void startDTD(String name, String publicId, String systemId) throws SAXException { 559 handler = handler.startDTD(name, publicId, systemId); 560 } 561 562 public void endDTD() throws SAXException { 563 handler = handler.endDTD(); 564 } 565 566 public void startEntity(String name) throws SAXException { 567 handler = handler.startEntity(name); 568 } 569 570 public void endEntity(String name) throws SAXException { 571 handler = handler.endEntity(name); 572 } 573 574 public void startCDATA() throws SAXException { 575 handler = handler.startCDATA(); 576 } 577 578 public void endCDATA() throws SAXException { 579 handler = handler.endCDATA(); 580 } 581 582 public void comment(char ch[], int start, int len) throws SAXException { 583 handler = handler.comment(ch, start, len); 584 } 585 } 586 | Popular Tags |