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 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package embedding.tools;
021:
022: //SAX
023: import org.xml.sax.ContentHandler;
024: import org.xml.sax.Locator;
025: import org.xml.sax.Attributes;
026: import org.xml.sax.SAXException;
027: import 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:
037: public class EasyGenerationContentHandlerProxy implements
038: ContentHandler {
039:
040: /** An empty Attributes object used when no attributes are needed. */
041: public static final Attributes EMPTY_ATTS = new AttributesImpl();
042:
043: private ContentHandler target;
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: * Sends the notification of the beginning of an element.
055: * @param name Name for the element.
056: * @throws SAXException Any SAX exception, possibly wrapping another exception.
057: */
058: public void startElement(String name) throws SAXException {
059: startElement(name, EMPTY_ATTS);
060: }
061:
062: /**
063: * Sends the notification of the beginning of an element.
064: * @param name Name for the element.
065: * @param atts The attributes attached to the element. If there are no
066: * attributes, it shall be an empty Attributes object.
067: * @throws SAXException Any SAX exception, possibly wrapping another exception.
068: */
069: public void startElement(String name, Attributes atts)
070: throws SAXException {
071: startElement(null, name, name, atts);
072: }
073:
074: /**
075: * Send a String of character data.
076: * @param s The content String
077: * @throws SAXException Any SAX exception, possibly wrapping another exception.
078: */
079: public void characters(String s) throws SAXException {
080: target.characters(s.toCharArray(), 0, s.length());
081: }
082:
083: /**
084: * Send the notification of the end of an element.
085: * @param name Name for the element.
086: * @throws SAXException Any SAX exception, possibly wrapping another exception.
087: */
088: public void endElement(String name) throws SAXException {
089: endElement(null, name, name);
090: }
091:
092: /**
093: * Sends notifications for a whole element with some String content.
094: * @param name Name for the element.
095: * @param value Content of the element.
096: * @throws SAXException Any SAX exception, possibly wrapping another exception.
097: */
098: public void element(String name, String value) throws SAXException {
099: element(name, value, EMPTY_ATTS);
100: }
101:
102: /**
103: * Sends notifications for a whole element with some String content.
104: * @param name Name for the element.
105: * @param value Content of the element.
106: * @param atts The attributes attached to the element. If there are no
107: * attributes, it shall be an empty Attributes object.
108: * @throws SAXException Any SAX exception, possibly wrapping another exception.
109: */
110: public void element(String name, String value, Attributes atts)
111: throws SAXException {
112: startElement(name, atts);
113: if (value != null) {
114: characters(value.toCharArray(), 0, value.length());
115: }
116: endElement(name);
117: }
118:
119: /* =========== ContentHandler interface =========== */
120:
121: /**
122: * @see org.xml.sax.ContentHandler#setDocumentLocator(Locator)
123: */
124: public void setDocumentLocator(Locator locator) {
125: target.setDocumentLocator(locator);
126: }
127:
128: /**
129: * @see org.xml.sax.ContentHandler#startDocument()
130: */
131: public void startDocument() throws SAXException {
132: target.startDocument();
133: }
134:
135: /**
136: * @see org.xml.sax.ContentHandler#endDocument()
137: */
138: public void endDocument() throws SAXException {
139: target.endDocument();
140: }
141:
142: /**
143: * @see org.xml.sax.ContentHandler#startPrefixMapping(String, String)
144: */
145: public void startPrefixMapping(String prefix, String uri)
146: throws SAXException {
147: target.startPrefixMapping(prefix, uri);
148: }
149:
150: /**
151: * @see org.xml.sax.ContentHandler#endPrefixMapping(String)
152: */
153: public void endPrefixMapping(String prefix) throws SAXException {
154: target.endPrefixMapping(prefix);
155: }
156:
157: /**
158: * @see org.xml.sax.ContentHandler#startElement(String, String, String, Attributes)
159: */
160: public void startElement(String namespaceURI, String localName,
161: String qName, Attributes atts) throws SAXException {
162: target.startElement(namespaceURI, localName, qName, atts);
163: }
164:
165: /**
166: * @see org.xml.sax.ContentHandler#endElement(String, String, String)
167: */
168: public void endElement(String namespaceURI, String localName,
169: String qName) throws SAXException {
170: target.endElement(namespaceURI, localName, qName);
171: }
172:
173: /**
174: * @see org.xml.sax.ContentHandler#characters(char[], int, int)
175: */
176: public void characters(char[] ch, int start, int length)
177: throws SAXException {
178: target.characters(ch, start, length);
179: }
180:
181: /**
182: * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
183: */
184: public void ignorableWhitespace(char[] ch, int start, int length)
185: throws SAXException {
186: target.ignorableWhitespace(ch, start, length);
187: }
188:
189: /**
190: * @see org.xml.sax.ContentHandler#processingInstruction(String, String)
191: */
192: public void processingInstruction(String target, String data)
193: throws SAXException {
194: this .target.processingInstruction(target, data);
195: }
196:
197: /**
198: * @see org.xml.sax.ContentHandler#skippedEntity(String)
199: */
200: public void skippedEntity(String name) throws SAXException {
201: target.skippedEntity(name);
202: }
203:
204: }
|