diff --git a/src/org/opentdk/api/datastorage/JSONDataContainer.java b/src/org/opentdk/api/datastorage/JSONDataContainer.java index a62f935..2f5d04c 100644 --- a/src/org/opentdk/api/datastorage/JSONDataContainer.java +++ b/src/org/opentdk/api/datastorage/JSONDataContainer.java @@ -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; @@ -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()) { diff --git a/src/org/opentdk/api/datastorage/SpecificContainer.java b/src/org/opentdk/api/datastorage/SpecificContainer.java index deddceb..cda06db 100644 --- a/src/org/opentdk/api/datastorage/SpecificContainer.java +++ b/src/org/opentdk/api/datastorage/SpecificContainer.java @@ -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 diff --git a/src/org/opentdk/api/datastorage/TabularContainer.java b/src/org/opentdk/api/datastorage/TabularContainer.java index 48baa51..89c3daf 100644 --- a/src/org/opentdk/api/datastorage/TabularContainer.java +++ b/src/org/opentdk/api/datastorage/TabularContainer.java @@ -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 { @@ -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()); @@ -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 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 diff --git a/src/org/opentdk/api/datastorage/XMLDataContainer.java b/src/org/opentdk/api/datastorage/XMLDataContainer.java index 007a287..295381d 100644 --- a/src/org/opentdk/api/datastorage/XMLDataContainer.java +++ b/src/org/opentdk/api/datastorage/XMLDataContainer.java @@ -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); diff --git a/src/org/opentdk/api/datastorage/YAMLDataContainer.java b/src/org/opentdk/api/datastorage/YAMLDataContainer.java index 1336411..38abd6e 100644 --- a/src/org/opentdk/api/datastorage/YAMLDataContainer.java +++ b/src/org/opentdk/api/datastorage/YAMLDataContainer.java @@ -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); } diff --git a/src/org/opentdk/api/io/XMLEditor.java b/src/org/opentdk/api/io/XMLEditor.java index 43ce2f2..3e7867e 100644 --- a/src/org/opentdk/api/io/XMLEditor.java +++ b/src/org/opentdk/api/io/XMLEditor.java @@ -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);