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 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 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 42 43 public void characters(CharSequence chars, int locationId, int properties) throws XPathException { 44 if (level == 0) { 45 if (Whitespace.isWhite(chars)) { 46 return; } 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 57 58 public void endElement() throws XPathException { 59 level--; 60 super.endElement(); 61 } 62 63 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 | Popular Tags |