| 1 |
package com.thaiopensource.relaxng.jaxp; |
| 2 |
|
| 3 |
import com.thaiopensource.datatype.xsd.DatatypeLibraryFactoryImpl; |
| 4 |
import com.thaiopensource.datatype.xsd.regex.java.RegexEngineImpl; |
| 5 |
import com.thaiopensource.relaxng.parse.IllegalSchemaException; |
| 6 |
import com.thaiopensource.relaxng.parse.Parseable; |
| 7 |
import com.thaiopensource.relaxng.pattern.SchemaBuilderImpl; |
| 8 |
import com.thaiopensource.relaxng.pattern.SchemaPatternBuilder; |
| 9 |
import com.thaiopensource.relaxng.pattern.Pattern; |
| 10 |
import com.thaiopensource.relaxng.pattern.NameClass; |
| 11 |
import com.thaiopensource.relaxng.pattern.CommentListImpl; |
| 12 |
import com.thaiopensource.relaxng.pattern.AnnotationsImpl; |
| 13 |
import com.thaiopensource.resolver.Resolver; |
| 14 |
import com.thaiopensource.resolver.xml.ls.LS; |
| 15 |
import com.thaiopensource.resolver.xml.sax.SAXResolver; |
| 16 |
import com.thaiopensource.validation.Schema2; |
| 17 |
import com.thaiopensource.validation.SchemaFactory2; |
| 18 |
import com.thaiopensource.xml.sax.DraconianErrorHandler; |
| 19 |
import com.thaiopensource.util.VoidValue; |
| 20 |
import org.relaxng.datatype.DatatypeLibraryFactory; |
| 21 |
import org.w3c.dom.ls.LSResourceResolver; |
| 22 |
import org.xml.sax.ErrorHandler; |
| 23 |
import org.xml.sax.SAXException; |
| 24 |
import org.xml.sax.SAXNotRecognizedException; |
| 25 |
import org.xml.sax.SAXNotSupportedException; |
| 26 |
import org.xml.sax.SAXParseException; |
| 27 |
import org.xml.sax.Locator; |
| 28 |
|
| 29 |
import javax.xml.transform.sax.SAXSource; |
| 30 |
import java.io.IOException; |
| 31 |
|
| 32 |
/** |
| 33 |
* A SchemaFactory that supports RELAX NG. |
| 34 |
* This class is abstract: it has two concrete subclasses, one that supports the XML syntax |
| 35 |
* and one that supports the compact syntax. |
| 36 |
* @see XMLSyntaxSchemaFactory |
| 37 |
* @see CompactSyntaxSchemaFactory |
| 38 |
*/ |
| 39 |
public abstract class SchemaFactoryImpl extends SchemaFactory2 { |
| 40 |
private DatatypeLibraryFactory datatypeLibraryFactory = null; |
| 41 |
/* If this is true, then logically datatypeLibraryFactory is an instance of DatatypeLibraryLoader, |
| 42 |
but we create it lazily, so that we don't need to create it if the user specifies their own. */ |
| 43 |
private boolean defaultDatatypeLibraryFactory = true; |
| 44 |
|
| 45 |
/** |
| 46 |
* The name of the property that can be used to specify a DatatypeLibraryFactory. |
| 47 |
* The value of the property must implement org.relaxng.datatype.DatatypeLibraryFactory. |
| 48 |
* By default, a datatype library factory that supports XML Schema Datatypes is used. |
| 49 |
* If the value of this property is set to null, then only the built-in datatypes will be |
| 50 |
* supported. By default, datatype libraries will not be discovered dynamically; in order |
| 51 |
* to enable this, the value can be set to an instance of |
| 52 |
* org.relaxng.datatype.helpers.DatatypeLibraryLoader. |
| 53 |
* @see DatatypeLibraryFactory |
| 54 |
* @see org.relaxng.datatype.helpers.DatatypeLibraryLoader |
| 55 |
* @see #setProperty |
| 56 |
* @see #getProperty |
| 57 |
*/ |
| 58 |
static final public String PROPERTY_DATATYPE_LIBRARY_FACTORY = "http://relaxng.org/properties/datatype-library-factory"; |
| 59 |
|
| 60 |
protected SchemaFactoryImpl() { |
| 61 |
} |
| 62 |
|
| 63 |
public Schema2 newSchema(SAXSource source) throws SAXException { |
| 64 |
Resolver resolver = null; |
| 65 |
LSResourceResolver resourceResolver = getResourceResolver(); |
| 66 |
if (resourceResolver != null) |
| 67 |
resolver = LS.createResolver(resourceResolver); |
| 68 |
SAXResolver saxResolver = new SAXResolver(resolver); |
| 69 |
ErrorHandler eh = getErrorHandler(); |
| 70 |
if (eh == null) |
| 71 |
eh = new DraconianErrorHandler(); |
| 72 |
Parseable<Pattern, NameClass, Locator, VoidValue, CommentListImpl, AnnotationsImpl> parseable |
| 73 |
= createParseable(source, saxResolver, eh); |
| 74 |
SchemaPatternBuilder spb = new SchemaPatternBuilder(); |
| 75 |
try { |
| 76 |
return new SchemaImpl(this, spb, SchemaBuilderImpl.parse(parseable, eh, getDatatypeLibraryFactory(), spb, false)); |
| 77 |
} |
| 78 |
catch (IOException io) { |
| 79 |
// this is a truly bizarre API; why can't we just throw the IOException |
| 80 |
SAXParseException e = new SAXParseException(io.getMessage(), null, io); |
| 81 |
eh.fatalError(e); |
| 82 |
throw e; |
| 83 |
} |
| 84 |
catch (IllegalSchemaException e) { |
| 85 |
// we have already reported something for this error, so don't give it to the error handler |
| 86 |
throw new SAXException("invalid schema"); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
public void setProperty(String name, Object object) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 91 |
if (PROPERTY_DATATYPE_LIBRARY_FACTORY.equals(name)) { |
| 92 |
if (object instanceof DatatypeLibraryFactory) { |
| 93 |
datatypeLibraryFactory = (DatatypeLibraryFactory)object; |
| 94 |
defaultDatatypeLibraryFactory = false; |
| 95 |
} |
| 96 |
else |
| 97 |
throw new SAXNotSupportedException("value of \"" + PROPERTY_DATATYPE_LIBRARY_FACTORY + |
| 98 |
"\" property does not implement org.relaxng.datatype.DatatypeLibraryFactory"); |
| 99 |
} |
| 100 |
else |
| 101 |
super.setProperty(name, object); |
| 102 |
} |
| 103 |
|
| 104 |
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 105 |
if (PROPERTY_DATATYPE_LIBRARY_FACTORY.equals(name)) |
| 106 |
return getDatatypeLibraryFactory(); |
| 107 |
return super.getProperty(name); |
| 108 |
} |
| 109 |
|
| 110 |
private DatatypeLibraryFactory getDatatypeLibraryFactory() { |
| 111 |
if (defaultDatatypeLibraryFactory) { |
| 112 |
datatypeLibraryFactory = new DatatypeLibraryFactoryImpl(new RegexEngineImpl()); |
| 113 |
defaultDatatypeLibraryFactory = false; |
| 114 |
} |
| 115 |
return datatypeLibraryFactory; |
| 116 |
} |
| 117 |
|
| 118 |
abstract protected Parseable<Pattern, NameClass, Locator, VoidValue, CommentListImpl, AnnotationsImpl> |
| 119 |
createParseable(SAXSource source, SAXResolver resolver, ErrorHandler eh) throws SAXException; |
| 120 |
} |