1:
37:
38: package ;
39:
40: import ;
41: import ;
42: import ;
43: import ;
44: import ;
45: import ;
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56:
57:
63: public abstract class SchemaFactory
64: {
65: protected SchemaFactory()
66: {
67: }
68:
69:
75: public static final SchemaFactory newInstance(String schemaLanguage)
76: {
77: ClassLoader loader = Thread.currentThread().getContextClassLoader();
78: if (loader == null)
79: {
80: loader = SchemaFactory.class.getClassLoader();
81: }
82: final String factoryClassName = "javax.xml.validation.SchemaFactory";
83: String className = null;
84: int count = 0;
85: do
86: {
87: className = getFactoryClassName(loader, schemaLanguage, count++);
88: if (className != null)
89: {
90: try
91: {
92: Class t = (loader != null) ? loader.loadClass(className) :
93: Class.forName(className);
94: return (SchemaFactory) t.newInstance();
95: }
96: catch (Exception e)
97: {
98:
99:
100:
101: className = null;
102: }
103: }
104: }
105: while (className == null && count < 2);
106: try
107: {
108: String serviceKey = "/META-INF/services/" + factoryClassName;
109: InputStream in = (loader != null) ?
110: loader.getResourceAsStream(serviceKey) :
111: SchemaFactory.class.getResourceAsStream(serviceKey);
112: if (in != null)
113: {
114: BufferedReader r =
115: new BufferedReader(new InputStreamReader(in));
116: try
117: {
118: for (String line = r.readLine(); line != null;
119: line = r.readLine())
120: {
121: Class t = (loader != null) ? loader.loadClass(className) :
122: Class.forName(className);
123: SchemaFactory ret = (SchemaFactory) t.newInstance();
124: if (ret.isSchemaLanguageSupported(schemaLanguage))
125: return ret;
126: }
127: }
128: catch (Exception e)
129: {
130:
131: }
132: finally
133: {
134: r.close();
135: }
136: }
137: }
138: catch (IOException e)
139: {
140: }
141:
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);
147: }
148:
149: private static String getFactoryClassName(ClassLoader loader,
150: String schemaLanguage, int attempt)
151: {
152: final String factoryClassName = "javax.xml.validation.SchemaFactory";
153: final String propertyName = factoryClassName + ":" + schemaLanguage;
154: switch (attempt)
155: {
156: case 0:
157: return System.getProperty(propertyName);
158: case 1:
159: try
160: {
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();
166: props.load(in);
167: in.close();
168: return props.getProperty(propertyName);
169: }
170: catch (IOException e)
171: {
172: return null;
173: }
174: default:
175: return null;
176: }
177: }
178:
179:
184: public abstract boolean isSchemaLanguageSupported(String schemaLanguage);
185:
186: public boolean getFeature(String name)
187: throws SAXNotRecognizedException, SAXNotSupportedException
188: {
189: throw new SAXNotRecognizedException(name);
190: }
191:
192: public void setFeature(String name, boolean value)
193: throws SAXNotRecognizedException, SAXNotSupportedException
194: {
195: throw new SAXNotRecognizedException(name);
196: }
197:
198: public Object getProperty(String name)
199: throws SAXNotRecognizedException, SAXNotSupportedException
200: {
201: throw new SAXNotRecognizedException(name);
202: }
203:
204: public void setProperty(String name, Object value)
205: throws SAXNotRecognizedException, SAXNotSupportedException
206: {
207: throw new SAXNotRecognizedException(name);
208: }
209:
210: public abstract ErrorHandler getErrorHandler();
211:
212: public abstract void setErrorHandler(ErrorHandler errorHandler);
213:
214: public abstract LSResourceResolver getResourceResolver();
215:
216: public abstract void setResourceResolver(LSResourceResolver resourceResolver);
217:
218:
222: public Schema newSchema(Source schema)
223: throws SAXException
224: {
225: return newSchema(new Source[] { schema });
226: }
227:
228:
232: public Schema newSchema(File schema)
233: throws SAXException
234: {
235: return newSchema(new StreamSource(schema));
236: }
237:
238:
242: public Schema newSchema(URL schema)
243: throws SAXException
244: {
245: return newSchema(new StreamSource(schema.toString()));
246: }
247:
248:
254: public abstract Schema newSchema(Source[] schemata)
255: throws SAXException;
256:
257:
261: public abstract Schema newSchema()
262: throws SAXException;
263:
264: }