AriadnePlus/dnet-ariadneplus-graphdb-pu.../src/main/java/eu/dnetlib/ariadneplus/reader/json/ParseRDFJSON.java

96 lines
2.4 KiB
Java

package eu.dnetlib.ariadneplus.reader.json;
import java.util.Iterator;
import java.util.LinkedHashMap;
import eu.dnetlib.ariadneplus.reader.RunSPARQLQueryService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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 {
private static final Log log = LogFactory.getLog(ParseRDFJSON.class);
static JSONObject map ;
@Value("${catalog.entry.path}")
private String catalogEntryJsonPath;
@Value("${catalog.entry.collection.path}")
private String catalogEntryCollectionJsonPath;
private boolean isCollection = false;
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);
log.debug(getCatalogEntryJsonPath());
JSONArray entries = jsonContext.read(getCatalogEntryJsonPath());
int size = entries.size();
log.debug("num elements in json: "+size);
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);
}
public String getCatalogEntryJsonPath() {
if (isCollection) {
return catalogEntryCollectionJsonPath;
}
return catalogEntryJsonPath;
}
public void setCatalogEntryJsonPath(String catalogEntryJsonPath) {
this.catalogEntryJsonPath = catalogEntryJsonPath;
}
public boolean isCollection() {
return isCollection;
}
public void setCollection(boolean collection) {
isCollection = collection;
}
}