001    /*
002     * Copyright (c) 2007 Henri Sivonen
003     *
004     * Permission is hereby granted, free of charge, to any person obtaining a 
005     * copy of this software and associated documentation files (the "Software"), 
006     * to deal in the Software without restriction, including without limitation 
007     * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
008     * and/or sell copies of the Software, and to permit persons to whom the 
009     * Software is furnished to do so, subject to the following conditions:
010     *
011     * The above copyright notice and this permission notice shall be included in 
012     * all copies or substantial portions of the Software.
013     *
014     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
015     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
016     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
017     * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
018     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
019     * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
020     * DEALINGS IN THE SOFTWARE.
021     */
022    
023    package nu.validator.htmlparser.tools;
024    
025    import org.xml.sax.Attributes;
026    import org.xml.sax.ContentHandler;
027    import org.xml.sax.Locator;
028    import org.xml.sax.SAXException;
029    
030    import org.xml.sax.helpers.AttributesImpl;
031    
032    /**
033     * Quick and dirty hack to work around Xalan xmlns weirdness.
034     * 
035     * @version $Id: XmlnsDropper.java 150 2007-08-16 19:21:25Z hsivonen $
036     * @author hsivonen
037     */
038    class XmlnsDropper implements ContentHandler {
039    
040        private final ContentHandler delegate;
041    
042        /**
043         * @param delegate
044         */
045        public XmlnsDropper(final ContentHandler delegate) {
046            this.delegate = delegate;
047        }
048    
049        /**
050         * @param ch
051         * @param start
052         * @param length
053         * @throws SAXException
054         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
055         */
056        public void characters(char[] ch, int start, int length) throws SAXException {
057            delegate.characters(ch, start, length);
058        }
059    
060        /**
061         * @throws SAXException
062         * @see org.xml.sax.ContentHandler#endDocument()
063         */
064        public void endDocument() throws SAXException {
065            delegate.endDocument();
066        }
067    
068        /**
069         * @param uri
070         * @param localName
071         * @param qName
072         * @throws SAXException
073         * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
074         */
075        public void endElement(String uri, String localName, String qName) throws SAXException {
076            delegate.endElement(uri, localName, qName);
077        }
078    
079        /**
080         * @param prefix
081         * @throws SAXException
082         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
083         */
084        public void endPrefixMapping(String prefix) throws SAXException {
085            delegate.endPrefixMapping(prefix);
086        }
087    
088        /**
089         * @param ch
090         * @param start
091         * @param length
092         * @throws SAXException
093         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
094         */
095        public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
096            delegate.ignorableWhitespace(ch, start, length);
097        }
098    
099        /**
100         * @param target
101         * @param data
102         * @throws SAXException
103         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String, java.lang.String)
104         */
105        public void processingInstruction(String target, String data) throws SAXException {
106            delegate.processingInstruction(target, data);
107        }
108    
109        /**
110         * @param locator
111         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
112         */
113        public void setDocumentLocator(Locator locator) {
114            delegate.setDocumentLocator(locator);
115        }
116    
117        /**
118         * @param name
119         * @throws SAXException
120         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
121         */
122        public void skippedEntity(String name) throws SAXException {
123            delegate.skippedEntity(name);
124        }
125    
126        /**
127         * @throws SAXException
128         * @see org.xml.sax.ContentHandler#startDocument()
129         */
130        public void startDocument() throws SAXException {
131            delegate.startDocument();
132        }
133    
134        /**
135         * @param uri
136         * @param localName
137         * @param qName
138         * @param atts
139         * @throws SAXException
140         * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
141         */
142        public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
143            AttributesImpl ai = new AttributesImpl();
144            for (int i = 0; i < atts.getLength(); i++) {
145                String u = atts.getURI(i);
146                String t = atts.getType(i);
147                String v = atts.getValue(i);
148                String n = atts.getLocalName(i);
149                String q = atts.getQName(i);
150                if (q != null) {
151                    if ("xmlns".equals(q) || q.startsWith("xmlns:")) {
152                        continue;
153                    }
154                }
155                ai.addAttribute(u, n, q, t, v); 
156            }
157            delegate.startElement(uri, localName, qName, ai);
158        }
159    
160        /**
161         * @param prefix
162         * @param uri
163         * @throws SAXException
164         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String, java.lang.String)
165         */
166        public void startPrefixMapping(String prefix, String uri) throws SAXException {
167            delegate.startPrefixMapping(prefix, uri);
168        }
169        
170    }