1// This file is part of TagSoup and is Copyright 2002-2008 by John Cowan.
2//
3// TagSoup is licensed under the Apache License,
4// Version 2.0.  You may obtain a copy of this license at
5// http://www.apache.org/licenses/LICENSE-2.0 .  You may also have
6// additional legal rights not granted by this license.
7//
8// TagSoup is distributed in the hope that it will be useful, but
9// unless required by applicable law or agreed to in writing, TagSoup
10// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
11// OF ANY KIND, either express or implied; not even the implied warranty
12// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14package org.ccil.cowan.tagsoup.jaxp;
15
16import java.io.IOException;
17import javax.xml.parsers.*;
18
19import org.xml.sax.*;
20
21/**
22 * This is a simpler adapter class that allows using SAX1 interface on top
23 * of basic SAX2 implementation, such as TagSoup.
24 *
25 * @author Tatu Saloranta (cowtowncoder@yahoo.com)
26 * @deprecated
27 */
28public class SAX1ParserAdapter
29    implements org.xml.sax.Parser
30{
31    final XMLReader xmlReader;
32
33    public SAX1ParserAdapter(XMLReader xr)
34    {
35        xmlReader = xr;
36    }
37
38    // Sax1 API impl
39
40    public void parse(InputSource source)
41        throws SAXException
42    {
43        try {
44            xmlReader.parse(source);
45        } catch (IOException ioe) {
46            throw new SAXException(ioe);
47        }
48    }
49
50    public void parse(String systemId)
51        throws SAXException
52    {
53        try {
54            xmlReader.parse(systemId);
55        } catch (IOException ioe) {
56            throw new SAXException(ioe);
57        }
58    }
59
60    /**
61     * @deprecated
62     */
63    public void setDocumentHandler(DocumentHandler h)
64    {
65        xmlReader.setContentHandler(new DocHandlerWrapper(h));
66    }
67
68    public void setDTDHandler(DTDHandler h)
69    {
70        xmlReader.setDTDHandler(h);
71    }
72
73    public void setEntityResolver(EntityResolver r)
74    {
75        xmlReader.setEntityResolver(r);
76    }
77
78    public void setErrorHandler(ErrorHandler h)
79    {
80        xmlReader.setErrorHandler(h);
81    }
82
83    public void setLocale(java.util.Locale locale)
84        throws SAXException
85    {
86        /* I have no idea what this is supposed to do... so let's
87         * throw an exception
88         */
89        throw new SAXNotSupportedException("TagSoup does not implement setLocale() method");
90    }
91
92    // Helper classes:
93
94    /**
95     * We need another helper class to deal with differences between
96     * Sax2 handler (content handler), and Sax1 handler (document handler)
97     * @deprecated
98     */
99    final static class DocHandlerWrapper
100        implements ContentHandler
101    {
102        final DocumentHandler docHandler;
103
104        final AttributesWrapper mAttrWrapper = new AttributesWrapper();
105
106        /**
107         * @deprecated
108         */
109        DocHandlerWrapper(DocumentHandler h)
110        {
111            docHandler = h;
112        }
113
114        public void characters(char[] ch, int start, int length)
115            throws SAXException
116        {
117            docHandler.characters(ch, start, length);
118        }
119
120        public void endDocument()
121            throws SAXException
122        {
123            docHandler.endDocument();
124        }
125
126        public void endElement(String uri, String localName, String qName)
127            throws SAXException
128        {
129            if (qName == null) {
130                qName = localName;
131            }
132            docHandler.endElement(qName);
133        }
134
135        public void endPrefixMapping(String prefix)
136        {
137            // no equivalent in SAX1, ignore
138        }
139
140        public void ignorableWhitespace(char[] ch, int start, int length)
141            throws SAXException
142        {
143            docHandler.ignorableWhitespace(ch, start, length);
144        }
145
146        public void processingInstruction(String target, String data)
147            throws SAXException
148        {
149            docHandler.processingInstruction(target, data);
150        }
151
152        public void setDocumentLocator(Locator locator)
153        {
154            docHandler.setDocumentLocator(locator);
155        }
156
157        public void skippedEntity(String name)
158        {
159            // no equivalent in SAX1, ignore
160        }
161
162        public void startDocument()
163            throws SAXException
164        {
165            docHandler.startDocument();
166        }
167
168        public void startElement(String uri, String localName, String qName,
169                                 Attributes attrs)
170            throws SAXException
171        {
172            if (qName == null) {
173                qName = localName;
174            }
175            // Also, need to wrap Attributes to look like AttributeLost
176            mAttrWrapper.setAttributes(attrs);
177            docHandler.startElement(qName, mAttrWrapper);
178        }
179
180        public void startPrefixMapping(String prefix, String uri)
181        {
182            // no equivalent in SAX1, ignore
183        }
184    }
185
186    /**
187     * And one more helper to deal with attribute access differences
188     * @deprecated
189     */
190    final static class AttributesWrapper
191        implements AttributeList
192    {
193        Attributes attrs;
194
195        public AttributesWrapper() { }
196
197        public void setAttributes(Attributes a) {
198            attrs = a;
199        }
200
201        public int getLength()
202        {
203            return attrs.getLength();
204        }
205
206        public String getName(int i)
207        {
208            String n = attrs.getQName(i);
209            return (n == null) ? attrs.getLocalName(i) : n;
210        }
211
212        public String getType(int i)
213        {
214            return attrs.getType(i);
215        }
216
217        public String getType(String name)
218        {
219            return attrs.getType(name);
220        }
221
222        public String getValue(int i)
223        {
224            return attrs.getValue(i);
225        }
226
227        public String getValue(String name)
228        {
229            return attrs.getValue(name);
230        }
231    }
232}
233