aboutsummaryrefslogtreecommitdiff
path: root/src/org/ccil/cowan/tagsoup/jaxp/SAX1ParserAdapter.java
blob: 883a3e779536e6a8fb97afef4300fafef6fb193f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// This file is part of TagSoup and is Copyright 2002-2008 by John Cowan.
//
// TagSoup is licensed under the Apache License,
// Version 2.0.  You may obtain a copy of this license at
// http://www.apache.org/licenses/LICENSE-2.0 .  You may also have
// additional legal rights not granted by this license.
//
// TagSoup is distributed in the hope that it will be useful, but
// unless required by applicable law or agreed to in writing, TagSoup
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
// OF ANY KIND, either express or implied; not even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

package org.ccil.cowan.tagsoup.jaxp;

import java.io.IOException;
import javax.xml.parsers.*;

import org.xml.sax.*;

/**
 * This is a simpler adapter class that allows using SAX1 interface on top
 * of basic SAX2 implementation, such as TagSoup.
 *
 * @author Tatu Saloranta (cowtowncoder@yahoo.com)
 * @deprecated
 */
public class SAX1ParserAdapter
    implements org.xml.sax.Parser
{
    final XMLReader xmlReader;

    public SAX1ParserAdapter(XMLReader xr)
    {
        xmlReader = xr;
    }

    // Sax1 API impl

    public void parse(InputSource source)
        throws SAXException
    {
        try {
            xmlReader.parse(source);
        } catch (IOException ioe) {
            throw new SAXException(ioe);
        }
    }

    public void parse(String systemId)
        throws SAXException
    {
        try {
            xmlReader.parse(systemId);
        } catch (IOException ioe) {
            throw new SAXException(ioe);
        }
    }

    /**
     * @deprecated
     */
    public void setDocumentHandler(DocumentHandler h)
    {
        xmlReader.setContentHandler(new DocHandlerWrapper(h));
    }

    public void setDTDHandler(DTDHandler h)
    {
        xmlReader.setDTDHandler(h);
    }

    public void setEntityResolver(EntityResolver r)
    {
        xmlReader.setEntityResolver(r);
    }

    public void setErrorHandler(ErrorHandler h)
    {
        xmlReader.setErrorHandler(h);
    }

    public void setLocale(java.util.Locale locale) 
        throws SAXException
    {
        /* I have no idea what this is supposed to do... so let's
         * throw an exception
         */
        throw new SAXNotSupportedException("TagSoup does not implement setLocale() method");
    }

    // Helper classes:

    /**
     * We need another helper class to deal with differences between
     * Sax2 handler (content handler), and Sax1 handler (document handler)
     * @deprecated
     */
    final static class DocHandlerWrapper
        implements ContentHandler
    {
        final DocumentHandler docHandler;

        final AttributesWrapper mAttrWrapper = new AttributesWrapper();

        /**
         * @deprecated
         */
        DocHandlerWrapper(DocumentHandler h)
        {
            docHandler = h;
        }

        public void characters(char[] ch, int start, int length)
            throws SAXException
        {
            docHandler.characters(ch, start, length);
        }

        public void endDocument()
            throws SAXException
        {
            docHandler.endDocument();
        }

        public void endElement(String uri, String localName, String qName)
            throws SAXException
        {
            if (qName == null) {
                qName = localName;
            }
            docHandler.endElement(qName);
        }

        public void endPrefixMapping(String prefix)
        {
            // no equivalent in SAX1, ignore
        }

        public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException
        {
            docHandler.ignorableWhitespace(ch, start, length);
        }

        public void processingInstruction(String target, String data)
            throws SAXException
        {
            docHandler.processingInstruction(target, data);
        }

        public void setDocumentLocator(Locator locator)
        {
            docHandler.setDocumentLocator(locator);
        }

        public void skippedEntity(String name)
        {
            // no equivalent in SAX1, ignore
        }

        public void startDocument()
            throws SAXException
        {
            docHandler.startDocument();
        }

        public void startElement(String uri, String localName, String qName,
                                 Attributes attrs)
            throws SAXException
        {
            if (qName == null) {
                qName = localName;
            }
            // Also, need to wrap Attributes to look like AttributeLost
            mAttrWrapper.setAttributes(attrs);
            docHandler.startElement(qName, mAttrWrapper);
        }

        public void startPrefixMapping(String prefix, String uri)
        {
            // no equivalent in SAX1, ignore
        }
    }

    /**
     * And one more helper to deal with attribute access differences
     * @deprecated
     */ 
    final static class AttributesWrapper
        implements AttributeList
    {
        Attributes attrs;

        public AttributesWrapper() { }

        public void setAttributes(Attributes a) {
            attrs = a;
        }

        public int getLength()
        {
            return attrs.getLength();
        }

        public String getName(int i)
        {
            String n = attrs.getQName(i);
            return (n == null) ? attrs.getLocalName(i) : n;
        }

        public String getType(int i)
        {
            return attrs.getType(i);
        }

        public String getType(String name)
        {
            return attrs.getType(name);
        }

        public String getValue(int i)
        {
            return attrs.getValue(i);
        }

        public String getValue(String name)     
        {
            return attrs.getValue(name);
        }
    }
}