Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2019-2023 Cefriel.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.cefriel.template.utils;
import com.cefriel.template.io.Reader;
import com.cefriel.template.io.csv.CSVReader;
import com.cefriel.template.io.json.JSONReader;
import com.cefriel.template.io.rdf.RDFReader;
import com.cefriel.template.io.xml.XMLReader;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.sail.SailRepository;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.sail.memory.MemoryStore;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TemplateFunctions {
private String prefix;
/**
* If a prefix is set, removes it from the parameter {@code String s}. If a prefix is not set,
* or the prefix is not contained in the given string it returns the string as it is.
* @param s String representing an IRI
* @return String value
*/
public String rp(String s) {
if (s!=null && prefix!=null)
if (s.contains(prefix)) {
return s.replace(prefix, "");
}
return s;
}
/**
* Set the prefix used by the {@link #rp(String s)} function
* @param prefix String prefix
*/
public void setPrefix(String prefix) {
if (prefix != null)
this.prefix = prefix;
}
/**
* Returns the substring of the parameter {@code String s} after the first occurrence
* of the parameter {@code String substring}.
* @param s String to be modified
* @param substring Pattern to get the substring
* @return Suffix substring
*/
public String sp(String s, String substring) {
if (s!=null) {
return s.substring(s.indexOf(substring) + substring.length());
}
return s;
}
/**
* Returns the substring of the parameter {@code String s} before the first occurrence
* of the parameter {@code String substring}.
* @param s String to be modified
* @param substring Pattern to get the substring
* @return Prefix substring
*/
public String p(String s, String substring) {
if (s!=null) {
return s.substring(0, s.indexOf(substring));
}
return s;
}
/**
* Returns a string replacing all the occurrences of the regex with the replacement provided.
* @param s String to be modified
* @param regex Regex to be matched
* @param replacement String to be used as replacement
* @return Modified string
*/
public String replace(String s, String regex, String replacement) {
if (s != null)
return s.replaceAll(regex, replacement);
return null;
}
/**
* Returns a new line char.
* @return A new line char.
*/
public String newline() {
return "\n";
}
/**
* Returns a string representing the hash of the parameter {@code String s}.
* @param s String to be hashed.
* @return String representing the computed hash.
*/
public String hash(String s) {
if (s == null)
return null;
return Integer.toString(s.hashCode());
}
/**
* Returns {@code true} if the string is not null and not an empty string.
* @param s String to be checked
* @return boolean
*/
public boolean checkString(String s){
return s != null && !s.trim().isEmpty();
}
/**
* Creates a support data structure ({@link java.util.HashMap}) to access query results faster. Builds a map associating
* a single row with its value w.r.t a specified column ({@code key} parameter). The assumption is
* that for each row the value for the given column is unique, otherwise, the result will be incomplete.
* @param results The result of a SPARL query
* @param key The variable to be used to build the map
* @return Map associating to each value of the variable {@code key} a row of the query results.
*/
public Map> getMap(List