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.transformation;
018:
019: import org.apache.avalon.framework.activity.Disposable;
020: import org.apache.avalon.framework.component.ComponentManager;
021: import org.apache.avalon.framework.component.Composable;
022: import org.apache.avalon.framework.parameters.Parameters;
023:
024: import org.apache.cocoon.ProcessingException;
025: import org.apache.cocoon.environment.SourceResolver;
026: import org.apache.cocoon.xml.dom.DOMBuilder;
027: import org.apache.cocoon.xml.dom.DOMStreamer;
028:
029: import org.w3c.dom.Document;
030: import org.xml.sax.SAXException;
031: import org.xml.sax.Locator;
032: import org.xml.sax.Attributes;
033:
034: import java.io.IOException;
035: import java.util.Map;
036:
037: /**
038: * An Abstract DOM Transformer, for use when a transformer needs a DOM-based
039: * view of the document.
040: * Subclass this interface and implement <code>transform(Document doc)</code>.
041: * If you need a ComponentManager there is an instance variable
042: * <code>manager</code> for use.
043: *
044: * @author <a href="mailto:rossb@apache.org">Ross Burton</a>
045: * @author <a href="mailto:brobertson@mta.ca">Bruce G. Robertson</a>
046: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
047: * @version CVS $Id: AbstractDOMTransformer.java 433543 2006-08-22 06:22:54Z crossley $
048: */
049: public abstract class AbstractDOMTransformer extends
050: AbstractTransformer implements DOMBuilder.Listener, Composable,
051: Disposable {
052:
053: /**
054: * The SAX entity resolver
055: */
056: protected SourceResolver resolver;
057:
058: /**
059: * The request object model
060: */
061: protected Map objectModel;
062:
063: /**
064: * The URI requested
065: */
066: protected String source;
067:
068: /**
069: * Parameters in the sitemap
070: */
071: protected Parameters parameters;
072:
073: /**
074: * A <code>ComponentManager</code> which is available for use.
075: */
076: protected ComponentManager manager;
077:
078: /**
079: * The <code>DOMBuilder</code> used to build DOM tree out of
080: *incoming SAX events.
081: */
082: protected DOMBuilder builder;
083:
084: public AbstractDOMTransformer() {
085: super ();
086: this .builder = new DOMBuilder(this );
087: }
088:
089: /**
090: * Set the component manager.
091: */
092: public void compose(ComponentManager manager) {
093: this .manager = manager;
094: }
095:
096: /**
097: * Set the <code>SourceResolver</code>, objectModel <code>Map</code>,
098: * the source and sitemap <code>Parameters</code> used to process the request.
099: *
100: * If you wish to process the parameters, override this method, call
101: * <code>super()</code> and then add your code.
102: */
103: public void setup(SourceResolver resolver, Map objectModel,
104: String src, Parameters par) throws ProcessingException,
105: SAXException, IOException {
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 doc) throws SAXException {
138: // Call the user's transform method
139: Document 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 transform(Document doc);
152:
153: //
154: // SAX Methods. Send incoming SAX events to the DOMBuilder.
155: //
156:
157: public void setDocumentLocator(Locator locator) {
158: builder.setDocumentLocator(locator);
159: }
160:
161: public void startDocument() throws SAXException {
162: builder.startDocument();
163: }
164:
165: public void endDocument() throws SAXException {
166: builder.endDocument();
167: }
168:
169: public void startPrefixMapping(String prefix, String uri)
170: throws SAXException {
171: builder.startPrefixMapping(prefix, uri);
172: }
173:
174: public void endPrefixMapping(String prefix) throws SAXException {
175: builder.endPrefixMapping(prefix);
176: }
177:
178: public void startElement(String uri, String loc, String raw,
179: Attributes a) throws SAXException {
180: builder.startElement(uri, loc, raw, a);
181: }
182:
183: public void endElement(String uri, String loc, String raw)
184: throws SAXException {
185: builder.endElement(uri, loc, raw);
186: }
187:
188: public void characters(char c[], int start, int len)
189: throws SAXException {
190: builder.characters(c, start, len);
191: }
192:
193: public void ignorableWhitespace(char c[], int start, int len)
194: throws SAXException {
195: builder.ignorableWhitespace(c, start, len);
196: }
197:
198: public void processingInstruction(String target, String data)
199: throws SAXException {
200: builder.processingInstruction(target, data);
201: }
202:
203: public void skippedEntity(String name) throws SAXException {
204: builder.skippedEntity(name);
205: }
206:
207: public void startDTD(String name, String publicId, String systemId)
208: throws SAXException {
209: builder.startDTD(name, publicId, systemId);
210: }
211:
212: public void endDTD() throws SAXException {
213: builder.endDTD();
214: }
215:
216: public void startEntity(String name) throws SAXException {
217: builder.startEntity(name);
218: }
219:
220: public void endEntity(String name) throws SAXException {
221: builder.endEntity(name);
222: }
223:
224: public void startCDATA() throws SAXException {
225: builder.startCDATA();
226: }
227:
228: public void endCDATA() throws SAXException {
229: builder.endCDATA();
230: }
231:
232: public void comment(char ch[], int start, int len)
233: throws SAXException {
234: builder.comment(ch, start, len);
235: }
236: }
|