package com.thaiopensource.relaxng; import com.thaiopensource.validate.auto.AutoSchemaReader; import com.thaiopensource.util.PropertyMapBuilder; import com.thaiopensource.validate.Flag; import com.thaiopensource.validate.IncorrectSchemaException; import com.thaiopensource.validate.SchemaReader; import com.thaiopensource.xml.sax.XMLReaderCreator; import com.thaiopensource.validate.Schema; import com.thaiopensource.validate.ValidateProperty; import com.thaiopensource.validate.prop.rng.RngProperty; import com.thaiopensource.validate.rng.CompactSchemaReader; import org.relaxng.datatype.DatatypeLibraryFactory; import org.xml.sax.ErrorHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import java.io.IOException; /** * A factory for RELAX NG schemas. The factory creates Schema objects from their * XML representation. * * A single SchemaFactory is not safe for concurrent * access by multiple threads; it must be accessed by at most one thread at a time. * Schemas can be created concurrently by using a distinct SchemaFactory for each * thread. However, the Schema objects created are safe for concurrent * access by multiple threads. * * @author James Clark */ public class SchemaFactory { private PropertyMapBuilder properties = new PropertyMapBuilder(); private boolean compactSyntax = false; private SchemaReader autoSchemaLanguage = new AutoSchemaReader(); /** * Constructs a schema factory. */ public SchemaFactory() { } /** * Creates a schema by parsing an XML document. A non-null XMLReaderCreator must be specified * with setXMLReaderCreator before calling createSchema. The ErrorHandler * is allowed to be null. The DatatypeLibraryFactory is allowed to be null. * *

Normally, if a schema cannot be created, createSchema will throw * a IncorrectSchemaException; however, * before doing so, one or more errors will be reported using the ErrorHandler if it is non-null. If the * ErrorHandler throws a SAXException, then createSchema will pass this * through rather than throwing a IncorrectSchemaException. Similarly, if XMLReader.parse * throws a SAXException or IOException, then createSchema will pass * this through rather than throwing a IncorrectSchemaException. Thus, if an error handler * is specified that reports errors to the user, there is no need to report any additional message to the * user if createSchema throws IncorrectSchemaException. * * @param in the InputSource containing the XML document to be parsed; * must not be null * @return the Schema constructed from the XML document; * never null. * * @throws IOException if an I/O error occurs * @throws SAXException if there is an XML parsing error and the XMLReader or ErrorHandler * throws a SAXException * @throws com.thaiopensource.validate.IncorrectSchemaException if the XML document was not a correct RELAX NG schema * @throws NullPointerException if the current XMLReaderCreator is null */ public Schema createSchema(InputSource in) throws IOException, SAXException, IncorrectSchemaException { SchemaReader r = compactSyntax ? CompactSchemaReader.getInstance() : autoSchemaLanguage; return r.createSchema(in, properties.toPropertyMap()); } /** * Specifies the XMLReaderCreator to be used for creating XMLReaders for parsing * the XML document. Because of include and externalRef elements, * parsing a single RELAX NG may require the creation of multiple more than one XMLReader. * A non-null XMLReaderCreator must be specified before calling createSchema. * * @param xrc the XMLReaderCreator to be used for parsing the XML document containing * the schema; may be null * @see #getXMLReaderCreator */ public void setXMLReaderCreator(XMLReaderCreator xrc) { properties.put(ValidateProperty.XML_READER_CREATOR, xrc); } /** * Returns the current XMLReaderCreator as specified by setXMLReaderCreator. * If XMLReaderCreator has never been called, then getXMLReaderCreator * returns null. * * @return the XMLReaderCreator that will be used for parsing the XML document containing * the schema; may be null * * @see #setXMLReaderCreator */ public XMLReaderCreator getXMLReaderCreator() { return (XMLReaderCreator)properties.get(ValidateProperty.XML_READER_CREATOR); } /** * Specifies the ErrorHandler to be used for reporting errors while creating the schema. * This does not affect the error handler used for validation. * * @param eh the ErrorHandler to be used for reporting errors while creating the schema; * may be null. * @see #getErrorHandler */ public void setErrorHandler(ErrorHandler eh) { properties.put(ValidateProperty.ERROR_HANDLER, eh); } /** * Returns the ErrorHandler that will be used for reporting errors while creating the * schema. If setErrorHandler has not been called for this SchemaFactory, * then getErrorHandler returns null. * * @return the ErrorHandler to be used for reporting errors while creating the schema; * may be null. * @see #setErrorHandler */ public ErrorHandler getErrorHandler() { return (ErrorHandler)properties.get(ValidateProperty.ERROR_HANDLER); } /** * Specifies the DatatypeLibraryFactory to be used for handling datatypes in the schema. * This also determines how datatypes are handled during validation. If null is * specified then only the builtin datatypes will be supported. * * @param dlf the DatatypeLibraryFactory to be used for handling datatypes in the schema * @see #getDatatypeLibraryFactory */ public void setDatatypeLibraryFactory(DatatypeLibraryFactory dlf) { properties.put(RngProperty.DATATYPE_LIBRARY_FACTORY, dlf); } /** * Returns the DatatypeLibraryFactory that will be used for handling datatypes in the * schema. If setDatatypeLibraryFactory has not been called for this SchemaFactory, * then getDatatypeLibraryFactory returns null. * * @return the DatatypeLibraryFactory to be used for handling datatypes in the schema; * may be null. * @see #setDatatypeLibraryFactory */ public DatatypeLibraryFactory getDatatypeLibraryFactory() { return (DatatypeLibraryFactory)properties.get(RngProperty.DATATYPE_LIBRARY_FACTORY); } /** * Specifies whether to perform checking of ID/IDREF/IDREFS attributes in accordance with * RELAX NG DTD Compatibility. * * @param checkIdIdref true if ID/IDREF/IDREFS checking should be performed; * false otherwise * * @see #getCheckIdIdref * @see RELAX NG DTD Compatibility */ public void setCheckIdIdref(boolean checkIdIdref) { properties.put(RngProperty.CHECK_ID_IDREF, checkIdIdref ? Flag.PRESENT : null); } /** * Indicates whether ID/IDREF/IDREFS attributes will be checked in accordance RELAX NG DTD * Compatibility. If setCheckIdIdref has not been called for this SchemaFactory, * then getCheckIdref will return false. * * @return true if ID/IDREF/IDREFS attributes will be checked; * false otherwise. * * @see #setCheckIdIdref * @see RELAX NG DTD Compatibility */ public boolean getCheckIdIdref() { return properties.contains(RngProperty.CHECK_ID_IDREF); } /** * Specifies whether to use the compact syntax to parse the RELAX NG schema rather than the normal XML syntax. * * @param compactSyntax true if the compact syntax should be used; false * if the XML syntax should be used * @see #getCompactSyntax */ public void setCompactSyntax(boolean compactSyntax) { this.compactSyntax = compactSyntax; } /** * Indicates whether the compact syntax will be used to parse the RELAX NG schema rather than * the normal XML syntax. * * @return true if the compact syntax will be used; false if the XML * syntax will be used */ public boolean getCompactSyntax() { return compactSyntax; } public void setFeasible(boolean feasible) { properties.put(RngProperty.FEASIBLE, feasible ? Flag.PRESENT : null); } public boolean getFeasible() { return properties.contains(RngProperty.FEASIBLE); } }