KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > AbstractDOMTransformer


1 /*
2  * Copyright 1999-2004 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.transformation;
17
18 import org.apache.avalon.framework.activity.Disposable;
19 import org.apache.avalon.framework.component.ComponentManager;
20 import org.apache.avalon.framework.component.Composable;
21 import org.apache.avalon.framework.parameters.Parameters;
22
23 import org.apache.avalon.excalibur.pool.Recyclable;
24
25 import org.apache.cocoon.ProcessingException;
26 import org.apache.cocoon.environment.SourceResolver;
27 import org.apache.cocoon.xml.dom.DOMBuilder;
28 import org.apache.cocoon.xml.dom.DOMStreamer;
29
30 import org.w3c.dom.Document JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.Locator JavaDoc;
33 import org.xml.sax.Attributes JavaDoc;
34
35 import java.io.IOException JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * An Abstract DOM Transformer, for use when a transformer needs a DOM-based
40  * view of the document.
41  * Subclass this interface and implement <code>transform(Document doc)</code>.
42  * If you need a ComponentManager there is an instance variable
43  * <code>manager</code> for use.
44  *
45  * @author <a HREF="mailto:rossb@apache.org">Ross Burton</a>
46  * @author <a HREF="mailto:brobertson@mta.ca">Bruce G. Robertson</a>
47  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
48  * @version CVS $Id: AbstractDOMTransformer.java 158483 2005-03-21 16:56:56Z bdelacretaz $
49  */

50 public abstract class AbstractDOMTransformer extends AbstractTransformer
51         implements Transformer, DOMBuilder.Listener, Composable, Disposable, Recyclable {
52
53     /**
54      * The SAX entity resolver
55      */

56     protected SourceResolver resolver;
57
58     /**
59      * The request object model
60      */

61     protected Map JavaDoc objectModel;
62
63     /**
64      * The URI requested
65      */

66     protected String JavaDoc source;
67
68     /**
69      * Parameters in the sitemap
70      */

71     protected Parameters parameters;
72
73     /**
74      * A <code>ComponentManager</code> which is available for use.
75      */

76     protected ComponentManager manager;
77
78     /**
79      * The <code>DOMBuilder</code> used to build DOM tree out of
80      *incoming SAX events.
81      */

82     protected DOMBuilder builder;
83
84
85     public AbstractDOMTransformer() {
86         super();
87         this.builder = new DOMBuilder(this);
88     }
89
90     /**
91      * Set the component manager.
92      */

93     public void compose(ComponentManager manager) {
94         this.manager = manager;
95     }
96
97     /**
98      * Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
99      * the source and sitemap <code>Parameters</code> used to process the request.
100      *
101      * If you wish to process the parameters, override this method, call
102      * <code>super()</code> and then add your code.
103      */

104     public void setup(SourceResolver resolver, Map JavaDoc objectModel, String JavaDoc src, Parameters par)
105             throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
106
107         this.resolver = resolver;
108         this.objectModel = objectModel;
109         this.source = src;
110         this.parameters = par;
111     }
112
113     /**
114      * Recycle the component.
115      */

116     public void recycle() {
117         this.resolver = null;
118         this.source = null;
119         this.objectModel = null;
120         this.parameters = null;
121         this.builder.recycle();
122     }
123
124     /**
125      * dispose
126      */

127     public void dispose() {
128         this.builder = null;
129         this.manager = null;
130     }
131
132     /**
133      * This method is called when the Document is finished.
134      * @param doc The DOM Document object representing this SAX stream
135      * @see org.apache.cocoon.xml.dom.DOMBuilder.Listener
136      */

137     public void notify(Document JavaDoc doc) throws SAXException JavaDoc {
138         // Call the user's transform method
139
Document JavaDoc newdoc = transform(doc);
140
141         // Now we stream the resulting DOM tree down the pipe
142
DOMStreamer s = new DOMStreamer(contentHandler, lexicalHandler);
143         s.stream(newdoc);
144     }
145
146     /**
147      * Transform the specified DOM, returning a new DOM to stream down the pipeline.
148      * @param doc The DOM Document representing the SAX stream
149      * @return A DOM Document to stream down the pipeline
150      */

151     protected abstract Document JavaDoc transform(Document JavaDoc doc);
152
153
154     //
155
// SAX Methods. Send incoming SAX events to the DOMBuilder.
156
//
157

158     public void setDocumentLocator(Locator JavaDoc locator) {
159         builder.setDocumentLocator(locator);
160     }
161
162     public void startDocument() throws SAXException JavaDoc {
163         builder.startDocument();
164     }
165
166     public void endDocument() throws SAXException JavaDoc {
167         builder.endDocument();
168     }
169
170     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri) throws SAXException JavaDoc {
171         builder.startPrefixMapping(prefix, uri);
172     }
173
174     public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
175         builder.endPrefixMapping(prefix);
176     }
177
178     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
179             throws SAXException JavaDoc {
180         builder.startElement(uri, loc, raw, a);
181     }
182
183     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
184             throws SAXException JavaDoc {
185         builder.endElement(uri, loc, raw);
186     }
187
188     public void characters(char c[], int start, int len)
189             throws SAXException JavaDoc {
190         builder.characters(c, start, len);
191     }
192
193     public void ignorableWhitespace(char c[], int start, int len)
194             throws SAXException JavaDoc {
195         builder.ignorableWhitespace(c, start, len);
196     }
197
198     public void processingInstruction(String JavaDoc target, String JavaDoc data)
199             throws SAXException JavaDoc {
200         builder.processingInstruction(target, data);
201     }
202
203     public void skippedEntity(String JavaDoc name)
204             throws SAXException JavaDoc {
205         builder.skippedEntity(name);
206     }
207
208     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
209             throws SAXException JavaDoc {
210         builder.startDTD(name, publicId, systemId);
211     }
212
213     public void endDTD()
214             throws SAXException JavaDoc {
215         builder.endDTD();
216     }
217
218     public void startEntity(String JavaDoc name)
219             throws SAXException JavaDoc {
220         builder.startEntity(name);
221     }
222
223     public void endEntity(String JavaDoc name)
224             throws SAXException JavaDoc {
225         builder.endEntity(name);
226     }
227
228     public void startCDATA()
229             throws SAXException JavaDoc {
230         builder.startCDATA();
231     }
232
233     public void endCDATA()
234             throws SAXException JavaDoc {
235         builder.endCDATA();
236     }
237
238     public void comment(char ch[], int start, int len)
239             throws SAXException JavaDoc {
240         builder.comment(ch, start, len);
241     }
242 }
243
Popular Tags