Search in sources :

Example 6 with IRIFactory

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

the class JenaOSGITest method testJenaIRI.

@Test
public void testJenaIRI() throws Exception {
    IRIFactory iriFactory = IRIFactory.iriImplementation();
    IRI iri = iriFactory.create("http://example.com/");
    assertEquals("http://example.com/", iri.toASCIIString());
}
Also used : IRI(org.apache.jena.iri.IRI) IRIFactory(org.apache.jena.iri.IRIFactory) Test(org.junit.Test)

Example 7 with IRIFactory

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

the class SetupJenaIRI method setupCheckerIRIFactory.

/**
 * IRI Factory with "checker" settings.
 */
/*package*/
static final IRIFactory setupCheckerIRIFactory() {
    // See IRIProviderJenaIRI.exceptions for context specific tuning.
    // See Checker.iriViolations for filtering and output from parsers.
    IRIFactory iriCheckerFactory = new IRIFactory();
    // iriCheckerInst.shouldViolation(false,true);
    // These two are from IRIFactory.iriImplementation() ...
    iriCheckerFactory.useSpecificationIRI(true);
    iriCheckerFactory.useSchemeSpecificRules("*", true);
    // Allow relative references for file: URLs.
    iriCheckerFactory.setSameSchemeRelativeReferences("file");
    // See also Checker.iriViolations and IRProviderJenaIRI where this is restricted to the scheme component.
    setErrorWarning(iriCheckerFactory, ViolationCodes.LOWERCASE_PREFERRED, false, true);
    // Jena3 compatibility (false, false) for this one.
    setErrorWarning(iriCheckerFactory, ViolationCodes.PERCENT_ENCODING_SHOULD_BE_UPPERCASE, false, false);
    // -- Scheme specific rules.
    setErrorWarning(iriCheckerFactory, ViolationCodes.SCHEME_PATTERN_MATCH_FAILED, false, true);
    // jena-iri produces an error for PROHIBITED_COMPONENT_PRESENT regardless.
    // See Checker.iriViolations for handling this
    // setErrorWarning(iriCheckerFactory, ViolationCodes.PROHIBITED_COMPONENT_PRESENT, false, true);
    // == Scheme
    setErrorWarning(iriCheckerFactory, ViolationCodes.UNREGISTERED_IANA_SCHEME, false, false);
    setErrorWarning(iriCheckerFactory, ViolationCodes.UNREGISTERED_NONIETF_SCHEME_TREE, false, false);
    // == DNS name.
    setErrorWarning(iriCheckerFactory, ViolationCodes.NOT_DNS_NAME, false, false);
    // RFC3986 allows present-encoded DNS names.
    setErrorWarning(iriCheckerFactory, ViolationCodes.USE_PUNYCODE_NOT_PERCENTS, false, false);
    // == Port related
    setErrorWarning(iriCheckerFactory, ViolationCodes.PORT_SHOULD_NOT_BE_EMPTY, false, true);
    setErrorWarning(iriCheckerFactory, ViolationCodes.PORT_SHOULD_NOT_START_IN_ZERO, false, true);
    setErrorWarning(iriCheckerFactory, ViolationCodes.DEFAULT_PORT_SHOULD_BE_OMITTED, false, true);
    // Warning in Jena3.  "Well known" is ports 0 to 1023.
    setErrorWarning(iriCheckerFactory, ViolationCodes.PORT_SHOULD_NOT_BE_WELL_KNOWN, false, false);
    // == Authority
    setErrorWarning(iriCheckerFactory, ViolationCodes.HAS_PASSWORD, false, true);
    setErrorWarning(iriCheckerFactory, ViolationCodes.PROHIBITED_COMPONENT_PRESENT, false, true);
    // == Path
    setErrorWarning(iriCheckerFactory, ViolationCodes.NON_INITIAL_DOT_SEGMENT, false, false);
    // == Character related.
    // setErrorWarning(iriFactoryInst, ViolationCodes.NOT_NFC,  false, false);
    // NFKC is not mentioned in RDF 1.1. Switch off.
    setErrorWarning(iriCheckerFactory, ViolationCodes.NOT_NFKC, false, false);
    // ** Applies to various unicode blocks.
    // Needed to be (false, false) for some Turtle tests (due to EricP!)
    setErrorWarning(iriCheckerFactory, ViolationCodes.COMPATIBILITY_CHARACTER, false, false);
    setErrorWarning(iriCheckerFactory, ViolationCodes.UNDEFINED_UNICODE_CHARACTER, false, false);
    // Otherwise the set of legal characters depends on the Java version.
    // If not set, this causes test failures in Turtle and Trig eval tests.
    setErrorWarning(iriCheckerFactory, ViolationCodes.UNASSIGNED_UNICODE_CHARACTER, false, false);
    // == Percent encoded.
    setErrorWarning(iriCheckerFactory, ViolationCodes.SUPERFLUOUS_NON_ASCII_PERCENT_ENCODING, false, true);
    setErrorWarning(iriCheckerFactory, ViolationCodes.SUPERFLUOUS_ASCII_PERCENT_ENCODING, false, true);
    return iriCheckerFactory;
}
Also used : IRIFactory(org.apache.jena.iri.IRIFactory)

