KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > transformation > EffectPipe


1 /*
2  * Copyright 1999-2005 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.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 JavaDoc;
22 import org.xml.sax.Locator JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
24 import org.xml.sax.helpers.LocatorImpl JavaDoc;
25
26 import java.util.LinkedList JavaDoc;
27
28 /**
29  * Base class for XMLPipe's. Allows the structure of the source code of
30  * the XMLPipe to match the structure of the data being transformed.
31  *
32  * @version $Id: EffectPipe.java 289538 2005-09-16 13:46:22Z sylvain $
33  */

34 public class EffectPipe extends AbstractXMLPipe {
35
36     /**
37      * Handler interface. Accepts SAX events, can return other handler
38      * to replace self for further events.
39      */

40     protected interface Handler {
41         public Handler startDocument()
42         throws SAXException JavaDoc;
43
44         public void endDocument()
45         throws SAXException JavaDoc;
46
47         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
48         throws SAXException JavaDoc;
49
50         public void endPrefixMapping(String JavaDoc prefix)
51         throws SAXException JavaDoc;
52
53         public Handler startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs)
54         throws SAXException JavaDoc;
55
56         /**
57          * Called before startElement, handler can decide what other handler should process
58          * next startElement.
59          */

60         public Handler nestedElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs)
61         throws SAXException JavaDoc;
62
63         public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
64         throws SAXException JavaDoc;
65
66         public Handler characters(char ch[], int start, int length)
67         throws SAXException JavaDoc;
68
69         public Handler ignorableWhitespace(char ch[], int start, int length)
70         throws SAXException JavaDoc;
71
72         public Handler processingInstruction(String JavaDoc target, String JavaDoc data)
73         throws SAXException JavaDoc;
74
75         public Handler skippedEntity(String JavaDoc name)
76         throws SAXException JavaDoc;
77
78         public Handler startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
79         throws SAXException JavaDoc;
80
81         public Handler endDTD()
82         throws SAXException JavaDoc;
83
84         public Handler startEntity(String JavaDoc name)
85         throws SAXException JavaDoc;
86
87         public Handler endEntity(String JavaDoc name)
88         throws SAXException JavaDoc;
89
90         public Handler startCDATA()
91         throws SAXException JavaDoc;
92
93         public Handler endCDATA()
94         throws SAXException JavaDoc;
95
96         public Handler comment(char c[], int start, int len)
97         throws SAXException JavaDoc;
98     }
99
100     /**
101      * Ignores all events
102      */

103     protected class NullHandler implements Handler {
104         public Handler startDocument() throws SAXException JavaDoc {
105             return this;
106         }
107
108         public void endDocument() throws SAXException JavaDoc {
109         }
110
111         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
112         }
113
114         public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
115         }
116
117         public Handler nestedElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs) throws SAXException JavaDoc {
118             return this;
119         }
120
121         public Handler startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs) throws SAXException JavaDoc {
122             return this;
123         }
124
125         public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
126         }
127
128         public Handler characters(char ch[], int start, int length) throws SAXException JavaDoc {
129             return this;
130         }
131
132         public Handler ignorableWhitespace(char ch[], int start, int length) throws SAXException JavaDoc {
133             return this;
134         }
135
136         public Handler processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
137             return this;
138         }
139
140         public Handler skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
141             return this;
142         }
143
144         public Handler startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
145             return this;
146         }
147
148         public Handler endDTD() throws SAXException JavaDoc {
149             return this;
150         }
151
152         public Handler startEntity(String JavaDoc name) throws SAXException JavaDoc {
153             return this;
154         }
155
156         public Handler endEntity(String JavaDoc name) throws SAXException JavaDoc {
157             return this;
158         }
159
160         public Handler startCDATA() throws SAXException JavaDoc {
161             return this;
162         }
163
164         public Handler endCDATA() throws SAXException JavaDoc {
165             return this;
166         }
167
168         public Handler comment(char c[], int start, int len) throws SAXException JavaDoc {
169             return this;
170         }
171     }
172
173     /**
174      * Buffers content into the pipe's SAX buffer.
175      */

