Skip to content

Commit

Permalink
Adjustments after regression test 1.6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
SoccerFive18 committed Feb 7, 2024
1 parent d4d5414 commit 34a010f
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 67 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- Coordinates -->
<groupId>org.opentdk</groupId>
<artifactId>opentdk-api</artifactId>
<version>1.6.4</version>
<version>1.6.5</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand Down
2 changes: 1 addition & 1 deletion src/org/opentdk/api/application/BaseApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "";
Expand Down
7 changes: 5 additions & 2 deletions src/org/opentdk/api/datastorage/DataContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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()) {
Expand Down
88 changes: 50 additions & 38 deletions src/org/opentdk/api/datastorage/XMLDataContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}


Expand All @@ -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();
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<FilterRule> implFilterRules = dc.getImplFilterRules(fltr);
public Object[] get(String headerName, Filter filter, String returnType) {

List<Element> filteredElements = new ArrayList<>();
List<String> 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);
Expand All @@ -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<Element> retElements = new ArrayList<>(filteredElements);
List<String> 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());
Expand Down Expand Up @@ -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;
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/org/opentdk/api/datastorage/YAMLDataContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}


Expand Down
4 changes: 2 additions & 2 deletions src/org/opentdk/api/dispatcher/BaseDispatchComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
22 changes: 9 additions & 13 deletions src/org/opentdk/api/dispatcher/BaseDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -189,22 +188,19 @@ public static void setDataContainer(Class<?> dispatcherClass, String dispatcherF
*/
public static void setDataContainer(Class<?> dispatcherClass, DataContainer dc, boolean createFile) {
Map<String, BaseDispatchComponent> 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) {
Expand Down
6 changes: 3 additions & 3 deletions src/org/opentdk/api/io/XMLEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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");
}
}

Expand Down

0 comments on commit 34a010f

Please sign in to comment.