Example 8 with IRIFactory

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

the class JenaReader method processArpOptions.

/**
 * Supported properties:
 * error-mode (String) default, lax, strict,
 * strict-ignore, strict-warning, strict-error, strict.error <br/>
 * embedding (String/Boolean) true, false<br/>
 * ERR_* (String/Integer) em_warning, em.error, em_ignore, em_error<br/>
 * IGN_* ditto<br/>
 * WARN_* ditto<br/>
 * iri-rules (String), "Jena", "IRI", "strict", "lax"
 */
@SuppressWarnings("deprecation")
static Object processArpOptions(ARPOptions options, String str, Object v, RDFErrorHandler eh) {
    // ARPOptions options = arpf.getOptions();
    str = str.toUpperCase();
    if (v == null)
        v = "";
    if (v instanceof String) {
        v = ((String) v).toUpperCase(Locale.ENGLISH);
    }
    if (str.equals("ERROR-MODE")) {
        if (v instanceof String) {
            String val = (String) v;
            if (val.equals("LAX")) {
                options.setLaxErrorMode();
                return null;
            }
            if (val.equals("DEFAULT")) {
                options.setDefaultErrorMode();
                return null;
            }
            if (val.equals("STRICT")) {
                options.setStrictErrorMode();
                return null;
            }
            if (val.equals("STRICT-WARNING")) {
                options.setStrictErrorMode(EM_WARNING);
                return null;
            }
            if (val.equals("STRICT-FATAL")) {
                options.setStrictErrorMode(EM_FATAL);
                return null;
            }
            if (val.equals("STRICT-IGNORE")) {
                options.setStrictErrorMode(EM_IGNORE);
                return null;
            }
            if (val.equals("STRICT-ERROR")) {
                options.setStrictErrorMode(EM_ERROR);
                return null;
            }
        }
        eh.error(new IllegalArgumentException("Property \"ERROR-MODE\" takes the following values: " + "\"default\", \"lax\", \"strict\", \"strict-ignore\", \"strict-warning\", \"strict-error\", \"strict.error\"."));
        return null;
    }
    if (str.equals("EMBEDDING")) {
        if (v instanceof String) {
            v = Boolean.valueOf((String) v);
        }
        if ((v instanceof Boolean))
            return options.setEmbedding(((Boolean) v).booleanValue());
        // Illegal value.
        eh.error(new IllegalArgumentException("Property \"EMBEDDING\" requires a boolean value."));
        boolean old = options.setEmbedding(false);
        options.setEmbedding(old);
        return old;
    }
    if (str.startsWith("ERR_") || str.startsWith("IGN_") || str.startsWith("WARN_")) {
        int cond = ParseException.errorCode(str);
        if (cond == -1) {
        // error, see end of function.
        } else {
            if (v instanceof String) {
                if (!((String) v).startsWith("EM_")) {
                // error, see below.
                } else {
                    int val = ParseException.errorCode((String) v);
                    if (val == -1) {
                    // error, see below.
                    } else {
                        int rslt = options.setErrorMode(cond, val);
                        return rslt;
                    }
                }
            } else if (v instanceof Integer) {
                int val = ((Integer) v).intValue();
                switch(val) {
                    case EM_IGNORE:
                    case EM_WARNING:
                    case EM_ERROR:
                    case EM_FATAL:
                        int rslt = options.setErrorMode(cond, val);
                        return rslt;
                    default:
                }
            }
            // Illegal value.
            eh.error(new IllegalArgumentException("Property \"" + str + "\" cannot have value: " + v.toString()));
            int old = options.setErrorMode(cond, EM_ERROR);
            options.setErrorMode(cond, old);
            return old;
        }
    }
    if (str.equals("IRI-RULES")) {
        IRIFactory old = options.getIRIFactory();
        if (v.equals("STRICT")) {
            options.setIRIFactory(IRIFactory.semanticWebImplementation());
        } else if (v.equals("IRI")) {
            options.setIRIFactory(IRIFactory.iriImplementation());
        } else if (v.equals("LAX")) {
            options.setIRIFactory(IRIFactory.jenaImplementation());
        } else
            eh.error(new IllegalArgumentException("Property \"IRI-RULES\" requires one of 'STRICT', 'IRI' or 'LAX'"));
        return old;
    }
    eh.error(new UnknownPropertyException(str));
    return null;
}
Also used : IRIFactory(org.apache.jena.iri.IRIFactory) UnknownPropertyException(org.apache.jena.shared.UnknownPropertyException)

Example 9 with IRIFactory

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

the class MoreTests method testIRIRules_2.

