org.apache.jena.riot.RDFParser Java Examples

The following examples show how to use org.apache.jena.riot.RDFParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RDFStar2RDFTest.java    From RDFstarTools with Apache License 2.0 6 votes vote down vote up
protected Graph convertAndLoadIntoGraph( String filename )
{
    final String fullFilename = getClass().getResource("/TurtleStar/"+filename).getFile();
    String result;
    try( ByteArrayOutputStream os = new ByteArrayOutputStream() ) {
        new RDFStar2RDF().convert(fullFilename, os);
        result = os.toString();
    }
    catch ( IOException e ) {
        fail( "Closing the output stream failed: " + e.getMessage() );
        return null;
    }

	final StringReader reader = new StringReader(result);
       final Graph g = ModelFactory.createDefaultModel().getGraph();
       final StreamRDF dest = StreamRDFLib.graph(g);

       RDFParser.create()
                .source(reader)
                .lang(Lang.TURTLE)
                .parse(dest);

	return g;
}
 
Example #2
Source File: UndefinedSubjectValidator.java    From rdflint with MIT License 6 votes vote down vote up
@Override
public void setParameters(RdfLintParameters params) {
  super.setParameters(params);

  List<Map<String, String>> paramList = getValidationParameterMapList();
  for (Map<String, String> map : paramList) {
    String url = map.get("url");
    String startswith = map.get("startswith");
    String langtype = map.get("langtype");

    // skip loaded url
    if (additionalUrlSubjectsMap.get(url) != null) {
      additionalStartswithSubjectsMap.put(startswith, additionalUrlSubjectsMap.get(url));
      continue;
    }
    Lang lang = Lang.TURTLE;
    if ("rdfxml".equalsIgnoreCase(langtype) || "rdf".equalsIgnoreCase(langtype)) {
      lang = Lang.RDFXML;
    }
    Set<String> sets = loadSubjects(RDFParser.source(url), startswith, lang);
    if (sets != null) {
      additionalStartswithSubjectsMap.put(startswith, sets);
      additionalUrlSubjectsMap.put(url, sets);
    }
  }
}
 
Example #3
Source File: DatasetLoader.java    From rdflint with MIT License 6 votes vote down vote up
static Model loadRdfSet(RdfLintParameters params, String targetDir) throws IOException {
  String parentPath = new File(targetDir).getCanonicalPath();
  String baseUri = params.getBaseUri();

  Graph g = Factory.createGraphMem();
  Files.walk(Paths.get(parentPath))
      .filter(e -> e.toString().endsWith(".rdf") || e.toString().endsWith(".ttl"))
      .forEach(e -> {
        Graph gf = Factory.createGraphMem();
        String filename = e.toString().substring(parentPath.length() + 1);
        String subdir = filename.substring(0, filename.lastIndexOf('/') + 1);
        RDFParser.source(e.toString()).base(baseUri + subdir).parse(gf);
        List<Triple> lst = gf.find().toList();
        gf.close();
        lst.forEach(g::add);

        gf.getPrefixMapping().getNsPrefixMap()
            .forEach((k, v) -> g.getPrefixMapping().setNsPrefix(k, v));
      });

  return ModelFactory.createModelForGraph(g);
}
 
Example #4
Source File: ValidationRunner.java    From rdflint with MIT License 6 votes vote down vote up
private Map<String, List<Triple>> loadFileTripleSet(String parentPath, String baseUri)
    throws IOException {
  return Files
      .walk(Paths.get(parentPath))
      .filter(e -> e.toString().endsWith(".rdf") || e.toString().endsWith(".ttl"))
      .collect(Collectors.toMap(
          e -> e.toString().substring(parentPath.length() + 1),
          e -> {
            Graph g = Factory.createGraphMem();
            String filename = e.toString().substring(parentPath.length() + 1);
            String subdir = filename.substring(0, filename.lastIndexOf(File.separator) + 1);
            if (File.separatorChar == '\\') {
              subdir = filename.replaceAll("\\\\", "/");
            }
            RDFParser.source(e.toString()).base(baseUri + subdir).parse(g);
            List<Triple> lst = g.find().toList();
            g.close();
            return lst;
          }
      ));
}
 
