001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.transformation;
018:
019: import java.util.LinkedList;
020:
021: import org.apache.cocoon.xml.AbstractXMLPipe;
022: import org.apache.cocoon.xml.SaxBuffer;
023:
024: import org.xml.sax.Attributes;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.Locator;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.ext.LexicalHandler;
029: import org.xml.sax.helpers.LocatorImpl;
030:
031: /**
032: * Base class for XMLPipe's. Allows the structure of the source code of
033: * the XMLPipe to match the structure of the data being transformed.
034: *
035: * @version $Id: EffectPipe.java 454815 2006-10-10 16:36:12Z vgritsenko $
036: */
037: public class EffectPipe extends AbstractXMLPipe {
038:
039: /**
040: * Java 1.3 contentHandler access method.
041: * <br>
042: * Access to {#contentHandler} from inner class on Java 1.3 causes NoSuchMethod error.
043: */
044: private ContentHandler getContentHandler() {
045: return super .contentHandler;
046: }
047:
048: /**
049: * Java 1.3 lexicalHandler access method.
050: * <br>
051: * Access to {#lexicalHandler} from inner class on Java 1.3 causes NoSuchMethod error.
052: */
053: private LexicalHandler getLexicalHandler() {
054: return super .lexicalHandler;
055: }
056:
057: /**
058: * Handler interface. Accepts SAX events, can return other handler
059: * to replace self for further events.
060: */
061: protected interface Handler {
062: public Handler startDocument() throws SAXException;
063:
064: public void endDocument() throws SAXException;
065:
066: public void startPrefixMapping(String prefix, String uri)
067: throws SAXException;
068:
069: public void endPrefixMapping(String prefix) throws SAXException;
070:
071: public Handler startElement(String uri, String loc, String raw,
072: Attributes attrs) throws SAXException;
073:
074: /**
075: * Called before startElement, handler can decide what other handler should process
076: * next startElement.
077: */
078: public Handler nestedElement(String uri, String loc,
079: String raw, Attributes attrs) throws SAXException;
080:
081: public void endElement(String uri, String loc, String raw)
082: throws SAXException;
083:
084: public Handler characters(char ch[], int start, int length)
085: throws SAXException;
086:
087: public Handler ignorableWhitespace(char ch[], int start,
088: int length) throws SAXException;
089:
090: public Handler processingInstruction(String target, String data)
091: throws SAXException;
092:
093: public Handler skippedEntity(String name) throws SAXException;
094:
095: public Handler startDTD(String name, String publicId,
096: String systemId) throws SAXException;
097:
098: public Handler endDTD() throws SAXException;
099:
100: public Handler startEntity(String name) throws SAXException;
101:
102: public Handler endEntity(String name) throws SAXException;
103:
104: public Handler startCDATA() throws SAXException;
105:
106: public Handler endCDATA() throws SAXException;
107:
108: public Handler comment(char c[], int start, int len)
109: throws SAXException;
110: }
111:
112: /**
113: * Ignores all events
114: */
115: protected class NullHandler implements Handler {
116: public Handler startDocument() throws SAXException {
117: return this ;
118: }
119:
120: public void endDocument() throws SAXException {
121: }
122:
123: public void startPrefixMapping(String prefix, String uri)
124: throws SAXException {
125: }
126:
127: public void endPrefixMapping(String prefix) throws SAXException {
128: }
129:
130: public Handler nestedElement(String uri, String loc,
131: String raw, Attributes attrs) throws SAXException {
132: return this ;
133: }
134:
135: public Handler startElement(String uri, String loc, String raw,
136: Attributes attrs) throws SAXException {
137: return this ;
138: }
139:
140: public void endElement(String uri, String loc, String raw)
141: throws SAXException {
142: }
143:
144: public Handler characters(char ch[], int start, int length)
145: throws SAXException {
146: return this ;
147: }
148:
149: public Handler ignorableWhitespace(char ch[], int start,
150: int length) throws SAXException {
151: return this ;
152: }
153:
154: public Handler processingInstruction(String target, String data)
155: throws SAXException {
156: return this ;
157: }
158:
159: public Handler skippedEntity(String name) throws SAXException {
160: return this ;
161: }
162:
163: public Handler startDTD(String name, String publicId,
164: String systemId) throws SAXException {
165: return this ;
166: }
167:
168: public Handler endDTD() throws SAXException {
169: return this ;
170: }
171:
172: public Handler startEntity(String name) throws SAXException {
173: return this ;
174: }
175:
176: public Handler endEntity(String name) throws SAXException {
177: return this ;
178: }
179:
180: public Handler startCDATA() throws SAXException {
181: return this ;
182: }
183:
184: public Handler endCDATA() throws SAXException {
185: return this ;
186: }
187:
188: public Handler comment(char c[], int start, int len)
189: throws SAXException {
190: return this ;
191: }
192: }
193:
194: /**
195: * Buffers content into the pipe's SAX buffer.
196: */
197: protected class BufferHandler extends NullHandler {
198: public Handler startDocument() throws SAXException {
199: if (buffer != null)
200: buffer.startDocument();
201: return this ;
202: }
203:
204: public void setDocumentLocator(Locator paramLocator) {
205: locator = new LocatorImpl(paramLocator);
206: if (buffer != null)
207: buffer.setDocumentLocator(paramLocator);
208: }
209:
210: public void endDocument() throws SAXException {
211: if (buffer != null)
212: buffer.endDocument();
213: }
214:
215: public void startPrefixMapping(String prefix, String uri)
216: throws SAXException {
217: if (buffer != null)
218: buffer.startPrefixMapping(prefix, uri);
219: }
220:
221: public void endPrefixMapping(String prefix) throws SAXException {
222: if (buffer != null)
223: buffer.endPrefixMapping(prefix);
224: }
225:
226: public Handler startElement(String uri, String loc, String raw,
227: Attributes attrs) throws SAXException {
228: if (buffer != null)
229: buffer.startElement(uri, loc, raw, attrs);
230: return this ;
231: }
232:
233: public void endElement(String uri, String loc, String raw)
234: throws SAXException {
235: if (buffer != null)
236: buffer.endElement(uri, loc, raw);
237: }
238:
239: public Handler characters(char ch[], int start, int length)
240: throws SAXException {
241: if (buffer != null)
242: buffer.characters(ch, start, length);
243: return this ;
244: }
245:
246: public Handler ignorableWhitespace(char ch[], int start,
247: int length) throws SAXException {
248: if (buffer != null)
249: buffer.ignorableWhitespace(ch, start, length);
250: return this ;
251: }
252:
253: public Handler processingInstruction(String target, String data)
254: throws SAXException {
255: if (buffer != null)
256: buffer.processingInstruction(target, data);
257: return this ;
258: }
259:
260: public Handler skippedEntity(String name) throws SAXException {
261: if (buffer != null)
262: buffer.skippedEntity(name);
263: return this ;
264: }
265:
266: public Handler startDTD(String name, String publicId,
267: String systemId) throws SAXException {
268: if (buffer != null)
269: buffer.startDTD(name, publicId, systemId);
270: return this ;
271: }
272:
273: public Handler endDTD() throws SAXException {
274: if (buffer != null)
275: buffer.endDTD();
276: return this ;
277: }
278:
279: public Handler startEntity(String name) throws SAXException {
280: if (buffer != null)
281: buffer.startEntity(name);
282: return this ;
283: }
284:
285: public Handler endEntity(String name) throws SAXException {
286: if (buffer != null)
287: buffer.endEntity(name);
288: return this ;
289: }
290:
291: public Handler startCDATA() throws SAXException {
292: if (buffer != null)
293: buffer.startCDATA();
294: return this ;
295: }
296:
297: public Handler endCDATA() throws SAXException {
298: if (buffer != null)
299: buffer.endCDATA();
300: return this ;
301: }
302:
303: public Handler comment(char c[], int start, int len)
304: throws SAXException {
305: if (buffer != null)
306: buffer.comment(c, start, len);
307: return this ;
308: }
309: }
310:
311: /**
312: * Copies events over into the contentHandler
313: */
314: protected class CopyHandler extends NullHandler {
315: public Handler startDocument() throws SAXException {
316: getContentHandler().startDocument();
317: return this ;
318: }
319:
320: public void endDocument() throws SAXException {
321: getContentHandler().endDocument();
322: }
323:
324: public void startPrefixMapping(String prefix, String uri)
325: throws SAXException {
326: getContentHandler().startPrefixMapping(prefix, uri);
327: }
328:
329: public void endPrefixMapping(String prefix) throws SAXException {
330: getContentHandler().endPrefixMapping(prefix);
331: }
332:
333: public Handler startElement(String uri, String loc, String raw,
334: Attributes attrs) throws SAXException {
335: getContentHandler().startElement(uri, loc, raw, attrs);
336: return this ;
337: }
338:
339: public void endElement(String uri, String loc, String raw)
340: throws SAXException {
341: getContentHandler().endElement(uri, loc, raw);
342: }
343:
344: public Handler characters(char ch[], int start, int length)
345: throws SAXException {
346: getContentHandler().characters(ch, start, length);
347: return this ;
348: }
349:
350: public Handler ignorableWhitespace(char ch[], int start,
351: int length) throws SAXException {
352: getContentHandler().ignorableWhitespace(ch, start, length);
353: return this ;
354: }
355:
356: public Handler processingInstruction(String target, String data)
357: throws SAXException {
358: getContentHandler().processingInstruction(target, data);
359: return this ;
360: }
361:
362: public Handler skippedEntity(String name) throws SAXException {
363: getContentHandler().skippedEntity(name);
364: return this ;
365: }
366:
367: public Handler startDTD(String name, String publicId,
368: String systemId) throws SAXException {
369: if (getLexicalHandler() != null)
370: getLexicalHandler().startDTD(name, publicId, systemId);
371: return this ;
372: }
373:
374: public Handler endDTD() throws SAXException {
375: if (getLexicalHandler() != null)
376: getLexicalHandler().endDTD();
377: return this ;
378: }
379:
380: public Handler startEntity(String name) throws SAXException {
381: if (getLexicalHandler() != null)
382: getLexicalHandler().startEntity(name);
383: return this ;
384: }
385:
386: public Handler endEntity(String name) throws SAXException {
387: if (getLexicalHandler() != null)
388: getLexicalHandler().endEntity(name);
389: return this ;
390: }
391:
392: public Handler startCDATA() throws SAXException {
393: if (getLexicalHandler() != null)
394: getLexicalHandler().startCDATA();
395: return this ;
396: }
397:
398: public Handler endCDATA() throws SAXException {
399: if (getLexicalHandler() != null)
400: getLexicalHandler().endCDATA();
401: return this ;
402: }
403:
404: public Handler comment(char c[], int start, int len)
405: throws SAXException {
406: if (getLexicalHandler() != null)
407: getLexicalHandler().comment(c, start, len);
408: return this ;
409: }
410: }
411:
412: /**
413: * Throws exception on most events, with the exception of ignorableWhitespace.
414: */
415: protected class ErrorHandler extends NullHandler {
416: protected String getName() {
417: return "<unknown>";
418: }
419:
420: public Handler startDocument() throws SAXException {
421: throw new SAXException("Unexpected startDocument in '"
422: + getName() + "' (" + getLocation() + ")");
423: }
424:
425: public void endDocument() throws SAXException {
426: throw new SAXException("Unexpected endDocument in '"
427: + getName() + "' (" + getLocation() + ")");
428: }
429:
430: public void startPrefixMapping(String prefix, String uri)
431: throws SAXException {
432: throw new SAXException("Unexpected startPrefixMapping in '"
433: + getName() + "' (" + getLocation() + ")");
434: }
435:
436: public void endPrefixMapping(String prefix) throws SAXException {
437: throw new SAXException("Unexpected endPrefixMapping in '"
438: + getName() + "' (" + getLocation() + ")");
439: }
440:
441: public Handler startElement(String uri, String loc, String raw,
442: Attributes attrs) throws SAXException {
443: throw new SAXException("Unexpected startElement in '"
444: + getName() + "' (" + getLocation() + ")");
445: }
446:
447: public void endElement(String uri, String loc, String raw)
448: throws SAXException {
449: throw new SAXException("Unexpected endElement in '"
450: + getName() + "' (" + getLocation() + ")");
451: }
452:
453: public Handler characters(char ch[], int start, int length)
454: throws SAXException {
455: throw new SAXException("Unexpected characters in '"
456: + getName() + "' (" + getLocation() + ")");
457: }
458:
459: public Handler processingInstruction(String target, String data)
460: throws SAXException {
461: throw new SAXException(
462: "Unexpected processingInstruction in '" + getName()
463: + "' (" + getLocation() + ")");
464: }
465:
466: public Handler skippedEntity(String name) throws SAXException {
467: throw new SAXException("Unexpected skippedEntity in '"
468: + getName() + "' (" + getLocation() + ")");
469: }
470:
471: public Handler startDTD(String name, String publicId,
472: String systemId) throws SAXException {
473: throw new SAXException("Unexpected startDTD in '"
474: + getName() + "' (" + getLocation() + ")");
475: }
476:
477: public Handler endDTD() throws SAXException {
478: throw new SAXException("Unexpected endDTD in '" + getName()
479: + "' (" + getLocation() + ")");
480: }
481:
482: public Handler startEntity(String name) throws SAXException {
483: throw new SAXException("Unexpected startEntity in '"
484: + getName() + "' (" + getLocation() + ")");
485: }
486:
487: public Handler endEntity(String name) throws SAXException {
488: throw new SAXException("Unexpected endEntity in '"
489: + getName() + "' (" + getLocation() + ")");
490: }
491:
492: public Handler startCDATA() throws SAXException {
493: throw new SAXException("Unexpected startCDATA in '"
494: + getName() + "' (" + getLocation() + ")");
495: }
496:
497: public Handler endCDATA() throws SAXException {
498: throw new SAXException("Unexpected endCDATA in '"
499: + getName() + "' (" + getLocation() + ")");
500: }
501:
502: public Handler comment(char c[], int start, int len)
503: throws SAXException {
504: throw new SAXException("Unexpected comment in '"
505: + getName() + "' (" + getLocation() + ")");
506: }
507: }
508:
509: protected final Handler hNull = new NullHandler();
510: protected final Handler hBuffer = new BufferHandler();
511:
512: protected Locator locator;
513: private LinkedList handlers;
514: private Handler handler;
515:
516: private LinkedList buffers;
517: private LinkedList locators;
518: protected SaxBuffer buffer;
519:
520: /**
521: * Initialize the pipe before starting processing
522: */
523: protected void init(Handler top) {
524: locators = new LinkedList();
525: handlers = new LinkedList();
526: handler = top;
527: }
528:
529: /**
530: * Recycle the pipe after processing
531: */
532: public void recycle() {
533: super .recycle();
534: handlers = null;
535: handler = null;
536: buffers = null;
537: buffer = null;
538: locator = null;
539: locators = null;
540: }
541:
542: /**
543: * @return current location (if known)
544: */
545: protected String getLocation() {
546: if (locator == null) {
547: return "unknown";
548: }
549:
550: return " (" + locator.getSystemId() + ":"
551: + locator.getLineNumber() + ":"
552: + locator.getColumnNumber() + ")";
553: }
554:
555: protected void pushHandler(Handler handler) {
556: this .handlers.addFirst(this .handler);
557: this .handler = handler;
558: }
559:
560: protected void popHandler() {
561: this .handler = (Handler) this .handlers.removeFirst();
562: }
563:
564: protected void beginBuffer() {
565: if (this .buffer != null) {
566: if (this .buffers == null) {
567: this .buffers = new LinkedList();
568: }
569: this .buffers.addFirst(this .buffer);
570: }
571: if (locator != null) {
572: locators.addFirst(locator);
573: locator = new LocatorImpl(locator);
574: }
575: this .buffer = new SaxBuffer();
576: }
577:
578: protected SaxBuffer endBuffer() {
579: SaxBuffer buffer = this .buffer;
580: if (this .buffers != null && this .buffers.size() > 0) {
581: this .buffer = (SaxBuffer) this .buffers.removeFirst();
582: } else {
583: this .buffer = null;
584: }
585: if (locator != null) {
586: locator = (Locator) locators.removeFirst();
587: }
588: return buffer;
589: }
590:
591: //
592: // ContentHandler methods
593: //
594:
595: public void setDocumentLocator(Locator locator) {
596: this .locator = locator;
597: }
598:
599: public void startDocument() throws SAXException {
600: pushHandler(handler.startDocument());
601: }
602:
603: public void endDocument() throws SAXException {
604: handler.endDocument();
605: popHandler();
606: }
607:
608: public void startPrefixMapping(String prefix, String uri)
609: throws SAXException {
610: handler.startPrefixMapping(prefix, uri);
611: }
612:
613: public void endPrefixMapping(String prefix) throws SAXException {
614: handler.endPrefixMapping(prefix);
615: }
616:
617: public void startElement(String uri, String loc, String raw,
618: Attributes attrs) throws SAXException {
619: pushHandler(handler.nestedElement(uri, loc, raw, attrs));
620: handler = handler.startElement(uri, loc, raw, attrs);
621: }
622:
623: public void endElement(String uri, String loc, String raw)
624: throws SAXException {
625: handler.endElement(uri, loc, raw);
626: popHandler();
627: }
628:
629: public void characters(char ch[], int start, int len)
630: throws SAXException {
631: handler = handler.characters(ch, start, len);
632: }
633:
634: public void ignorableWhitespace(char ch[], int start, int len)
635: throws SAXException {
636: handler = handler.ignorableWhitespace(ch, start, len);
637: }
638:
639: public void processingInstruction(String target, String data)
640: throws SAXException {
641: handler = handler.processingInstruction(target, data);
642: }
643:
644: public void skippedEntity(String name) throws SAXException {
645: handler = handler.skippedEntity(name);
646: }
647:
648: //
649: // LexicalHandler methods
650: //
651:
652: public void startDTD(String name, String publicId, String systemId)
653: throws SAXException {
654: handler = handler.startDTD(name, publicId, systemId);
655: }
656:
657: public void endDTD() throws SAXException {
658: handler = handler.endDTD();
659: }
660:
661: public void startEntity(String name) throws SAXException {
662: handler = handler.startEntity(name);
663: }
664:
665: public void endEntity(String name) throws SAXException {
666: handler = handler.endEntity(name);
667: }
668:
669: public void startCDATA() throws SAXException {
670: handler = handler.startCDATA();
671: }
672:
673: public void endCDATA() throws SAXException {
674: handler = handler.endCDATA();
675: }
676:
677: public void comment(char ch[], int start, int len)
678: throws SAXException {
679: handler = handler.comment(ch, start, len);
680: }
681: }
|