|
|
|
|
@@ -19,11 +19,7 @@
|
|
|
|
|
|
|
|
|
|
package ch.psi.fda.model;
|
|
|
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
|
import javax.xml.bind.JAXBElement;
|
|
|
|
|
@@ -32,13 +28,7 @@ import javax.xml.bind.Marshaller;
|
|
|
|
|
import javax.xml.bind.UnmarshalException;
|
|
|
|
|
import javax.xml.bind.Unmarshaller;
|
|
|
|
|
import javax.xml.namespace.QName;
|
|
|
|
|
import javax.xml.parsers.ParserConfigurationException;
|
|
|
|
|
import javax.xml.transform.Source;
|
|
|
|
|
import javax.xml.transform.Transformer;
|
|
|
|
|
import javax.xml.transform.TransformerException;
|
|
|
|
|
import javax.xml.transform.TransformerFactory;
|
|
|
|
|
import javax.xml.transform.TransformerFactoryConfigurationError;
|
|
|
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
|
|
import javax.xml.transform.stream.StreamSource;
|
|
|
|
|
import javax.xml.validation.Schema;
|
|
|
|
|
import javax.xml.validation.SchemaFactory;
|
|
|
|
|
@@ -47,95 +37,18 @@ import org.xml.sax.SAXException;
|
|
|
|
|
import org.xml.sax.SAXParseException;
|
|
|
|
|
|
|
|
|
|
import ch.psi.fda.model.v1.Configuration;
|
|
|
|
|
//import ch.psi.fda.model.v1.Data;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manage the serialization and deserialization of the model
|
|
|
|
|
* Manage the serialization and deserialization of the FDA data model
|
|
|
|
|
* @author ebner
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public class ModelManager {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deserialize an instance of a data model of the type Configuration
|
|
|
|
|
* Before creating the object model variables will be replaced
|
|
|
|
|
* De-serialize an instance of the FDA data model
|
|
|
|
|
*
|
|
|
|
|
* @param file Scan file
|
|
|
|
|
* @param variables Scan variables
|
|
|
|
|
* @throws JAXBException Something went wrong while unmarshalling
|
|
|
|
|
* @throws SAXException Cannot read model schema file
|
|
|
|
|
* @throws ParserConfigurationException
|
|
|
|
|
* @throws IOException
|
|
|
|
|
* @throws TransformerFactoryConfigurationError
|
|
|
|
|
* @throws TransformerException
|
|
|
|
|
*/
|
|
|
|
|
public static Configuration unmarshall(File file, HashMap<String, String> variables) throws JAXBException, SAXException, IOException,
|
|
|
|
|
ParserConfigurationException, TransformerFactoryConfigurationError, TransformerException {
|
|
|
|
|
|
|
|
|
|
// Load template file
|
|
|
|
|
Transformer xformer = TransformerFactory.newInstance().newTransformer(new StreamSource(file));
|
|
|
|
|
// Overwrite parameters
|
|
|
|
|
for(String key: variables.keySet()){
|
|
|
|
|
xformer.setParameter(key, variables.get(key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Workaround for complex parameters (parameter including xml fragments)
|
|
|
|
|
// How the workaround works:
|
|
|
|
|
// Because we want to set complex parameters we first have to apply the xslt transformation to the input
|
|
|
|
|
// stream and create an other output stream. In the template file the complex parameter must be referenced as follows
|
|
|
|
|
// <xsl:value-of select="$var.regions" disable-output-escaping="yes" />
|
|
|
|
|
// If it is not done this way the < and > of the tags will be replaced by < and >
|
|
|
|
|
// Also if we directly transform to a DOMResult the dom tree would not include the
|
|
|
|
|
// nodes added by the parameter but just a text object. Therefor we do the workaround via the StreamResult ...
|
|
|
|
|
ByteArrayOutputStream bstream = new ByteArrayOutputStream();
|
|
|
|
|
StreamResult sresult = new StreamResult(bstream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Perform transformation (using template file also as input source)
|
|
|
|
|
xformer.transform(new StreamSource(file), sresult );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Workaround for complex parameters
|
|
|
|
|
ByteArrayInputStream bistream = new ByteArrayInputStream(bstream.toByteArray());
|
|
|
|
|
StreamSource bsource = new StreamSource(bistream);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JAXBContext context = JAXBContext.newInstance(Configuration.class);
|
|
|
|
|
Unmarshaller u = context.createUnmarshaller();
|
|
|
|
|
|
|
|
|
|
// Validation
|
|
|
|
|
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
|
|
|
|
Source s = new StreamSource(Configuration.class.getResourceAsStream("/model-v1.xsd"));
|
|
|
|
|
Schema schema = sf.newSchema(new Source[]{s}); // Use schema reference provided in XML
|
|
|
|
|
u.setSchema(schema);
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
Configuration model = (Configuration) u.unmarshal(bsource, Configuration.class).getValue();
|
|
|
|
|
|
|
|
|
|
// // Ensure that the filename inside the xml file is always the file name
|
|
|
|
|
// Data d = model.getData();
|
|
|
|
|
// if(d==null){
|
|
|
|
|
// d = new Data();
|
|
|
|
|
// model.setData(d);
|
|
|
|
|
// }
|
|
|
|
|
// d.setFileName(file.getName().replaceAll("\\.xml$", ""));
|
|
|
|
|
|
|
|
|
|
return (model);
|
|
|
|
|
}
|
|
|
|
|
catch(UnmarshalException e){
|
|
|
|
|
// Check
|
|
|
|
|
if(e.getLinkedException() instanceof SAXParseException){
|
|
|
|
|
throw new RuntimeException("Configuration file does not comply to required model specification\nCause: "+e.getLinkedException().getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deserialize an instance of a data model of the type Configuration
|
|
|
|
|
* @param file
|
|
|
|
|
* @param file File to deserialize
|
|
|
|
|
* @throws JAXBException Something went wrong while unmarshalling
|
|
|
|
|
* @throws SAXException Cannot read model schema file
|
|
|
|
|
*/
|
|
|
|
|
@@ -152,15 +65,6 @@ public class ModelManager {
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
Configuration model = (Configuration) u.unmarshal(new StreamSource(file), Configuration.class).getValue();
|
|
|
|
|
|
|
|
|
|
// // Ensure that the filename inside the xml file is always the file name
|
|
|
|
|
// Data d = model.getData();
|
|
|
|
|
// if(d==null){
|
|
|
|
|
// d = new Data();
|
|
|
|
|
// model.setData(d);
|
|
|
|
|
// }
|
|
|
|
|
// d.setFileName(file.getName().replaceAll("\\.xml$", ""));
|
|
|
|
|
|
|
|
|
|
return (model);
|
|
|
|
|
}
|
|
|
|
|
catch(UnmarshalException e){
|
|
|
|
|
@@ -173,7 +77,8 @@ public class ModelManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Serialize an instance of a data model of the type Configuration
|
|
|
|
|
* Serialize an instance of the FDA data model
|
|
|
|
|
*
|
|
|
|
|
* @param model Model datastructure
|
|
|
|
|
* @param file File to write the model data into
|
|
|
|
|
* @throws JAXBException Something went wrong while marshalling model
|
|
|
|
|
@@ -192,14 +97,6 @@ public class ModelManager {
|
|
|
|
|
Schema schema = sf.newSchema(new Source[]{s}); // Use schema reference provided in XML
|
|
|
|
|
m.setSchema(schema);
|
|
|
|
|
|
|
|
|
|
// // Set scan name equal to file name
|
|
|
|
|
// Data d = model.getData();
|
|
|
|
|
// if(d==null){
|
|
|
|
|
// d = new Data();
|
|
|
|
|
// model.setData(d);
|
|
|
|
|
// }
|
|
|
|
|
// d.setFileName(file.getName().replaceAll("\\.xml$", ""));
|
|
|
|
|
|
|
|
|
|
m.marshal( new JAXBElement<Configuration>(qname, Configuration.class, model ), file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|