2020-01-28 16:53:59 +01:00
|
|
|
package eu.dnetlib.ariadneplus.reader.json;
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
|
2020-06-16 02:36:16 +02:00
|
|
|
import eu.dnetlib.ariadneplus.reader.RunSPARQLQueryService;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
2020-01-28 16:53:59 +01:00
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import com.jayway.jsonpath.DocumentContext;
|
|
|
|
import com.jayway.jsonpath.JsonPath;
|
|
|
|
|
|
|
|
import net.minidev.json.JSONArray;
|
|
|
|
import net.minidev.json.JSONObject;
|
|
|
|
import net.minidev.json.parser.JSONParser;
|
|
|
|
import net.minidev.json.parser.ParseException;
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
|
public class ParseRDFJSON {
|
|
|
|
|
2020-06-16 02:36:16 +02:00
|
|
|
private static final Log log = LogFactory.getLog(ParseRDFJSON.class);
|
|
|
|
|
2020-01-28 16:53:59 +01:00
|
|
|
static JSONObject map ;
|
|
|
|
|
2020-06-12 18:14:41 +02:00
|
|
|
@Value("${catalog.entry.path}")
|
2020-06-10 19:39:53 +02:00
|
|
|
private String catalogEntryJsonPath;
|
2020-01-28 16:53:59 +01:00
|
|
|
|
2020-06-16 02:36:16 +02:00
|
|
|
@Value("${catalog.entry.collection.path}")
|
|
|
|
private String catalogEntryCollectionJsonPath;
|
|
|
|
|
|
|
|
private boolean isCollection = false;
|
|
|
|
|
2020-01-28 16:53:59 +01:00
|
|
|
private String json;
|
|
|
|
|
|
|
|
private Iterator<Object> it ;
|
|
|
|
|
|
|
|
public String getJson() {
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setJson(String json) throws ParseException {
|
|
|
|
this.json = json;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void fillMap() throws ParseException {
|
|
|
|
map = (JSONObject)(new JSONParser(JSONParser.MODE_PERMISSIVE).parse(json));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void parse(String json) throws ParseException {
|
|
|
|
setJson(json);
|
|
|
|
fillMap();
|
|
|
|
DocumentContext jsonContext = JsonPath.parse(json);
|
2020-06-16 02:36:16 +02:00
|
|
|
log.debug(getCatalogEntryJsonPath());
|
2020-06-10 19:39:53 +02:00
|
|
|
JSONArray entries = jsonContext.read(getCatalogEntryJsonPath());
|
2020-01-28 16:53:59 +01:00
|
|
|
int size = entries.size();
|
2020-06-16 02:36:16 +02:00
|
|
|
log.debug("num elements in json: "+size);
|
2020-01-28 16:53:59 +01:00
|
|
|
it = entries.iterator();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasNextElement(){
|
|
|
|
return it.hasNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
public LinkedHashMap getNextElement(){
|
|
|
|
return (LinkedHashMap)it.next();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static JSONObject get(String key){
|
|
|
|
return (JSONObject) map.get(key);
|
|
|
|
}
|
|
|
|
|
2020-06-10 19:39:53 +02:00
|
|
|
public String getCatalogEntryJsonPath() {
|
2020-06-16 02:36:16 +02:00
|
|
|
if (isCollection) {
|
|
|
|
return catalogEntryCollectionJsonPath;
|
|
|
|
}
|
2020-06-10 19:39:53 +02:00
|
|
|
return catalogEntryJsonPath;
|
|
|
|
}
|
2020-01-28 16:53:59 +01:00
|
|
|
|
2020-06-10 19:39:53 +02:00
|
|
|
public void setCatalogEntryJsonPath(String catalogEntryJsonPath) {
|
|
|
|
this.catalogEntryJsonPath = catalogEntryJsonPath;
|
|
|
|
}
|
2020-06-16 02:36:16 +02:00
|
|
|
|
|
|
|
public boolean isCollection() {
|
|
|
|
return isCollection;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCollection(boolean collection) {
|
|
|
|
isCollection = collection;
|
|
|
|
}
|
2020-01-28 16:53:59 +01:00
|
|
|
}
|