DOMValidatorHelperpublic final class DOMValidatorHelper extends Object implements ValidatorHelper, EntityStateA validator helper for DOMSource s. |
Fields Summary |
---|
private static final int | CHUNK_SIZEChunk size (1024). | private static final int | CHUNK_MASKChunk mask (CHUNK_SIZE - 1). | private static final String | ERROR_REPORTERProperty identifier: error reporter. | private static final String | NAMESPACE_CONTEXTProperty identifier: namespace context. | private static final String | SCHEMA_VALIDATORProperty identifier: XML Schema validator. | private static final String | SYMBOL_TABLEProperty identifier: symbol table. | private static final String | VALIDATION_MANAGERProperty identifier: validation manager. | private XMLErrorReporter | fErrorReporterError reporter. | private NamespaceSupport | fNamespaceContextThe namespace context of this document: stores namespaces in scope. | private DOMNamespaceContext | fDOMNamespaceContextThe namespace context of the DOMSource, includes context from ancestor nodes. | private XMLSchemaValidator | fSchemaValidatorSchema validator. | private SymbolTable | fSymbolTableSymbol table | private ValidationManager | fValidationManagerValidation manager. | private XMLSchemaValidatorComponentManager | fComponentManagerComponent manager. | private final SimpleLocator | fXMLLocatorSimple Locator. | private DOMDocumentHandler | fDOMValidatorHandlerDOM document handler. | private final DOMResultAugmentor | fDOMResultAugmentorDOM result augmentor. | private final DOMResultBuilder | fDOMResultBuilderDOM result builder. | private NamedNodeMap | fEntitiesMap for tracking unparsed entities. | private char[] | fCharBufferArray for holding character data. | private Node | fRootRoot node. | private Node | fCurrentElementCurrent element. | final QName | fElementQNameFields for start element, end element and characters. | final QName | fAttributeQName | final XMLAttributesImpl | fAttributes | final XMLString | fTempString |
Constructors Summary |
---|
public DOMValidatorHelper(XMLSchemaValidatorComponentManager componentManager)
fComponentManager = componentManager;
fErrorReporter = (XMLErrorReporter) fComponentManager.getProperty(ERROR_REPORTER);
fNamespaceContext = (NamespaceSupport) fComponentManager.getProperty(NAMESPACE_CONTEXT);
fSchemaValidator = (XMLSchemaValidator) fComponentManager.getProperty(SCHEMA_VALIDATOR);
fSymbolTable = (SymbolTable) fComponentManager.getProperty(SYMBOL_TABLE);
fValidationManager = (ValidationManager) fComponentManager.getProperty(VALIDATION_MANAGER);
|
Methods Summary |
---|
private void | beginNode(org.w3c.dom.Node node)Do processing for the start of a node.
switch (node.getNodeType()) {
case Node.ELEMENT_NODE:
fCurrentElement = node;
// push namespace context
fNamespaceContext.pushContext();
// start element
fillQName(fElementQName, node);
processAttributes(node.getAttributes());
fSchemaValidator.startElement(fElementQName, fAttributes, null);
break;
case Node.TEXT_NODE:
if (fDOMValidatorHandler != null) {
fDOMValidatorHandler.setIgnoringCharacters(true);
sendCharactersToValidator(node.getNodeValue());
fDOMValidatorHandler.setIgnoringCharacters(false);
fDOMValidatorHandler.characters((Text) node);
}
else {
sendCharactersToValidator(node.getNodeValue());
}
break;
case Node.CDATA_SECTION_NODE:
if (fDOMValidatorHandler != null) {
fDOMValidatorHandler.setIgnoringCharacters(true);
fSchemaValidator.startCDATA(null);
sendCharactersToValidator(node.getNodeValue());
fSchemaValidator.endCDATA(null);
fDOMValidatorHandler.setIgnoringCharacters(false);
fDOMValidatorHandler.cdata((CDATASection) node);
}
else {
fSchemaValidator.startCDATA(null);
sendCharactersToValidator(node.getNodeValue());
fSchemaValidator.endCDATA(null);
}
break;
case Node.PROCESSING_INSTRUCTION_NODE:
/**
* The validator does nothing with processing instructions so bypass it.
* Send the ProcessingInstruction node directly to the result builder.
*/
if (fDOMValidatorHandler != null) {
fDOMValidatorHandler.processingInstruction((ProcessingInstruction) node);
}
break;
case Node.COMMENT_NODE:
/**
* The validator does nothing with comments so bypass it.
* Send the Comment node directly to the result builder.
*/
if (fDOMValidatorHandler != null) {
fDOMValidatorHandler.comment((Comment) node);
}
break;
case Node.DOCUMENT_TYPE_NODE:
/**
* Send the DocumentType node directly to the result builder.
*/
if (fDOMValidatorHandler != null) {
fDOMValidatorHandler.doctypeDecl((DocumentType) node);
}
break;
default: // Ignore other node types.
break;
}
| private void | fillQName(com.sun.org.apache.xerces.internal.xni.QName toFill, org.w3c.dom.Node node)
final String prefix = node.getPrefix();
final String localName = node.getLocalName();
final String rawName = node.getNodeName();
final String namespace = node.getNamespaceURI();
toFill.uri = (namespace != null && namespace.length() > 0) ? fSymbolTable.addSymbol(namespace) : null;
toFill.rawname = (rawName != null) ? fSymbolTable.addSymbol(rawName) : XMLSymbols.EMPTY_STRING;
// Is this a DOM level1 document?
if (localName == null) {
int k = rawName.indexOf(':");
if (k > 0) {
toFill.prefix = fSymbolTable.addSymbol(rawName.substring(0, k));
toFill.localpart = fSymbolTable.addSymbol(rawName.substring(k + 1));
}
else {
toFill.prefix = XMLSymbols.EMPTY_STRING;
toFill.localpart = toFill.rawname;
}
}
else {
toFill.prefix = (prefix != null) ? fSymbolTable.addSymbol(prefix) : XMLSymbols.EMPTY_STRING;
toFill.localpart = (localName != null) ? fSymbolTable.addSymbol(localName) : XMLSymbols.EMPTY_STRING;
}
| private void | finishNode(org.w3c.dom.Node node)Do processing for the end of a node.
if (node.getNodeType() == Node.ELEMENT_NODE) {
fCurrentElement = node;
// end element
fillQName(fElementQName, node);
fSchemaValidator.endElement(fElementQName, null);
// pop namespace context
fNamespaceContext.popContext();
}
| org.w3c.dom.Node | getCurrentElement()
return fCurrentElement;
| public boolean | isEntityDeclared(java.lang.String name)
return false;
| public boolean | isEntityUnparsed(java.lang.String name)
if (fEntities != null) {
Entity entity = (Entity) fEntities.getNamedItem(name);
if (entity != null) {
return (entity.getNotationName() != null);
}
}
return false;
| private void | processAttributes(org.w3c.dom.NamedNodeMap attrMap)
final int attrCount = attrMap.getLength();
fAttributes.removeAllAttributes();
for (int i = 0; i < attrCount; ++i) {
Attr attr = (Attr) attrMap.item(i);
String value = attr.getValue();
if (value == null) {
value = XMLSymbols.EMPTY_STRING;
}
fillQName(fAttributeQName, attr);
// REVISIT: Assuming all attributes are of type CDATA. The actual type may not matter. -- mrglavas
fAttributes.addAttributeNS(fAttributeQName, XMLSymbols.fCDATASymbol, value);
fAttributes.setSpecified(i, attr.getSpecified());
// REVISIT: Should we be looking at non-namespace attributes
// for additional mappings? Should we detect illegal namespace
// declarations and exclude them from the context? -- mrglavas
if (fAttributeQName.uri == NamespaceContext.XMLNS_URI) {
// process namespace attribute
if (fAttributeQName.prefix == XMLSymbols.PREFIX_XMLNS) {
fNamespaceContext.declarePrefix(fAttributeQName.localpart, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);
}
else {
fNamespaceContext.declarePrefix(XMLSymbols.EMPTY_STRING, value.length() != 0 ? fSymbolTable.addSymbol(value) : null);
}
}
}
| private void | sendCharactersToValidator(java.lang.String str)
if (str != null) {
final int length = str.length();
final int remainder = length & CHUNK_MASK;
if (remainder > 0) {
str.getChars(0, remainder, fCharBuffer, 0);
fTempString.setValues(fCharBuffer, 0, remainder);
fSchemaValidator.characters(fTempString, null);
}
int i = remainder;
while (i < length) {
str.getChars(i, i += CHUNK_SIZE, fCharBuffer, 0);
fTempString.setValues(fCharBuffer, 0, CHUNK_SIZE);
fSchemaValidator.characters(fTempString, null);
}
}
| private void | setupDOMResultHandler(javax.xml.transform.dom.DOMSource source, javax.xml.transform.dom.DOMResult result)Sets up handler for DOMResult .
// If there's no DOMResult, unset the validator handler
if (result == null) {
fDOMValidatorHandler = null;
fSchemaValidator.setDocumentHandler(null);
return;
}
final Node nodeResult = result.getNode();
// If the source node and result node are the same use the DOMResultAugmentor.
// Otherwise use the DOMResultBuilder.
if (source.getNode() == nodeResult) {
fDOMValidatorHandler = fDOMResultAugmentor;
fDOMResultAugmentor.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultAugmentor);
return;
}
if (result.getNode() == null) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
result.setNode(builder.newDocument());
}
catch (ParserConfigurationException e) {
throw new SAXException(e);
}
}
fDOMValidatorHandler = fDOMResultBuilder;
fDOMResultBuilder.setDOMResult(result);
fSchemaValidator.setDocumentHandler(fDOMResultBuilder);
| private void | setupEntityMap(org.w3c.dom.Document doc)Extracts NamedNodeMap of entities. We need this to validate
elements and attributes of type xs:ENTITY, xs:ENTITIES or
types dervied from them.
if (doc != null) {
DocumentType docType = doc.getDoctype();
if (docType != null) {
fEntities = docType.getEntities();
return;
}
}
fEntities = null;
| public void | validate(javax.xml.transform.Source source, javax.xml.transform.Result result)
if (result instanceof DOMResult || result == null) {
final DOMSource domSource = (DOMSource) source;
final DOMResult domResult = (DOMResult) result;
Node node = domSource.getNode();
fRoot = node;
if (node != null) {
fComponentManager.reset();
fValidationManager.setEntityState(this);
fDOMNamespaceContext.reset();
String systemId = domSource.getSystemId();
fXMLLocator.setLiteralSystemId(systemId);
fXMLLocator.setExpandedSystemId(systemId);
fErrorReporter.setDocumentLocator(fXMLLocator);
try {
// regardless of what type of node this is, fire start and end document events
setupEntityMap((node.getNodeType() == Node.DOCUMENT_NODE) ? (Document) node : node.getOwnerDocument());
setupDOMResultHandler(domSource, domResult);
fSchemaValidator.startDocument(fXMLLocator, null, fDOMNamespaceContext, null);
validate(node);
fSchemaValidator.endDocument(null);
}
catch (XMLParseException e) {
throw Util.toSAXParseException(e);
}
catch (XNIException e) {
throw Util.toSAXException(e);
}
finally {
// Release references to application objects
fRoot = null;
fCurrentElement = null;
fEntities = null;
if (fDOMValidatorHandler != null) {
fDOMValidatorHandler.setDOMResult(null);
}
}
}
return;
}
throw new IllegalArgumentException(JAXPValidationMessageFormatter.formatMessage(Locale.getDefault(),
"SourceResultMismatch",
new Object [] {source.getClass().getName(), result.getClass().getName()}));
| private void | validate(org.w3c.dom.Node node)Traverse the DOM and fire events to the schema validator.
final Node top = node;
// Performs a non-recursive traversal of the DOM. This
// will avoid a stack overflow for DOMs with high depth.
while (node != null) {
beginNode(node);
Node next = node.getFirstChild();
while (next == null) {
finishNode(node);
if (top == node) {
break;
}
next = node.getNextSibling();
if (next == null) {
node = node.getParentNode();
if (node == null || top == node) {
if (node != null) {
finishNode(node);
}
next = null;
break;
}
}
}
node = next;
}
|
|