eu.unicore.uas.json.Builder Maven / Gradle / Ivy
The newest version!
package eu.unicore.uas.json;
import java.io.File;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.io.FileUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import eu.unicore.client.Job;
/**
* Helper to convert a UNICORE job description in JSON to JSDL form
*
* @author schuller
*/
public class Builder {
protected final List sweeps;
protected final JSONObject json;
protected final Setrequirements=new HashSet<>();
protected boolean initialised;
protected boolean isSweepJob=false;
protected String preferredProtocol = "BFT";
/**
* reads a JSON string from the supplied File
* and creates the builder from it
*
* @param jsonFile
* @throws Exception
*/
public Builder(File jsonFile)throws Exception{
this(FileUtils.readFileToString(jsonFile, "UTF-8"));
}
/**
* Creates the builder from the supplied JSON string
*
* @param jsonString
* @throws IllegalArgumentException on JSON parsing errors
*/
public Builder(String jsonString) {
sweeps = new ArrayList<>();
try{
json=JSONUtil.read(jsonString);
isSweepJob = json.optBoolean("isSweepJob", false);
}catch(JSONException ex){
String message=JSONUtil.makeParseErrorMessage(jsonString, ex);
throw new IllegalArgumentException(message);
}
}
/**
* Creates an empty builder. All content has to be set via the API
* @throws Exception
*/
public Builder()throws Exception{
this("{}");
json.put("Output",".");
}
public JSONObject getJSON() {
return json;
}
public String[] getTags(){
JSONArray tags = json.optJSONArray("Tags");
if(tags==null)tags = json.optJSONArray("tags");
if(tags!=null){
String[] ret = new String[tags.length()];
for(int i=0;igetRequirements(){
build();
return requirements;
}
@Override
public String toString(){
return json.toString(2);
}
public boolean isSweepJob(){
return isSweepJob;
}
}