use of org.relaxng.datatype.DatatypeException in project validator by validator.
the class MessageEmitterAdapter method elaboration.
private void elaboration(Exception e) throws SAXException {
if (!(e instanceof AbstractValidationException || e instanceof VnuBadAttrValueException || e instanceof VnuBadElementNameException || e instanceof DatatypeMismatchException)) {
return;
}
if (e instanceof ImpossibleAttributeIgnoredException) {
ImpossibleAttributeIgnoredException ex = (ImpossibleAttributeIgnoredException) e;
Name elt = ex.getCurrentElement();
elaborateElementSpecificAttributes(elt, ex.getAttributeName());
} else if (e instanceof OnlyTextNotAllowedException) {
OnlyTextNotAllowedException ex = (OnlyTextNotAllowedException) e;
Name elt = ex.getCurrentElement();
elaborateContentModel(elt);
} else if (e instanceof OutOfContextElementException) {
OutOfContextElementException ex = (OutOfContextElementException) e;
Name parent = ex.getParent();
Name child = ex.getCurrentElement();
elaborateContentModelandContext(parent, child);
} else if (e instanceof RequiredAttributesMissingException) {
RequiredAttributesMissingException ex = (RequiredAttributesMissingException) e;
Name elt = ex.getCurrentElement();
elaborateElementSpecificAttributes(elt);
} else if (e instanceof RequiredAttributesMissingOneOfException) {
RequiredAttributesMissingOneOfException ex = (RequiredAttributesMissingOneOfException) e;
Name elt = ex.getCurrentElement();
elaborateElementSpecificAttributes(elt);
} else if (e instanceof RequiredElementsMissingException) {
RequiredElementsMissingException ex = (RequiredElementsMissingException) e;
Name elt = ex.getParent();
elaborateContentModel(elt);
} else if (e instanceof RequiredElementsMissingOneOfException) {
RequiredElementsMissingOneOfException ex = (RequiredElementsMissingOneOfException) e;
Name elt = ex.getParent();
elaborateContentModel(elt);
} else if (e instanceof StringNotAllowedException) {
StringNotAllowedException ex = (StringNotAllowedException) e;
Name elt = ex.getCurrentElement();
elaborateContentModel(elt);
} else if (e instanceof TextNotAllowedException) {
TextNotAllowedException ex = (TextNotAllowedException) e;
Name elt = ex.getCurrentElement();
elaborateContentModel(elt);
} else if (e instanceof UnfinishedElementException) {
UnfinishedElementException ex = (UnfinishedElementException) e;
Name elt = ex.getCurrentElement();
elaborateContentModel(elt);
} else if (e instanceof UnfinishedElementOneOfException) {
UnfinishedElementOneOfException ex = (UnfinishedElementOneOfException) e;
Name elt = ex.getCurrentElement();
elaborateContentModel(elt);
} else if (e instanceof UnknownElementException) {
UnknownElementException ex = (UnknownElementException) e;
Name elt = ex.getParent();
elaborateContentModel(elt);
} else if (e instanceof BadAttributeValueException) {
BadAttributeValueException ex = (BadAttributeValueException) e;
Map<String, DatatypeException> map = ex.getExceptions();
elaborateDatatypes(map);
} else if (e instanceof VnuBadAttrValueException) {
VnuBadAttrValueException ex = (VnuBadAttrValueException) e;
Map<String, DatatypeException> map = ex.getExceptions();
elaborateDatatypes(map);
} else if (e instanceof VnuBadElementNameException) {
VnuBadElementNameException ex = (VnuBadElementNameException) e;
Map<String, DatatypeException> map = ex.getExceptions();
elaborateDatatypes(map);
} else if (e instanceof DatatypeMismatchException) {
DatatypeMismatchException ex = (DatatypeMismatchException) e;
Map<String, DatatypeException> map = ex.getExceptions();
elaborateDatatypes(map);
} else if (e instanceof StringNotAllowedException) {
StringNotAllowedException ex = (StringNotAllowedException) e;
Map<String, DatatypeException> map = ex.getExceptions();
elaborateDatatypes(map);
}
}
use of org.relaxng.datatype.DatatypeException in project validator by validator.
the class MessageEmitterAdapter method vnuBadAttrValueMessageText.
private void vnuBadAttrValueMessageText(VnuBadAttrValueException e) throws SAXException {
MessageTextHandler messageTextHandler = emitter.startText();
if (messageTextHandler != null) {
boolean isWarning = false;
Map<String, DatatypeException> datatypeErrors = e.getExceptions();
for (Map.Entry<String, DatatypeException> entry : datatypeErrors.entrySet()) {
DatatypeException dex = entry.getValue();
if (dex instanceof Html5DatatypeException) {
Html5DatatypeException ex5 = (Html5DatatypeException) dex;
if (ex5.isWarning()) {
isWarning = true;
}
}
}
if (isWarning) {
messageTextString(messageTextHandler, POTENTIALLY_BAD_VALUE, false);
} else {
messageTextString(messageTextHandler, BAD_VALUE, false);
}
if (e.getAttributeValue().length() < 200) {
codeString(messageTextHandler, e.getAttributeValue());
}
messageTextString(messageTextHandler, FOR, false);
attribute(messageTextHandler, e.getAttributeName(), e.getCurrentElement(), false);
messageTextString(messageTextHandler, ON, false);
element(messageTextHandler, e.getCurrentElement(), false);
emitDatatypeErrors(messageTextHandler, e.getExceptions());
}
emitter.endText();
}
use of org.relaxng.datatype.DatatypeException in project validator by validator.
the class TextContentChecker method endElement.
/**
* @see nu.validator.checker.Checker#endElement(java.lang.String,
* java.lang.String, java.lang.String)
*/
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (inEmptyTitleOrOption && XHTML_URL.equals(uri) && "title".equals(localName)) {
err("Element \u201Ctitle\u201d must not be empty.");
inEmptyTitleOrOption = false;
} else if (inEmptyTitleOrOption && XHTML_URL.equals(uri) && "option".equals(localName) && !(parent != null && XHTML_URL.equals(parent[0]) && "datalist".equals(parent[1]))) {
err("Element \u201Coption\u201d without " + "attribute \u201clabel\u201d must not be empty.");
inEmptyTitleOrOption = false;
}
openElements.pop();
DatatypeStreamingValidator dsv = stack.removeLast();
if (dsv != null) {
try {
dsv.checkValid();
} catch (DatatypeException e) {
String msg = e.getMessage();
if (msg == null) {
err("The text content of element \u201C" + localName + "\u201D from namespace \u201C" + uri + "\u201D was not in the required format.");
} else {
if ("time".equals(localName)) {
try {
errBadTextContent(e, TimeDatetime.class, localName, uri);
} catch (ClassNotFoundException ce) {
}
} else if ("script".equals(localName)) {
try {
errBadTextContent(e, ScriptDocumentation.class, localName, uri);
} catch (ClassNotFoundException ce) {
}
} else {
err("The text content of element \u201C" + localName + // + "\u201D from namespace \u201C" + uri
"\u201D was not in the required format: " + msg.split(": ")[1]);
}
}
}
}
}
use of org.relaxng.datatype.DatatypeException in project validator by validator.
the class XmlPiChecker method checkXmlStylesheetPiData.
private void checkXmlStylesheetPiData(String data) throws SAXException {
boolean hasHref = false;
boolean hasTitle = false;
boolean hasMedia = false;
boolean hasCharset = false;
boolean hasAlternate = false;
boolean hasNonEmptyTitle = false;
boolean alternateIsYes = false;
boolean badDatatype = false;
if (inDoctype) {
warn("An \u201cxml-stylesheet\u201d instruction should not be used" + " within a \u201cDOCTYPE\u201d declaration.");
}
if (alreadyHasElement) {
err("Any \u201cxml-stylesheet\u201d instruction in a document must" + " occur before any elements in the document." + " Suppressing any further errors for this" + " \u201cxml-stylesheet\u201d instruction.");
return;
}
if (!"".equals(data)) {
Html5DatatypeLibrary dl = new Html5DatatypeLibrary();
AttributesImpl patts = getPseudoAttributesFromPiData(data);
String attrName;
String attrValue;
for (int i = 0; i < patts.getLength(); i++) {
attrName = patts.getQName(i);
attrValue = patts.getValue(i);
switch(PseudoAttrName.toCaps(attrName)) {
case HREF:
hasHref = true;
if (attrValue == null) {
break;
}
try {
IriRef ir = (IriRef) dl.createDatatype("iri-ref");
ir.checkValid(attrValue);
} catch (DatatypeException e) {
try {
errBadPseudoAttrDatatype(e, IriRef.class, "href", attrValue);
} catch (ClassNotFoundException ce) {
}
}
break;
case TYPE:
if (attrValue == null) {
break;
}
try {
MimeType mt = (MimeType) dl.createDatatype("mime-type");
mt.checkValid(attrValue);
attrValue = newAsciiLowerCaseStringFromString(attrValue);
} catch (DatatypeException e) {
badDatatype = true;
try {
errBadPseudoAttrDatatype(e, MimeType.class, "type", attrValue);
} catch (ClassNotFoundException ce) {
}
}
if (!badDatatype) {
if (attrValue.matches("application/xml(;.*)?") || attrValue.matches("text/xml(;.*)?") || attrValue.matches("application/xslt+xml(;.*)?") || attrValue.matches("text/xsl(;.*)?") || attrValue.matches("text/xslt(;.*)?")) {
if (!attrValue.matches("text/xsl(;.*)?")) {
warn("For indicating XSLT," + " \u201ctext/xsl\u201d is the only" + " MIME type for the" + " \u201cxml-stylesheet\u201d" + " pseudo-attribute \u201ctype\u201d" + " that is supported across browsers.");
}
if (hasXsltPi) {
warn("Browsers do not support multiple" + " \u201cxml-stylesheet\u201d" + " instructions with a" + " \u201ctype\u201d value that" + " indicates XSLT.");
}
hasXsltPi = true;
} else if (!attrValue.matches("^text/css(;.*)?$")) {
warn("\u201ctext/css\u201d and" + " \u201ctext/xsl\u201d are the only" + " MIME types for the" + " \u201cxml-stylesheet\u201d" + " pseudo-attribute \u201ctype\u201d" + " that are supported across browsers.");
}
}
break;
case TITLE:
hasTitle = true;
if (attrValue == null) {
break;
}
if (!"".equals(attrValue)) {
hasNonEmptyTitle = true;
}
break;
case MEDIA:
hasMedia = true;
if (attrValue == null) {
break;
}
try {
MediaQuery mq = (MediaQuery) dl.createDatatype("media-query");
mq.checkValid(attrValue);
} catch (DatatypeException e) {
try {
errBadPseudoAttrDatatype(e, MediaQuery.class, "media", attrValue);
} catch (ClassNotFoundException ce) {
}
}
break;
case CHARSET:
hasCharset = true;
if (attrValue == null) {
break;
}
try {
Charset c = (Charset) dl.createDatatype("charset");
c.checkValid(attrValue);
} catch (DatatypeException e) {
try {
errBadPseudoAttrDatatype(e, Charset.class, "charset", attrValue);
} catch (ClassNotFoundException ce) {
}
}
break;
case ALTERNATE:
hasAlternate = true;
if (attrValue == null) {
break;
}
if ("yes".equals(attrValue)) {
alternateIsYes = true;
} else if (!"no".equals(attrValue)) {
err("The value of the \u201cxml-stylesheet\u201d" + " pseudo-attribute \u201calternate\u201d" + " must be either \u201cyes\u201d or" + " \u201cno\u201d.");
}
break;
default:
err("Pseudo-attribute \u201c" + attrName + "\u201d not allowed in" + " \u201cxml-stylesheet\u201d instruction.");
break;
}
}
if (alternateIsYes && !hasNonEmptyTitle) {
err("An \u201cxml-stylesheet\u201d instruction with an" + " \u201calternate\u201d pseudo-attribute whose value is" + " \u201cyes\u201d must also have a \u201ctitle\u201d" + " pseudo-attribute with a non-empty value.");
}
}
if (!hasHref) {
err("\u201cxml-stylesheet\u201d instruction lacks \u201chref\u201d" + " pseudo-attribute." + " The \u201chref\u201d pseudo-attribute is required in all" + " \u201cxml-stylesheet\u201d instructions.");
}
if (hasXsltPi && (hasTitle || hasMedia || hasCharset || hasAlternate)) {
warn("When processing \u201cxml-stylesheet\u201d instructions," + " browsers ignore the pseudo-attributes \u201ctitle\u201d," + " \u201cmedia\u201d, \u201ccharset\u201d, and" + " \u201calternate\u201d.");
} else if (hasCharset) {
warn("Some browsers ignore the value of the" + " \u201cxml-stylesheet\u201d pseudo-attribute" + " \u201ccharset\u201d.");
}
}
use of org.relaxng.datatype.DatatypeException in project validator by validator.
the class PrudentHttpEntityResolver method resolveEntity.
/**
* @see org.xml.sax.EntityResolver#resolveEntity(java.lang.String,
* java.lang.String)
*/
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
if (requestsLeft > -1) {
if (requestsLeft == 0) {
throw new IOException("Number of permitted HTTP requests exceeded.");
} else {
requestsLeft--;
}
}
HttpGet m = null;
try {
URL url = null;
try {
url = URL.parse(systemId);
} catch (GalimatiasParseException e) {
IOException ioe = (IOException) new IOException(e.getMessage()).initCause(e);
SAXParseException spe = new SAXParseException(e.getMessage(), publicId, systemId, -1, -1, ioe);
if (errorHandler != null) {
errorHandler.fatalError(spe);
}
throw ioe;
}
String scheme = url.scheme();
if (!("http".equals(scheme) || "https".equals(scheme))) {
String msg = "Unsupported URI scheme: \u201C" + scheme + "\u201D.";
SAXParseException spe = new SAXParseException(msg, publicId, systemId, -1, -1, new IOException(msg));
if (errorHandler != null) {
errorHandler.fatalError(spe);
}
throw spe;
}
systemId = url.toString();
try {
m = new HttpGet(systemId);
} catch (IllegalArgumentException e) {
SAXParseException spe = new SAXParseException(e.getMessage(), publicId, systemId, -1, -1, (IOException) new IOException(e.getMessage()).initCause(e));
if (errorHandler != null) {
errorHandler.fatalError(spe);
}
throw spe;
}
m.setHeader("User-Agent", userAgent);
m.setHeader("Accept", buildAccept());
m.setHeader("Accept-Encoding", "gzip");
if (request != null && request.getAttribute("http://validator.nu/properties/accept-language") != null) {
m.setHeader("Accept-Language", (String) request.getAttribute("http://validator.nu/properties/accept-language"));
}
log4j.info(systemId);
try {
if (url.port() > 65535) {
throw new IOException("Port number must be less than 65536.");
}
} catch (NumberFormatException e) {
throw new IOException("Port number must be less than 65536.");
}
HttpResponse response = client.execute(m);
boolean ignoreResponseStatus = false;
if (request != null && request.getAttribute("http://validator.nu/properties/ignore-response-status") != null) {
ignoreResponseStatus = (boolean) request.getAttribute("http://validator.nu/properties/ignore-response-status");
}
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != 200 && !ignoreResponseStatus) {
String msg = "HTTP resource not retrievable." + " The HTTP status from the remote server was: " + statusCode + ".";
SAXParseException spe = new SAXParseException(msg, publicId, m.getURI().toString(), -1, -1, new SystemIdIOException(m.getURI().toString(), msg));
if (errorHandler != null) {
errorHandler.fatalError(spe);
}
throw new ResourceNotRetrievableException(String.format("%s: %s", m.getURI().toString(), msg));
}
HttpEntity entity = response.getEntity();
long len = entity.getContentLength();
if (sizeLimit > -1 && len > sizeLimit) {
SAXParseException spe = new SAXParseException("Resource size exceeds limit.", publicId, m.getURI().toString(), -1, -1, new StreamBoundException("Resource size exceeds limit."));
if (errorHandler != null) {
errorHandler.fatalError(spe);
}
throw spe;
}
TypedInputSource is;
org.apache.http.Header ct = response.getFirstHeader("Content-Type");
String contentType = null;
final String baseUri = m.getURI().toString();
if (ct != null) {
contentType = ct.getValue();
}
is = contentTypeParser.buildTypedInputSource(baseUri, publicId, contentType);
Header cl = response.getFirstHeader("Content-Language");
if (cl != null) {
is.setLanguage(cl.getValue().trim());
}
Header xuac = response.getFirstHeader("X-UA-Compatible");
if (xuac != null) {
String val = xuac.getValue().trim();
if (!"ie=edge".equalsIgnoreCase(val)) {
SAXParseException spe = new SAXParseException("X-UA-Compatible HTTP header must have the value \u201CIE=edge\u201D," + " was \u201C" + val + "\u201D.", publicId, systemId, -1, -1);
errorHandler.error(spe);
}
}
Header csp = response.getFirstHeader("Content-Security-Policy");
if (csp != null) {
try {
ContentSecurityPolicy.THE_INSTANCE.checkValid(csp.getValue().trim());
} catch (DatatypeException e) {
SAXParseException spe = new SAXParseException("Content-Security-Policy HTTP header: " + e.getMessage(), publicId, systemId, -1, -1);
Html5DatatypeException ex5 = (Html5DatatypeException) e;
if (ex5.isWarning()) {
errorHandler.warning(spe);
} else {
errorHandler.error(spe);
}
}
}
final HttpGet meth = m;
InputStream stream = entity.getContent();
if (sizeLimit > -1) {
stream = new BoundedInputStream(stream, sizeLimit, baseUri);
}
Header ce = response.getFirstHeader("Content-Encoding");
if (ce != null) {
String val = ce.getValue().trim();
if ("gzip".equalsIgnoreCase(val) || "x-gzip".equalsIgnoreCase(val)) {
stream = new GZIPInputStream(stream);
if (sizeLimit > -1) {
stream = new BoundedInputStream(stream, sizeLimit, baseUri);
}
}
}
is.setByteStream(new ObservableInputStream(stream, new StreamObserver() {
private final Logger log4j = Logger.getLogger("nu.validator.xml.PrudentEntityResolver.StreamObserver");
private boolean released = false;
@Override
public void closeCalled() {
log4j.debug("closeCalled");
if (!released) {
log4j.debug("closeCalled, not yet released");
released = true;
try {
meth.releaseConnection();
} catch (Exception e) {
log4j.debug("closeCalled, releaseConnection", e);
}
}
}
@Override
public void exceptionOccurred(Exception ex) throws IOException {
if (!released) {
released = true;
try {
meth.abort();
} catch (Exception e) {
log4j.debug("exceptionOccurred, abort", e);
} finally {
try {
meth.releaseConnection();
} catch (Exception e) {
log4j.debug("exceptionOccurred, releaseConnection", e);
}
}
}
if (ex instanceof SystemIdIOException) {
throw (SystemIdIOException) ex;
} else if (ex instanceof IOException) {
IOException ioe = (IOException) ex;
throw new SystemIdIOException(baseUri, ioe.getMessage(), ioe);
} else if (ex instanceof RuntimeException) {
throw (RuntimeException) ex;
} else {
throw new RuntimeException("API contract violation. Wrong exception type.", ex);
}
}
@Override
public void finalizerCalled() {
if (!released) {
released = true;
try {
meth.abort();
} catch (Exception e) {
log4j.debug("finalizerCalled, abort", e);
} finally {
try {
meth.releaseConnection();
} catch (Exception e) {
log4j.debug("finalizerCalled, releaseConnection", e);
}
}
}
}
}));
return is;
} catch (IOException | RuntimeException | SAXException e) {
if (m != null) {
try {
m.abort();
} catch (Exception ex) {
log4j.debug("abort", ex);
} finally {
try {
m.releaseConnection();
} catch (Exception ex) {
log4j.debug("releaseConnection", ex);
}
}
}
throw e;
}
}
Aggregations