001: package net.sf.saxon.event;
002:
003: import net.sf.saxon.trans.DynamicError;
004: import net.sf.saxon.trans.XPathException;
005: import net.sf.saxon.value.Whitespace;
006:
007: /**
008: * DocumentValidator checks that a document is well-formed: specifically, that it contains a single element
009: * node child and no text node children.
010: */
011:
012: public class DocumentValidator extends ProxyReceiver {
013: boolean foundElement = false;
014: int level = 0;
015:
016: public void setPipelineConfiguration(PipelineConfiguration config) {
017: super .setPipelineConfiguration(config);
018: }
019:
020: /**
021: * Start of an element
022: * @param nameCode
023: * @param typeCode
024: * @param locationId
025: * @param properties
026: * @throws XPathException
027: */
028:
029: public void startElement(int nameCode, int typeCode,
030: int locationId, int properties) throws XPathException {
031: if (foundElement && level == 0) {
032: DynamicError de = new DynamicError(
033: "A valid document must have only one child element");
034: throw de;
035: }
036: foundElement = true;
037: level++;
038: super .startElement(nameCode, typeCode, locationId, properties);
039: }
040:
041: /**
042: * Character data
043: */
044:
045: public void characters(CharSequence chars, int locationId,
046: int properties) throws XPathException {
047: if (level == 0) {
048: if (Whitespace.isWhite(chars)) {
049: return; // ignore whitespace outside the outermost element
050: }
051: DynamicError de = new DynamicError(
052: "A valid document must contain no text outside the outermost element");
053: throw de;
054: }
055: super .characters(chars, locationId, properties);
056: }
057:
058: /**
059: * End of element
060: */
061:
062: public void endElement() throws XPathException {
063: level--;
064: super .endElement();
065: }
066:
067: /**
068: * Notify the end of a document node
069: */
070:
071: public void endDocument() throws XPathException {
072: if (level == 0) {
073: if (!foundElement) {
074: DynamicError de = new DynamicError(
075: "A valid document must have a child element");
076: throw de;
077: }
078: foundElement = false;
079: super .endDocument();
080: level = -1;
081: }
082: }
083: }
084:
085: //
086: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
087: // you may not use this file except in compliance with the License. You may obtain a copy of the
088: // License at http://www.mozilla.org/MPL/
089: //
090: // Software distributed under the License is distributed on an "AS IS" basis,
091: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
092: // See the License for the specific language governing rights and limitations under the License.
093: //
094: // The Original Code is: all this file.
095: //
096: // The Initial Developer of the Original Code is Michael H. Kay.
097: //
098: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
099: //
100: // Contributor(s): none.
101: //
|