protected void createN3Writer() throws UnsupportedEncodingException, FileNotFoundException { if(outputFilePath != null) { OutputStreamWriter fw = new OutputStreamWriter(new FileOutputStream(outputFilePath), "UTF-8"); BufferedWriter bw = new BufferedWriter(fw); PrintWriter pw = new PrintWriter(bw); N3KR2RMLRDFWriter n3Writer = new N3KR2RMLRDFWriter(new URIFormatter(), pw); if(baseURI != null) { n3Writer.setBaseURI(baseURI); } writers.add(n3Writer); } if (outputFileJSONPath != null) { JSONKR2RMLRDFWriter jsonWriter = new JSONKR2RMLRDFWriter(new PrintWriter(outputFileJSONPath), baseURI); writers.add(jsonWriter); } }
private String constructTripleWithLiteralObject(String subjUri, String predicateUri, String value, String literalType, String language) { value = escapeValue(value); if (subjUri.indexOf("<") != -1 && subjUri.indexOf(">") != -1) { String tmp = subjUri.substring(1, subjUri.length() - 1); subjUri = "<" + normalizeURI(tmp) + ">"; } //https://www.w3.org/TeamSubmission/turtle/ - Literals may be given either a language suffix or a datatype URI but not both if(language != null && !language.equals("")) { return subjUri + " " + uriFormatter.getExpandedAndNormalizedUri(predicateUri) + " \"" + value + "\"" + "@" + language + " ."; } else if (literalType != null && !literalType.equals("")) { // Add the RDF literal type to the literal if present return subjUri + " " + uriFormatter.getExpandedAndNormalizedUri(predicateUri) + " \"" + value + "\"" + "^^<" + literalType + "> ."; } return subjUri + " " + uriFormatter.getExpandedAndNormalizedUri(predicateUri) + " \"" + value + "\" ."; }
public String getExpandedAndNormalizedUri(String uri) { // Check if the predicate contains a predicate. if (replacePrefixesWithNamespaces && !uri.startsWith("<") && !uri.startsWith("http:") && !uri.startsWith("https:") && uri.contains(":") && !uri.startsWith("_:")) { // Replace the prefix with proper namespace by looking into the ontology manager String prefix = uri.substring(0, uri.indexOf(":")); String namespace = this.prefixToNamespaceMap.get(prefix); if (namespace == null || namespace.isEmpty()) { if(reportErrors) { this.errorReport.addReportMessage(new ReportMessage("Error creating predicate's URI: " + uri, "No namespace found for the prefix: " + prefix, Priority.high)); } } else { uri = namespace + uri.substring(uri.indexOf(":")+1); } } // Remove all unwanted characters uri = normalizeUri(uri); // Put angled brackets if required if (!uri.startsWith(Uris.BLANK_NODE_PREFIX) && !uri.startsWith("<") && !uri.endsWith(">")) { uri = "<" + uri + ">"; } return uri; }
Map<String, List<String>> results = new HashMap<>(); URIFormatter uriFormatter = new URIFormatter(workspace.getOntologyManager(), new ErrorReport()); if(sameAsPredicate!= null && !sameAsPredicate.trim().isEmpty()) sameAsPredicate = uriFormatter.getExpandedAndNormalizedUri(sameAsPredicate);
public URIFormatter(OntologyManager ontMgr, ErrorReport errorReport) { reportErrors = true; replacePrefixesWithNamespaces = true; prefixToNamespaceMap = new HashMap<>(); populatePrefixToNamespaceMap(ontMgr); this.errorReport = errorReport; } public URIFormatter(List<Prefix> prefixes, ErrorReport errorReport)
public URIFormatter(List<Prefix> prefixes, ErrorReport errorReport) { reportErrors = true; replacePrefixesWithNamespaces = true; prefixToNamespaceMap = new HashMap<>(); populatePrefixToNamespaceMap(prefixes); this.errorReport = errorReport; } public String getExpandedAndNormalizedUri(String uri) {
@Override protected KR2RMLRDFWriter configureRDFWriter(StringWriter sw) { PrintWriter pw = new PrintWriter(sw); URIFormatter uriFormatter = new URIFormatter(); N3KR2RMLRDFWriter outWriter = new N3KR2RMLRDFWriter(uriFormatter, pw); outWriter.setBaseURI(karma.getBaseURI()); return outWriter; } }
private String constructTripleWithURIObject(String subjUri, String predicateUri, String objectUri) { if (subjUri.indexOf("<") != -1 && subjUri.indexOf(">") != -1) { String tmp = subjUri.substring(1, subjUri.length() - 1); subjUri = "<" + normalizeURI(tmp) + ">"; } if (objectUri.indexOf("<") != -1 && objectUri.indexOf(">") != -1) { String tmp = objectUri.substring(1, objectUri.length() - 1); objectUri = "<" + normalizeURI(tmp) + ">"; } return subjUri + " " + uriFormatter.getExpandedAndNormalizedUri(predicateUri) + " " + objectUri + " ."; }
private void initializeMemberVariables(Worksheet worksheet, Workspace workspace, String outputFileName, boolean addColumnContextInformation, KR2RMLMapping kr2rmlMapping, ErrorReport errorReport) { // this.kr2rmlMapping = kr2rmlMapping; this.workspace = workspace; this.factory = workspace.getFactory(); this.worksheet = worksheet; this.outputFileName = outputFileName; this.errorReport = errorReport; this.uriFormatter = new URIFormatter(kr2rmlMapping.getPrefixes(), errorReport); this.hNodeToContextUriMap = new ConcurrentHashMap<>(); this.addColumnContextInformation = addColumnContextInformation; this.translator = new KR2RMLMappingColumnNameHNodeTranslator(factory, worksheet); this.outWriters = new LinkedList<>(); }
if(URIify) value = formatter.getExpandedAndNormalizedUri(value);
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(f),"UTF-8")); N3KR2RMLRDFWriter writer = new N3KR2RMLRDFWriter(new URIFormatter(workspace.getOntologyManager(), errorReport), new PrintWriter (bw)); writer.setBaseURI(rdfSourceNamespace); writers.add(writer);
@Override protected void outputTriple(KR2RMLRDFWriter outWriter, PopulatedTemplateTermSet subject, PopulatedTemplateTermSet predicate, PopulatedTemplateTermSet object) { if(objectTemplateTermSetPopulator.getTerms().isSingleUriString()) { outWriter.outputTripleWithURIObject(pom, subject.getURI(), predicate.getURI(), uriFormatter.getExpandedAndNormalizedUri(object.getURI())); } else { outWriter.outputTripleWithLiteralObject(pom, subject.getURI(), predicate.getURI(), object.getURI(), literalTemplateValue, literalLanguage); } } }
void generateRDF(String modelName, String jsonData, boolean addProvenance, PrintWriter pw, ServletContextParameterMap contextParameters) throws KarmaException, JSONException, IOException { URIFormatter uriFormatter = new URIFormatter(); KR2RMLRDFWriter outWriter = new N3KR2RMLRDFWriter(uriFormatter, pw); RDFGeneratorRequest request = new RDFGeneratorRequest(modelName, null); request.setAddProvenance(addProvenance); request.setDataType(InputType.JSON); request.setInputData(jsonData); request.addWriter(outWriter); request.setContextParameters(contextParameters); this.generateRDF(request); }
@Override protected void outputTriple(KR2RMLRDFWriter outWriter, PopulatedTemplateTermSet subject, PopulatedTemplateTermSet predicate, PopulatedTemplateTermSet object) { if(objectTemplateTermSetPopulator.getTerms().isSingleUriString() || predicate.getURI().equals("<" + Uris.RDF_TYPE_URI + ">")) { outWriter.outputTripleWithURIObject(pom, subject.getURI(), predicate.getURI(), uriFormatter.getExpandedAndNormalizedUri(object.getURI())); } else if(generateContext && objectTemplateTermSetPopulator.getTerms().isSingleColumnTerm()) { try { outWriter.outputQuadWithLiteralObject(pom, subject.getURI(), predicate.getURI(), object.getURI(), literalTemplateValue, literalLanguage, getColumnContextUri(translator.getHNodeIdForColumnName(objectTemplateTermSetPopulator.getTerms().getAllTerms().get(0).getTemplateTermValue()))); } catch (HNodeNotFoundKarmaException e) { LOG.error("No hnode found for context " +objectTemplateTermSetPopulator.getTerms().getAllTerms().get(0).getTemplateTermValue() + " " + e); } } else { outWriter.outputTripleWithLiteralObject(pom, subject.getURI(), predicate.getURI(), object.getURI(), literalTemplateValue, literalLanguage); } }