001    /*
002     * Copyright (c) 2006 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 fi.iki.hsivonen.verifierservlet;
024    
025    import java.util.Iterator;
026    import java.util.LinkedList;
027    import java.util.List;
028    
029    import org.xml.sax.Attributes;
030    import org.xml.sax.ContentHandler;
031    import org.xml.sax.Locator;
032    import org.xml.sax.SAXException;
033    
034    public class BufferingRootNamespaceSniffer implements ContentHandler {
035    
036        private ContentHandler ch = null;
037    
038        private Locator locator = null;
039        
040        private List namespaces = new LinkedList();
041        
042        private VerifierServletTransaction vst;
043        
044        public BufferingRootNamespaceSniffer(VerifierServletTransaction vst) {
045            super();
046            this.vst = vst;
047        }
048    
049        public void setContentHandler(ContentHandler contentHandler) throws SAXException {
050            this.ch = contentHandler;
051            ch.startDocument();
052            if (locator != null) {
053                ch.setDocumentLocator(locator);
054            }
055            for (Iterator iter = namespaces.iterator(); iter.hasNext();) {
056                String[] element = (String[]) iter.next();
057                ch.startPrefixMapping(element[0], element[1]);
058            }
059        }
060        
061        /**
062         * @see org.xml.sax.ContentHandler#characters(char[], int, int)
063         */
064        public void characters(char[] arg0, int arg1, int arg2) throws SAXException {
065            if (ch != null) {
066                ch.characters(arg0, arg1, arg2);
067            }
068        }
069    
070        /**
071         * @see org.xml.sax.ContentHandler#endDocument()
072         */
073        public void endDocument() throws SAXException {
074            if (ch != null) {
075                ch.endDocument();
076            }
077        }
078    
079        /**
080         * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
081         *      java.lang.String, java.lang.String)
082         */
083        public void endElement(String arg0, String arg1, String arg2)
084                throws SAXException {
085            if (ch != null) {
086                ch.endElement(arg0, arg1, arg2);
087            }
088        }
089    
090        /**
091         * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
092         */
093        public void endPrefixMapping(String arg0) throws SAXException {
094            if (ch != null) {
095                ch.endPrefixMapping(arg0);
096            }
097        }
098    
099        /**
100         * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
101         */
102        public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
103                throws SAXException {
104            if (ch != null) {
105                ch.ignorableWhitespace(arg0, arg1, arg2);
106            }
107        }
108    
109        /**
110         * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
111         *      java.lang.String)
112         */
113        public void processingInstruction(String arg0, String arg1)
114                throws SAXException {
115            if (ch != null) {
116                ch.processingInstruction(arg0, arg1);
117            }
118        }
119    
120        /**
121         * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
122         */
123        public void setDocumentLocator(Locator arg0) {
124            locator = arg0;
125        }
126    
127        /**
128         * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
129         */
130        public void skippedEntity(String arg0) throws SAXException {
131            if (ch != null) {
132                ch.skippedEntity(arg0);
133            }
134        }
135    
136        /**
137         * @see org.xml.sax.ContentHandler#startDocument()
138         */
139        public void startDocument() throws SAXException {
140    
141        }
142    
143        /**
144         * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
145         *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
146         */
147        public void startElement(String arg0, String arg1, String arg2,
148                Attributes arg3) throws SAXException {
149            if (ch != null) {
150                ch.startElement(arg0, arg1, arg2, arg3);
151            } else {
152                vst.rootNamespace(arg0, locator);
153                ch.startElement(arg0, arg1, arg2, arg3);
154            }
155        }
156    
157        /**
158         * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
159         *      java.lang.String)
160         */
161        public void startPrefixMapping(String arg0, String arg1)
162                throws SAXException {
163            if (ch != null) {
164                ch.startPrefixMapping(arg0, arg1);
165            } else {
166                String[] arr = new String[2];
167                arr[0] = arg0;
168                arr[1] = arg1;
169                namespaces.add(arr);
170            }
171        }
172    
173    }