Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.xml/share/classes/javax/xml/validation/SchemaFactory.java
40948 views
1
/*
2
* Copyright (c) 2003, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package javax.xml.validation;
27
28
import com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory;
29
import java.io.File;
30
import java.net.URL;
31
import javax.xml.transform.Source;
32
import javax.xml.transform.stream.StreamSource;
33
import jdk.xml.internal.SecuritySupport;
34
import org.w3c.dom.ls.LSResourceResolver;
35
import org.xml.sax.ErrorHandler;
36
import org.xml.sax.SAXException;
37
import org.xml.sax.SAXNotRecognizedException;
38
import org.xml.sax.SAXNotSupportedException;
39
import org.xml.sax.SAXParseException;
40
41
/**
42
* Factory that creates {@link Schema} objects. Entry-point to
43
* the validation API.
44
*
45
* <p>
46
* {@link SchemaFactory} is a schema compiler. It reads external
47
* representations of schemas and prepares them for validation.
48
*
49
* <p>
50
* The {@link SchemaFactory} class is not thread-safe. In other words,
51
* it is the application's responsibility to ensure that at most
52
* one thread is using a {@link SchemaFactory} object at any
53
* given moment. Implementations are encouraged to mark methods
54
* as {@code synchronized} to protect themselves from broken clients.
55
*
56
* <p>
57
* {@link SchemaFactory} is not re-entrant. While one of the
58
* {@code newSchema} methods is being invoked, applications
59
* may not attempt to recursively invoke the {@code newSchema} method,
60
* even from the same thread.
61
*
62
* <h2><a id="schemaLanguage"></a>Schema Language</h2>
63
* <p>
64
* This spec uses a namespace URI to designate a schema language.
65
* The following table shows the values defined by this specification.
66
* <p>
67
* To be compliant with the spec, the implementation
68
* is only required to support W3C XML Schema 1.0. However,
69
* if it chooses to support other schema languages listed here,
70
* it must conform to the relevant behaviors described in this spec.
71
*
72
* <p>
73
* Schema languages not listed here are expected to
74
* introduce their own URIs to represent themselves.
75
* The {@link SchemaFactory} class is capable of locating other
76
* implementations for other schema languages at run-time.
77
*
78
* <p>
79
* Note that because the XML DTD is strongly tied to the parsing process
80
* and has a significant effect on the parsing process, it is impossible
81
* to define the DTD validation as a process independent from parsing.
82
* For this reason, this specification does not define the semantics for
83
* the XML DTD. This doesn't prohibit implementors from implementing it
84
* in a way they see fit, but <em>users are warned that any DTD
85
* validation implemented on this interface necessarily deviate from
86
* the XML DTD semantics as defined in the XML 1.0</em>.
87
*
88
* <table class="striped">
89
* <caption>URIs for Supported Schema languages</caption>
90
* <thead>
91
* <tr>
92
* <th scope="col">value</th>
93
* <th scope="col">language</th>
94
* </tr>
95
* </thead>
96
* <tbody>
97
* <tr>
98
* <th scope="row">{@link javax.xml.XMLConstants#W3C_XML_SCHEMA_NS_URI} ("{@code http://www.w3.org/2001/XMLSchema}")</th>
99
* <td><a href="http://www.w3.org/TR/xmlschema-1">W3C XML Schema 1.0</a></td>
100
* </tr>
101
* <tr>
102
* <th scope="row">{@link javax.xml.XMLConstants#RELAXNG_NS_URI} ("{@code http://relaxng.org/ns/structure/1.0}")</th>
103
* <td><a href="http://www.relaxng.org/">RELAX NG 1.0</a></td>
104
* </tr>
105
* </tbody>
106
* </table>
107
*
108
* @author Kohsuke Kawaguchi
109
* @author Neeraj Bajaj
110
*
111
* @since 1.5
112
*/
113
public abstract class SchemaFactory {
114
115
/**
116
* Constructor for derived classes.
117
*
118
* <p>The constructor does nothing.
119
*
120
* <p>Derived classes must create {@link SchemaFactory} objects that have
121
* {@code null} {@link ErrorHandler} and
122
* {@code null} {@link LSResourceResolver}.
123
*/
124
protected SchemaFactory() {
125
}
126
127
/**
128
* Creates a new instance of the {@code SchemaFactory} builtin
129
* system-default implementation.
130
*
131
* @implSpec The {@code SchemaFactory} builtin
132
* system-default implementation is only required to support the
133
* <a href="http://www.w3.org/TR/xmlschema-1">W3C XML Schema 1.0</a>,
134
* but may support additional <a href="#schemaLanguage">schema languages</a>.
135
*
136
* @return A new instance of the {@code SchemaFactory} builtin
137
* system-default implementation.
138
*
139
* @since 9
140
*/
141
public static SchemaFactory newDefaultInstance() {
142
return new XMLSchemaFactory();
143
}
144
145
/**
146
* Obtains a new instance of a {@code SchemaFactory} that supports
147
* the specified schema language. This method uses the
148
* <a href="../../../module-summary.html#LookupMechanism">JAXP Lookup Mechanism</a>
149
* to determine and load the {@code SchemaFactory} implementation that supports
150
* the specified schema language.
151
*
152
* <h4>Tip for Trouble-shooting:</h4>
153
* <p>See {@link java.util.Properties#load(java.io.InputStream)} for
154
* exactly how a property file is parsed. In particular, colons ':'
155
* need to be escaped in a property file, so make sure schema language
156
* URIs are properly escaped in it. For example:
157
* <pre>
158
* http\://www.w3.org/2001/XMLSchema=org.acme.foo.XSSchemaFactory
159
* </pre>
160
*
161
* @param schemaLanguage
162
* Specifies the schema language which the returned
163
* SchemaFactory will understand. See
164
* <a href="#schemaLanguage">the list of available
165
* schema languages</a> for the possible values.
166
*
167
* @return New instance of a {@code SchemaFactory}
168
*
169
* @throws IllegalArgumentException
170
* If no implementation of the schema language is available.
171
* @throws NullPointerException
172
* If the {@code schemaLanguage} parameter is null.
173
* @throws SchemaFactoryConfigurationError
174
* If a configuration error is encountered.
175
*
176
* @see #newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader)
177
*/
178
public static SchemaFactory newInstance(String schemaLanguage) {
179
ClassLoader cl;
180
cl = SecuritySupport.getContextClassLoader();
181
182
if (cl == null) {
183
//cl = ClassLoader.getSystemClassLoader();
184
//use the current class loader
185
cl = SchemaFactory.class.getClassLoader();
186
}
187
188
SchemaFactory f = new SchemaFactoryFinder(cl).newFactory(schemaLanguage);
189
if (f == null) {
190
throw new IllegalArgumentException(
191
"No SchemaFactory"
192
+ " that implements the schema language specified by: " + schemaLanguage
193
+ " could be loaded");
194
}
195
return f;
196
}
197
198
/**
199
* Obtain a new instance of a {@code SchemaFactory} from class name. {@code SchemaFactory}
200
* is returned if specified factory class name supports the specified schema language.
201
* This function is useful when there are multiple providers in the classpath.
202
* It gives more control to the application as it can specify which provider
203
* should be loaded.
204
*
205
* <h4>Tip for Trouble-shooting</h4>
206
* <p>Setting the {@code jaxp.debug} system property will cause
207
* this method to print a lot of debug messages
208
* to {@code System.err} about what it is doing and where it is looking at.
209
*
210
* <p> If you have problems try:
211
* <pre>
212
* java -Djaxp.debug=1 YourProgram ....
213
* </pre>
214
*
215
* @param schemaLanguage Specifies the schema language which the returned
216
* {@code SchemaFactory} will understand. See
217
* <a href="#schemaLanguage">the list of available
218
* schema languages</a> for the possible values.
219
*
220
* @param factoryClassName fully qualified factory class name that provides implementation of {@code javax.xml.validation.SchemaFactory}.
221
*
222
* @param classLoader {@code ClassLoader} used to load the factory class. If {@code null}
223
* current {@code Thread}'s context classLoader is used to load the factory class.
224
*
225
* @return New instance of a {@code SchemaFactory}
226
*
227
* @throws IllegalArgumentException
228
* if {@code factoryClassName} is {@code null}, or
229
* the factory class cannot be loaded, instantiated or doesn't
230
* support the schema language specified in {@code schemLanguage}
231
* parameter.
232
*
233
* @throws NullPointerException
234
* If the {@code schemaLanguage} parameter is null.
235
*
236
* @see #newInstance(String schemaLanguage)
237
*
238
* @since 1.6
239
*/
240
public static SchemaFactory newInstance(String schemaLanguage, String factoryClassName, ClassLoader classLoader){
241
ClassLoader cl = classLoader;
242
243
if (cl == null) {
244
cl = SecuritySupport.getContextClassLoader();
245
}
246
247
SchemaFactory f = new SchemaFactoryFinder(cl).createInstance(factoryClassName);
248
if (f == null) {
249
throw new IllegalArgumentException(
250
"Factory " + factoryClassName
251
+ " could not be loaded to implement the schema language specified by: " + schemaLanguage);
252
}
253
//if this factory supports the given schemalanguage return this factory else thrown exception
254
if(f.isSchemaLanguageSupported(schemaLanguage)){
255
return f;
256
}else{
257
throw new IllegalArgumentException(
258
"Factory " + f.getClass().getName()
259
+ " does not implement the schema language specified by: " + schemaLanguage);
260
}
261
262
}
263
264
/**
265
* Is specified schema supported by this {@code SchemaFactory}?
266
*
267
* @param schemaLanguage Specifies the schema language which the returned {@code SchemaFactory} will understand.
268
* {@code schemaLanguage} must specify a <a href="#schemaLanguage">valid</a> schema language.
269
*
270
* @return {@code true} if {@code SchemaFactory} supports {@code schemaLanguage}, else {@code false}.
271
*
272
* @throws NullPointerException If {@code schemaLanguage} is {@code null}.
273
* @throws IllegalArgumentException If {@code schemaLanguage.length() == 0}
274
* or {@code schemaLanguage} does not specify a <a href="#schemaLanguage">valid</a> schema language.
275
*/
276
public abstract boolean isSchemaLanguageSupported(String schemaLanguage);
277
278
/**
279
* Look up the value of a feature flag.
280
*
281
* <p>The feature name is any fully-qualified URI. It is
282
* possible for a {@link SchemaFactory} to recognize a feature name but
283
* temporarily be unable to return its value.
284
*
285
* <p>Implementors are free (and encouraged) to invent their own features,
286
* using names built on their own URIs.
287
*
288
* @param name The feature name, which is a non-null fully-qualified URI.
289
*
290
* @return The current value of the feature (true or false).
291
*
292
* @throws SAXNotRecognizedException If the feature
293
* value can't be assigned or retrieved.
294
* @throws SAXNotSupportedException When the
295
* {@link SchemaFactory} recognizes the feature name but
296
* cannot determine its value at this time.
297
* @throws NullPointerException If {@code name} is {@code null}.
298
*
299
* @see #setFeature(String, boolean)
300
*/
301
public boolean getFeature(String name)
302
throws SAXNotRecognizedException, SAXNotSupportedException {
303
304
if (name == null) {
305
throw new NullPointerException("the name parameter is null");
306
}
307
throw new SAXNotRecognizedException(name);
308
}
309
310
/**
311
* Set a feature for this {@code SchemaFactory},
312
* {@link Schema}s created by this factory, and by extension,
313
* {@link Validator}s and {@link ValidatorHandler}s created by
314
* those {@link Schema}s.
315
*
316
* <p>Implementors and developers should pay particular attention
317
* to how the special {@link Schema} object returned by {@link
318
* #newSchema()} is processed. In some cases, for example, when the
319
* {@code SchemaFactory} and the class actually loading the
320
* schema come from different implementations, it may not be possible
321
* for {@code SchemaFactory} features to be inherited automatically.
322
* Developers should
323
* make sure that features, such as secure processing, are explicitly
324
* set in both places.
325
*
326
* <p>The feature name is any fully-qualified URI. It is
327
* possible for a {@link SchemaFactory} to expose a feature value but
328
* to be unable to change the current value.
329
*
330
* <p>All implementations are required to support the {@link javax.xml.XMLConstants#FEATURE_SECURE_PROCESSING} feature.
331
* When the feature is:
332
* <ul>
333
* <li>
334
* {@code true}: the implementation will limit XML processing to conform to implementation limits.
335
* Examples include entity expansion limits and XML Schema constructs that would consume large amounts of resources.
336
* If XML processing is limited for security reasons, it will be reported via a call to the registered
337
* {@link ErrorHandler#fatalError(SAXParseException exception)}.
338
* See {@link #setErrorHandler(ErrorHandler errorHandler)}.
339
* </li>
340
* <li>
341
* {@code false}: the implementation will processing XML according to the XML specifications without
342
* regard to possible implementation limits.
343
* </li>
344
* </ul>
345
*
346
* @param name The feature name, which is a non-null fully-qualified URI.
347
* @param value The requested value of the feature (true or false).
348
*
349
* @throws SAXNotRecognizedException If the feature
350
* value can't be assigned or retrieved.
351
* @throws SAXNotSupportedException When the
352
* {@link SchemaFactory} recognizes the feature name but
353
* cannot set the requested value.
354
* @throws NullPointerException If {@code name} is {@code null}.
355
*
356
* @see #getFeature(String)
357
*/
358
public void setFeature(String name, boolean value)
359
throws SAXNotRecognizedException, SAXNotSupportedException {
360
361
if (name == null) {
362
throw new NullPointerException("the name parameter is null");
363
}
364
throw new SAXNotRecognizedException(name);
365
}
366
367
/**
368
* Set the value of a property.
369
*
370
* <p>The property name is any fully-qualified URI. It is
371
* possible for a {@link SchemaFactory} to recognize a property name but
372
* to be unable to change the current value.
373
*
374
* <p>
375
* All implementations that implement JAXP 1.5 or newer are required to
376
* support the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} and
377
* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} properties.
378
*
379
* <ul>
380
* <li>
381
* <p>Access to external DTDs in Schema files is restricted to the protocols
382
* specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.
383
* If access is denied during the creation of new Schema due to the restriction
384
* of this property, {@link org.xml.sax.SAXException} will be thrown by the
385
* {@link #newSchema(Source)} or {@link #newSchema(File)}
386
* or {@link #newSchema(URL)} or {@link #newSchema(Source[])} method.
387
*
388
* <p>Access to external DTDs in xml source files is restricted to the protocols
389
* specified by the {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_DTD} property.
390
* If access is denied during validation due to the restriction
391
* of this property, {@link org.xml.sax.SAXException} will be thrown by the
392
* {@link javax.xml.validation.Validator#validate(Source)} or
393
* {@link javax.xml.validation.Validator#validate(Source, Result)} method.
394
*
395
* <p>Access to external reference set by the schemaLocation attribute is
396
* restricted to the protocols specified by the
397
* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property.
398
* If access is denied during validation due to the restriction of this property,
399
* {@link org.xml.sax.SAXException} will be thrown by the
400
* {@link javax.xml.validation.Validator#validate(Source)} or
401
* {@link javax.xml.validation.Validator#validate(Source, Result)} method.
402
*
403
* <p>Access to external reference set by the Import
404
* and Include element is restricted to the protocols specified by the
405
* {@link javax.xml.XMLConstants#ACCESS_EXTERNAL_SCHEMA} property.
406
* If access is denied during the creation of new Schema due to the restriction
407
* of this property, {@link org.xml.sax.SAXException} will be thrown by the
408
* {@link #newSchema(Source)} or {@link #newSchema(File)}
409
* or {@link #newSchema(URL)} or {@link #newSchema(Source[])} method.
410
* </li>
411
* </ul>
412
*
413
* @param name The property name, which is a non-null fully-qualified URI.
414
* @param object The requested value for the property.
415
*
416
* @throws SAXNotRecognizedException If the property
417
* value can't be assigned or retrieved.
418
* @throws SAXNotSupportedException When the
419
* {@link SchemaFactory} recognizes the property name but
420
* cannot set the requested value.
421
* @throws NullPointerException If {@code name} is {@code null}.
422
*/
423
public void setProperty(String name, Object object)
424
throws SAXNotRecognizedException, SAXNotSupportedException {
425
426
if (name == null) {
427
throw new NullPointerException("the name parameter is null");
428
}
429
throw new SAXNotRecognizedException(name);
430
}
431
432
/**
433
* Look up the value of a property.
434
*
435
* <p>The property name is any fully-qualified URI. It is
436
* possible for a {@link SchemaFactory} to recognize a property name but
437
* temporarily be unable to return its value.
438
*
439
* <p>{@link SchemaFactory}s are not required to recognize any specific
440
* property names.
441
*
442
* <p>Implementors are free (and encouraged) to invent their own properties,
443
* using names built on their own URIs.
444
*
445
* @param name The property name, which is a non-null fully-qualified URI.
446
*
447
* @return The current value of the property.
448
*
449
* @throws SAXNotRecognizedException If the property
450
* value can't be assigned or retrieved.
451
* @throws SAXNotSupportedException When the
452
* XMLReader recognizes the property name but
453
* cannot determine its value at this time.
454
* @throws NullPointerException If {@code name} is {@code null}.
455
*
456
* @see #setProperty(String, Object)
457
*/
458
public Object getProperty(String name)
459
throws SAXNotRecognizedException, SAXNotSupportedException {
460
461
if (name == null) {
462
throw new NullPointerException("the name parameter is null");
463
}
464
throw new SAXNotRecognizedException(name);
465
}
466
467
/**
468
* Sets the {@link ErrorHandler} to receive errors encountered
469
* during the {@code newSchema} method invocation.
470
*
471
* <p>
472
* Error handler can be used to customize the error handling process
473
* during schema parsing. When an {@link ErrorHandler} is set,
474
* errors found during the parsing of schemas will be first sent
475
* to the {@link ErrorHandler}.
476
*
477
* <p>
478
* The error handler can abort the parsing of a schema immediately
479
* by throwing {@link SAXException} from the handler. Or for example
480
* it can print an error to the screen and try to continue the
481
* processing by returning normally from the {@link ErrorHandler}
482
*
483
* <p>
484
* If any {@link Throwable} (or instances of its derived classes)
485
* is thrown from an {@link ErrorHandler},
486
* the caller of the {@code newSchema} method will be thrown
487
* the same {@link Throwable} object.
488
*
489
* <p>
490
* {@link SchemaFactory} is not allowed to
491
* throw {@link SAXException} without first reporting it to
492
* {@link ErrorHandler}.
493
*
494
* <p>
495
* Applications can call this method even during a {@link Schema}
496
* is being parsed.
497
*
498
* <p>
499
* When the {@link ErrorHandler} is null, the implementation will
500
* behave as if the following {@link ErrorHandler} is set:
501
* <pre>
502
* class DraconianErrorHandler implements {@link ErrorHandler} {
503
* public void fatalError( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {
504
* throw e;
505
* }
506
* public void error( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {
507
* throw e;
508
* }
509
* public void warning( {@link org.xml.sax.SAXParseException} e ) throws {@link SAXException} {
510
* // noop
511
* }
512
* }
513
* </pre>
514
*
515
* <p>
516
* When a new {@link SchemaFactory} object is created, initially
517
* this field is set to null. This field will <em>NOT</em> be
518
* inherited to {@link Schema}s, {@link Validator}s, or
519
* {@link ValidatorHandler}s that are created from this {@link SchemaFactory}.
520
*
521
* @param errorHandler A new error handler to be set.
522
* This parameter can be {@code null}.
523
*/
524
public abstract void setErrorHandler(ErrorHandler errorHandler);
525
526
/**
527
* Gets the current {@link ErrorHandler} set to this {@link SchemaFactory}.
528
*
529
* @return
530
* This method returns the object that was last set through
531
* the {@link #setErrorHandler(ErrorHandler)} method, or null
532
* if that method has never been called since this {@link SchemaFactory}
533
* has created.
534
*
535
* @see #setErrorHandler(ErrorHandler)
536
*/
537
public abstract ErrorHandler getErrorHandler();
538
539
/**
540
* Sets the {@link LSResourceResolver} to customize
541
* resource resolution when parsing schemas.
542
*
543
* <p>
544
* {@link SchemaFactory} uses a {@link LSResourceResolver}
545
* when it needs to locate external resources while parsing schemas,
546
* although exactly what constitutes "locating external resources" is
547
* up to each schema language. For example, for W3C XML Schema,
548
* this includes files {@code <include>}d or {@code <import>}ed,
549
* and DTD referenced from schema files, etc.
550
*
551
* <p>
552
* Applications can call this method even during a {@link Schema}
553
* is being parsed.
554
*
555
* <p>
556
* When the {@link LSResourceResolver} is null, the implementation will
557
* behave as if the following {@link LSResourceResolver} is set:
558
* <pre>
559
* class DumbDOMResourceResolver implements {@link LSResourceResolver} {
560
* public {@link org.w3c.dom.ls.LSInput} resolveResource(
561
* String publicId, String systemId, String baseURI) {
562
*
563
* return null; // always return null
564
* }
565
* }
566
* </pre>
567
*
568
* <p>
569
* If a {@link LSResourceResolver} throws a {@link RuntimeException}
570
* (or instances of its derived classes),
571
* then the {@link SchemaFactory} will abort the parsing and
572
* the caller of the {@code newSchema} method will receive
573
* the same {@link RuntimeException}.
574
*
575
* <p>
576
* When a new {@link SchemaFactory} object is created, initially
577
* this field is set to null. This field will <em>NOT</em> be
578
* inherited to {@link Schema}s, {@link Validator}s, or
579
* {@link ValidatorHandler}s that are created from this {@link SchemaFactory}.
580
*
581
* @param resourceResolver
582
* A new resource resolver to be set. This parameter can be null.
583
*/
584
public abstract void setResourceResolver(LSResourceResolver resourceResolver);
585
586
/**
587
* Gets the current {@link LSResourceResolver} set to this {@link SchemaFactory}.
588
*
589
* @return
590
* This method returns the object that was last set through
591
* the {@link #setResourceResolver(LSResourceResolver)} method, or null
592
* if that method has never been called since this {@link SchemaFactory}
593
* has created.
594
*
595
* @see #setErrorHandler(ErrorHandler)
596
*/
597
public abstract LSResourceResolver getResourceResolver();
598
599
/**
600
* Parses the specified source as a schema and returns it as a schema.
601
*
602
* <p>This is a convenience method for {@link #newSchema(Source[] schemas)}.
603
*
604
* @param schema Source that represents a schema.
605
*
606
* @return New {@code Schema} from parsing {@code schema}.
607
*
608
* @throws SAXException If a SAX error occurs during parsing.
609
* @throws NullPointerException if {@code schema} is null.
610
*/
611
public Schema newSchema(Source schema) throws SAXException {
612
return newSchema(new Source[]{schema});
613
}
614
615
/**
616
* Parses the specified {@code File} as a schema and returns it as a {@code Schema}.
617
*
618
* <p>This is a convenience method for {@link #newSchema(Source schema)}.
619
*
620
* @param schema File that represents a schema.
621
*
622
* @return New {@code Schema} from parsing {@code schema}.
623
*
624
* @throws SAXException If a SAX error occurs during parsing.
625
* @throws NullPointerException if {@code schema} is null.
626
*/
627
public Schema newSchema(File schema) throws SAXException {
628
return newSchema(new StreamSource(schema));
629
}
630
631
/**
632
* Parses the specified {@code URL} as a schema and returns it as a {@code Schema}.
633
*
634
* <p>This is a convenience method for {@link #newSchema(Source schema)}.
635
*
636
* @param schema {@code URL} that represents a schema.
637
*
638
* @return New {@code Schema} from parsing {@code schema}.
639
*
640
* @throws SAXException If a SAX error occurs during parsing.
641
* @throws NullPointerException if {@code schema} is null.
642
*/
643
public Schema newSchema(URL schema) throws SAXException {
644
return newSchema(new StreamSource(schema.toExternalForm()));
645
}
646
647
/**
648
* Parses the specified source(s) as a schema and returns it as a schema.
649
*
650
* <p>
651
* The callee will read all the {@link Source}s and combine them into a
652
* single schema. The exact semantics of the combination depends on the schema
653
* language that this {@link SchemaFactory} object is created for.
654
*
655
* <p>
656
* When an {@link ErrorHandler} is set, the callee will report all the errors
657
* found in sources to the handler. If the handler throws an exception, it will
658
* abort the schema compilation and the same exception will be thrown from
659
* this method. Also, after an error is reported to a handler, the callee is allowed
660
* to abort the further processing by throwing it. If an error handler is not set,
661
* the callee will throw the first error it finds in the sources.
662
*
663
* <h4>W3C XML Schema 1.0</h4>
664
* <p>
665
* The resulting schema contains components from the specified sources.
666
* The same result would be achieved if all these sources were
667
* imported, using appropriate values for schemaLocation and namespace,
668
* into a single schema document with a different targetNamespace
669
* and no components of its own, if the import elements were given
670
* in the same order as the sources. Section 4.2.3 of the XML Schema
671
* recommendation describes the options processors have in this
672
* regard. While a processor should be consistent in its treatment of
673
* JAXP schema sources and XML Schema imports, the behaviour between
674
* JAXP-compliant parsers may vary; in particular, parsers may choose
675
* to ignore all but the first {@code <import>} for a given namespace,
676
* regardless of information provided in schemaLocation.
677
*
678
* <p>
679
* If the parsed set of schemas includes error(s) as
680
* specified in the section 5.1 of the XML Schema spec, then
681
* the error must be reported to the {@link ErrorHandler}.
682
*
683
* <h4>RELAX NG</h4>
684
*
685
* <p>For RELAX NG, this method must throw {@link UnsupportedOperationException}
686
* if {@code schemas.length!=1}.
687
*
688
*
689
* @param schemas
690
* inputs to be parsed. {@link SchemaFactory} is required
691
* to recognize {@link javax.xml.transform.sax.SAXSource},
692
* {@link StreamSource},
693
* {@link javax.xml.transform.stax.StAXSource},
694
* and {@link javax.xml.transform.dom.DOMSource}.
695
* Input schemas must be XML documents or
696
* XML elements and must not be null. For backwards compatibility,
697
* the results of passing anything other than
698
* a document or element are implementation-dependent.
699
* Implementations must either recognize and process the input
700
* or thrown an IllegalArgumentException.
701
*
702
* @return
703
* Always return a non-null valid {@link Schema} object.
704
* Note that when an error has been reported, there is no
705
* guarantee that the returned {@link Schema} object is
706
* meaningful.
707
*
708
* @throws SAXException
709
* If an error is found during processing the specified inputs.
710
* When an {@link ErrorHandler} is set, errors are reported to
711
* there first. See {@link #setErrorHandler(ErrorHandler)}.
712
* @throws NullPointerException
713
* If the {@code schemas} parameter itself is null or
714
* any item in the array is null.
715
* @throws IllegalArgumentException
716
* If any item in the array is not recognized by this method.
717
* @throws UnsupportedOperationException
718
* If the schema language doesn't support this operation.
719
*/
720
public abstract Schema newSchema(Source[] schemas) throws SAXException;
721
722
/**
723
* Creates a special {@link Schema} object.
724
*
725
* <p>The exact semantics of the returned {@link Schema} object
726
* depend on the schema language for which this {@link SchemaFactory}
727
* is created.
728
*
729
* <p>Also, implementations are allowed to use implementation-specific
730
* property/feature to alter the semantics of this method.
731
*
732
* <p>Implementors and developers should pay particular attention
733
* to how the features set on this {@link SchemaFactory} are
734
* processed by this special {@link Schema}.
735
* In some cases, for example, when the
736
* {@link SchemaFactory} and the class actually loading the
737
* schema come from different implementations, it may not be possible
738
* for {@link SchemaFactory} features to be inherited automatically.
739
* Developers should
740
* make sure that features, such as secure processing, are explicitly
741
* set in both places.
742
*
743
* <h4>W3C XML Schema 1.0</h4>
744
* <p>
745
* For XML Schema, this method creates a {@link Schema} object that
746
* performs validation by using location hints specified in documents.
747
*
748
* <p>
749
* The returned {@link Schema} object assumes that if documents
750
* refer to the same URL in the schema location hints,
751
* they will always resolve to the same schema document. This
752
* asusmption allows implementations to reuse parsed results of
753
* schema documents so that multiple validations against the same
754
* schema will run faster.
755
*
756
* <p>
757
* Note that the use of schema location hints introduces a
758
* vulnerability to denial-of-service attacks.
759
*
760
*
761
* <h4>RELAX NG</h4>
762
* <p>
763
* RELAX NG does not support this operation.
764
*
765
* @return
766
* Always return non-null valid {@link Schema} object.
767
*
768
* @throws UnsupportedOperationException
769
* If this operation is not supported by the callee.
770
* @throws SAXException
771
* If this operation is supported but failed for some reason.
772
*/
773
public abstract Schema newSchema() throws SAXException;
774
}
775
776