it.uniroma2.art.sheet2rdf.cfg.GraphApplicationConfigurationLoader Maven / Gradle / Ivy
package it.uniroma2.art.sheet2rdf.cfg;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import org.apache.poi.util.IOUtils;
import org.apache.xmlbeans.impl.piccolo.io.FileFormatException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import it.uniroma2.art.sheet2rdf.header.AdvancedGraphApplication;
import it.uniroma2.art.sheet2rdf.header.NodeConversion;
import it.uniroma2.art.sheet2rdf.header.SimpleHeader;
public class GraphApplicationConfigurationLoader {
public static final String XLABEL_CONFIGURATION_FILE = "xlabel.cfg";
public static List getAvailableConfigurations() {
return Arrays.asList("xlabel");
}
/**
* Returns the configuration file with the given name
* @param fileName
* @return
* @throws IOException
* @throws
*/
public static File getConfigurationFile(String fileName) throws IOException {
InputStream is = GraphApplicationConfigurationLoader.class.getResourceAsStream(fileName);
File cfgFile = File.createTempFile("configFile", ".cfg");
try (OutputStream os = new FileOutputStream(cfgFile)) {
IOUtils.copy(is, os);
}
return cfgFile;
}
public static ObjectNode loadConfigurationFromYAMLFiles(File cfgFile) throws IOException {
YAMLFactory fact = new YAMLFactory();
fact.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES);
fact.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER);
ObjectMapper mapper = new ObjectMapper(fact);
ObjectReader objReader = mapper.reader();
ObjectNode obj = mapper.createObjectNode();
try (Reader reader = new InputStreamReader(new FileInputStream(cfgFile), StandardCharsets.UTF_8)) {
JsonNode jsonNode = objReader.readTree(reader);
if (jsonNode != null) {
if (!(jsonNode instanceof ObjectNode)) {
throw new FileFormatException("YAML file not cotaining an object node: " + cfgFile);
}
obj.setAll((ObjectNode) jsonNode);
}
} catch (JsonMappingException e) {
// Swallow exception due to empty property files
if (!(e.getPath().isEmpty() && e.getMessage().contains("end-of-input"))) {
throw e;
}
}
return obj;
}
public static void loadConfigurationInHeader(SimpleHeader header, String cfgFileName, String headerPlc) throws IOException {
File xlabelCfgFile = getConfigurationFile(cfgFileName);
ObjectNode cfgJson = loadConfigurationFromYAMLFiles(xlabelCfgFile);
GraphApplicationConfigurationParser parser = new GraphApplicationConfigurationParser(cfgJson, headerPlc);
List nodes = parser.getNodeConversions();
header.setNodeConversions(nodes);
AdvancedGraphApplication graphApplication = parser.getAdvancedGraphApplication(nodes);
header.addGraphApplication(graphApplication);
}
}