diff --git a/pom.xml b/pom.xml index a419767..b83ce83 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ org.opentdk opentdk-api - 1.6.4 + 1.6.5 17 17 diff --git a/src/org/opentdk/api/application/BaseApplication.java b/src/org/opentdk/api/application/BaseApplication.java index 56525fa..8c594f5 100644 --- a/src/org/opentdk/api/application/BaseApplication.java +++ b/src/org/opentdk/api/application/BaseApplication.java @@ -197,7 +197,7 @@ public BaseApplication(String[] args) { * @param runtimePropertiesClass The class of type {@link BaseDispatcher} which includes the * declaration of all runtime properties * @param args String array with keys and values passed by the commandline (e.g. - * -homedir=c:/applications/myApp) + * {@literal -homedir=c:/applications/myApp)} */ public final void parseArgs(Class runtimePropertiesClass, String[] args) { String value = ""; diff --git a/src/org/opentdk/api/datastorage/DataContainer.java b/src/org/opentdk/api/datastorage/DataContainer.java index d2ff25d..62c5ad5 100644 --- a/src/org/opentdk/api/datastorage/DataContainer.java +++ b/src/org/opentdk/api/datastorage/DataContainer.java @@ -32,6 +32,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.Files; import java.sql.ResultSet; import java.util.Map; @@ -172,7 +173,9 @@ private DataContainer(File sourceFile) { inputFile = sourceFile; instance = adaptContainer(); try { - instance.readData(sourceFile); + if(sourceFile.exists() && sourceFile.isFile() && Files.size(sourceFile.toPath()) > 0) { + instance.readData(sourceFile); + } } catch (IOException e) { MLogger.getInstance().log(Level.SEVERE, e); } @@ -650,7 +653,7 @@ public void set(String parameterName, String value, Filter fltr, boolean allOccu tabInstance().setValues(parameterName, value, fltr, allOccurences); } else if (isTree()) { if(isXML()) { - xmlInstance().set(parameterName, value, fltr); + xmlInstance().set(parameterName, value, fltr, allOccurences); } else if (isJSON()) { jsonInstance().set(parameterName, value, fltr); } else if(isYAML()) { diff --git a/src/org/opentdk/api/datastorage/XMLDataContainer.java b/src/org/opentdk/api/datastorage/XMLDataContainer.java index 786d373..c6e356c 100644 --- a/src/org/opentdk/api/datastorage/XMLDataContainer.java +++ b/src/org/opentdk/api/datastorage/XMLDataContainer.java @@ -30,7 +30,7 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; - +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -61,28 +61,43 @@ public class XMLDataContainer implements SpecificContainer { */ private XMLEditor xEdit; - private String rootNode = "root"; + private String rootNode; public static XMLDataContainer newInstance() { return new XMLDataContainer(); + } + + private XMLDataContainer() { + initXmlEditor(null); } - private XMLDataContainer() { -// StringBuilder sb = new StringBuilder(); -// sb.append("<").append(rootNode).append("/>"); - xEdit = new XMLEditor(rootNode); + public void initXmlEditor(String root) { + xEdit = new XMLEditor(root); + rootNode = xEdit.getRootNodeName(); } + + @Override + public String asString() { + String ret = ""; + if (xEdit != null) { + ret = xEdit.asString(); + } + return ret; + } + @Override public void readData(File sourceFile) throws IOException { - xEdit = new XMLEditor(sourceFile); - setRootNode(xEdit.getRootNodeName()); + if(sourceFile.exists() && sourceFile.isFile() && Files.size(sourceFile.toPath()) > 0) { + xEdit = new XMLEditor(sourceFile); + rootNode = xEdit.getRootNodeName(); + } } @Override public void readData(InputStream stream) throws IOException { xEdit = new XMLEditor(stream); - setRootNode(xEdit.getRootNodeName()); + rootNode = xEdit.getRootNodeName(); } @@ -107,9 +122,10 @@ public String getRootNode() { return rootNode; } - public void setRootNode(String root) { - rootNode = root; - } +// +// public void setRootNode(String root) { +// rootNode = root; +// } // public void readXMLData(File inputFile) { // StringBuilder sb = new StringBuilder(); @@ -172,15 +188,6 @@ public void add(String name, String attr, String oldValue, String value, Filter } } - @Override - public String asString() { - String ret = ""; - if (xEdit != null) { - ret = xEdit.asString(); - } - return ret; - } - public void delete(String name, String value) { delete(name, value, new Filter()); } @@ -238,24 +245,24 @@ public Element get(String tagName, String attributName, String attributValue){ * formats (table, tree etc.). * * @param headerName The name of the tag that will be searched within an XML document. - * @param fltr Filter rules that will be applied as additional search criteria for the + * @param filter Filter rules that will be applied as additional search criteria for the * returning tags. * @param returnType values = String Array with all values of the matching tags; elements = Element * Array with all matching tag objects * @return Array of the type, specified by returnType argument */ - public Object[] get(String headerName, Filter fltr, String returnType) { -// List implFilterRules = dc.getImplFilterRules(fltr); + public Object[] get(String headerName, Filter filter, String returnType) { + List filteredElements = new ArrayList<>(); List filteredValues = new ArrayList<>(); /* * Filter all tags and values that match an implicit XPath filter rule. */ - if (fltr.getFilterRules().size() > 0) { - for (FilterRule frImpl : fltr.getFilterRules()) { - if (frImpl.getHeaderName().equalsIgnoreCase("XPath")) { - for (Element tagElement : xEdit.getElementsListByXPath(frImpl.getValue())) { + if (filter.getFilterRules().size() > 0) { + for (FilterRule rule : filter.getFilterRules()) { + if (rule.getHeaderName().equalsIgnoreCase("XPath")) { + for (Element tagElement : xEdit.getElementsListByXPath(rule.getValue())) { for (Element childE : xEdit.getChildren(tagElement)) { if (childE.getTagName().equals(headerName)) { filteredElements.add(childE); @@ -276,12 +283,12 @@ public Object[] get(String headerName, Filter fltr, String returnType) { } } /* - * Finally remove all tags that do not apply to any other filter rule defined by the fltr argument. + * Finally remove all tags that do not apply to any other filter rule defined by the filter argument. */ List retElements = new ArrayList<>(filteredElements); List retValues = new ArrayList<>(filteredValues); - for (FilterRule fr : fltr.getFilterRules()) { - if (fltr.getFilterRules().contains(fr) == false) { + for (FilterRule fr : filter.getFilterRules()) { + if (!fr.getHeaderName().equalsIgnoreCase("XPath")) { for (Element fltrE : filteredElements) { if (fr.isValidValue(fltrE.getTextContent(), fr.getValue()) == false) { retValues.remove(fltrE.getTextContent()); @@ -349,17 +356,22 @@ public Element getRootElement(){ } public void set(String name, String value) { - set(name, value, new Filter()); + set(name, value, new Filter(), false); } - public void set(String tagName, String tagValue, Filter filter) { + public void set(String tagName, String tagValue, Filter filter, boolean allOccurrences) { + // Creates the missing nodes if not present for (FilterRule fltrRule : filter.getFilterRules()) { if (fltrRule.getHeaderName().equalsIgnoreCase("XPath")) { - xEdit.checkXPath(fltrRule.getValue() + "/" + tagName, true); // Creates the hierarchy if not present - Element[] elements = (Element[]) get(tagName, filter, "elements"); - for (int i = 0; i < elements.length; i++) { - xEdit.setElementValue(elements[i], tagValue); - } + xEdit.checkXPath(fltrRule.getValue() + "/" + tagName, true); + break; + } + } + + Element[] elements = (Element[]) get(tagName, filter, "elements"); + for (int i = 0; i < elements.length; i++) { + xEdit.setElementValue(elements[i], tagValue); + if(!allOccurrences) { break; } } diff --git a/src/org/opentdk/api/datastorage/YAMLDataContainer.java b/src/org/opentdk/api/datastorage/YAMLDataContainer.java index b98a18a..ad54c80 100644 --- a/src/org/opentdk/api/datastorage/YAMLDataContainer.java +++ b/src/org/opentdk/api/datastorage/YAMLDataContainer.java @@ -71,12 +71,7 @@ public static YAMLDataContainer newInstance() { private YAMLDataContainer() { yaml = new Yaml(); - json = JSONDataContainer.newInstance(); - if (content == null) { - MLogger.getInstance().log(Level.WARNING, "YAML object is not initialized or empty ==> No YAML content to read", getClass().getSimpleName(), "constructor"); - } else { - json.setJsonWithMap(content); - } + json = JSONDataContainer.newInstance(); } @Override @@ -101,11 +96,21 @@ public String asString(EContainerFormat format) { @Override public void readData(File sourceFile) throws IOException { content = yaml.load(FileUtil.getRowsAsString(sourceFile)); + if (content == null) { + MLogger.getInstance().log(Level.WARNING, "YAML object is not initialized or empty ==> No YAML content to read", getClass().getSimpleName(), "constructor"); + } else { + json.setJsonWithMap(content); + } } @Override public void readData(InputStream stream) throws IOException { - content = yaml.load(stream); + content = yaml.load(stream); + if (content == null) { + MLogger.getInstance().log(Level.WARNING, "YAML object is not initialized or empty ==> No YAML content to read", getClass().getSimpleName(), "constructor"); + } else { + json.setJsonWithMap(content); + } } diff --git a/src/org/opentdk/api/dispatcher/BaseDispatchComponent.java b/src/org/opentdk/api/dispatcher/BaseDispatchComponent.java index 724e2bf..466dbf8 100644 --- a/src/org/opentdk/api/dispatcher/BaseDispatchComponent.java +++ b/src/org/opentdk/api/dispatcher/BaseDispatchComponent.java @@ -174,7 +174,7 @@ public BaseDispatchComponent(Class parentClass, String paramName, String pxp, DataContainer dc = DataContainer.newContainer(EContainerFormat.CSV); if (StringUtils.isNotBlank(parentXPath)) { dc = DataContainer.newContainer(EContainerFormat.XML); - dc.xmlInstance().setRootNode(getRootNode()); + dc.xmlInstance().initXmlEditor(retrieveRootNode()); } BaseDispatcher.setDataContainer(parentClass, dc); } @@ -565,7 +565,7 @@ public String getName() { return parameterName; } - public String getRootNode() { + public String retrieveRootNode() { String rootNode = ""; String[] nodeArray = parentXPath.split("/"); if (nodeArray.length > 0) { diff --git a/src/org/opentdk/api/dispatcher/BaseDispatcher.java b/src/org/opentdk/api/dispatcher/BaseDispatcher.java index f27c082..47ecaf0 100644 --- a/src/org/opentdk/api/dispatcher/BaseDispatcher.java +++ b/src/org/opentdk/api/dispatcher/BaseDispatcher.java @@ -37,7 +37,6 @@ import java.util.logging.Level; import java.lang.reflect.Field; -import org.apache.commons.lang3.StringUtils; import org.opentdk.api.datastorage.DataContainer; import org.opentdk.api.logger.MLogger; @@ -189,22 +188,19 @@ public static void setDataContainer(Class dispatcherClass, String dispatcherF */ public static void setDataContainer(Class dispatcherClass, DataContainer dc, boolean createFile) { Map dcomp = BaseDispatcher.getDeclaredComponents(dispatcherClass); - String rn = ""; - for (String key : dcomp.keySet()) { - rn = dcomp.get(key).getRootNode(); - if (!rn.isEmpty()) { - break; - } - } + if (createFile) { if (dc.isTree() && dc.isXML()) { - if (StringUtils.isBlank(dc.xmlInstance().getRootNode())) { - dc.xmlInstance().setRootNode(rn); - } else { - if (!dc.xmlInstance().getRootNode().contentEquals(rn)) { - throw new IllegalArgumentException("Root node of dispatcher class and data container are not equal"); + String rn = ""; + for (String key : dcomp.keySet()) { + rn = dcomp.get(key).retrieveRootNode(); + if (!rn.isEmpty()) { + break; } } + if (!dc.xmlInstance().getRootNode().contentEquals(rn)) { + dc.xmlInstance().initXmlEditor(rn); + } } try { if(dc.getInputFile().exists() == false) { diff --git a/src/org/opentdk/api/io/XMLEditor.java b/src/org/opentdk/api/io/XMLEditor.java index 8ec4764..3de6219 100644 --- a/src/org/opentdk/api/io/XMLEditor.java +++ b/src/org/opentdk/api/io/XMLEditor.java @@ -110,7 +110,7 @@ public XMLEditor() { * @param rootNode {@link #rootNodeName} */ public XMLEditor(String rootNode) { - if (StringUtils.isNotBlank(rootNode)) { + if (rootNode != null && StringUtils.isNotBlank(rootNode)) { rootNodeName = rootNode; } createXMLEditor(); @@ -185,9 +185,9 @@ private void createXMLEditor() { } catch (ParserConfigurationException e) { MLogger.getInstance().log(Level.SEVERE, "The feature '" + feature + "' is probably not supported the XML processor.", getClass().getSimpleName(), "createXMLEditor"); } catch (SAXException e) { - MLogger.getInstance().log(Level.SEVERE, "A DOCTYPE was passed into the XML document.", getClass().getSimpleName(), "createXMLEditor"); + MLogger.getInstance().log(Level.SEVERE, e.getMessage(), getClass().getSimpleName(), "createXMLEditor"); } catch (IOException e) { - MLogger.getInstance().log(Level.SEVERE, "Exception occured: XXE may still possible ==> XML will not be read.", getClass().getSimpleName(), "createXMLEditor"); + MLogger.getInstance().log(Level.SEVERE, e.getMessage(), getClass().getSimpleName(), "createXMLEditor"); } }