Imported GNU Classpath 0.19 + gcj-import-20051115.
[official-gcc.git] / libjava / classpath / gnu / xml / aelfred2 / JAXPFactory.java
blob011ca3c6a3d95e62325ef6d35ed45488098accfa
1 /* JAXPFactory.java --
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)
9 any later version.
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., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
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
24 combination.
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;
55 /**
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
64 extends SAXParserFactory
67 private Hashtable flags = new Hashtable();
69 /**
70 * Constructs a factory which normally returns a non-validating
71 * parser.
73 public JAXPFactory()
77 public SAXParser newSAXParser()
78 throws ParserConfigurationException, SAXException
80 JaxpParser jaxp = new JaxpParser();
81 Enumeration e = flags.keys();
82 XMLReader parser = jaxp.getXMLReader();
84 parser.setFeature(SAXDriver.FEATURE + "namespaces",
85 isNamespaceAware());
86 parser.setFeature(SAXDriver.FEATURE + "validation",
87 isValidating());
88 // that makes SAX2 feature flags trump JAXP
90 while (e.hasMoreElements())
92 String uri = (String) e.nextElement();
93 Boolean value = (Boolean) flags.get(uri);
94 parser.setFeature(uri, value.booleanValue());
97 return jaxp;
100 // yes, this "feature transfer" mechanism doesn't play well
102 public void setFeature(String name, boolean value)
103 throws ParserConfigurationException, SAXNotRecognizedException,
104 SAXNotSupportedException
108 // force "early" detection of errors where possible
109 // (flags can't necessarily be set before parsing)
110 new JaxpParser().getXMLReader().setFeature(name, value);
112 flags.put(name, Boolean.valueOf(value));
114 catch (SAXNotRecognizedException e)
116 throw new SAXNotRecognizedException(name);
118 catch (SAXNotSupportedException e)
120 throw new SAXNotSupportedException(name);
122 catch (Exception e)
124 throw new ParserConfigurationException(e.getClass().getName()
125 + ": "
126 + e.getMessage());
130 public boolean getFeature(String name)
131 throws ParserConfigurationException, SAXNotRecognizedException,
132 SAXNotSupportedException
134 Boolean value = (Boolean) flags.get(name);
136 if (value != null)
138 return value.booleanValue();
140 else
144 return new JaxpParser().getXMLReader().getFeature(name);
146 catch (SAXNotRecognizedException e)
148 throw new SAXNotRecognizedException(name);
150 catch (SAXNotSupportedException e)
152 throw new SAXNotSupportedException(name);
154 catch (SAXException e)
156 throw new ParserConfigurationException(e.getClass().getName()
157 + ": "
158 + e.getMessage());
163 private static class JaxpParser
164 extends SAXParser
167 private XmlReader ae2 = new XmlReader();
168 private XMLReaderAdapter parser = null;
170 JaxpParser()
174 public void setProperty(String id, Object value)
175 throws SAXNotRecognizedException, SAXNotSupportedException
177 ae2.setProperty(id, value);
180 public Object getProperty(String id)
181 throws SAXNotRecognizedException, SAXNotSupportedException
183 return ae2.getProperty(id);
186 public Parser getParser()
187 throws SAXException
189 if (parser == null)
191 parser = new XMLReaderAdapter(ae2);
193 return parser;
196 public XMLReader getXMLReader ()
197 throws SAXException
199 return ae2;
202 public boolean isNamespaceAware()
206 return ae2.getFeature(SAXDriver.FEATURE + "namespaces");
208 catch (Exception e)
210 throw new Error();
214 public boolean isValidating()
218 return ae2.getFeature(SAXDriver.FEATURE + "validation");
220 catch (Exception e)
222 throw new Error();
226 // TODO isXIncludeAware()