1 /* 2 * File: DelegateContentHandler.java 3 * Created: 16-dub-06 10:41:18 4 * 5 * Copyright 2006 Michal Burda. 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 */ 21 package net.sf.webmancer.util.xml.sax; 22 23 import org.xml.sax.Attributes; 24 import org.xml.sax.ContentHandler; 25 import org.xml.sax.Locator; 26 import org.xml.sax.SAXException; 27 28 /** 29 * This is the {@link org.xml.sax.ContentHandler} implementation that simply delegates all SAX parsing events to the 30 * target {@link org.xml.sax.ContentHandler} that may be set at any time by the 31 * {@link #setTargetContentHandler(ContentHandler)} method. If the target {@link org.xml.sax.ContentHandler} is set to 32 * <code>null</code>, the content handler simply does nothing. 33 * 34 * @author Michal Burda 35 */ 36 public class DelegateContentHandler implements ContentHandler { 37 /** 38 * The target content handler 39 */ 40 private ContentHandler handler; 41 42 /** 43 * Create new delegate content handler. 44 * 45 * @param handler 46 * The possibly <code>null</code> target content handler 47 */ 48 public DelegateContentHandler(final ContentHandler handler) { 49 super(); 50 this.handler = handler; 51 } 52 53 /** 54 * Convenient constructor that creates new delegate content handler with target content handler set to 55 * <code>null</code>. 56 */ 57 public DelegateContentHandler() { 58 this(null); 59 } 60 61 /** 62 * Set the target content handler. The method could be called at any time to enable complex behaviour. If the target 63 * {@link org.xml.sax.ContentHandler} is set to <code>null</code>, the content handler would simply do nothing. 64 * 65 * @param handler 66 * The target content handler 67 */ 68 public void setTargetContentHandler(final ContentHandler handler) { 69 this.handler = handler; 70 } 71 72 /** 73 * Get the actually used target content handler. 74 * 75 * @return The actually used target content handler 76 */ 77 public ContentHandler getTargetContentHandler() { 78 return this.handler; 79 } 80 81 /** 82 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 83 * <code>null</code>. 84 * 85 * @see #setTargetContentHandler(ContentHandler) 86 * @see org.xml.sax.ContentHandler#startDocument() 87 */ 88 public void startDocument() throws SAXException { 89 if (this.handler != null) { 90 this.handler.startDocument(); 91 } 92 } 93 94 /** 95 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 96 * <code>null</code>. 97 * 98 * @see #setTargetContentHandler(ContentHandler) 99 * @see org.xml.sax.ContentHandler#endDocument() 100 */ 101 public void endDocument() throws SAXException { 102 if (this.handler != null) { 103 this.handler.endDocument(); 104 } 105 } 106 107 /** 108 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 109 * <code>null</code>. 110 * 111 * @see #setTargetContentHandler(ContentHandler) 112 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator) 113 */ 114 public void setDocumentLocator(final Locator locator) { 115 if (this.handler != null) { 116 this.handler.setDocumentLocator(locator); 117 } 118 } 119 120 /** 121 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 122 * <code>null</code>. 123 * 124 * @see #setTargetContentHandler(ContentHandler) 125 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String) 126 */ 127 public void startPrefixMapping(final String prefix, final String uri) throws SAXException { 128 if (this.handler != null) { 129 this.handler.startPrefixMapping(prefix, uri); 130 } 131 } 132 133 /** 134 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 135 * <code>null</code>. 136 * 137 * @see #setTargetContentHandler(ContentHandler) 138 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String) 139 */ 140 public void endPrefixMapping(final String prefix) throws SAXException { 141 if (this.handler != null) { 142 this.handler.endPrefixMapping(prefix); 143 } 144 } 145 146 /** 147 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 148 * <code>null</code>. 149 * 150 * @see #setTargetContentHandler(ContentHandler) 151 * @see org.xml.sax.ContentHandler#characters(char[], int, int) 152 */ 153 public void characters(final char[] ch, final int start, final int length) throws SAXException { 154 if (this.handler != null) { 155 this.handler.characters(ch, start, length); 156 } 157 } 158 159 /** 160 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 161 * <code>null</code>. 162 * 163 * @see #setTargetContentHandler(ContentHandler) 164 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) 165 */ 166 public void ignorableWhitespace(final char[] ch, final int start, final int length) throws SAXException { 167 if (this.handler != null) { 168 this.handler.ignorableWhitespace(ch, start, length); 169 } 170 } 171 172 /** 173 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 174 * <code>null</code>. 175 * 176 * @see #setTargetContentHandler(ContentHandler) 177 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String) 178 */ 179 public void processingInstruction(final String target, final String data) throws SAXException { 180 if (this.handler != null) { 181 this.handler.processingInstruction(target, data); 182 } 183 } 184 185 /** 186 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 187 * <code>null</code>. 188 * 189 * @see #setTargetContentHandler(ContentHandler) 190 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String) 191 */ 192 public void skippedEntity(final String name) throws SAXException { 193 if (this.handler != null) { 194 this.handler.skippedEntity(name); 195 } 196 } 197 198 /** 199 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 200 * <code>null</code>. 201 * 202 * @see #setTargetContentHandler(ContentHandler) 203 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, 204 * org.xml.sax.Attributes) 205 */ 206 public void startElement(final String uri, final String localName, final String qName, final Attributes atts) throws SAXException { 207 if (this.handler != null) { 208 this.handler.startElement(uri, localName, qName, atts); 209 } 210 } 211 212 /** 213 * Delegates the SAX event to the target content handler or simply does nothing, if the target content handler is 214 * <code>null</code>. 215 * 216 * @see #setTargetContentHandler(ContentHandler) 217 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String) 218 */ 219 public void endElement(final String uri, final String localName, final String qName) throws SAXException { 220 if (this.handler != null) { 221 this.handler.endElement(uri, localName, qName); 222 } 223 } 224 }