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.*;
17import java.util.*;
18import javax.xml.parsers.*;
19
20import org.xml.sax.*;
21
22import org.ccil.cowan.tagsoup.Parser;
23
24/**
25 * This is a simple implementation of JAXP {@link SAXParser},
26 * to allow easier integration of TagSoup with the default JDK
27 * xml processing stack.
28 *
29 * @author Tatu Saloranta (cowtowncoder@yahoo.com)
30 */
31public class SAXParserImpl
32    extends SAXParser
33{
34    final org.ccil.cowan.tagsoup.Parser parser;
35
36    protected SAXParserImpl() // used by factory, for prototypes
37    {
38        super();
39        parser = new org.ccil.cowan.tagsoup.Parser();
40    }
41
42    public static SAXParserImpl newInstance(Map features)
43        throws SAXException
44    {
45        SAXParserImpl parser = new SAXParserImpl();
46        if (features != null) {
47            Iterator it = features.entrySet().iterator();
48            while (it.hasNext()) {
49                Map.Entry entry = (Map.Entry) it.next();
50                parser.setFeature((String) entry.getKey(), ((Boolean) entry.getValue()).booleanValue());
51            }
52        }
53        return parser;
54    }
55
56    // // // JAXP API implementation:
57
58    /**
59     * To support SAX1 interface, we'll need to use an adapter.
60     * @deprecated
61     */
62    public org.xml.sax.Parser getParser()
63        throws SAXException
64    {
65        return new SAX1ParserAdapter(parser);
66    }
67
68    public XMLReader getXMLReader() { return parser; }
69
70    public boolean isNamespaceAware()
71    {
72        try {
73            return parser.getFeature(Parser.namespacesFeature);
74        } catch (SAXException sex) { // should never happen... so:
75            throw new RuntimeException(sex.getMessage());
76        }
77    }
78
79    public boolean isValidating()
80    {
81        try {
82            return parser.getFeature(Parser.validationFeature);
83        } catch (SAXException sex) { // should never happen... so:
84            throw new RuntimeException(sex.getMessage());
85        }
86    }
87
88    public void setProperty(String name, Object value)
89        throws SAXNotRecognizedException, SAXNotSupportedException
90    {
91        parser.setProperty(name, value);
92    }
93
94    public Object getProperty(String name)
95        throws SAXNotRecognizedException, SAXNotSupportedException
96    {
97        return parser.getProperty(name);
98    }
99
100    // // // Additional convenience methods
101
102    public void setFeature(String name, boolean value)
103        throws SAXNotRecognizedException, SAXNotSupportedException
104    {
105        parser.setFeature(name, value);
106    }
107
108    public boolean getFeature(String name)
109        throws SAXNotRecognizedException, SAXNotSupportedException
110    {
111        return parser.getFeature(name);
112    }
113}
114