1 /* 2 * Redistribution and use of this software and associated documentation ("Software"), with or 3 * without modification, are permitted provided that the following conditions are met: 4 * 5 * 1. Redistributions of source code must retain copyright statements and notices. Redistributions 6 * must also contain a copy of this document. 7 * 8 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of 9 * conditions and the following disclaimer in the documentation and/or other materials provided with 10 * the distribution. 11 * 12 * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software 13 * without prior written permission of Intalio, Inc. For written permission, please contact 14 * info@exolab.org. 15 * 16 * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in 17 * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of 18 * Intalio, Inc. 19 * 20 * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/). 21 * 22 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR 23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 24 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY 29 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Copyright 2004 (C) Intalio, Inc. All Rights Reserved. 32 * 33 * 34 */ 35 package org.exolab.castor.xml.util; 36 37 import org.exolab.castor.xml.Namespaces; 38 import org.exolab.castor.xml.NamespacesStack; 39 import org.xml.sax.Attributes; 40 import org.xml.sax.ContentHandler; 41 import org.xml.sax.DocumentHandler; 42 import org.xml.sax.Locator; 43 import org.xml.sax.SAXException; 44 import org.xml.sax.helpers.AttributeListImpl; 45 46 /** 47 * A ContentHandler implementation that wraps a DocumentHandler. This ContentHandler was written for 48 * the Marshaller and expects that QNames are non-null in calls to startElement and endElement 49 * methods as well as inside the Attributes list. 50 * 51 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a> 52 * @version $Revision$ $Date: 2004-09-10 12:15:10 -0600 (Fri, 10 Sep 2004) $ 53 */ 54 public class DocumentHandlerAdapter implements ContentHandler { 55 56 private static final String CDATA = "CDATA"; 57 58 private DocumentHandler _handler = null; 59 60 /** 61 * Represents the namespaces stack. 62 */ 63 private NamespacesStack namespacesStack = null; 64 65 private boolean _createNamespaceScope = true; 66 67 /** 68 * Creates a new DocumentHandlerAdapter 69 * 70 * @param handler the DocumentHandler to wrap (non-null). 71 */ 72 public DocumentHandlerAdapter(DocumentHandler handler) { 73 if (handler == null) { 74 throw new IllegalArgumentException("The argument 'handler' must not be null."); 75 } 76 _handler = handler; 77 namespacesStack = new NamespacesStack(); 78 } 79 80 /** 81 * @see org.xml.sax.ContentHandler#characters(char[], int, int) 82 */ 83 public void characters(char[] chars, int start, int length) throws SAXException { 84 _handler.characters(chars, start, length); 85 } 86 87 /** 88 * @see org.xml.sax.ContentHandler#endDocument() 89 */ 90 public void endDocument() throws SAXException { 91 _handler.endDocument(); 92 } 93 94 /** 95 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, 96 * java.lang.String) 97 */ 98 public void endElement(String uri, String localName, String qName) throws SAXException { 99 _handler.endElement(qName); 100 namespacesStack.removeNamespaceScope(); 101 } 102 103 /** 104 * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String) 105 */ 106 public void endPrefixMapping(String prefix) throws SAXException { 107 // -- do nothing here, this is handled in endElement 108 // -- by simply removing the current namespace scope 109 110 } 111 112 /** 113 * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int) 114 */ 115 public void ignorableWhitespace(char[] chars, int start, int length) throws SAXException { 116 _handler.ignorableWhitespace(chars, start, length); 117 } 118 119 /** 120 * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String) 121 */ 122 public void processingInstruction(String target, String data) throws SAXException { 123 _handler.processingInstruction(target, data); 124 } 125 126 /** 127 * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator) 128 */ 129 public void setDocumentLocator(Locator locator) { 130 _handler.setDocumentLocator(locator); 131 } 132 133 /** 134 * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String) 135 */ 136 public void skippedEntity(String arg0) throws SAXException { 137 // -- do nothing 138 } 139 140 /** 141 * @see org.xml.sax.ContentHandler#startDocument() 142 */ 143 public void startDocument() throws SAXException { 144 _handler.startDocument(); 145 } 146 147 /** 148 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, 149 * java.lang.String, org.xml.sax.Attributes) 150 */ 151 public void startElement(String uri, String localName, String qName, Attributes atts) 152 throws SAXException { 153 AttributeListImpl attList = new AttributeListImpl(); 154 155 // -- Create a new namespace scope if necessary and 156 // -- make sure the flag is reset to true 157 if (_createNamespaceScope) { 158 // -- no current namespaces, but we create a new scope 159 // -- to make things easier in the endElement method 160 namespacesStack.addNewNamespaceScope(); 161 } else { 162 _createNamespaceScope = true; 163 namespacesStack.declareAsAttributes(attList, true); 164 } 165 166 // -- copy Attributes to AttributeList 167 for (int i = 0; i < atts.getLength(); i++) { 168 attList.addAttribute(atts.getQName(i), CDATA, atts.getValue(i)); 169 } 170 171 _handler.startElement(qName, attList); 172 173 } 174 175 /** 176 * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String) 177 */ 178 public void startPrefixMapping(String prefix, String uri) throws SAXException { 179 if (_createNamespaceScope) { 180 namespacesStack.addNewNamespaceScope(); 181 _createNamespaceScope = false; 182 } 183 184 namespacesStack.addNamespace(prefix, uri); 185 } 186 }