KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > event > DocumentValidator


1 package net.sf.saxon.event;
2 import net.sf.saxon.trans.DynamicError;
3 import net.sf.saxon.trans.XPathException;
4 import net.sf.saxon.value.Whitespace;
5
6 /**
7  * DocumentValidator checks that a document is well-formed: specifically, that it contains a single element
8  * node child and no text node children.
9  */

10
11 public class DocumentValidator extends ProxyReceiver
12 {
13     boolean foundElement = false;
14     int level = 0;
15
16     public void setPipelineConfiguration(PipelineConfiguration config) {
17         super.setPipelineConfiguration(config);
18     }
19
20     /**
21      * Start of an element
22      * @param nameCode
23      * @param typeCode
24      * @param locationId
25      * @param properties
26      * @throws XPathException
27      */

28
29     public void startElement(int nameCode, int typeCode, int locationId, int properties) throws XPathException {
30         if (foundElement && level==0) {
31             DynamicError de = new DynamicError("A valid document must have only one child element");
32             throw de;
33         }
34         foundElement = true;
35         level++;
36         super.startElement(nameCode, typeCode, locationId, properties);
37     }
38
39     /**
40      * Character data
41      */

42
43     public void characters(CharSequence JavaDoc chars, int locationId, int properties) throws XPathException {
44         if (level == 0) {
45             if (Whitespace.isWhite(chars)) {
46                 return; // ignore whitespace outside the outermost element
47
}
48             DynamicError de = new DynamicError("A valid document must contain no text outside the outermost element");
49             throw de;
50         }
51         super.characters(chars, locationId, properties);
52     }
53
54     /**
55      * End of element
56      */

57
58     public void endElement() throws XPathException {
59         level--;
60         super.endElement();
61     }
62
63     /**
64      * Notify the end of a document node
65      */

66
67     public void endDocument() throws XPathException {
68         if (level==0) {
69             if (!foundElement) {
70                 DynamicError de = new DynamicError("A valid document must have a child element");
71                 throw de;
72             }
73             foundElement = false;
74             super.endDocument();
75             level = -1;
76         }
77     }
78 }
79
80 //
81
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
82
// you may not use this file except in compliance with the License. You may obtain a copy of the
83
// License at http://www.mozilla.org/MPL/
84
//
85
// Software distributed under the License is distributed on an "AS IS" basis,
86
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
87
// See the License for the specific language governing rights and limitations under the License.
88
//
89
// The Original Code is: all this file.
90
//
91
// The Initial Developer of the Original Code is Michael H. Kay.
92
//
93
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
94
//
95
// Contributor(s): none.
96
//
97
Popular Tags