use of nu.validator.io.DataUriException in project validator by validator.
the class IriRef method checkValid.
@Override
public void checkValid(CharSequence literal) throws DatatypeException {
String messagePrologue = "";
int length = literal.length();
String urlString = literal.toString();
if (reportValue()) {
if (length < ELIDE_LIMIT) {
messagePrologue = "\u201c" + literal + "\u201d: ";
} else {
StringBuilder sb = new StringBuilder(ELIDE_LIMIT + 1);
sb.append(literal, 0, ELIDE_LIMIT / 2);
sb.append('\u2026');
sb.append(literal, length - ELIDE_LIMIT / 2, length);
messagePrologue = "\u201c" + sb.toString() + "\u201d: ";
}
}
if ("".equals(trimHtmlSpaces(urlString))) {
throw newDatatypeException("Must be non-empty.");
}
URL url = null;
URLParsingSettings settings = URLParsingSettings.create().withErrorHandler(StrictErrorHandler.getInstance());
boolean data = false;
try {
CharSequencePair pair = splitScheme(literal);
if (pair == null) {
// no scheme or scheme is private
if (isAbsolute()) {
throw newDatatypeException("The string \u201c" + literal + "\u201d is not an absolute URL.");
} else {
if (mustBeHttpOrHttps()) {
throw newDatatypeException("Must contain only" + " \u201chttp\u201d or \u201chttps\u201d URLs.");
}
// in this case, doc's actual base URL isn't relevant,
// so just use http://example.org/foo/bar as base
url = URL.parse(settings, URL.parse("http://example.org/foo/bar"), urlString);
}
} else {
CharSequence scheme = pair.getHead();
CharSequence tail = pair.getTail();
if (mustBeHttpOrHttps() && !isHttpOrHttps(scheme)) {
throw newDatatypeException("Must contain only" + " \u201chttp\u201d or \u201chttps\u201d URLs.");
}
if (isWellKnown(scheme)) {
url = URL.parse(settings, urlString);
} else if ("javascript".contentEquals(scheme)) {
// Don't bother user with generic IRI syntax
url = null;
} else if ("data".contentEquals(scheme)) {
data = true;
url = URL.parse(settings, urlString);
} else if (isHttpAlias(scheme)) {
StringBuilder sb = new StringBuilder(5 + tail.length());
sb.append("http:").append(tail);
url = URL.parse(settings, sb.toString());
} else {
StringBuilder sb = new StringBuilder(2 + literal.length());
sb.append("x-").append(literal);
url = URL.parse(settings, sb.toString());
}
}
} catch (GalimatiasParseException e) {
throw newDatatypeException(messagePrologue + e.getMessage() + ".");
}
if (url != null) {
if (data) {
try {
DataUri dataUri = new DataUri(url);
InputStream is = dataUri.getInputStream();
while (is.read() >= 0) {
// spin
}
} catch (DataUriException e) {
throw newDatatypeException(e.getIndex(), e.getHead(), e.getLiteral(), e.getTail());
} catch (IOException e) {
String msg = e.getMessage();
if (WARN && "Fragment is not allowed for data: URIs according to RFC 2397.".equals(msg)) {
throw newDatatypeException(messagePrologue + msg, WARN);
} else {
throw newDatatypeException(messagePrologue + msg);
}
}
}
}
}
Aggregations