streamlet examples: encapsulate and hide abstract class fields v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 30 Jan 2020 14:19:14 +0100
branchv_0
changeset 75 ecbf6504915c
parent 74 a2aa84f310a5
child 76 679f1e793ee3
streamlet examples: encapsulate and hide abstract class fields
streamlet-examples/JarInfo.java
streamlet-examples/Streamlet.java
streamlet-examples/streamlet-common.h
streamlet-examples/xpath.cpp
--- a/streamlet-examples/JarInfo.java	Wed Jan 29 20:50:12 2020 +0100
+++ b/streamlet-examples/JarInfo.java	Thu Jan 30 14:19:14 2020 +0100
@@ -46,7 +46,7 @@
 	public static final String ATTRIBUTE_ENTRIES = "entries";
 	public static final String ATTRIBUTE_MAIN_CLASS = "main_class";
 
-	private List<String> jarAttributes = new LinkedList<>();
+	private final List<String> jarAttributes = new LinkedList<>();
 
 	public static void main(String[] args) throws IOException {
 		JarInfo s = new JarInfo();
@@ -90,7 +90,7 @@
 		List<Object> result = new LinkedList<>();
 
 		try {
-			JarFile jar = new JarFile(new File(currentFile));
+			JarFile jar = new JarFile(new File(getCurrentFile()));
 
 			for (String attributeName : jarAttributes) {
 				switch (attributeName) {
--- a/streamlet-examples/Streamlet.java	Wed Jan 29 20:50:12 2020 +0100
+++ b/streamlet-examples/Streamlet.java	Thu Jan 30 14:19:14 2020 +0100
@@ -179,12 +179,23 @@
 		}
 	}
 
-	protected List<String> versionsSupported = new LinkedList<>();
-	protected List<AttributeMetadata> inputAttributes = new ArrayList<>();
-	protected List<String> outputAttributeAliases = new ArrayList<>();
-	protected List<Option> options = new LinkedList<>();
-	protected String currentRelation;
-	protected String currentFile;
+	private List<String> versionsSupported = new LinkedList<>();
+	private List<AttributeMetadata> inputAttributes = new ArrayList<>();
+	private List<String> outputAttributeAliases = new ArrayList<>();
+	private List<Option> options = new LinkedList<>();
+	private String currentRelation;
+	private String currentFile;
+
+	/**
+	 * @return n.b. generic streamlet (later in relpipe-tr-streamler) will not have currentFile
+	 */
+	public String getCurrentFile() {
+		return currentFile;
+	}
+
+	public String getCurrentRelation() {
+		return currentRelation;
+	}
 
 	protected static enum Type {
 		BOOLEAN,
--- a/streamlet-examples/streamlet-common.h	Wed Jan 29 20:50:12 2020 +0100
+++ b/streamlet-examples/streamlet-common.h	Thu Jan 30 14:19:14 2020 +0100
@@ -146,14 +146,35 @@
 		}
 	};
 
+private:
 	std::vector<std::wstring> versionsSupported;
 	std::vector<AttributeMetadata> inputAttributes;
 	std::vector<std::wstring> outputAttributeAliases;
 	std::vector<Option> options;
 	std::wstring currentRelation;
 	std::wstring currentFile;
+	std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
 
-	std::wstring_convert < std::codecvt_utf8<wchar_t>> convertor; // TODO: support also other encodings. Or use always UTF-8 for communication with subprocesses.
+protected:
+
+	/**
+	 * n.b. generic streamlet (later in relpipe-tr-streamler) will not have currentFile
+	 */
+	std::wstring getCurrentFile() {
+		return currentFile;
+	}
+
+	std::wstring getCurrentRelation() {
+		return currentRelation;
+	}
+
+	std::wstring fromBytes(std::string s) {
+		return convertor.from_bytes(s);
+	}
+
+	std::string toBytes(std::wstring s) {
+		return convertor.to_bytes(s);
+	}
 
 	static const std::wstring BOOLEAN;
 	static const std::wstring INTEGER;
--- a/streamlet-examples/xpath.cpp	Wed Jan 29 20:50:12 2020 +0100
+++ b/streamlet-examples/xpath.cpp	Thu Jan 30 14:19:14 2020 +0100
@@ -53,8 +53,8 @@
 	}
 
 	void findXmlnsInOptions() {
-		for (Option o : getOptions(std::wregex(L"xmlns[:_](.*)"))) ns[convertor.to_bytes(o.nameMatch[1])] = convertor.to_bytes(o.value);
-		for (Option o : getOptions(std::wregex(L"xmlns"), std::wregex(L"([^:]+):(.*)"))) ns[convertor.to_bytes(o.valueMatch[1])] = convertor.to_bytes(o.valueMatch[2]);
+		for (Option o : getOptions(std::wregex(L"xmlns[:_](.*)"))) ns[toBytes(o.nameMatch[1])] = toBytes(o.value);
+		for (Option o : getOptions(std::wregex(L"xmlns"), std::wregex(L"([^:]+):(.*)"))) ns[toBytes(o.valueMatch[1])] = toBytes(o.valueMatch[2]);
 	}
 
 	// Modes should share the logic of relpipe-in-xmltable
@@ -74,7 +74,7 @@
 		else if (modeName == L"raw-xml") return Mode::RAW_XML;
 		else if (modeName == L"line-number") return Mode::LINE_NUMBER;
 		else if (modeName == L"xpath") return Mode::XPATH;
-		else throw std::invalid_argument("Unsupported mode: " + convertor.to_bytes(modeName));
+		else throw std::invalid_argument("Unsupported mode: " + toBytes(modeName));
 	}
 
 	std::wstring toType(Mode mode) {
@@ -118,16 +118,16 @@
 
 		try {
 			xmlpp::DomParser parser;
-			parser.parse_file(convertor.to_bytes(currentFile));
+			parser.parse_file(toBytes(getCurrentFile()));
 			xmlpp::Element* root = parser.get_document()->get_root_node();
 
 			for (XPathAttribute xpathAttribute : xpathAttributes) {
-				std::string xpath = convertor.to_bytes(xpathAttribute.xpath);
+				std::string xpath = toBytes(xpathAttribute.xpath);
 				std::wstring result;
 				bool isNull = false;
 
 				if (xpathAttribute.mode == Mode::STRING) {
-					result = convertor.from_bytes(root->eval_to_string(xpath, ns));
+					result = fromBytes(root->eval_to_string(xpath, ns));
 				} else if (xpathAttribute.mode == Mode::BOOLEAN) {
 					result = root->eval_to_boolean(xpath, ns) ? L"true" : L"false";
 				} else if (xpathAttribute.mode == Mode::LINE_NUMBER) {
@@ -136,7 +136,7 @@
 					else isNull = true;
 				} else if (xpathAttribute.mode == Mode::XPATH) {
 					xmlpp::NodeSet attributeNodes = root->find(xpath, ns);
-					if (attributeNodes.size()) result = convertor.from_bytes(attributeNodes[0]->get_path());
+					if (attributeNodes.size()) result = fromBytes(attributeNodes[0]->get_path());
 					else isNull = true;
 				} else if (xpathAttribute.mode == Mode::RAW_XML) {
 					throw std::logic_error("Raw XML mode is not yet implemented."); // TODO: implement also RAW_XML