Search in sources :

Example 6 with Violation

use of org.apache.jena.iri.Violation in project jena by apache.

the class IRIValidatorJSON method execute.

public static JsonObject execute(ValidationAction action) {
    JsonBuilder obj = new JsonBuilder();
    obj.startObject();
    String[] args = getArgs(action, paramIRI);
    if (args.length == 0)
        ServletOps.errorBadRequest("No IRIs supplied");
    obj.key(jIRIs);
    obj.startArray();
    for (String iriStr : args) {
        obj.startObject();
        obj.key(jIRI).value(iriStr);
        IRI iri = iriFactory.create(iriStr);
        List<String> errors = new ArrayList<>();
        List<String> warnings = new ArrayList<>();
        if (iri.isRelative())
            warnings.add("Relative IRI: " + iriStr);
        Iterator<Violation> vIter = iri.violations(true);
        for (; vIter.hasNext(); ) {
            Violation v = vIter.next();
            String str = v.getShortMessage();
            if (v.isError())
                errors.add(str);
            else
                warnings.add(str);
        }
        obj.key(jErrors);
        obj.startArray();
        for (String msg : errors) obj.value(msg);
        obj.finishArray();
        obj.key(jWarnings);
        obj.startArray();
        for (String msg : warnings) obj.value(msg);
        obj.finishArray();
        obj.finishObject();
    }
    obj.finishArray();
    obj.finishObject();
    return obj.build().getAsObject();
}
Also used : JsonBuilder(org.apache.jena.atlas.json.JsonBuilder) Violation(org.apache.jena.iri.Violation) IRI(org.apache.jena.iri.IRI) ArrayList(java.util.ArrayList)

Example 7 with Violation

use of org.apache.jena.iri.Violation in project jena by apache.

the class IRIValidatorHTML method executeHTML.

public static void executeHTML(HttpServletRequest httpRequest, HttpServletResponse httpResponse) {
    try {
        String[] args = httpRequest.getParameterValues(paramIRI);
        ServletOutputStream outStream = httpResponse.getOutputStream();
        PrintStream stdout = System.out;
        PrintStream stderr = System.err;
        System.setOut(new PrintStream(outStream));
        System.setErr(new PrintStream(outStream));
        setHeaders(httpResponse);
        outStream.println("<html>");
        printHead(outStream, "Jena IRI Validator Report");
        outStream.println("<body>");
        outStream.println("<h1>IRI Report</h1>");
        startFixed(outStream);
        try {
            boolean first = true;
            for (String iriStr : args) {
                if (!first)
                    System.out.println();
                first = false;
                IRI iri = iriFactory.create(iriStr);
                System.out.println(iriStr + " ==> " + iri);
                if (iri.isRelative())
                    System.out.println("Relative IRI: " + iriStr);
                Iterator<Violation> vIter = iri.violations(true);
                for (; vIter.hasNext(); ) {
                    String str = vIter.next().getShortMessage();
                    str = htmlQuote(str);
                    System.out.println(str);
                }
            }
        } finally {
            finishFixed(outStream);
            System.out.flush();
            System.err.flush();
            System.setOut(stdout);
            System.setErr(stdout);
        }
        outStream.println("</body>");
        outStream.println("</html>");
    } catch (IOException ex) {
    }
}
Also used : Violation(org.apache.jena.iri.Violation) PrintStream(java.io.PrintStream) IRI(org.apache.jena.iri.IRI) ServletOutputStream(javax.servlet.ServletOutputStream) IOException(java.io.IOException)

Example 8 with Violation

use of org.apache.jena.iri.Violation in project jena by apache.

the class CheckerIRI method iriViolations.

/** Process violations on an IRI
     *  Calls the errorhandler on all errors and warnings (as warning).
     *  Assumes error handler throws exceptions on errors if needbe 
     */
