Integrated with the library Gson
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/dataminer-invocation-model@176035 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
d95591219e
commit
086f63d5be
|
@ -0,0 +1,126 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
package org.gcube.data.analysis.dminvocation;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
|
||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The Class DataMinerInvocationJSONAdaptor.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
* Dec 20, 2018
|
||||
*/
|
||||
public class DataMinerInvocationJSONAdaptor implements JsonDeserializer<DataMinerInvocation>, JsonSerializer<DataMinerInvocation>{
|
||||
|
||||
List<String> requiredFields = new ArrayList<String>();
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(DataMinerInvocationJSONAdaptor.class);
|
||||
|
||||
/**
|
||||
* Register required field.
|
||||
*
|
||||
* @param fieldName the field name
|
||||
*/
|
||||
void registerRequiredField(String fieldName) {
|
||||
|
||||
requiredFields.add(fieldName);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
|
||||
*/
|
||||
@Override
|
||||
public DataMinerInvocation deserialize(
|
||||
JsonElement json, Type arg1, JsonDeserializationContext arg2)
|
||||
throws JsonParseException {
|
||||
|
||||
JsonObject jsonObject = (JsonObject) json;
|
||||
for (String fieldName : requiredFields) {
|
||||
if (jsonObject.get(fieldName) == null) {
|
||||
throw new JsonParseException("Required Field Not Found: " +fieldName);
|
||||
}
|
||||
}
|
||||
return new Gson().fromJson(json, DataMinerInvocation.class);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.google.gson.JsonSerializer#serialize(java.lang.Object, java.lang.reflect.Type, com.google.gson.JsonSerializationContext)
|
||||
*/
|
||||
@Override
|
||||
public JsonElement serialize(DataMinerInvocation dmi, Type arg1, JsonSerializationContext arg2) throws JsonParseException{
|
||||
|
||||
try{
|
||||
List<String> errors = new ArrayList<String>();
|
||||
checkRequiredFiels(dmi, errors);
|
||||
if(errors.isEmpty()){
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
return gson.toJsonTree(dmi);
|
||||
}
|
||||
|
||||
throw new JsonParseException(errors.toString());
|
||||
|
||||
}catch(Exception e){
|
||||
log.error("Error on check required fields: ", e);
|
||||
throw new JsonParseException("Error on validating the instance: "+dmi, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static void checkRequiredFiels(Object theInstance, List<String> errors) throws IllegalArgumentException, IllegalAccessException{
|
||||
|
||||
log.info("Checking instance: "+theInstance+" of Class: "+theInstance.getClass());
|
||||
|
||||
for(Field field : theInstance.getClass().getDeclaredFields()){
|
||||
Class<?> type = field.getType();
|
||||
String name = field.getName();
|
||||
Annotation[] annotations = field.getDeclaredAnnotations();
|
||||
log.debug("Field: "+name);
|
||||
for (Annotation annotation : annotations) {
|
||||
if(annotation instanceof XmlElement){
|
||||
field.setAccessible(true);
|
||||
XmlElement xmlEl = (XmlElement) annotation;
|
||||
if(xmlEl.required()){
|
||||
Object value = field.get(theInstance);
|
||||
if(value==null){
|
||||
String error = "Required field '"+xmlEl.name() + "' of "+type+" is null";
|
||||
log.error("Required field '"+xmlEl.name() + "' is null");
|
||||
errors.add(error);
|
||||
}else if (String.class.equals(type) && ((String) value).isEmpty()){
|
||||
String error = "Required field '"+xmlEl.name() + "' of "+type+" is empty";
|
||||
log.error("Required field '"+xmlEl.name() + "' is null");
|
||||
errors.add(error);
|
||||
}else if(!type.isPrimitive() && !String.class.equals(type)){
|
||||
log.debug("The field "+xmlEl.name()+ " is not a primitive or a String\n\n");
|
||||
checkRequiredFiels(value, errors);
|
||||
}
|
||||
}
|
||||
}else
|
||||
log.trace("Another annotation (not instance of XmlElement): "+annotation);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -18,7 +18,7 @@ import com.google.gson.annotations.SerializedName;
|
|||
|
||||
/**
|
||||
* The Class DataMinerInvocationJSONWrapper.
|
||||
* Used only to add the root name "dataminer-invocation" in the JSON
|
||||
* Used just to add the root name "dataminer-invocation" in the JSON
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
* Dec 19, 2018
|
||||
*/
|
||||
|
|
|
@ -84,18 +84,21 @@ public class DataMinerInvocationManager {
|
|||
|
||||
|
||||
/**
|
||||
* Marshaling.
|
||||
* Marshaling xml.
|
||||
*
|
||||
* @param dmInvocation the dm invocation
|
||||
* @param validateModel the validate model. If true performs model validation against the xml schema generated for {@link DataMinerInvocation}
|
||||
* @return the marshal XML
|
||||
* @param validateModel the validate model
|
||||
* @param prettyPrint set Pretty Printing
|
||||
* @return the string
|
||||
* @throws JAXBException the JAXB exception
|
||||
*/
|
||||
public String marshalingXML(DataMinerInvocation dmInvocation, boolean validateModel) throws JAXBException
|
||||
public String marshalingXML(DataMinerInvocation dmInvocation, boolean validateModel, boolean prettyPrint) throws JAXBException
|
||||
{
|
||||
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
||||
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
if(prettyPrint)
|
||||
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||
|
||||
if(validateModel)
|
||||
jaxbMarshaller.setSchema(schema);
|
||||
|
||||
|
@ -110,23 +113,30 @@ public class DataMinerInvocationManager {
|
|||
* Marshaling json.
|
||||
*
|
||||
* @param dmInvocation the dm invocation
|
||||
* @return the marshal JSON
|
||||
* @param validateModel the validate model. If true checks the input DataMinerInvocation against the dataminer-invocation-model
|
||||
* @param prettyPrint set Pretty Printing
|
||||
* @return the string
|
||||
* @throws JAXBException the JAXB exception
|
||||
*/
|
||||
public String marshalingJSON(DataMinerInvocation dmInvocation) throws JAXBException
|
||||
{
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
public String marshalingJSON(DataMinerInvocation dmInvocation, boolean validateModel, boolean prettyPrint) throws JAXBException{
|
||||
|
||||
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
DataMinerInvocationJSONAdaptor jsonAdaptor = new DataMinerInvocationJSONAdaptor();
|
||||
jsonAdaptor.registerRequiredField("operator-id");
|
||||
gsonBuilder.registerTypeAdapter(DataMinerInvocation.class, jsonAdaptor);
|
||||
|
||||
Gson gson = prettyPrint?gsonBuilder.setPrettyPrinting().create():gsonBuilder.create();
|
||||
DataMinerInvocationJSONWrapper wrapper = new DataMinerInvocationJSONWrapper(dmInvocation);
|
||||
return gson.toJson(wrapper);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Unmarshaling xml.
|
||||
*
|
||||
* @param dmInvocationIS the dm invocation input stream
|
||||
* @param validateModel the validate model. If true performs model validation against the xml schema generated for {@link DataMinerInvocation}
|
||||
* @param dmInvocationIS the dm invocation is
|
||||
* @param validateModel the validate model. If true checks the InputStream against the dataminer-invocation-model
|
||||
* @return the data miner invocation
|
||||
* @throws JAXBException the JAXB exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
|
@ -146,16 +156,24 @@ public class DataMinerInvocationManager {
|
|||
* Unmarshaling json.
|
||||
*
|
||||
* @param dmInvocationIS the dm invocation is
|
||||
* @param validate the validate
|
||||
* @return the data miner invocation
|
||||
* @throws JsonSyntaxException the json syntax exception
|
||||
* @throws IOException Signals that an I/O exception has occurred.
|
||||
*/
|
||||
public DataMinerInvocation unmarshalingJSON(InputStream dmInvocationIS) throws JsonSyntaxException, IOException {
|
||||
String json = convertToString(dmInvocationIS);
|
||||
Gson gson = new Gson();
|
||||
DataMinerInvocationJSONWrapper wrapper = gson.fromJson(json, DataMinerInvocationJSONWrapper.class);
|
||||
return wrapper.getDataminerInvocation();
|
||||
public DataMinerInvocation unmarshalingJSON(InputStream dmInvocationIS, boolean validate) throws JsonSyntaxException, IOException {
|
||||
|
||||
GsonBuilder gsonBuilder = new GsonBuilder();
|
||||
|
||||
if(validate){
|
||||
DataMinerInvocationJSONAdaptor jsonAdaptor = new DataMinerInvocationJSONAdaptor();
|
||||
gsonBuilder.registerTypeAdapter(DataMinerInvocation.class, jsonAdaptor);
|
||||
}
|
||||
|
||||
Gson gson = gsonBuilder.create();
|
||||
String json = convertToString(dmInvocationIS);
|
||||
DataMinerInvocationJSONWrapper wrapper = gson.fromJson(json, DataMinerInvocationJSONWrapper.class);
|
||||
return wrapper.getDataminerInvocation();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,54 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.data.analysis.dminvocation;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
|
||||
import org.gcube.data.analysis.dminvocation.model.DataMinerParam;
|
||||
import org.gcube.data.analysis.dminvocation.model.DataMinerParamList;
|
||||
|
||||
|
||||
/**
|
||||
* The Class DataMinerParamListAdaptor.
|
||||
*
|
||||
* @author Francesco Mangiacrapa at ISTI-CNR (francesco.mangiacrapa@isti.cnr.it)
|
||||
* Dec 18, 2018
|
||||
*/
|
||||
public class DataMinerParamListAdaptor extends XmlAdapter<DataMinerParamList, List<DataMinerParam>>
|
||||
{
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public List<DataMinerParam> unmarshal(DataMinerParamList list) throws Exception{
|
||||
System.out.println("Unmarshal called: ");
|
||||
List<DataMinerParam> retVal = new ArrayList<DataMinerParam>();
|
||||
for (DataMinerParam dmp : list.getListParam()){
|
||||
System.out.println("key: "+dmp.getKey()+" value: "+ dmp.getValue());
|
||||
retVal.add(new DataMinerParam(dmp.getKey(), dmp.getValue()));
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see javax.xml.bind.annotation.adapters.XmlAdapter#marshal(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public DataMinerParamList marshal(List<DataMinerParam> list) throws Exception{
|
||||
System.out.println("Marshal called: ");
|
||||
DataMinerParamList retVal = new DataMinerParamList();
|
||||
System.out.println(list);
|
||||
// List<DataMinerParam> values = new ArrayList<DataMinerParam>();
|
||||
// for (DataMinerParam dmp : list){
|
||||
// System.out.println("key: "+dmp.getKey()+" value: "+ dmp.getValue());
|
||||
// values.add(new DataMinerParam(dmp.getKey(), dmp.getValue()));
|
||||
// }
|
||||
retVal.setListParam(list);
|
||||
return retVal;
|
||||
}
|
||||
}
|
|
@ -1,49 +0,0 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
package org.gcube.data.analysis.dminvocation;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Dec 19, 2018
|
||||
*/
|
||||
public class MyDeserializer implements JsonDeserializer<DataMinerInvocation> {
|
||||
|
||||
List<String> requiredFields = new ArrayList<String>();
|
||||
|
||||
void registerRequiredField(String fieldName) {
|
||||
|
||||
requiredFields.add(fieldName);
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, com.google.gson.JsonDeserializationContext)
|
||||
*/
|
||||
@Override
|
||||
public DataMinerInvocation deserialize(
|
||||
JsonElement json, Type arg1, JsonDeserializationContext arg2)
|
||||
throws JsonParseException {
|
||||
|
||||
JsonObject jsonObject = (JsonObject) json;
|
||||
for (String fieldName : requiredFields) {
|
||||
if (jsonObject.get(fieldName) == null) {
|
||||
throw new JsonParseException("Required Field Not Found: " +
|
||||
fieldName);
|
||||
}
|
||||
}
|
||||
return new Gson().fromJson(json, DataMinerInvocation.class);
|
||||
}
|
||||
}
|
|
@ -13,9 +13,6 @@ import java.util.Map;
|
|||
|
||||
import javax.xml.bind.JAXBException;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.eclipse.persistence.jaxb.JAXBContext;
|
||||
import org.gcube.data.analysis.dminvocation.DataMinerInvocationJSONWrapper;
|
||||
import org.gcube.data.analysis.dminvocation.DataMinerInvocationManager;
|
||||
import org.gcube.data.analysis.dminvocation.model.DataMinerInvocation;
|
||||
import org.gcube.data.analysis.dminvocation.model.DataMinerParam;
|
||||
|
@ -25,9 +22,6 @@ import org.junit.Before;
|
|||
import org.junit.Test;
|
||||
import org.xml.sax.SAXException;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -75,15 +69,47 @@ public class DataMinerInvocationTest {
|
|||
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
|
||||
// System.out.println(new String(outStreamJSON.toByteArray()));
|
||||
|
||||
String marshXML = dmMng.marshalingXML(dmInvocation, true);
|
||||
String marshXML = dmMng.marshalingXML(dmInvocation, true, true);
|
||||
System.out.println(marshXML);
|
||||
|
||||
|
||||
String marshJSON = dmMng.marshalingJSON(dmInvocation);
|
||||
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
|
||||
System.out.println(marshJSON);
|
||||
}
|
||||
|
||||
@Test
|
||||
//@Test
|
||||
public void marshallingTest2() throws JAXBException, IOException, SAXException {
|
||||
System.out.println("marshallingTest called");
|
||||
//LOADING PARAMETERS
|
||||
List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
|
||||
for (String pm : parameters.keySet()) {
|
||||
inParams.add(new DataMinerParam(pm, parameters.get(pm)));
|
||||
}
|
||||
// DataMinerInputParams inputParams = new DataMinerInputParams(inParams);
|
||||
// DataMinerOutputParams outputParams = new DataMinerOutputParams(null);
|
||||
|
||||
// Map<String, String> inputList = new HashMap<String, String>();
|
||||
// inputList.putAll(parameters);
|
||||
//new DataMinerParamList(parameters)
|
||||
DataMinerParameters parameters = new DataMinerParameters(new DataMinerParamList(inParams), null);
|
||||
|
||||
DataMinerInvocation dmInvocation = new DataMinerInvocation();
|
||||
// dmInvocation.setOperatorId(operatorID);
|
||||
// dmInvocation.setParameters(parameters);
|
||||
// System.out.println(dmInvocation);
|
||||
|
||||
// ByteArrayOutputStream outStreamJSON = DataMinerInvocationManager.marshaling(dmInvocation, MediaType.ApplicationJSON);
|
||||
// System.out.println(new String(outStreamJSON.toByteArray()));
|
||||
|
||||
// String marshXML = dmMng.marshalingXML(dmInvocation, true);
|
||||
// System.out.println(marshXML);
|
||||
|
||||
|
||||
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
|
||||
System.out.println(marshJSON);
|
||||
}
|
||||
|
||||
//@Test
|
||||
public void unmarshallingXMLTest() throws JAXBException, IOException, SAXException{
|
||||
System.out.println("unmarshallingXMLTest called");
|
||||
FileInputStream dmInvocationXMLFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.xml"));
|
||||
|
@ -91,84 +117,91 @@ public class DataMinerInvocationTest {
|
|||
DataMinerInvocation dmInvocation = dmMng.unmarshalingXML(dmInvocationXMLFile, true);
|
||||
System.out.println(dmInvocation);
|
||||
|
||||
String marshXML = dmMng.marshalingXML(dmInvocation, true);
|
||||
System.out.println(marshXML);
|
||||
String marshXML = dmMng.marshalingXML(dmInvocation, true, true);
|
||||
System.out.println("TO XML: \n"+marshXML);
|
||||
//
|
||||
String marshJSON = dmMng.marshalingJSON(dmInvocation);
|
||||
System.out.println(marshJSON);
|
||||
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
|
||||
System.out.println("TO JSON: \n"+marshJSON);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
public void unmarshallingJSONTest() throws JAXBException, IOException, SAXException{
|
||||
System.out.println("unmarshallingJSONTest called");
|
||||
FileInputStream dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
||||
|
||||
System.out.println("To String is: \n"+IOUtils.toString(dmInvocationJSONFile));
|
||||
|
||||
// dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
||||
// DataMinerInvocation dmInvocation = dmMng.unmarshaling(dmInvocationJSONFile, MediaType.ApplicationJSON, false);
|
||||
// System.out.println(dmInvocation);
|
||||
DataMinerInvocation dmInvocation = dmMng.unmarshalingJSON(dmInvocationJSONFile, true);
|
||||
System.out.println(dmInvocation);
|
||||
|
||||
String marshXML = dmMng.marshalingXML(dmInvocation, true, true);
|
||||
System.out.println("TO XML: \n"+marshXML);
|
||||
//
|
||||
// ByteArrayOutputStream outStreamXML = dmMng.marshaling(dmInvocation, MediaType.ApplicationXML, true);
|
||||
// System.out.println(new String(outStreamXML.toByteArray()));
|
||||
String marshJSON = dmMng.marshalingJSON(dmInvocation, true, true);
|
||||
System.out.println("TO JSON: \n"+marshJSON);
|
||||
|
||||
}
|
||||
|
||||
|
||||
// public static void main(String[] args) throws JAXBException, IOException, SAXException {
|
||||
// parameters.put("[key1]", "[value1]");
|
||||
// parameters.put("[key2]", "[value2]");
|
||||
// dmMng = DataMinerInvocationManager.getInstance();
|
||||
// System.out.println(DataMinerInvocationTest.class.getMethods()[2].getName()+" called");
|
||||
// FileInputStream dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
||||
// DataMinerInvocation dmInvocation = dmMng.unmarshaling(dmInvocationJSONFile, MediaType.ApplicationJSON, true);
|
||||
// System.out.println(dmInvocation);
|
||||
//
|
||||
// ByteArrayOutputStream outStreamXML = dmMng.marshaling(dmInvocation, MediaType.ApplicationXML, true);
|
||||
// System.out.println(new String(outStreamXML.toByteArray()));
|
||||
// }
|
||||
|
||||
|
||||
/*
|
||||
public static void main(String[] args) {
|
||||
|
||||
try{
|
||||
System.out.println(JAXBContext.newInstance(DataMinerInvocation.class).getClass());
|
||||
|
||||
parameters.put("fileId", "http://publicLinkToFile");
|
||||
parameters.put("[key2]", "[value2]");
|
||||
dmMng = DataMinerInvocationManager.getInstance();
|
||||
System.out.println("marshallingTest called");
|
||||
//LOADING PARAMETERS
|
||||
List<DataMinerParam> inParams = new ArrayList<DataMinerParam>();
|
||||
for (String pm : parameters.keySet()) {
|
||||
inParams.add(new DataMinerParam(pm, parameters.get(pm)));
|
||||
}
|
||||
|
||||
System.out.println("unmarshallingJSONTest called");
|
||||
FileInputStream dmInvocationJSONFile = new FileInputStream(new File("./src/test/resources/DataMinerInvocation.json"));
|
||||
DataMinerInvocation dm = new DataMinerInvocation();
|
||||
dm.setOperatorId("the operator");
|
||||
DataMinerParameters parameters = new DataMinerParameters(null, null);
|
||||
dm.setParameters(parameters);
|
||||
|
||||
DataMinerInvocation datatminerInvocation = dmMng.unmarshalingJSON(dmInvocationJSONFile);
|
||||
System.out.println("To String is: \n"+datatminerInvocation.toString());
|
||||
List<String> errors = new ArrayList<String>();
|
||||
checkRequiredFiels(dm, errors);
|
||||
System.out.println(errors);
|
||||
|
||||
// String builder = new GsonBuilder().create().t
|
||||
|
||||
//System.out.println(gson.fromJson(json, DataMinerInvocationWrapper.class));
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
DataMinerInvocationJSONWrapper wrapper = new DataMinerInvocationJSONWrapper(datatminerInvocation);
|
||||
System.out.println(gson.toJson(wrapper));
|
||||
|
||||
|
||||
|
||||
|
||||
// Gson gson = new Gson();
|
||||
// JsonElement je = gson.toJsonTree(new DataMinerInvocation());
|
||||
// JsonObject jo = new JsonObject();
|
||||
// jo.add("dataminer-invocation", je);
|
||||
// System.out.println(jo.toString());
|
||||
|
||||
|
||||
// ByteArrayOutputStream outStreamXML = dmMng.marshaling(dmInvocation, MediaType.ApplicationXML, true);
|
||||
// System.out.println(new String(outStreamXML.toByteArray()));
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
private static void checkRequiredFiels(Object theInstance, List<String> errors) throws IllegalArgumentException, IllegalAccessException{
|
||||
|
||||
System.out.println("Checking instance: "+theInstance);
|
||||
|
||||
for(Field field : theInstance.getClass().getDeclaredFields()){
|
||||
Class<?> type = field.getType();
|
||||
String name = field.getName();
|
||||
Annotation[] annotations = field.getDeclaredAnnotations();
|
||||
System.out.println("Field: "+name);
|
||||
for (Annotation annotation : annotations) {
|
||||
if(annotation instanceof XmlElement){
|
||||
field.setAccessible(true);
|
||||
XmlElement xmlEl = (XmlElement) annotation;
|
||||
Object value = field.get(theInstance);
|
||||
if(value==null){
|
||||
String error = "Required field '"+xmlEl.name() + "' of "+type+" is null";
|
||||
System.err.println("Required field '"+xmlEl.name() + "' is null");
|
||||
errors.add(error);
|
||||
}else if (String.class.equals(type) && ((String) value).isEmpty()){
|
||||
String error = "Required field '"+xmlEl.name() + "' of "+type+" is empty";
|
||||
System.err.println("Required field '"+xmlEl.name() + "' is null");
|
||||
errors.add(error);
|
||||
}else if(!type.isPrimitive() && type != String.class){
|
||||
System.out.println("The field "+xmlEl.name()+ " is not a primitive or a String\n\n");
|
||||
checkRequiredFiels(value, errors);
|
||||
}
|
||||
|
||||
//System.out.println("XmlElement "+annotation.annotationType()+" is required: "+xmlEl.required());
|
||||
}else
|
||||
System.out.println("Another annotation: "+annotation);
|
||||
//if (annotation.annotationType() instanceof XmlElement.c)
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue