
nl.tno.bim.nmd.config.NmdConfigImpl Maven / Gradle / Ivy
package nl.tno.bim.nmd.config;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
public class NmdConfigImpl implements NmdConfig {
private String token;
private int id;
private String nmd2path;
private String rootPath;
private static final String rootNode = "nmd";
public NmdConfigImpl() {
File fi = (new File(System.getProperty("user.dir")));
Path rootPath = Paths.get(fi.toString());
this.load_config(rootPath);
}
public NmdConfigImpl(Path rootPath) {
this.load_config(rootPath);
}
private void load_config(Path rootPath) {
this.rootPath = rootPath.toString();
Map nmdPropertyMap = NmdConfigImpl.loadResources(rootPath, rootNode);
this.setToken(nmdPropertyMap.get("token"));
this.setClientId(Integer.parseInt(nmdPropertyMap.get("id")));
this.setNmd2DbPath(nmdPropertyMap.get("nmd2Path"));
}
/**
* load resources from a path containing a config.xml file
* @param rootPath Path where config.xml should be present
* @param rootNode rootnode to start reading the xml from
* @return Map with key value config parameters.
*/
private static Map loadResources(Path rootPath, String rootNode) {
Map res = new HashMap();
Path configPath = rootPath.resolve("config.xml");
if (Files.exists(configPath)) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
try (InputStream newInputStream = Files.newInputStream(configPath)) {
Document doc = documentBuilder.parse(newInputStream);
Consumer addNodedata = (node) -> {
res.put(node.getNodeName(), node.getTextContent());
};
for(int i = 0; i < doc.getChildNodes().getLength(); i++) {
Node node = doc.getChildNodes().item(i);
performMethodForRecursiveNode(node, addNodedata);
}
}
} catch (Exception e) {
System.err.println(e.getMessage());
}
} else {
System.err.println("config.xml not found");
}
return res;
}
private static void performMethodForRecursiveNode(Node node, Consumer method) {
method.accept(node);
for(int i = 0; i < node.getChildNodes().getLength(); i++) {
Node child = node.getChildNodes().item(i);
performMethodForRecursiveNode(child, method);
}
}
@Override
public String getToken() {
return this.token;
}
@Override
public Integer getClientId() {
return this.id;
}
public void setToken(String token) {
this.token = token;
}
public void setClientId(int id) {
this.id = id;
}
/**
* Only for testing purposes. This path stores where the NMD2.X database file is stored
* @return file path string to database
*/
@Override
public String getNmd2DbPath() {
return this.rootPath + "//" + this.nmd2path;
}
public void setNmd2DbPath(String path) {
this.nmd2path = path;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy