1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.cocoon.xml;
18
19 import org.apache.avalon.excalibur.pool.Recyclable;
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.xml.sax.ContentHandler;
22 import org.xml.sax.ext.LexicalHandler;
23 import org.xml.sax.helpers.DefaultHandler;
24
25 /**
26 * This abstract class provides default implementation of the methods specified
27 * by the <code>XMLProducer</code> interface.
28 *
29 * @author <a href="mailto:pier@apache.org">Pierpaolo Fumagalli</a>
30 * (Apache Software Foundation)
31 * @version CVS $Id: AbstractXMLProducer.java 433543 2006-08-22 06:22:54Z crossley $
32 */
33 public abstract class AbstractXMLProducer extends AbstractLogEnabled
34 implements XMLProducer, Recyclable {
35
36 protected static final ContentHandler EMPTY_CONTENT_HANDLER = new DefaultHandler();
37
38 /** The <code>XMLConsumer</code> receiving SAX events. */
39 protected XMLConsumer xmlConsumer;
40
41 /** The <code>ContentHandler</code> receiving SAX events. */
42 protected ContentHandler contentHandler = EMPTY_CONTENT_HANDLER;
43
44 /** The <code>LexicalHandler</code> receiving SAX events. */
45 protected LexicalHandler lexicalHandler = DefaultLexicalHandler.NULL_HANDLER;
46
47 /**
48 * Set the <code>XMLConsumer</code> that will receive XML data.
49 * <br>
50 * This method will simply call <code>setContentHandler(consumer)</code>
51 * and <code>setLexicalHandler(consumer)</code>.
52 */
53 public void setConsumer(XMLConsumer consumer) {
54 this.xmlConsumer = consumer;
55 setContentHandler(consumer);
56 setLexicalHandler(consumer);
57 }
58
59 /**
60 * Set the <code>ContentHandler</code> that will receive XML data.
61 * <br>
62 * Subclasses may retrieve this <code>ContentHandler</code> instance
63 * accessing the protected <code>super.contentHandler</code> field.
64 */
65 public void setContentHandler(ContentHandler handler) {
66 this.contentHandler = handler;
67 }
68
69 /**
70 * Set the <code>LexicalHandler</code> that will receive XML data.
71 * <br>
72 * Subclasses may retrieve this <code>LexicalHandler</code> instance
73 * accessing the protected <code>super.lexicalHandler</code> field.
74 */
75 public void setLexicalHandler(LexicalHandler handler) {
76 this.lexicalHandler = handler;
77 }
78
79 /**
80 * Recycle the producer by removing references, and resetting handlers to
81 * null (empty) implementations.
82 */
83 public void recycle() {
84 this.xmlConsumer = null;
85 this.contentHandler = EMPTY_CONTENT_HANDLER;
86 this.lexicalHandler = DefaultLexicalHandler.NULL_HANDLER;
87 }
88 }