public class XMLSchema extends Document
accept(document)
, which returns true if the document is
valid, and false otherwise;, or to call validate(document)
,
which succeeds silently, but raises a SemanticError exception if the
document is invalid (fails to conform to the grammar).
The internal constraints imposed by XMSchema are designed to ensure that the root Element is named "schema" and that it declares itself part of the W3C XML Schema namespace. It may use the prefixes "xs:" or "xsd:" and declares whether these are to be used by default in all Elements, or Attributes, in the schema document. It ensures that any declared target namespace matches the default namespace. It also ensures that every top-level definition in the schema has a name. The XMLSchema class is so-named with the "XML-" prefix, to allow for other kinds of schema to be added in future, such as RelaxNG.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.String,ElementRule> |
grammars
The possibly-multiple roots of the grammar trees represented by this
XMLSchema.
|
private static Doctype |
schemaDoctype
The non-normative W3C Doctype specification for an XML schema.
|
Constructor and Description |
---|
XMLSchema()
Creates a default empty XMLSchema with the standard XML Declaration.
|
XMLSchema(Document document)
Creates an XMLSchema from an existing Document.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(Document document)
Reports whether this XMLSchema accepts a given Document as valid.
|
boolean |
accept(Element element)
Reports whether this XMLSchema accepts a root Element as valid.
|
private void |
addRootAttributes(ElementRule rule)
Adds extra permissible AttributeRules to each top-level ElementRule.
|
private void |
checkElementNames(Element root)
Checks the names of the elements in this schema.
|
private void |
checkNamespaces(Element root)
Checks this XMLSchema for valid use of XML namespaces.
|
XMLSchema |
clone()
Clones this XMLSchema node.
|
void |
compileGrammar()
Compiles the grammar represented by this XMLSchema.
|
private void |
error(java.lang.String message)
Raises a SemanticError with this XMLSchema as the context.
|
ElementRule |
getGrammar(java.lang.String identifier)
Accesses the top ElementRule for a named Element whose grammar is
defined by this XMLSchema.
|
XMLSchema |
setDeclaration(Declaration declaration)
Sets the XML declaration associated with this XMLSchema.
|
XMLSchema |
setDoctype(Doctype doctype)
Sets the optional XML Doctype associated with this XMLSchema.
|
XMLSchema |
setEncoding(java.lang.String encoding)
Sets the character encoding associated with this Document.
|
XMLSchema |
setRootElement(Element root)
Sets the root Element of this XMLSchema.
|
XMLSchema |
setVersion(java.lang.String version)
Sets the XML version associated with this Document.
|
void |
validate(Document document)
Validates a given Document, raising a SemanticError if it is invalid.
|
void |
validate(Element element)
Validates a root Element, raising a SemanticError if it is invalid.
|
addContent, getContents, getDeclaration, getDoctype, getEncoding, getRootElement, getType, getVersion, hasContents, isStandalone, isWellFormed, removeContent, setParent
addAttribute, addContent, descendants, descendants, detach, getAttributes, getComment, getContent, getContent, getContent, getContents, getDocument, getIdentifier, getParent, getText, iterator, iterator, removeContent, removeContents, toString
private static final Doctype schemaDoctype
private java.util.Map<java.lang.String,ElementRule> grammars
public XMLSchema()
public XMLSchema(Document document) throws SemanticError
document
- the Document.SemanticError
- if the document does not contain an XML schema.private void error(java.lang.String message) throws SemanticError
message
- the desired error message.SemanticError
- containing this message.public XMLSchema clone()
public XMLSchema setDeclaration(Declaration declaration) throws SemanticError
setDeclaration
in class Document
declaration
- the new XML declaration.SemanticError
- if an invalid XML declaration is supplied.public XMLSchema setDoctype(Doctype doctype)
setDoctype
in class Document
doctype
- the Doctype node.public XMLSchema setRootElement(Element root) throws SemanticError
setRootElement
in class Document
root
- the root Element.SemanticError
- if the root is not a valid schema root.public XMLSchema setVersion(java.lang.String version)
setVersion
in class Document
version
- the XML version, typically "1.0".public XMLSchema setEncoding(java.lang.String encoding)
setEncoding
in class Document
encoding
- the character encoding, for example "UTF-8".public ElementRule getGrammar(java.lang.String identifier) throws SemanticError
identifier
- the identifier of the root Element.SemanticError
- if no such ElementRule exists.private void addRootAttributes(ElementRule rule)
rule
- the XSD schemaRoot node.public void compileGrammar() throws SemanticError
SemanticError
- if this Schema is inconsistent.public boolean accept(Document document) throws SemanticError
document
- a Document.SemanticError
- if no grammar is defined for the root Element.public boolean accept(Element element) throws SemanticError
element
- an Element.SemanticError
- if no global rule exists for the Element.public void validate(Document document) throws SemanticError
document
- a Document.SemanticError
- if the Document is invalid, with respect to this
schema, or no global rule exists for the root Element.public void validate(Element element) throws SemanticError
element
- an Element.SemanticError
- if the element is invalid, or no global rule
exists for it.private void checkElementNames(Element root) throws SemanticError
root
- the root Element.SemanticError
- if any condition is violated.private void checkNamespaces(Element root)
root
- the root schema Element.