How to convert RDF to pretty nested JSON using java rdf4j - java

I have a simple RDF file and want to convert it to nice nested JSON.
_:b0 a <http://schema.org/Book> ;
<http://schema.org/name> "Semantic Web Primer (First Edition)" ;
<http://schema.org/offers> _:b1 ;
<http://schema.org/publisher> "Linked Data Tools" .
_:b1 a <http://schema.org/Offer> ;
<http://schema.org/price> "2.95" ;
<http://schema.org/priceCurrency> "USD" .
should become
{
"type" : "Book",
"name" : "Semantic Web Primer (First Edition)",
"offers" : {
"type" : "Offer",
"price" : "2.95",
"priceCurrency" : "USD"
},
"publisher" : "Linked Data Tools"
}

Use framing
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.rio.RDFFormat;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.RDFParser;
import org.eclipse.rdf4j.rio.RDFWriter;
import org.eclipse.rdf4j.rio.Rio;
import org.eclipse.rdf4j.rio.helpers.StatementCollector;
import org.junit.Test;
import com.github.jsonldjava.core.JsonLdOptions;
import com.github.jsonldjava.core.JsonLdProcessor;
import com.github.jsonldjava.utils.JsonUtils;
import com.google.common.base.Charsets;
public class HowToConvertRdfToJson {
#Test
public void convertRdfToPrettyJson(){
try(InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("book.ttl")){
System.out.println(getPrettyJsonLdString(in,RDFFormat.TURTLE));
}catch(Exception e){
throw new RuntimeException(e);
}
}
/**
* #param in Input stream with rdf data
* #param format format of the rdf data
* #return a pretty JSON document as String
*/
public static String getPrettyJsonLdString(InputStream in, RDFFormat format) {
return getPrettyJsonLdString(
readRdfToString(in, format, RDFFormat.JSONLD, ""));
}
/**
* #param statements rdf statements collected
* #return a pretty JSON document as String
*/
public static String getPrettyJsonLdString(Collection<Statement> statements) {
return getPrettyJsonLdString(
graphToString(statements, RDFFormat.JSONLD));
}
private static String getPrettyJsonLdString(String rdfGraphAsJson) {
try {
//#formatter:off
return JsonUtils
.toPrettyString(
removeGraphArray(
getFramedJson(
createJsonObject(
rdfGraphAsJson))));
//#formatter:on
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Map<String, Object> removeGraphArray(Map<String, Object> framedJson) {
List<Map<String,Object>> graph = (List<Map<String, Object>>) framedJson.get("#graph");
return graph.get(0);
}
private static Map<String, Object> getFramedJson(Object json) {
try {
return JsonLdProcessor.frame(json, getFrame(), new JsonLdOptions());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static Map<String, Object> getFrame() {
Map<String, Object> context = new HashMap<>();
context.put("#context", "http://schema.org/");
// if this does not work use the direct address as follows
// context.put("#context","https://schema.org/docs/jsonldcontext.jsonld");
return context;
}
private static Object createJsonObject(String ld) {
try (InputStream inputStream =
new ByteArrayInputStream(ld.getBytes(Charsets.UTF_8))) {
Object jsonObject = JsonUtils.fromInputStream(inputStream);
return jsonObject;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Collection<Statement> readRdfToGraph(
final InputStream inputStream, final RDFFormat inf,
final String baseUrl) {
try {
final RDFParser rdfParser = Rio.createParser(inf);
final StatementCollector collector = new StatementCollector();
rdfParser.setRDFHandler(collector);
rdfParser.parse(inputStream, baseUrl);
return collector.getStatements();
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
public static String readRdfToString(InputStream in, RDFFormat inf,
RDFFormat outf, String baseUrl) {
Collection<Statement> myGraph = null;
myGraph = readRdfToGraph(in, inf, baseUrl);
return graphToString(myGraph, outf);
}
public static String graphToString(Collection<Statement> myGraph,
RDFFormat outf) {
StringWriter out = new StringWriter();
RDFWriter writer = Rio.createWriter(outf, out);
try {
writer.startRDF();
for (Statement st : myGraph) {
writer.handleStatement(st);
}
writer.endRDF();
} catch (RDFHandlerException e) {
throw new RuntimeException(e);
}
return out.getBuffer().toString();
}
}
Prints
{
"id" : "_:b0",
"type" : "Book",
"name" : "Semantic Web Primer (First Edition)",
"offers" : {
"id" : "_:b1",
"type" : "Offer",
"price" : "2.95",
"priceCurrency" : "USD"
},
"publisher" : "Linked Data Tools"
}
With pom
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-runtime</artifactId>
<version>2.2</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.jsonld-java</groupId>
<artifactId>jsonld-java</artifactId>
<version>0.10.0</version>
</dependency>

Related

Generate JSON from map values

I have written a method which takes map entity and location as parameters, using Jackson object mapper
public class EntityGenerator {
private static void generatePartiallySearchableEntity(Map<String, Set<String>> synMap, String root_dir_loc) throws Exception {
Set < String > namedEntitySet = null;
synMap.put("severity", namedEntitySet);
ObjectMapper mapperobj = new ObjectMapper();
mapperobj.writeValue(new File(root_dir_loc), synMap);
System.out.println("Test.json file created");
}
public static void main(String args[]) throws Exception {
Map < String, Set < String >> sysMap = new HashMap<String, Set<String>>();
Set < String > severityEntitySet = new HashSet<String>();
severityEntitySet.add("Critical");
severityEntitySet.add("Error ");
severityEntitySet.add("Warning ");
severityEntitySet.add("Information ");
sysMap.put("Severity", severityEntitySet);
Set < String > impactEntitySet = new HashSet<String>();
impactEntitySet.add("Inciden");
impactEntitySet.add("Risk");
impactEntitySet.add("Event");
sysMap.put("Imapct", impactEntitySet);
String root_dir_loc = "C:\\Users\\rakshitm\\Documents\\test.json";
generatePartiallySearchableEntity(sysMap, root_dir_loc);
}
I'm getting JSON output like this, like different what I expected from
{"severity":null,"Severity":["Error ","Information ","Critical","Warning "],"Imapct":["Inciden","Risk","Event"]}
I need JSON output of this type
[
{
"value": "event",
"synonyms": [
"event"
]
},
{
"value": "impact",
"synonyms": [
"impact"
]
},
{
"value": "severity",
"synonyms": [
"severity"
]
},
{
"value": "notes",
"synonyms": [
"notes"
]
}
]
See the code below:
package com.abhi.learning.stackoverflow;
import java.util.List;
public class Example {
private String value;
private List<String> synonyms = null;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<String> getSynonyms() {
return synonyms;
}
public void setSynonyms(List<String> synonyms) {
this.synonyms = synonyms;
}
}
Main Class:
package com.abhi.learning.stackoverflow;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
Example emp = new Example();
ArrayList<String> al = new ArrayList<String>();
al.add("Critical");
al.add("Error ");
al.add("Warning ");
emp.setSynonyms(al);
emp.setValue("Severity");
Example emp1 = new Example();
ArrayList<String> al1 = new ArrayList<String>();
al1.add("Inciden");
al1.add("Risk");
al1.add("Event");
emp1.setSynonyms(al1);
emp1.setValue("Imapct");
List<Example> lstEx = new ArrayList<Example>();
lstEx.add(emp1);
lstEx.add(emp);
try {
objectMapper.writeValue(new File("target/employee.json"), lstEx);
} catch ( IOException e) {
e.printStackTrace();
}
}
}
This is the json i got :
[
{
"value":"Imapct",
"synonyms":[
"Inciden",
"Risk",
"Event"
]
},
{
"value":"Severity",
"synonyms":[
"Critical",
"Error ",
"Warning "
]
}
]
You can add more Example objects for "events" & "Notes"
You need a Pojo of following specification.
class Event {
private String value;
private List<String> synonyms;
}
Then you make a list of this class and that would parse your Json correctly.
List<Event> events = new ArrayList<>();

NullPointerException in Custom ClassLoader - Can't find zipEntry

I'm making a classloader which can take a jar, and based on that, it can take a package name, and is allowed to load only classes that are in that package. When I try to load a class from there I get the error :
Exception in thread "main" java.lang.NullPointerException
at com.classloader.CustomClassLoader.getClassBytes(CustomClassLoader.java:64)
at com.classloader.CustomClassLoader.getClass(CustomClassLoader.java:48)
at com.classloader.CustomClassLoader.loadClass(CustomClassLoader.java:89)
at com.classloader.TestJar.main(TestJar.java:46)
Even though the package name and entry name are correct. Any ideas where the problem is and how to fix it ? The code is below and I believe it's pretty self explanatory.
CustomLoader
package com.classloader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
public class CustomClassLoader extends ClassLoader implements LoaderConstraints {
private Map<String, Class> loaded = new HashMap<String, Class>();
private Map<String, String> available = new LinkedHashMap<String, String>();
private Set<String> allowed = new LinkedHashSet<String>();
public Set<String> getPermited() {
return allowed;
}
public Map<String, String> getAvailable() {
return available;
}
public Map<String, Class> getLoaded() {
return loaded;
}
public CustomClassLoader() {
super(CustomClassLoader.class.getClassLoader());
}
private Class<?> getClass(String className, String pack) throws ClassNotFoundException {
Class<?> c = null;
String classPath = className.replace('.', File.separatorChar) + ".class";
byte[] b = null;
try {
b = getClassBytes(classPath, pack);
c = defineClass(className, b, 0, b.length);
resolveClass(c);
return c;
} catch (IOException e) {
e.printStackTrace();
}
return c;
}
private byte[] getClassBytes(String classPath, String pack) throws IOException {
ZipFile zip = new ZipFile(pack);
// System.out.println(classPath); classPath is right , as well as pack
ZipEntry entry = zip.getEntry(classPath);//This return null, for some reason ???
InputStream in = zip.getInputStream(zip.getEntry(classPath));
long size = entry.getSize();
byte buff[] = new byte[(int)size];
in.read(buff);
in.close();
return buff;
}
#Override
public Class<?> loadClass(String className) throws ClassNotFoundException {
Class<?> found = null;
if(loaded.get(className) != null){
return loaded.get(className);
}
if(available.get(className ) != null){
if(allowed.contains(className ) == true){
found = getClass(className, available.get(className));
if(found != null)
loaded.put(className, found);
}
}
else{
found = super.loadClass(className);
if(found != null)
loaded.put(className, found);
}
return found;
}
public void files(ZipFile zip, Map<String,String> list) throws ZipException, IOException{
Enumeration<? extends ZipEntry> entries = zip.entries();
while(entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory() && entry.getName().endsWith(".class")) {
String className = entry.getName().replace('/', '.');
list.put(className.substring(0, className.length() - ".class".length())
,zip.getName());
}
}
}
public void addJar (File jarFile) {
try {
files(new ZipFile(jarFile), available);
} catch (ZipException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteJar(File jarFile) {
Map<String,String> removes = new HashMap<String, String>();
try {
files(new ZipFile(jarFile), removes );
} catch (ZipException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
available.keySet().removeAll(removes.keySet());
}
public void allowPackage(final String p) {
for(String s : available.keySet()){
if(s.startsWith(p))
allowed.add(new String(s));
}
}
public void denyPackage(final String p) {
Set<String> newPermited = new HashSet<String>();
for(String s : allowed){
if(!s.startsWith(p))
newPermited.add(new String(s));
}
allowed = newPermited;
}
}
Test
public static void main(String[] args) throws ZipException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
CustomClassLoader ccl = new CustomClassLoader();
ccl.addJar(new File("derby.jar"));
ccl.allowPackage("org.apache.derby.jdbc");
//System.out.println(ccl.getAvailable().get("org.apache.derby.jdbc.EmbeddedDriver")); --> returns "derby.jar"
Class<?> clazz = ccl.loadClass("org.apache.derby.jdbc.EmbeddedDriver");//Gives exception , line 46
System.out.println(clazz.getClass());
Object instance = clazz.newInstance();
System.out.println(instance.getClass());
}
The derby.jar is located in the project folder. Any help is welcomed :)
Try to replace File.separatorChar with '/'

Different Result on DBPedia Spotlight by using the code and DBPedia Spotlight endpoint

This is the main class in which query is being fired
package extractKeyword;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.methods.GetMethod;
import org.dbpedia.spotlight.exceptions.AnnotationException;
import org.dbpedia.spotlight.model.DBpediaResource;
import org.dbpedia.spotlight.model.Text;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.LinkedList;
import java.util.List;
public class db extends AnnotationClient {
//private final static String API_URL = "http://jodaiber.dyndns.org:2222/";
private static String API_URL = "http://spotlight.dbpedia.org/";
private static double CONFIDENCE = 0.0;
private static int SUPPORT = 0;
// private static String powered_by ="non";
// private static String spotter ="CoOccurrenceBasedSelector";//"LingPipeSpotter"=Annotate all spots
//AtLeastOneNounSelector"=No verbs and adjs.
//"CoOccurrenceBasedSelector" =No 'common words'
//"NESpotter"=Only Per.,Org.,Loc.
//private static String disambiguator ="Default";//Default ;Occurrences=Occurrence-centric;Document=Document-centric
//private static String showScores ="yes";
#SuppressWarnings("static-access")
public void configiration(double CONFIDENCE,int SUPPORT)
//, String powered_by,String spotter,String disambiguator,String showScores)
{
this.CONFIDENCE=CONFIDENCE;
this.SUPPORT=SUPPORT;
// this.powered_by=powered_by;
//this.spotter=spotter;
//this.disambiguator=disambiguator;
//showScores=showScores;
}
public List<DBpediaResource> extract(Text text) throws AnnotationException {
// LOG.info("Querying API.");
String spotlightResponse;
try {
String Query=API_URL + "rest/annotate/?" +
"confidence=" + CONFIDENCE
+ "&support=" + SUPPORT
// + "&spotter=" + spotter
// + "&disambiguator=" + disambiguator
// + "&showScores=" + showScores
// + "&powered_by=" + powered_by
+ "&text=" + URLEncoder.encode(text.text(), "utf-8");
//LOG.info(Query);
GetMethod getMethod = new GetMethod(Query);
getMethod.addRequestHeader(new Header("Accept", "application/json"));
spotlightResponse = request(getMethod);
} catch (UnsupportedEncodingException e) {
throw new AnnotationException("Could not encode text.", e);
}
assert spotlightResponse != null;
JSONObject resultJSON = null;
JSONArray entities = null;
try {
resultJSON = new JSONObject(spotlightResponse);
entities = resultJSON.getJSONArray("Resources");
} catch (JSONException e) {
//throw new AnnotationException("Received invalid response from DBpedia Spotlight API.");
}
LinkedList<DBpediaResource> resources = new LinkedList<DBpediaResource>();
if(entities!=null)
for(int i = 0; i < entities.length(); i++) {
try {
JSONObject entity = entities.getJSONObject(i);
resources.add(
new DBpediaResource(entity.getString("#URI"),
Integer.parseInt(entity.getString("#support"))));
} catch (JSONException e) {
//((Object) LOG).error("JSON exception "+e);
}
}
return resources;
}
}
The extended class
package extractKeyword;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.dbpedia.spotlight.exceptions.AnnotationException;
import org.dbpedia.spotlight.model.DBpediaResource;
import org.dbpedia.spotlight.model.Text;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;
import javax.ws.rs.HttpMethod;
/**
* #author pablomendes
*/
public abstract class AnnotationClient {
//public Logger LOG = Logger.getLogger(this.getClass());
private List<String> RES = new ArrayList<String>();
// Create an instance of HttpClient.
private static HttpClient client = new HttpClient();
public List<String> getResu(){
return RES;
}
public String request(GetMethod getMethod) throws AnnotationException {
String response = null;
// Provide custom retry handler is necessary
( getMethod).getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
try {
// Execute the method.
int statusCode = client.executeMethod((org.apache.commons.httpclient.HttpMethod) getMethod);
if (statusCode != HttpStatus.SC_OK) {
// LOG.error("Method failed: " + ((HttpMethodBase) method).getStatusLine());
}
// Read the response body.
byte[] responseBody = ((HttpMethodBase) getMethod).getResponseBody(); //TODO Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
response = new String(responseBody);
} catch (HttpException e) {
// LOG.error("Fatal protocol violation: " + e.getMessage());
throw new AnnotationException("Protocol error executing HTTP request.",e);
} catch (IOException e) {
//((Object) LOG).error("Fatal transport error: " + e.getMessage());
//((Object) LOG).error(((HttpMethodBase) method).getQueryString());
throw new AnnotationException("Transport error executing HTTP request.",e);
} finally {
// Release the connection.
((HttpMethodBase) getMethod).releaseConnection();
}
return response;
}
protected static String readFileAsString(String filePath) throws java.io.IOException{
return readFileAsString(new File(filePath));
}
protected static String readFileAsString(File file) throws IOException {
byte[] buffer = new byte[(int) file.length()];
#SuppressWarnings("resource")
BufferedInputStream f = new BufferedInputStream(new FileInputStream(file));
f.read(buffer);
return new String(buffer);
}
static abstract class LineParser {
public abstract String parse(String s) throws ParseException;
static class ManualDatasetLineParser extends LineParser {
public String parse(String s) throws ParseException {
return s.trim();
}
}
static class OccTSVLineParser extends LineParser {
public String parse(String s) throws ParseException {
String result = s;
try {
result = s.trim().split("\t")[3];
} catch (ArrayIndexOutOfBoundsException e) {
throw new ParseException(e.getMessage(), 3);
}
return result;
}
}
}
public void saveExtractedEntitiesSet(String Question, LineParser parser, int restartFrom) throws Exception {
String text = Question;
int i=0;
//int correct =0 ; int error = 0;int sum = 0;
for (String snippet: text.split("\n")) {
String s = parser.parse(snippet);
if (s!= null && !s.equals("")) {
i++;
if (i<restartFrom) continue;
List<DBpediaResource> entities = new ArrayList<DBpediaResource>();
try {
entities = extract(new Text(snippet.replaceAll("\\s+"," ")));
System.out.println(entities.get(0).getFullUri());
} catch (AnnotationException e) {
// error++;
//LOG.error(e);
e.printStackTrace();
}
for (DBpediaResource e: entities) {
RES.add(e.uri());
}
}
}
}
public abstract List<DBpediaResource> extract(Text text) throws AnnotationException;
public void evaluate(String Question) throws Exception {
evaluateManual(Question,0);
}
public void evaluateManual(String Question, int restartFrom) throws Exception {
saveExtractedEntitiesSet(Question,new LineParser.ManualDatasetLineParser(), restartFrom);
}
}
The Main Class
package extractKeyword;
public class startAnnonation {
public static void main(String[] args) throws Exception {
String question = "What is the winning chances of BJP in New Delhi elections?";
db c = new db ();
c.configiration(0.25,0);
//, 0, "non", "AtLeastOneNounSelector", "Default", "yes");
c.evaluate(question);
System.out.println("resource : "+c.getResu());
}
}
The main problem is here when I am using DBPedia spotlight using spotlight jar (above code)then i am getting different result as compared to the dbpedia spotlight endpoint(dbpedia-spotlight.github.io/demo/)
Result using the above code:-
Text :-What is the winning chances of BJP in New Delhi elections?
Confidence level:-0.35
resource : [Election]
Result on DBPedia Spotlight endpoint(//dbpedia-spotlight.github.io/demo/)
Text:-What is the winning chances of BJP in New Delhi elections?
Confidence level:-0.35
resource : [Bharatiya_Janata_Party, New_Delhi, Election]
Why also the spotlight now don't have support as a parameter?

Parameterized runner class with 2 arguments in constructor

I wish to use a Parameterized Junit class to read from a .csv file. I want to:-
Read a 'placeID' (a String) and append it to a base url to form a webpage
Assert that the Place name 'name' (a String) is as I expect it to be for the place
The tab delimited .csv file contains 2 records as follows (will have 100's records eventually):
132
The Big House
I'm currently getting an Illegal argument exception. What's a slicker way of achieving this? I guess having the relative URL and then test data in seperate files would be better.
My code:
#RunWith(Parameterized.class)
public class PlaceTest {
public static WebDriver driver;
private String placeId;
private String name;
private PropertyPage propertyPage;
public PlaceTest(String page, String name) {
this.placeId = page;
this.name = name;
}
#Parameterized.Parameters
public static Collection data() {
return csvFileAsCollectionOfStringArrays(
System.getProperty("user.dir") +
"/src/test/resources/" +
"place_ids.csv");
}
private static Collection<String[]> csvFileAsCollectionOfStringArrays(String csvFileName) {
List<String[]> csvRows = new ArrayList<String[]>();
String rawCSVRow;
BufferedReader csvFileReader = null;
String delimiter = "\t";
System.out.println("Reading data from " + csvFileName);
try {
csvFileReader = new BufferedReader(new FileReader(csvFileName));
} catch (FileNotFoundException e) {
System.out.println("Could not find file " + csvFileName);
e.printStackTrace();
}
int rowNumber = 1;
try {
if (csvFileReader != null) {
while ((rawCSVRow = csvFileReader.readLine()) != null) {
String delimitedItems[] = rawCSVRow.split(delimiter);
csvRows.add(delimitedItems);
rowNumber++;
}
}
} catch (IOException e) {
System.out.println("Error reading row number " + rowNumber);
e.printStackTrace();
}
try {
assert csvFileReader != null;
csvFileReader.close();
} catch (IOException e) {
System.out.println("Error closing file " + e.getMessage());
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return csvRows;
}
#BeforeClass
public static void startDriver() {
driver = Driver.get();
}
#Before
public void getNextPage() {
propertyPage = new PropertyPage(driver);
driver.get(TestWebApp.getURL() + this.placeId);
}
#Test
public void checkNamePresent() {
WebElement placeName = propertyPage.checkName();
assertEquals("Expected match on name", this.name, placeName.getText());
}
#AfterClass
public static void quitDriver() {
driver.quit();
}
}
Try this:
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import au.com.bytecode.opencsv.CSVReader;
#RunWith(Parameterized.class)
public class PlaceTest {
private String placeId;
private String name;
public PlaceTest(String page, String name) {
this.placeId = page;
this.name = name;
}
#Parameterized.Parameters
public static Collection<String[]> data() {
CSVReader reader = new CSVReader(new InputStreamReader(PlaceTest.class.getResourceAsStream("place_ids.csv")));
List<String[]> lines;
try {
lines = reader.readAll();
return lines;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ArrayList<String[]>();
}
#Test
public void checkNamePresent() {
System.out.println(this.placeId + " " + this.name);
}
}
The place_ids.csv has to be in: \src\test\resources\<your package>\place_ids.csv
Update your pom with CSVReader dependency:
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
Update:
csv file:
132, Some text
133, Other text
Your example above has one word per line. The code above was compile and tested.

Sonar plugin, how to save violations

I'm developing a sonar plugin to analyze TrueScript code using tslint.
I downloaded exampled plugin from [github.com/SonarSource/sonar-examples] and edited ExampleSensor.java [github.com/SonarSource/sonar-examples/tree/master/plugins/sonar-reference-plugin/src/main/java/com/mycompany/sonar/reference/batch]. Now my sensor looks as in this file
package com.mycompany.sonar.reference.batch;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import org.apache.commons.io.IOUtils;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.Sensor;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.config.Settings;
import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.Violation;
import pl.sollers.utils.FileSearcher;
public class ExampleSensor implements Sensor {
private static final Logger LOG = LoggerFactory.getLogger(ExampleSensor.class);
private Settings settings;
/**
* Use of IoC to get Settings
*/
public ExampleSensor(Settings settings) {
this.settings = settings;
}
public boolean shouldExecuteOnProject(Project project) {
// This sensor is executed only for ts project type
return project.getLanguageKey().equals("js");
}
public void analyse(Project project, SensorContext sensorContext) {
// getting all files for analyzing
Collection<File> files = FileSearcher.getByExtensionRecursively("ts");
Process tslintProces;
JSONParser jsonParser = new JSONParser();
JSONArray warnings;
Object jsonObj;
for (File file : files) {
try {
// run tslint process and analyze file
tslintProces = new ProcessBuilder("tslint.cmd", "-c", "./tslint.json", "-t",
"json", "-f", file.getCanonicalPath()).start();
// copy tslint output
StringWriter writer = new StringWriter();
IOUtils.copy(tslintProces.getInputStream(), writer, "UTF-8");
String json = writer.toString();
// parse output and extract violation message, ruleName, line
jsonObj = jsonParser.parse(json);
if (jsonObj instanceof JSONArray) {
warnings = (JSONArray) (jsonObj);
} else {
throw new Exception("Oczekiwano obiektu klasy JSONArray");
}
for (int i = 0; i < warnings.size(); i++) {
JSONObject warning = (JSONObject) warnings.get(i);
String message = (String) warning.get("failure");
String ruleName = (String) warning.get("ruleName");
JSONObject position = (JSONObject) warning.get("startPosition");
Long line = (Long) position.get("line");
// nie widzę pola w Sonarze aby użyć numeru znaku w linii
Long character = (Long) position.get("character");
// HELP! I DO NOT KNOW HOW TO STORE VIOLATION IN SONAR
// FOLLOWING LINES DOES NOT WORK
// Rule rule = Rule.create("repositoryKey",
String.format("%s-%s", "key", ruleName), ruleName);
// org.sonar.api.resources.File resource = new org.sonar.api.resources.File(file
.getParentFile().getCanonicalPath(), file.getName());
// Violation violation = Violation.create(rule, resource);
// violation.setLineId(line.intValue());
// violation.setMessage(message);
// sensorContext.saveViolation(violation);
}
} catch (IOException e) {
LOG.error(e.getMessage());
e.printStackTrace();
} catch (ParseException e) {
LOG.error(e.getMessage());
e.printStackTrace();
} catch (Exception e) {
LOG.error(e.getMessage());
}
}
}
#Override
public String toString() {
return getClass().getSimpleName();
}
}
I have a problem in saving violations in Sonar database. This part of code should be in lines 80-90. Could anyone help me to store violations.
You should use Issueable rather than Violations (because it's deprecated and will be removed in 4.3).
import org.sonar.api.component.ResourcePerspectives;
public class MySensor extends Sensor {
private final ResourcePerspectives perspectives;
public MySensor(ResourcePerspectives p) {
this.perspectives = p;
}
public void analyse(Project project, SensorContext context) {
Resource myResource; // to be set
Issuable issuable = perspectives.as(Issuable.class, myResource);
if (issuable != null) {
// can be used
Issue issue = issuable.newIssueBuilder()
.setRuleKey(RuleKey.of("pmd", "AvoidArrayLoops")
.setLine(10)
.build();
issuable.addIssue(issue);
}
}
}

Categories