| 1 |
package com.thaiopensource.relaxng.jaxp; |
| 2 |
|
| 3 |
import com.thaiopensource.relaxng.match.Matcher; |
| 4 |
import com.thaiopensource.relaxng.pattern.Pattern; |
| 5 |
import com.thaiopensource.relaxng.pattern.PatternMatcher; |
| 6 |
import com.thaiopensource.relaxng.pattern.ValidatorPatternBuilder; |
| 7 |
import com.thaiopensource.relaxng.sax.Context; |
| 8 |
import com.thaiopensource.validation.ValidatorHandler2; |
| 9 |
import com.thaiopensource.xml.sax.DraconianErrorHandler; |
| 10 |
import com.thaiopensource.xml.util.Name; |
| 11 |
import org.w3c.dom.ls.LSResourceResolver; |
| 12 |
import org.xml.sax.Attributes; |
| 13 |
import org.xml.sax.ContentHandler; |
| 14 |
import org.xml.sax.DTDHandler; |
| 15 |
import org.xml.sax.ErrorHandler; |
| 16 |
import org.xml.sax.Locator; |
| 17 |
import org.xml.sax.SAXException; |
| 18 |
import org.xml.sax.SAXNotRecognizedException; |
| 19 |
import org.xml.sax.SAXNotSupportedException; |
| 20 |
import org.xml.sax.SAXParseException; |
| 21 |
|
| 22 |
import javax.xml.XMLConstants; |
| 23 |
import javax.xml.validation.TypeInfoProvider; |
| 24 |
|
| 25 |
class ValidatorHandlerImpl extends ValidatorHandler2 { |
| 26 |
private Matcher matcher; |
| 27 |
static private final ErrorHandler defaultErrorHandler = new DraconianErrorHandler(); |
| 28 |
private ErrorHandler specifiedErrorHandler = null; |
| 29 |
private ErrorHandler actualErrorHandler = defaultErrorHandler; |
| 30 |
|
| 31 |
private boolean bufferingCharacters = false; |
| 32 |
private final StringBuffer charBuf = new StringBuffer(); |
| 33 |
private Locator locator = null; |
| 34 |
private final Context context; |
| 35 |
private ContentHandler contentHandler = null; |
| 36 |
private DTDHandler dtdHandler; |
| 37 |
private LSResourceResolver resourceResolver = null; |
| 38 |
private boolean secureProcessing; |
| 39 |
|
| 40 |
ValidatorHandlerImpl(SchemaFactoryImpl factory, Pattern pattern, ValidatorPatternBuilder builder) { |
| 41 |
matcher = new PatternMatcher(pattern, builder); |
| 42 |
context = new Context(); |
| 43 |
// the docs say it gets the properties of its factory, not the features |
| 44 |
secureProcessing = false; |
| 45 |
} |
| 46 |
|
| 47 |
public void reset() { |
| 48 |
bufferingCharacters = false; |
| 49 |
locator = null; |
| 50 |
matcher = matcher.start(); |
| 51 |
context.reset(); |
| 52 |
} |
| 53 |
|
| 54 |
public void startElement(String namespaceURI, |
| 55 |
String localName, |
| 56 |
String qName, |
| 57 |
Attributes atts) throws SAXException { |
| 58 |
if (bufferingCharacters) { |
| 59 |
bufferingCharacters = false; |
| 60 |
check(matcher.matchTextBeforeStartTag(charBuf.toString(), context)); |
| 61 |
} |
| 62 |
Name name = new Name(namespaceURI, localName); |
| 63 |
check(matcher.matchStartTagOpen(name, qName, context)); |
| 64 |
int len = atts.getLength(); |
| 65 |
for (int i = 0; i < len; i++) { |
| 66 |
Name attName = new Name(atts.getURI(i), atts.getLocalName(i)); |
| 67 |
String attQName = atts.getQName(i); |
| 68 |
check(matcher.matchAttributeName(attName, attQName, context)); |
| 69 |
check(matcher.matchAttributeValue(atts.getValue(i), attName, attQName, context)); |
| 70 |
} |
| 71 |
check(matcher.matchStartTagClose(name, qName, context)); |
| 72 |
if (matcher.isTextTyped()) { |
| 73 |
bufferingCharacters = true; |
| 74 |
charBuf.setLength(0); |
| 75 |
} |
| 76 |
if (contentHandler != null) |
| 77 |
contentHandler.startElement(namespaceURI, localName, qName, atts); |
| 78 |
} |
| 79 |
|
| 80 |
public void endElement(String namespaceURI, |
| 81 |
String localName, |
| 82 |
String qName) throws SAXException { |
| 83 |
if (bufferingCharacters) { |
| 84 |
bufferingCharacters = false; |
| 85 |
if (charBuf.length() > 0) |
| 86 |
check(matcher.matchTextBeforeEndTag(charBuf.toString(), new Name(namespaceURI, localName), |
| 87 |
qName, context)); |
| 88 |
} |
| 89 |
check(matcher.matchEndTag(new Name(namespaceURI, localName), qName, context)); |
| 90 |
if (contentHandler != null) |
| 91 |
contentHandler.endElement(namespaceURI, localName, qName); |
| 92 |
} |
| 93 |
|
| 94 |
public void characters(char ch[], int start, int length) throws SAXException { |
| 95 |
if (bufferingCharacters) { |
| 96 |
charBuf.append(ch, start, length); |
| 97 |
return; |
| 98 |
} |
| 99 |
for (int i = 0; i < length; i++) { |
| 100 |
switch (ch[start + i]) { |
| 101 |
case ' ': |
| 102 |
case '\r': |
| 103 |
case '\t': |
| 104 |
case '\n': |
| 105 |
break; |
| 106 |
default: |
| 107 |
check(matcher.matchUntypedText(context)); |
| 108 |
return; |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
public void endDocument() throws SAXException { |
| 114 |
check(matcher.matchEndDocument()); |
| 115 |
if (contentHandler != null) |
| 116 |
contentHandler.endDocument(); |
| 117 |
} |
| 118 |
|
| 119 |
public void setDocumentLocator(Locator locator) { |
| 120 |
this.locator = locator; |
| 121 |
if (contentHandler != null) |
| 122 |
contentHandler.setDocumentLocator(locator); |
| 123 |
} |
| 124 |
|
| 125 |
public void startDocument() throws SAXException { |
| 126 |
check(matcher.matchStartDocument()); |
| 127 |
if (contentHandler != null) |
| 128 |
contentHandler.startDocument(); |
| 129 |
} |
| 130 |
|
| 131 |
public void processingInstruction(String target, String data) throws SAXException { |
| 132 |
if (contentHandler != null) |
| 133 |
contentHandler.processingInstruction(target, data); |
| 134 |
} |
| 135 |
|
| 136 |
public void skippedEntity(String name) throws SAXException { |
| 137 |
if (contentHandler != null) |
| 138 |
contentHandler.skippedEntity(name); |
| 139 |
} |
| 140 |
|
| 141 |
public void ignorableWhitespace(char[] ch, int start, int len) throws SAXException { |
| 142 |
if (contentHandler != null) |
| 143 |
contentHandler.ignorableWhitespace(ch, start, len); |
| 144 |
} |
| 145 |
|
| 146 |
private void check(boolean ok) throws SAXException { |
| 147 |
if (!ok) |
| 148 |
actualErrorHandler.error(new SAXParseException(matcher.getErrorMessage(), locator)); |
| 149 |
} |
| 150 |
|
| 151 |
public void setContentHandler(ContentHandler delegate) { |
| 152 |
this.contentHandler = delegate; |
| 153 |
} |
| 154 |
|
| 155 |
public ContentHandler getContentHandler() { |
| 156 |
return contentHandler; |
| 157 |
} |
| 158 |
|
| 159 |
public void setDTDHandler(DTDHandler dtdHandler) { |
| 160 |
this.dtdHandler = dtdHandler; |
| 161 |
} |
| 162 |
|
| 163 |
public DTDHandler getDTDHandler() { |
| 164 |
return dtdHandler; |
| 165 |
} |
| 166 |
|
| 167 |
public TypeInfoProvider getTypeInfoProvider() { |
| 168 |
return null; |
| 169 |
} |
| 170 |
|
| 171 |
public void setErrorHandler(ErrorHandler errorHandler) { |
| 172 |
this.specifiedErrorHandler = errorHandler; |
| 173 |
this.actualErrorHandler = errorHandler == null ? defaultErrorHandler : errorHandler; |
| 174 |
} |
| 175 |
|
| 176 |
public ErrorHandler getErrorHandler() { |
| 177 |
return specifiedErrorHandler; |
| 178 |
} |
| 179 |
|
| 180 |
public void setResourceResolver(LSResourceResolver resourceResolver) { |
| 181 |
this.resourceResolver = resourceResolver; |
| 182 |
} |
| 183 |
|
| 184 |
public LSResourceResolver getResourceResolver() { |
| 185 |
return resourceResolver; |
| 186 |
} |
| 187 |
|
| 188 |
public void startPrefixMapping(String prefix, String uri) throws SAXException { |
| 189 |
// namespace declarations on the start-tag shouldn't apply to the characters before the start-tag |
| 190 |
if (bufferingCharacters) { |
| 191 |
bufferingCharacters = false; |
| 192 |
check(matcher.matchTextBeforeStartTag(charBuf.toString(), context)); |
| 193 |
} |
| 194 |
context.startPrefixMapping(prefix, uri); |
| 195 |
if (contentHandler != null) |
| 196 |
contentHandler.startPrefixMapping(prefix, uri); |
| 197 |
} |
| 198 |
|
| 199 |
public void endPrefixMapping(String prefix) throws SAXException { |
| 200 |
context.endPrefixMapping(prefix); |
| 201 |
if (contentHandler != null) |
| 202 |
contentHandler.endPrefixMapping(prefix); |
| 203 |
} |
| 204 |
|
| 205 |
public void notationDecl(String name, String publicId, String systemId) throws SAXException { |
| 206 |
context.notationDecl(name, publicId, systemId); |
| 207 |
if (dtdHandler != null) |
| 208 |
dtdHandler.notationDecl(name, publicId, systemId); |
| 209 |
} |
| 210 |
|
| 211 |
public void unparsedEntityDecl(String name, String publicId, String systemId, String notationName) throws SAXException { |
| 212 |
context.unparsedEntityDecl(name, publicId, systemId, notationName); |
| 213 |
if (dtdHandler != null) |
| 214 |
dtdHandler.unparsedEntityDecl(name, publicId, systemId, notationName); |
| 215 |
} |
| 216 |
|
| 217 |
public void setFeature(String name, boolean value) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 218 |
if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) |
| 219 |
secureProcessing = value; |
| 220 |
else |
| 221 |
super.setFeature(name, value); |
| 222 |
} |
| 223 |
|
| 224 |
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 225 |
if (XMLConstants.FEATURE_SECURE_PROCESSING.equals(name)) |
| 226 |
return secureProcessing; |
| 227 |
return super.getFeature(name); |
| 228 |
} |
| 229 |
} |