Example #5
Source File: JenaIOService.java    From trellis with Apache License 2.0 6 votes vote down vote up
@Override
public Stream<Triple> read(final InputStream input, final RDFSyntax syntax, final String base) {
    requireNonNull(input, "The input stream may not be null!");
    requireNonNull(syntax, "The syntax value may not be null!");

    try {
        final org.apache.jena.graph.Graph graph = createDefaultGraph();
        final Lang lang = JenaCommonsRDF.toJena(syntax).orElseThrow(() ->
                new TrellisRuntimeException("Unsupported RDF Syntax: " + syntax.mediaType()));

        RDFParser.source(input).lang(lang).base(base).parse(graph);

        // Check the graph for any new namespace definitions
        final Set<String> namespaces = new HashSet<>(nsService.getNamespaces().values());
        graph.getPrefixMapping().getNsPrefixMap().forEach((prefix, namespace) -> {
            if (shouldAddNamespace(namespaces, namespace, base)) {
                LOGGER.debug("Setting prefix ({}) for namespace {}", prefix, namespace);
                nsService.setPrefix(prefix, namespace);
            }
        });
        return JenaCommonsRDF.fromJena(graph).stream().map(Triple.class::cast);
    } catch (final RiotException | AtlasException | IllegalArgumentException ex) {
        throw new TrellisRuntimeException(ex);
    }
}
 
Example #6
Source File: ContextUtils.java    From sparql-generate with Apache License 2.0 6 votes vote down vote up
public static void loadGraph(Context context, String sourceURI, String baseURI, StreamRDF dest) {
	if(getDataset(context).containsNamedModel(sourceURI)) {
		final Model model = getDataset(context).getNamedModel(sourceURI);
		StreamRDFOps.sendGraphToStream(model.getGraph(), dest);
		return;
	}
	if(!isRootContext(context)) {
		Context parentContext = (Context) context.get(PARENT_CONTEXT);
		loadGraph(parentContext, sourceURI, baseURI, dest);
		return;
	}
	final SPARQLExtStreamManager sm = (SPARQLExtStreamManager) context.get(SysRIOT.sysStreamManager);
	final String acceptHeader = "text/turtle;q=1.0,application/rdf+xml;q=0.9,*/*;q=0.1";
	final LookUpRequest request = new LookUpRequest(sourceURI, acceptHeader);
	try (TypedInputStream tin = sm.open(request);) {
		if(tin == null) {
			LOG.warn("Could not locate graph " + request);
			return;
		}
		Lang lang = RDFLanguages.contentTypeToLang(tin.getMediaType());
		RDFParser.create().source(tin).base(baseURI).context(context).lang(lang).parse(dest);
	} catch (RiotException ex) {
		LOG.warn("Error while loading graph " + sourceURI, ex);
	}
}
 
Example #7
Source File: RDF2RDFStar.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
public Parser( String inputFilename, PipedTriplesStream triplesStream, boolean enableChecking )
{
	this.inputFilename = inputFilename;
	this.triplesStream = triplesStream;

	builder = RDFParser.create();
	builder.labelToNode( LabelToNode.createUseLabelEncoded() );
	builder.source(inputFilename);
	builder.checking(enableChecking);
}
 
Example #8
Source File: RDFStar2RDF.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
public void execute()
{
    // print all prefixes and the base IRI to the output file
    try ( IndentedWriter writer = new IndentedWriter(outStream) ) {
        RiotLib.writePrefixes(writer, pmap);
        RiotLib.writeBase(writer, baseIRI);

        // second pass over the file to perform the conversion in a streaming manner
        final PipedRDFIterator<Triple> it = new PipedRDFIterator<>(BUFFER_SIZE);
        final PipedTriplesStream triplesStream = new PipedTriplesStream(it);

        // PipedRDFStream and PipedRDFIterator need to be on different threads
        final Runnable r = new Runnable() {
            @Override
            public void run() {
                RDFParser.create().labelToNode( LabelToNode.createUseLabelEncoded() )
                .source(inputFilename)
                .lang(LangTurtleStar.TURTLESTAR)
                .base(baseIRI)
                .build()
                .parse(triplesStream);
            }
        };
        final ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(r);

        final NodeFormatter nFmt = new NodeFormatterTTL(baseIRI, pmap);

        while ( it.hasNext() ) {
            printTriples(it.next(), writer, nFmt, false);
        }

        it.close();
        executor.shutdown();

        writer.write(" .");
        writer.flush();
    }
}
 
Example #9
Source File: RDFStar2RDF.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
public void execute()
{
	final PipedRDFIterator<Triple> it = new PipedRDFIterator<>(BUFFER_SIZE);
	final PipedTriplesStream triplesStream = new PipedTriplesStream(it);

	// PipedRDFStream and PipedRDFIterator need to be on different threads
	final Runnable r = new Runnable() {
		@Override
		public void run() {
			RDFParser.create().labelToNode(LabelToNode.createUseLabelEncoded())
                  .source(inputFilename)
                  .lang(LangTurtleStar.TURTLESTAR)
                  .base(baseIRI)
                  .build()
                  .parse(triplesStream);
		}
	};
	final ExecutorService executor = Executors.newSingleThreadExecutor();
	executor.submit(r);

	// consume the iterator
	while ( it.hasNext() ) {
       	it.next();
       }

	pmap = it.getPrefixes();
	if(baseIRI == null) {
		baseIRI = it.getBaseIri();
	}

	it.close();
	executor.shutdown();
}
 