176     protected class BufferHandler extends NullHandler {
177         public Handler startDocument() throws SAXException JavaDoc {
178             if (buffer != null) buffer.startDocument();
179             return this;
180         }
181
182         public void setDocumentLocator(Locator JavaDoc paramLocator) {
183             locator = new LocatorImpl JavaDoc(paramLocator);
184             if (buffer != null) buffer.setDocumentLocator(paramLocator);
185         }
186
187         public void endDocument() throws SAXException JavaDoc {
188             if (buffer != null) buffer.endDocument();
189         }
190
191         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
192             if (buffer != null) buffer.startPrefixMapping(prefix, uri);
193         }
194
195         public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
196             if (buffer != null) buffer.endPrefixMapping(prefix);
197         }
198
199         public Handler startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs) throws SAXException JavaDoc {
200             if (buffer != null) buffer.startElement(uri, loc, raw, attrs);
201             return this;
202         }
203
204         public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
205             if (buffer != null) buffer.endElement(uri, loc, raw);
206         }
207
208         public Handler characters(char ch[], int start, int length) throws SAXException JavaDoc {
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 JavaDoc {
214             if (buffer != null) buffer.ignorableWhitespace(ch, start, length);
215             return this;
216         }
217
218         public Handler processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
219             if (buffer != null) buffer.processingInstruction(target, data);
220             return this;
221         }
222
223         public Handler skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
224             if (buffer != null) buffer.skippedEntity(name);
225             return this;
226         }
227
228         public Handler startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
229             if (buffer != null) buffer.startDTD(name, publicId, systemId);
230             return this;
231         }
232
233         public Handler endDTD() throws SAXException JavaDoc {
234             if (buffer != null) buffer.endDTD();
235             return this;
236         }
237
238         public Handler startEntity(String JavaDoc name) throws SAXException JavaDoc {
239             if (buffer != null) buffer.startEntity(name);
240             return this;
241         }
242
243         public Handler endEntity(String JavaDoc name) throws SAXException JavaDoc {
244             if (buffer != null) buffer.endEntity(name);
245             return this;
246         }
247
248         public Handler startCDATA() throws SAXException JavaDoc {
249             if (buffer != null) buffer.startCDATA();
250             return this;
251         }
252
253         public Handler endCDATA() throws SAXException JavaDoc {
254             if (buffer != null) buffer.endCDATA();
255             return this;
256         }
257
258         public Handler comment(char c[], int start, int len) throws SAXException JavaDoc {
259             if (buffer != null) buffer.comment(c, start, len);
260             return this;
261         }
262     }
263
264     /**
265      * Copies events over into the contentHandler
266      */

