'How to pass variable from java to turtle file?

I have a project with the structure:

-config
  -repositories
    -test-repo.ttl

The test-repo.ttl file looks like this:

@prefix lookup: <http://www.metaphacts.com/ontologies/platform/repository/lookup#> .
@prefix pathfinder: <http://www.metaphacts.com/ontologies/platform/service/pathfinder/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rep: <http://www.openrdf.org/config/repository#> .
@prefix sail: <http://www.openrdf.org/config/sail#> .
@prefix sr: <http://www.openrdf.org/config/repository/sail#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix mph: <http://www.metaphacts.com/ontologies/platform/repository#> .
@prefix ephedra: <http://www.metaphacts.com/ontologies/platform/ephedra#> .
@prefix fedsail: <http://www.openrdf.org/config/sail/federation#> .
@prefix sparqlr: <http://www.openrdf.org/config/repository/sparql#> .

[] a rep:Repository;
  rep:repositoryID "test-repo";
  rdfs:label "Repository to access data.";
  rep:repositoryImpl [
      rep:repositoryType "openrdf:SailRepository";
      sr:sailImpl [
          sail:sailType "metaphacts:RESTService";
          ephedra:serviceURL "https://request-to-access-data.com";
          ephedra:implementsService ephedra:test-ephedra;
          ephedra:httpMethod "GET";
          ephedra:httpHeader [
              ephedra:name "Authorization";
              ephedra:value "my_secret_token"
            ]
        ]
    ] .

The string my_secret_token changes every time the project is run, so I would need to imput its value from a java file. I am an absolute beginner in java. Is it possible to have a java file in which I create the my_secret_token variable and assign it the value of "123" for example, and then to pass this value to the ephedra:value field in the turtle file?

If so, can you help me to achieve this?



Solution 1:[1]

I am not sure I fully understand what you're trying to do, but if it's purely about changing that literal string in your Turtle file, I guess one simple way to do is to read the file into an in-memory RDf model, change the value, then write the model back to file again. Something like this (untested, but it gives the general idea hopefully):

// read the file into an RDF4J Model
Model model = Rio.parse("/path/to/test-repo.ttl", RDFFormat.TURTLE);

// create an IRI object for the 'ephedra:value' property
IRI ephedraValue = Values.iri("http://www.metaphacts.com/ontologies/platform/ephedra#value");

// find the statement in the RDF model we want to change the object of
model.getStatements(null, ephedraValue, null)
   .forEach(st -> {
        Value object = st.getObject();
        if (object.isLiteral()) { 
            // remove the old value
            model.remove(st);
            Literal newObject = Values.literal("my_NEW_secret_token");
            // add the new value
            model.add(st.getSubject, ephedraValue, newObject());
        }
);
// write modified model to file
Rio.write(model, new FileOutputStream("/path/to/file.ttle"), RDFFormat.TURTLE);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jeen Broekstra