argos/dmp-backend/web/src/main/java/eu/eudat/logic/proxy/config/configloaders/DefaultConfigLoader.java

68 lines
2.2 KiB
Java

package eu.eudat.logic.proxy.config.configloaders;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.service.storage.StorageFileService;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayInputStream;
import java.util.LinkedList;
import java.util.List;
@Service("configLoader")
public class DefaultConfigLoader implements ConfigLoader {
private static final Logger logger = LoggerFactory.getLogger(DefaultConfigLoader.class);
private static final ObjectMapper mapper = new ObjectMapper();
private XWPFDocument document;
@Autowired
private StorageFileService storageFileService;
private void setDocument() {
byte[] bytes = this.storageFileService.getH2020TemplateFile();
try {
this.document = new XWPFDocument(new ByteArrayInputStream(bytes));
} catch (Exception ex) {
logger.error("Cannot find resource", ex);
}
}
public XWPFDocument getDocument() {
this.setDocument();
return document;
}
private List<String> getXmlValuesFromXPath(Document doc, String expression) {
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nodeList = null;
List<String> values = new LinkedList<>();
try {
nodeList = (NodeList) xPath.compile(expression).evaluate(doc, XPathConstants.NODESET);
} catch (XPathExpressionException e) {
logger.error(e.getMessage(), e);
}
if (nodeList != null) {
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.hasChildNodes()) {
values.add(nodeList.item(i).getChildNodes().item(0).getNodeValue());
}
}
}
return values;
}
}