267     protected class CopyHandler extends NullHandler {
268         public Handler startDocument() throws SAXException JavaDoc {
269             contentHandler.startDocument();
270             return this;
271         }
272
273         public void endDocument() throws SAXException JavaDoc {
274             contentHandler.endDocument();
275         }
276
277         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
278             contentHandler.startPrefixMapping(prefix, uri);
279         }
280
281         public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
282             contentHandler.endPrefixMapping(prefix);
283         }
284
285         public Handler startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs) throws SAXException JavaDoc {
286             contentHandler.startElement(uri, loc, raw, attrs);
287             return this;
288         }
289
290         public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
291             contentHandler.endElement(uri, loc, raw);
292         }
293
294         public Handler characters(char ch[], int start, int length) throws SAXException JavaDoc {
295             contentHandler.characters(ch, start, length);
296             return this;
297         }
298
299         public Handler ignorableWhitespace(char ch[], int start, int length) throws SAXException JavaDoc {
300             contentHandler.ignorableWhitespace(ch, start, length);
301             return this;
302         }
303
304         public Handler processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
305             contentHandler.processingInstruction(target, data);
306             return this;
307         }
308
309         public Handler skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
310             contentHandler.skippedEntity(name);
311             return this;
312         }
313
314         public Handler startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
315             if (lexicalHandler != null) lexicalHandler.startDTD(name, publicId, systemId);
316             return this;
317         }
318
319         public Handler endDTD() throws SAXException JavaDoc {
320             if (lexicalHandler != null) lexicalHandler.endDTD();
321             return this;
322         }
323
324         public Handler startEntity(String JavaDoc name) throws SAXException JavaDoc {
325             if (lexicalHandler != null) lexicalHandler.startEntity(name);
326             return this;
327         }
328
329         public Handler endEntity(String JavaDoc name) throws SAXException JavaDoc {
330             if (lexicalHandler != null) lexicalHandler.endEntity(name);
331             return this;
332         }
333
334         public Handler startCDATA() throws SAXException JavaDoc {
335             if (lexicalHandler != null) lexicalHandler.startCDATA();
336             return this;
337         }
338
339         public Handler endCDATA() throws SAXException JavaDoc {
340             if (lexicalHandler != null) lexicalHandler.endCDATA();
341             return this;
342         }
343
344         public Handler comment(char c[], int start, int len) throws SAXException JavaDoc {
345             if (lexicalHandler != null) lexicalHandler.comment(c, start, len);
346             return this;
347         }
348     }
349
350     /**
351      * Throws exception on most events, with the exception of ignorableWhitespace.
352      */

353     protected class ErrorHandler extends NullHandler {
354         protected String JavaDoc getName() {
355             return "<unknown>";
356         }
357
358         public Handler startDocument() throws SAXException JavaDoc {
359             throw new SAXException JavaDoc("Unexpected startDocument in '" + getName() + "' (" + getLocation() + ")");
360         }
361
362         public void endDocument() throws SAXException JavaDoc {
363             throw new SAXException JavaDoc("Unexpected endDocument in '" + getName() + "' (" + getLocation() + ")");
364         }
365
366         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
367             throw new SAXException JavaDoc("Unexpected startPrefixMapping in '" + getName() + "' (" + getLocation() + ")");
368         }
369
370         public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
371             throw new SAXException JavaDoc("Unexpected endPrefixMapping in '" + getName() + "' (" + getLocation() + ")");
372         }
373
374         public Handler startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs) throws SAXException JavaDoc {
375             throw new SAXException JavaDoc("Unexpected startElement in '" + getName() + "' (" + getLocation() + ")");
376         }
377
378         public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
379             throw new SAXException JavaDoc("Unexpected endElement in '" + getName() + "' (" + getLocation() + ")");
380         }
381
382         public Handler characters(char ch[], int start, int length) throws SAXException JavaDoc {
383             throw new SAXException JavaDoc("Unexpected characters in '" + getName() + "' (" + getLocation() + ")");
384         }
385
386         public Handler processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
387             throw new SAXException JavaDoc("Unexpected processingInstruction in '" + getName() + "' (" + getLocation() + ")");
388         }
389
390         public Handler skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
391             throw new SAXException JavaDoc("Unexpected skippedEntity in '" + getName() + "' (" + getLocation() + ")");
392         }
393
394         public Handler startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
395             throw new SAXException JavaDoc("Unexpected startDTD in '" + getName() + "' (" + getLocation() + ")");
396         }
397
398         public Handler endDTD() throws SAXException JavaDoc {
399             throw new SAXException JavaDoc("Unexpected endDTD in '" + getName() + "' (" + getLocation() + ")");
400         }
401
402         public Handler startEntity(String JavaDoc name) throws SAXException JavaDoc {
403             throw new SAXException JavaDoc("Unexpected startEntity in '" + getName() + "' (" + getLocation() + ")");
404         }
405
406         public Handler endEntity(String JavaDoc name) throws SAXException JavaDoc {
407             throw new SAXException JavaDoc("Unexpected endEntity in '" + getName() + "' (" + getLocation() + ")");
408         }
409
410         public Handler startCDATA() throws SAXException JavaDoc {
411             throw new SAXException JavaDoc("Unexpected startCDATA in '" + getName() + "' (" + getLocation() + ")");
412         }
413
414         public Handler endCDATA() throws SAXException JavaDoc {
415             throw new SAXException JavaDoc("Unexpected endCDATA in '" + getName() + "' (" + getLocation() + ")");
416         }
417
418         public Handler comment(char c[], int start, int len) throws SAXException JavaDoc {
419             throw new SAXException JavaDoc("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 JavaDoc locator;
427     private LinkedList JavaDoc handlers;
428     private Handler handler;
429
430     private LinkedList JavaDoc buffers;
431     private LinkedList JavaDoc locators;
432     private SaxBuffer buffer;
433
434
435     /**
436      * Initialize the pipe before starting processing
437      */

438     protected void init(Handler top) {
439         locators = new LinkedList JavaDoc();
440         handlers = new LinkedList JavaDoc();
441         handler = top;
442     }
443
444     /**
445      * Recycle the pipe after processing
446      */

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     /**
458      * @return current location (if known)
459      */

460     protected String JavaDoc getLocation() {
461         if (locator == null) {
462             return "unknown";
463         }
464
465         final String JavaDoc 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 JavaDoc();
484             }
485             this.buffers.addFirst(this.buffer);
486         }
487         locators.addFirst(locator);
488         locator = new LocatorImpl JavaDoc(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 JavaDoc)locators.removeFirst();
500         return buffer;
501     }
502
503     //
504
// ContentHandler methods
505
//
506