public static void iriViolations(IRI iri, ErrorHandler errorHandler, boolean allowRelativeIRIs, boolean includeIRIwarnings, long line, long col) {
    if (!allowRelativeIRIs && iri.isRelative())
        errorHandler.error("Relative IRI: " + iri, line, col);
    if (iri.hasViolation(includeIRIwarnings)) {
        Iterator<Violation> iter = iri.violations(includeIRIwarnings);
        boolean errorSeen = false;
        boolean warningSeen = false;
        // What to finally report.
        Violation vError = null;
        Violation vWarning = null;
        Violation xvSub = null;
        for (; iter.hasNext(); ) {
            Violation v = iter.next();
            int code = v.getViolationCode();
            boolean isError = v.isError();
            // Ignore these.
            if (code == Violation.LOWERCASE_PREFERRED || code == Violation.PERCENT_ENCODING_SHOULD_BE_UPPERCASE || code == Violation.SCHEME_PATTERN_MATCH_FAILED)
                continue;
            // Remember first error and first warning.
            if (isError) {
                errorSeen = true;
                if (vError == null)
                    // Remember first error
                    vError = v;
            } else {
                warningSeen = true;
                if (vWarning == null)
                    vWarning = v;
            }
            String msg = v.getShortMessage();
            String iriStr = iri.toString();
            // Ideally, we might want to output all messages relating to this IRI
            // then cause the error or continue.
            // But that's tricky given the current errorhandler architecture.
            //                // Put out warnings for all IRI issues - later, exception for errors.
            //                if (v.getViolationCode() == ViolationCodes.REQUIRED_COMPONENT_MISSING &&
            //                    v.getComponent() == IRIComponents.SCHEME)
            //                {
            //                    if (! allowRelativeIRIs )
            //                        handler.error("Relative URIs are not permitted in RDF: <"+iriStr+">", line, col);
            //                } 
            //                else
            {
                if (isError)
                    // IRI errors are warning at the level of parsing - they got through syntax checks.  
                    errorHandler.warning("Bad IRI: " + msg, line, col);
                else
                    errorHandler.warning("Not advised IRI: " + msg, line, col);
            }
        }
    //            // and report our choosen error.
    //            if ( errorSeen || (warningsAreErrors && warningSeen) )
    //            {
    //                String msg = null ;
    //                if ( vError != null ) msg = vError.getShortMessage() ;
    //                if ( msg == null && vWarning != null ) msg = vWarning.getShortMessage() ;
    //                if ( msg == null )
    //                    handler.error("Bad IRI: <"+iri+">", line, col) ;
    //                else
    //                    handler.error("Bad IRI: "+msg, line, col) ;
    //            }
    }
}
Also used : Violation(org.apache.jena.iri.Violation)

Example 9 with Violation

use of org.apache.jena.iri.Violation in project jena by apache.

the class AbsXMLContext method checkURI.

// abstract void baseUsed(XMLHandler forErrors, Taint taintMe, String
// relUri,
// String string) throws SAXParseException;
protected static void checkURI(XMLHandler forErrors, Taint taintMe, IRI rslt) throws SAXParseException {
    if (rslt.hasViolation(false)) {
        Iterator<Violation> it = rslt.violations(false);
        while (it.hasNext()) {
            Violation irie = it.next();
            // if (irie.getViolationCode() ==
            // ViolationCodes.REQUIRED_COMPONENT_MISSING)
            String msg = irie.getShortMessage();
            if (irie.getViolationCode() == ViolationCodes.REQUIRED_COMPONENT_MISSING && irie.getComponent() == IRIComponents.SCHEME) {
                if (!forErrors.allowRelativeURIs())
                    forErrors.warning(taintMe, WARN_RELATIVE_URI, "Relative URIs are not permitted in RDF: specifically <" + rslt.toString() + ">");
            } else
                forErrors.warning(taintMe, WARN_MALFORMED_URI, "Bad URI: " + msg);
        }
    }
}
Also used : Violation(org.apache.jena.iri.Violation)

Example 10 with Violation

use of org.apache.jena.iri.Violation in project jena by apache.

the class Main method check.

private void check(String string) {
    IRI iri = factory.create(string);
    if (iri.hasViolation(true)) {
        System.out.println("n: " + string);
        Iterator<Violation> it = iri.violations(true);
        while (it.hasNext()) {
            Violation v = it.next();
            System.out.println(v.getLongMessage());
        }
    } else {
        System.out.println("y: " + string);
    }
}
Also used : Violation(org.apache.jena.iri.Violation) IRI(org.apache.jena.iri.IRI)

Aggregations

Violation (org.apache.jena.iri.Violation)14 IRI (org.apache.jena.iri.IRI)12 IRIFactory (org.apache.jena.iri.IRIFactory)3 IOException (java.io.IOException)2 PrintStream (java.io.PrintStream)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 ArrayList (java.util.ArrayList)1 JsonBuilder (org.apache.jena.atlas.json.JsonBuilder)1 ExprEvalException (org.apache.jena.sparql.expr.ExprEvalException)1