public void testIRIRules_2() {
    Model model = ModelFactory.createDefaultModel();
    IRIFactory f = ARPOptions.getIRIFactoryGlobal();
    try {
        ARPOptions.setIRIFactoryGlobal(IRIFactory.iriImplementation());
        RDFReaderI r = model.getReader("RDF/XML");
        expected = new int[] { WARN_MALFORMED_URI, WARN_MALFORMED_URI };
        r.setErrorHandler(this);
        r.read(model, new StringReader(RDF_TEXT), "http://example/");
    } finally {
        ARPOptions.setIRIFactoryGlobal(f);
    }
    checkExpected();
}
Also used : RDFReaderI(org.apache.jena.rdf.model.RDFReaderI) Model(org.apache.jena.rdf.model.Model) IRIFactory(org.apache.jena.iri.IRIFactory)

Example 10 with IRIFactory

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

the class ExTDB6 method main.

public static void main(String[] args) throws Exception {
    /// turn off the "No BGP optimizer warning"
    TDB.setOptimizerWarningFlag(false);
    final IRIFactory iriFactory = IRIFactory.semanticWebImplementation();
    final String DATASET_DIR_NAME = "data0";
    final Dataset data0 = TDBFactory.createDataset(DATASET_DIR_NAME);
    // show the currently registered names
    for (Iterator<String> it = data0.listNames(); it.hasNext(); ) {
        out.println("NAME=" + it.next());
    }
    out.println("getting named model...");
    /// this is the OWL portion
    final Model model = data0.getNamedModel(MY_NS);
    out.println("Model := " + model);
    out.println("getting graph...");
    /// this is the DATA in that MODEL
    final Graph graph = model.getGraph();
    out.println("Graph := " + graph);
    if (graph.isEmpty()) {
        final Resource product1 = model.createResource(iriFactory.construct(MY_NS + "product/1").toString());
        final Property hasName = model.createProperty(MY_NS, "#hasName");
        final Statement stmt = model.createStatement(product1, hasName, model.createLiteral("Beach Ball", "en"));
        out.println("Statement = " + stmt);
        model.add(stmt);
        // just for fun
        out.println("Triple := " + stmt.asTriple().toString());
    } else {
        out.println("Graph is not Empty; it has " + graph.size() + " Statements");
        long t0, t1;
        t0 = System.currentTimeMillis();
        final Query q = QueryFactory.create("PREFIX exns: <" + MY_NS + "#>\n" + "PREFIX exprod: <" + MY_NS + "product/>\n" + " SELECT * " + // +" WHERE { exprod:1 exns:hasName ?name }"
        " WHERE { ?res ?pred ?obj }");
        out.println("Query := " + q);
        t1 = System.currentTimeMillis();
        out.println("QueryFactory.TIME=" + (t1 - t0));
        t0 = System.currentTimeMillis();
        final QueryExecution qExec = QueryExecutionFactory.create(q, model);
        t1 = System.currentTimeMillis();
        out.println("QueryExecutionFactory.TIME=" + (t1 - t0));
        try {
            t0 = System.currentTimeMillis();
            ResultSet rs = qExec.execSelect();
            t1 = System.currentTimeMillis();
            out.println("executeSelect.TIME=" + (t1 - t0));
            while (rs.hasNext()) {
                QuerySolution sol = rs.next();
                out.println("Solution := " + sol);
                for (Iterator<String> names = sol.varNames(); names.hasNext(); ) {
                    final String name = names.next();
                    out.println("\t" + name + " := " + sol.get(name));
                }
            }
        } finally {
            qExec.close();
        }
    }
    out.println("closing graph");
    graph.close();
    out.println("closing model");
    model.close();
    //out.println("closing DataSetGraph");
    //dsg.close();
    out.println("closing DataSet");
    data0.close();
}
Also used : Statement(org.apache.jena.rdf.model.Statement) Resource(org.apache.jena.rdf.model.Resource) Graph(org.apache.jena.graph.Graph) Model(org.apache.jena.rdf.model.Model) IRIFactory(org.apache.jena.iri.IRIFactory) Property(org.apache.jena.rdf.model.Property)

Aggregations

IRIFactory (org.apache.jena.iri.IRIFactory)15 IRI (org.apache.jena.iri.IRI)10 Violation (org.apache.jena.iri.Violation)4 Test (org.junit.Test)3 Model (org.apache.jena.rdf.model.Model)2 Graph (org.apache.jena.graph.Graph)1 SetupJenaIRI (org.apache.jena.irix.SetupJenaIRI)1 Property (org.apache.jena.rdf.model.Property)1 RDFReaderI (org.apache.jena.rdf.model.RDFReaderI)1 Resource (org.apache.jena.rdf.model.Resource)1 Statement (org.apache.jena.rdf.model.Statement)1 UnknownPropertyException (org.apache.jena.shared.UnknownPropertyException)1