507     public void setDocumentLocator(Locator JavaDoc locator) {
508         this.locator = locator;
509     }
510
511     public void startDocument() throws SAXException JavaDoc {
512         pushHandler(handler.startDocument());
513     }
514
515     public void endDocument() throws SAXException JavaDoc {
516         handler.endDocument();
517         popHandler();
518     }
519
520     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
521         handler.startPrefixMapping(prefix, uri);
522     }
523
524     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
525         handler.endPrefixMapping(prefix);
526     }
527
528     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc attrs) throws SAXException JavaDoc {
529         pushHandler(handler.nestedElement(uri, loc, raw, attrs));
530         handler = handler.startElement(uri, loc, raw, attrs);
531     }
532
533     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw) throws SAXException JavaDoc {
534         handler.endElement(uri, loc, raw);
535         popHandler();
536     }
537
538     public void characters(char ch[], int start, int len) throws SAXException JavaDoc {
539         handler = handler.characters(ch, start, len);
540     }
541
542     public void ignorableWhitespace(char ch[], int start, int len) throws SAXException JavaDoc {
543         handler = handler.ignorableWhitespace(ch, start, len);
544     }
545
546     public void processingInstruction(String JavaDoc target, String JavaDoc data) throws SAXException JavaDoc {
547         handler = handler.processingInstruction(target, data);
548     }
549
550     public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
551         handler = handler.skippedEntity(name);
552     }
553
554     //
555
// LexicalHandler methods
556
//
557

558     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
559         handler = handler.startDTD(name, publicId, systemId);
560     }
561
562     public void endDTD() throws SAXException JavaDoc {
563         handler = handler.endDTD();
564     }
565
566     public void startEntity(String JavaDoc name) throws SAXException JavaDoc {
567         handler = handler.startEntity(name);
568     }
569
570     public void endEntity(String JavaDoc name) throws SAXException JavaDoc {
571         handler = handler.endEntity(name);
572     }
573
574     public void startCDATA() throws SAXException JavaDoc {
575         handler = handler.startCDATA();
576     }
577
578     public void endCDATA() throws SAXException JavaDoc {
579         handler = handler.endCDATA();
580     }
581
582     public void comment(char ch[], int start, int len) throws SAXException JavaDoc {
583         handler = handler.comment(ch, start, len);
584     }
585 }
586
Popular Tags