Skip to content

Commit

Permalink
#59
Browse files Browse the repository at this point in the history
  • Loading branch information
SoccerFive18 committed Jul 5, 2024
1 parent c0c16f5 commit 6a4bb93
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 62 deletions.
15 changes: 1 addition & 14 deletions src/org/opentdk/api/datastorage/JSONDataContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public class JSONDataContainer implements SpecificContainer {

/**
* Container object for the JSON data. Supports several read and write methods. Gets initialized in
* {@link #readData(Filter)}.
* {@link #readData(File)}.
*/
private JSONObject json;

Expand Down Expand Up @@ -126,19 +126,6 @@ public void readData(File srcFile) throws IOException {
}
}


@Override
public void readData(File sourceFile, Filter filter) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void readData(InputStream stream, Filter filter) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeData(File srcFile) throws IOException {
if (json == null || json.isEmpty()) {
Expand Down
24 changes: 22 additions & 2 deletions src/org/opentdk/api/datastorage/SpecificContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,29 @@ default String asString(EContainerFormat exportAs) {
* @throws IOException if the reading failed the user can handle the cause
*/
void readData(File sourceFile) throws IOException;
void readData(File sourceFile, Filter filter) throws IOException;

/**
* Calls {@link #readData(File)}. The filter is not supported by default.
*/
default void readData(File sourceFile, Filter filter) throws IOException {
readData(sourceFile);
}

/**
* Each specific container needs to implement a method that reads data from a
* stream.
*
* @param stream Stream object with the content
* @throws IOException if the reading failed the user can handle the cause
*/
void readData(InputStream stream) throws IOException;
void readData(InputStream stream, Filter filter) throws IOException;

/**
* Calls {@link #readData(InputStream)}. The filter is not supported by default.
*/
default void readData(InputStream stream, Filter filter) throws IOException {
readData(stream);
}

/**
* Each specific container has to implement a method that writes the
Expand Down
39 changes: 23 additions & 16 deletions src/org/opentdk/api/datastorage/TabularContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.util.*;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TabularContainer implements SpecificContainer {

Expand Down Expand Up @@ -195,7 +194,7 @@ public JSONObject toJson() {
}

@Override
public void readData(File sourceFile) throws IOException {
public void readData(File sourceFile) {
if (sourceFile != null && sourceFile.exists()) {
if (StringUtils.isNotBlank(sourceFile.getPath())) {
putFile(sourceFile.getPath(), getColumnDelimiter());
Expand Down Expand Up @@ -326,18 +325,26 @@ private void putDatasetRows(String fileName, String columnDelimiter) {
}

@Override
public void readData(File sourceFile, Filter filter) throws IOException {
// TODO Auto-generated method stub
}

@Override
public void readData(InputStream stream) throws IOException {
// TODO Auto-generated method stub
}

@Override
public void readData(InputStream stream, Filter filter) throws IOException {
// TODO Auto-generated method stub
public void readData(InputStream stream) {
String content = null;
if (stream != null) {
InputStreamReader inputStreamReader = new InputStreamReader(stream);
Stream<String> streamOfString = new BufferedReader(inputStreamReader).lines();
content = streamOfString.collect(Collectors.joining());

streamOfString.close();
try {
inputStreamReader.close();
} catch (IOException e) {
MLogger.getInstance().log(Level.SEVERE, e);
}
}
if(content != null) {
// Store content in a temporary file to use the existing methods to add to the values object
String file = "~tmp/" + getClass().getSimpleName() + ".csv";
FileUtil.writeOutputFile(content, file);
putFile(file, columnDelimiter);
}
}

@Override
Expand Down
16 changes: 0 additions & 16 deletions src/org/opentdk/api/datastorage/XMLDataContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,6 @@ public void readData(InputStream stream) throws IOException {
rootNode = xEdit.getRootNodeName();
}

/**
* Calls {@link #readData(File)}. The filter is not supported.
*/
@Override
public void readData(File sourceFile, Filter filter) throws IOException {
readData(sourceFile);
}

/**
* Calls {@link #readData(InputStream)}. The filter is not supported.
*/
@Override
public void readData(InputStream stream, Filter filter) throws IOException {
readData(stream);
}

@Override
public void writeData(File outputFile) {
xEdit.save(outputFile);
Expand Down
15 changes: 2 additions & 13 deletions src/org/opentdk/api/datastorage/YAMLDataContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,14 @@ public void readData(InputStream stream) throws IOException {
json.setJsonWithMap(content);
}
}


@Override
public void readData(File sourceFile, Filter filter) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void readData(InputStream stream, Filter filter) throws IOException {
// TODO Auto-generated method stub

}

@Override
public void writeData(File outputFile) throws IOException {
yaml.dump(yaml.dumpAsMap(json.getJsonAsMap()), new FileWriter(outputFile.getPath()));
}

// The following methods just link to the JSONDataContainer

public void add(String name, String value) {
json.add(name, value);
}
Expand Down
2 changes: 1 addition & 1 deletion src/org/opentdk/api/io/XMLEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ public String asString(Node node, boolean skipHeader) {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}

removeEmptySpace(rootElement);
removeEmptySpace(node);

// Transform the node to a string
DOMSource source = new DOMSource(node);
Expand Down

0 comments on commit 6a4bb93

Please sign in to comment.