1 /* SchemaFactory.java --
2 Copyright (C) 2004, 2005, 2006 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., 51 Franklin Street, Fifth Floor, 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 javax.xml.validation;
40 import java.io.BufferedReader;
42 import java.io.FileInputStream;
43 import java.io.InputStream;
44 import java.io.InputStreamReader;
45 import java.io.IOException;
47 import java.util.Properties;
48 import javax.xml.XMLConstants;
49 import javax.xml.transform.Source;
50 import javax.xml.transform.stream.StreamSource;
51 import org.w3c.dom.ls.LSResourceResolver;
52 import org.xml.sax.ErrorHandler;
53 import org.xml.sax.SAXException;
54 import org.xml.sax.SAXNotRecognizedException;
55 import org.xml.sax.SAXNotSupportedException;
58 * Factory for obtaining schemata.
60 * @author Chris Burdess (dog@gnu.org)
63 public abstract class SchemaFactory
65 protected SchemaFactory()
70 * Returns an implementation of <code>SchemaFactory</code> that supports
71 * the specified schema language.
72 * @param schemaLanguage the URI of a schema language (see
73 * <code>XMLConstants</code>)
75 public static final SchemaFactory newInstance(String schemaLanguage)
77 ClassLoader loader = Thread.currentThread().getContextClassLoader();
80 loader = SchemaFactory.class.getClassLoader();
82 final String factoryClassName = "javax.xml.validation.SchemaFactory";
83 String className = null;
87 className = getFactoryClassName(loader, schemaLanguage, count++);
88 if (className != null)
92 Class<?> t = (loader != null) ? loader.loadClass(className) :
93 Class.forName(className);
94 return (SchemaFactory) t.newInstance();
98 // Ignore any errors and continue algorithm.
99 // This method doesn't have a means of propagating
100 // class instantiation errors.
105 while (className == null && count < 2);
108 String serviceKey = "/META-INF/services/" + factoryClassName;
109 InputStream in = (loader != null) ?
110 loader.getResourceAsStream(serviceKey) :
111 SchemaFactory.class.getResourceAsStream(serviceKey);
115 new BufferedReader(new InputStreamReader(in));
118 for (String line = r.readLine(); line != null;
121 Class<?> t = (loader != null) ? loader.loadClass(className) :
122 Class.forName(className);
123 SchemaFactory ret = (SchemaFactory) t.newInstance();
124 if (ret.isSchemaLanguageSupported(schemaLanguage))
130 // Fall through. See above.
138 catch (IOException e)
141 // Default schema factories for Classpath
142 if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(schemaLanguage))
143 return new gnu.xml.validation.xmlschema.XMLSchemaSchemaFactory();
144 if (XMLConstants.RELAXNG_NS_URI.equals(schemaLanguage))
145 return new gnu.xml.validation.relaxng.RELAXNGSchemaFactory();
146 throw new IllegalArgumentException(schemaLanguage);
149 private static String getFactoryClassName(ClassLoader loader,
150 String schemaLanguage, int attempt)
152 final String factoryClassName = "javax.xml.validation.SchemaFactory";
153 final String propertyName = factoryClassName + ":" + schemaLanguage;
157 return System.getProperty(propertyName);
161 File file = new File(System.getProperty("java.home"));
162 file = new File(file, "lib");
163 file = new File(file, "jaxp.properties");
164 InputStream in = new FileInputStream(file);
165 Properties props = new Properties();
168 return props.getProperty(propertyName);
170 catch (IOException e)
180 * Indicates whether the specified schema language is supported.
181 * @param schemaLanguage the URI of a schema language (see
182 * <code>XMLConstants</code>)
184 public abstract boolean isSchemaLanguageSupported(String schemaLanguage);
186 public boolean getFeature(String name)
187 throws SAXNotRecognizedException, SAXNotSupportedException
189 throw new SAXNotRecognizedException(name);
192 public void setFeature(String name, boolean value)
193 throws SAXNotRecognizedException, SAXNotSupportedException
195 throw new SAXNotRecognizedException(name);
198 public Object getProperty(String name)
199 throws SAXNotRecognizedException, SAXNotSupportedException
201 throw new SAXNotRecognizedException(name);
204 public void setProperty(String name, Object value)
205 throws SAXNotRecognizedException, SAXNotSupportedException
207 throw new SAXNotRecognizedException(name);
210 public abstract ErrorHandler getErrorHandler();
212 public abstract void setErrorHandler(ErrorHandler errorHandler);
214 public abstract LSResourceResolver getResourceResolver();
216 public abstract void setResourceResolver(LSResourceResolver resourceResolver);
219 * Returns a schema based on the specified source resource.
220 * @param schema the source resource
222 public Schema newSchema(Source schema)
225 return newSchema(new Source[] { schema });
229 * Returns a schema based on the specified source file.
230 * @param schema the source resource
232 public Schema newSchema(File schema)
235 return newSchema(new StreamSource(schema));
239 * Returns a schema based on the specified URL.
240 * @param schema the source resource
242 public Schema newSchema(URL schema)
245 return newSchema(new StreamSource(schema.toString()));
249 * Parses the specified sources, and combine them into a single schema.
250 * The exact procedure and semantics of this depends on the schema
252 * @param schemata the schema resources to load
254 public abstract Schema newSchema(Source[] schemata)
258 * Creates a special schema.
259 * The exact semantics of this depends on the schema language.
261 public abstract Schema newSchema()