Search in sources :

Example 1 with DataUri

use of nu.validator.io.DataUri in project validator by validator.

the class DataUriEntityResolver method resolveEntity.

@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
    if (DataUri.startsWithData(systemId)) {
        URL url;
        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 spe;
        }
        systemId = url.toString();
        DataUri du = new DataUri(systemId);
        TypedInputSource is = contentTypeParser.buildTypedInputSource(systemId, publicId, du.getContentType());
        is.setByteStream(du.getInputStream());
        return is;
    } else if (delegate != null) {
        return delegate.resolveEntity(publicId, systemId);
    } else {
        throw new IOException("Unsupported URI scheme.");
    }
}
Also used : GalimatiasParseException(io.mola.galimatias.GalimatiasParseException) SAXParseException(org.xml.sax.SAXParseException) IOException(java.io.IOException) URL(io.mola.galimatias.URL) DataUri(nu.validator.io.DataUri)

Example 2 with DataUri

use of nu.validator.io.DataUri 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);
                }
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URLParsingSettings(io.mola.galimatias.URLParsingSettings) URL(io.mola.galimatias.URL) GalimatiasParseException(io.mola.galimatias.GalimatiasParseException) DataUriException(nu.validator.io.DataUriException) DataUri(nu.validator.io.DataUri)

Aggregations

GalimatiasParseException (io.mola.galimatias.GalimatiasParseException)2 URL (io.mola.galimatias.URL)2 IOException (java.io.IOException)2 DataUri (nu.validator.io.DataUri)2 URLParsingSettings (io.mola.galimatias.URLParsingSettings)1 InputStream (java.io.InputStream)1 DataUriException (nu.validator.io.DataUriException)1 SAXParseException (org.xml.sax.SAXParseException)1