Example #10
Source File: RDFStarUtils.java    From RDFstarTools with Apache License 2.0 5 votes vote down vote up
/**
 * Adds the RDF* data from the given Turtle* serialization
 * to the given {@link Graph}. 
 */
static public void populateGraphFromTurtleStarSnippet( Graph graph, String snippet )
{
	final StringReader reader = new StringReader(snippet);
    final StreamRDF dest = StreamRDFLib.graph(graph);

    RDFParser.create()
             .source(reader)
             .lang(LangTurtleStar.TURTLESTAR)
             .parse(dest);
}
 
Example #11
Source File: WebAcService.java    From trellis with Apache License 2.0 5 votes vote down vote up
static Dataset generateDefaultRootAuthorizationsDataset(final String resource) {
    final Dataset dataset = rdf.createDataset();
    final Model model = createDefaultModel();
    try (final InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource)) {
        if (is != null) {
            LOGGER.debug("Using classpath resource for default root ACL: {}", resource);
            RDFParser.source(is).lang(TURTLE).base(TRELLIS_DATA_PREFIX).parse(model);
        } else {
            LOGGER.debug("Using external resource for default root ACL: {}", resource);
            RDFParser.source(resource).lang(TURTLE).base(TRELLIS_DATA_PREFIX).parse(model);
        }
        fromJena(model.getGraph()).stream().map(triple -> rdf.createQuad(Trellis.PreferAccessControl,
                    triple.getSubject(), triple.getPredicate(), triple.getObject())).forEach(dataset::add);
    } catch (final IOException | RiotException ex) {
        LOGGER.warn("Couldn't initialize root ACL with {}, falling back to default: {}", resource, ex.getMessage());
    } finally {
        model.close();
    }

    // Fallback to manual creation
    if (dataset.size() == 0) {
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Read));
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Write));
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Control));
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.mode, ACL.Append));
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.agentClass, FOAF.Agent));
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.default_, root));
        dataset.add(rdf.createQuad(Trellis.PreferAccessControl, rootAuth, ACL.accessTo, root));
    }
    return dataset;
}
 
Example #12
Source File: ASTest.java    From trellis with Apache License 2.0 5 votes vote down vote up
@Override
Graph getVocabulary(final String url) {
    final Graph graph = createDefaultGraph();
    try {
        RDFParser.source(url).httpAccept("application/ld+json").parse(graph);
    } catch (final HttpException ex) {
        LOGGER.warn("Could not fetch {}: {}", url, ex.getMessage());
        assumeTrue(false, "Error fetching the URL (" + url + "): skip the test");
    }
    return graph;
}
 
Example #13
Source File: DBResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
private Stream<Quad> fetchExtensionQuads(final IRI graphName) {
    final String query = "SELECT data FROM extension WHERE resource_id = ? AND ext = ?";
    final Model model = createDefaultModel();
    jdbi.withHandle(handle -> handle.select(query, data.getId(), extensions.get(graphName))
            .map((rs, ctx) -> rs.getString("data")).findFirst())
        .ifPresent(triples -> RDFParser.fromString(triples).lang(NTRIPLES).parse(model));
    return fromJena(model.getGraph()).stream().map(triple -> rdf.createQuad(graphName, triple.getSubject(),
                triple.getPredicate(), triple.getObject())).map(Quad.class::cast);
}
 
Example #14
Source File: CombineManifest.java    From incubator-taverna-language with Apache License 2.0 5 votes vote down vote up
private static Model parseRDF(Path metadata) throws IOException {
	Model model = createDefaultModel();
	try (InputStream in = newInputStream(metadata)) {
		RDFParser.create()
				.base(fakeFileURI(metadata))
				.lang(Lang.RDFXML)
				.source(in)
				// TAVERNA-1044 avoid bailing out on broken XML
				.errorHandler(ErrorHandlerFactory.errorHandlerWarn)
				.parse(model.getGraph());
	}
	//System.out.println("Parsed:");
	//model.write(System.out, "turtle");
	return model;
}
 
Example #15
Source File: LangTurtleStarTest.java    From RDFstarTools with Apache License 2.0 3 votes vote down vote up
protected Graph loadGraphFromTurtleStarFile( String filename ) {

		final String fullFilename = getClass().getResource("/TurtleStar/"+filename).getFile();

        final Graph g = ModelFactory.createDefaultModel().getGraph();
        final StreamRDF dest = StreamRDFLib.graph(g);

		assertEquals( LangTurtleStar.TURTLESTAR, RDFLanguages.filenameToLang(fullFilename) );

        RDFParser.create()
                 .source( "file://" + fullFilename )
                 .parse(dest);

		return g;
	}