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
018/* $Id: EasyGenerationContentHandlerProxy.java 24000 2010-04-28 00:12:36Z berkley $ */
019 
020package org.kepler.reporting.rio.fop.tools;
021
022//SAX
023import org.xml.sax.Attributes;
024import org.xml.sax.ContentHandler;
025import org.xml.sax.Locator;
026import org.xml.sax.SAXException;
027import org.xml.sax.helpers.AttributesImpl;
028
029/**
030 * This class is an implementation of ContentHandler which acts as a proxy to
031 * another ContentHandler and has the purpose to provide a few handy methods 
032 * that make life easier when generating SAX events.
033 * <br>
034 * Note: This class is only useful for simple cases with no namespaces. 
035 */
036
037public class EasyGenerationContentHandlerProxy implements ContentHandler {
038
039    /** An empty Attributes object used when no attributes are needed. */
040    public static final Attributes EMPTY_ATTS = new AttributesImpl();
041
042    private ContentHandler target;
043
044
045    /**
046     * Main constructor.
047     * @param forwardTo ContentHandler to forward the SAX event to.
048     */
049    public EasyGenerationContentHandlerProxy(ContentHandler forwardTo) {
050        this.target = forwardTo;
051    }
052
053
054    /**
055     * Sends the notification of the beginning of an element.
056     * @param name Name for the element.
057     * @throws SAXException Any SAX exception, possibly wrapping another exception.
058     */
059    public void startElement(String name) throws SAXException {
060        startElement(name, EMPTY_ATTS);
061    }
062
063
064    /**
065     * Sends the notification of the beginning of an element.
066     * @param name Name for the element.
067     * @param atts The attributes attached to the element. If there are no 
068     * attributes, it shall be an empty Attributes object. 
069     * @throws SAXException Any SAX exception, possibly wrapping another exception.
070     */
071    public void startElement(String name, Attributes atts) throws SAXException {
072        startElement(null, name, name, atts);
073    }
074
075
076    /**
077     * Send a String of character data.
078     * @param s The content String
079     * @throws SAXException Any SAX exception, possibly wrapping another exception.
080     */
081    public void characters(String s) throws SAXException {
082        target.characters(s.toCharArray(), 0, s.length());
083    }
084
085
086    /**
087     * Send the notification of the end of an element.
088     * @param name Name for the element.
089     * @throws SAXException Any SAX exception, possibly wrapping another exception.
090     */
091    public void endElement(String name) throws SAXException {
092        endElement(null, name, name);
093    }
094
095
096    /**
097     * Sends notifications for a whole element with some String content.
098     * @param name Name for the element.
099     * @param value Content of the element.
100     * @throws SAXException Any SAX exception, possibly wrapping another exception.
101     */
102    public void element(String name, String value) throws SAXException {
103        element(name, value, EMPTY_ATTS);
104    }
105
106
107    /**
108     * Sends notifications for a whole element with some String content.
109     * @param name Name for the element.
110     * @param value Content of the element.
111     * @param atts The attributes attached to the element. If there are no 
112     * attributes, it shall be an empty Attributes object. 
113     * @throws SAXException Any SAX exception, possibly wrapping another exception.
114     */
115    public void element(String name, String value, Attributes atts) throws SAXException {
116        startElement(name, atts);
117        if (value != null) {
118            characters(value.toCharArray(), 0, value.length());
119        }
120        endElement(name);
121    }
122
123    /* =========== ContentHandler interface =========== */
124
125    /**
126     * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)
127     */
128    public void setDocumentLocator(Locator locator) {
129        target.setDocumentLocator(locator);
130    }
131
132
133    /**
134     * @see org.xml.sax.ContentHandler#startDocument()
135     */
136    public void startDocument() throws SAXException {
137        target.startDocument();
138    }
139
140
141    /**
142     * @see org.xml.sax.ContentHandler#endDocument()
143     */
144    public void endDocument() throws SAXException {
145        target.endDocument();
146    }
147
148
149    /**
150     * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
151     */
152    public void startPrefixMapping(String prefix, String uri) throws SAXException {
153        target.startPrefixMapping(prefix, uri);
154    }
155
156
157    /**
158     * @see org.xml.sax.ContentHandler#endPrefixMapping(String)
159     */
160    public void endPrefixMapping(String prefix) throws SAXException {
161        target.endPrefixMapping(prefix);
162    }
163
164
165    /**
166     * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
167     */
168    public void startElement(String namespaceURI, String localName, 
169                        String qName, Attributes atts) throws SAXException {
170        target.startElement(namespaceURI, localName, qName, atts);
171    }
172
173
174    /**
175     * @see org.xml.sax.ContentHandler#endElement(String, String, String)
176     */
177    public void endElement(String namespaceURI, String localName, String qName) 
178                                                        throws SAXException {
179        target.endElement(namespaceURI, localName, qName);
180    }
181
182
183    /**
184     * @see org.xml.sax.ContentHandler#characters(char[], int, int)
185     */
186    public void characters(char[] ch, int start, int length) throws SAXException {
187        target.characters(ch, start, length);
188    }
189
190
191    /**
192     * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
193     */
194    public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
195        target.ignorableWhitespace(ch, start, length);
196    }
197
198
199    /**
200     * @see org.xml.sax.ContentHandler#processingInstruction(String, String)
201     */
202    public void processingInstruction(String target, String data) throws SAXException {
203        this.target.processingInstruction(target, data);
204    }
205
206
207    /**
208     * @see org.xml.sax.ContentHandler#skippedEntity(String)
209     */
210    public void skippedEntity(String name) throws SAXException {
211        target.skippedEntity(name);
212    }
213
214}