2 Copyright (C) 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.aelfred2;
40 import java.util.Enumeration;
41 import java.util.Hashtable;
43 import org.xml.sax.Parser;
44 import org.xml.sax.XMLReader;
45 import org.xml.sax.SAXException;
46 import org.xml.sax.SAXNotRecognizedException;
47 import org.xml.sax.SAXNotSupportedException;
48 import org.xml.sax.helpers.XMLReaderAdapter;
50 import javax.xml.parsers.ParserConfigurationException;
51 import javax.xml.parsers.SAXParser;
52 import javax.xml.parsers.SAXParserFactory;
56 * Configurable factory to create an Ælfred2 JAXP parser; required
57 * to bootstrap using JAXP. You should use SAX2 directly where possible,
58 * rather than through JAXP, since that gives you better control.
59 * This class would normally be configured as a platform default factory.
61 * @author David Brownell
63 public final class JAXPFactory extends SAXParserFactory
65 private Hashtable flags = new Hashtable ();
68 * Constructs a factory which normally returns a non-validating
71 public JAXPFactory () { }
73 public SAXParser newSAXParser ()
74 throws ParserConfigurationException, SAXException
76 JaxpParser jaxp = new JaxpParser ();
77 Enumeration e = flags.keys ();
78 XMLReader parser = jaxp.getXMLReader ();
81 SAXDriver.FEATURE + "namespaces",
84 SAXDriver.FEATURE + "validation",
86 // that makes SAX2 feature flags trump JAXP
88 while (e.hasMoreElements ()) {
89 String uri = (String) e.nextElement ();
90 Boolean value = (Boolean) flags.get (uri);
91 parser.setFeature (uri, value.booleanValue ());
97 // yes, this "feature transfer" mechanism doesn't play well
99 public void setFeature (String name, boolean value)
101 ParserConfigurationException,
102 SAXNotRecognizedException,
103 SAXNotSupportedException
106 // force "early" detection of errors where possible
107 // (flags can't necessarily be set before parsing)
108 new JaxpParser ().getXMLReader ().setFeature (name, value);
110 flags.put (name, new Boolean (value));
111 } catch (SAXNotRecognizedException e) {
112 throw new SAXNotRecognizedException (name);
113 } catch (SAXNotSupportedException e) {
114 throw new SAXNotSupportedException (name);
115 } catch (Exception e) {
116 throw new ParserConfigurationException (
117 e.getClass ().getName ()
123 public boolean getFeature (String name)
125 ParserConfigurationException,
126 SAXNotRecognizedException,
127 SAXNotSupportedException
129 Boolean value = (Boolean) flags.get (name);
132 return value.booleanValue ();
135 return new JaxpParser ().getXMLReader ().getFeature (name);
136 } catch (SAXNotRecognizedException e) {
137 throw new SAXNotRecognizedException (name);
138 } catch (SAXNotSupportedException e) {
139 throw new SAXNotSupportedException (name);
140 } catch (SAXException e) {
141 throw new ParserConfigurationException (
142 e.getClass ().getName ()
148 private static class JaxpParser extends SAXParser
150 private XmlReader ae2 = new XmlReader ();
151 private XMLReaderAdapter parser = null;
155 public void setProperty (String id, Object value)
156 throws SAXNotRecognizedException, SAXNotSupportedException
157 { ae2.setProperty (id, value); }
159 public Object getProperty (String id)
160 throws SAXNotRecognizedException, SAXNotSupportedException
161 { return ae2.getProperty (id); }
163 public Parser getParser ()
167 parser = new XMLReaderAdapter (ae2);
171 public XMLReader getXMLReader ()
175 public boolean isNamespaceAware ()
178 return ae2.getFeature (SAXDriver.FEATURE + "namespaces");
179 } catch (Exception e) {
184 public boolean isValidating ()
187 return ae2.getFeature (SAXDriver.FEATURE + "validation");
188 } catch (Exception e) {
193 // TODO isXIncludeAware()