You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@systemml.apache.org by na...@apache.org on 2017/07/26 21:25:05 UTC
systemml git commit: [SYSTEMML-1806] fix for DMLConfig#setText
Repository: systemml
Updated Branches:
refs/heads/master 7ae1b1c4c -> 3fd8e495e
[SYSTEMML-1806] fix for DMLConfig#setText
Project: http://git-wip-us.apache.org/repos/asf/systemml/repo
Commit: http://git-wip-us.apache.org/repos/asf/systemml/commit/3fd8e495
Tree: http://git-wip-us.apache.org/repos/asf/systemml/tree/3fd8e495
Diff: http://git-wip-us.apache.org/repos/asf/systemml/diff/3fd8e495
Branch: refs/heads/master
Commit: 3fd8e495e26ba70eed22bf16c51a7bf69474c1c3
Parents: 7ae1b1c
Author: Nakul Jindal <na...@gmail.com>
Authored: Wed Jul 26 14:24:53 2017 -0700
Committer: Nakul Jindal <na...@gmail.com>
Committed: Wed Jul 26 14:24:53 2017 -0700
----------------------------------------------------------------------
.../java/org/apache/sysml/conf/DMLConfig.java | 71 ++++++++++----------
.../org/apache/sysml/test/unit/UtilsTest.java | 60 +++++++++++++++++
2 files changed, 97 insertions(+), 34 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/systemml/blob/3fd8e495/src/main/java/org/apache/sysml/conf/DMLConfig.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/sysml/conf/DMLConfig.java b/src/main/java/org/apache/sysml/conf/DMLConfig.java
index a6a4b5e..415bb57 100644
--- a/src/main/java/org/apache/sysml/conf/DMLConfig.java
+++ b/src/main/java/org/apache/sysml/conf/DMLConfig.java
@@ -22,6 +22,7 @@ package org.apache.sysml.conf;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
+import java.io.InputStream;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
@@ -45,6 +46,7 @@ import org.apache.sysml.runtime.DMLRuntimeException;
import org.apache.sysml.runtime.io.IOUtilFunctions;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
+import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
@@ -97,6 +99,8 @@ public class DMLConfig
private String _fileName = null;
private Element _xmlRoot = null;
+ private DocumentBuilder _documentBuilder = null;
+ private Document _document = null;
static
{
@@ -130,7 +134,7 @@ public class DMLConfig
{
}
-
+
public DMLConfig(String fileName)
throws ParseException, FileNotFoundException
{
@@ -169,25 +173,32 @@ public class DMLConfig
*/
private void parseConfig () throws ParserConfigurationException, SAXException, IOException
{
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setIgnoringComments(true); //ignore XML comments
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document domTree = null;
+ DocumentBuilder builder = getDocumentBuilder();
+ _document = null;
if( _fileName.startsWith("hdfs:") || _fileName.startsWith("gpfs:")
|| IOUtilFunctions.isObjectStoreFileScheme(new Path(_fileName)) )
{
Path configFilePath = new Path(_fileName);
FileSystem DFS = IOUtilFunctions.getFileSystem(configFilePath);
- domTree = builder.parse(DFS.open(configFilePath));
+ _document = builder.parse(DFS.open(configFilePath));
}
else // config from local file system
{
- domTree = builder.parse(_fileName);
+ _document = builder.parse(_fileName);
}
-
- _xmlRoot = domTree.getDocumentElement();
+
+ _xmlRoot = _document.getDocumentElement();
}
-
+
+ private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
+ if (_documentBuilder == null) {
+ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+ factory.setIgnoringComments(true); //ignore XML comments
+ _documentBuilder = factory.newDocumentBuilder();
+ }
+ return _documentBuilder;
+ }
+
/**
* Method to get string value of a configuration parameter
* Handles processing of configuration parameters
@@ -242,21 +253,7 @@ public class DMLConfig
return textVal;
}
- /**
- * Method to update the string value of an element identified by a tag name
- * @param element the DOM element
- * @param tagName the tag name
- * @param newTextValue the new string value
- */
- private static void setTextValue(Element element, String tagName, String newTextValue) {
-
- NodeList list = element.getElementsByTagName(tagName);
- if (list != null && list.getLength() > 0) {
- Element elem = (Element) list.item(0);
- elem.getFirstChild().setNodeValue(newTextValue);
- }
- }
-
+
/**
* Method to update the key value
* @param paramName parameter name
@@ -264,17 +261,23 @@ public class DMLConfig
* @throws DMLRuntimeException if DMLRuntimeException occurs
*/
public void setTextValue(String paramName, String paramValue) throws DMLRuntimeException {
- if(_xmlRoot != null)
- DMLConfig.setTextValue(_xmlRoot, paramName, paramValue);
- else {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setIgnoringComments(true); //ignore XML comments
- DocumentBuilder builder;
+ if(_xmlRoot != null) {
+ NodeList list = _xmlRoot.getElementsByTagName(paramName);
+ if (list != null && list.getLength() > 0) {
+ Element elem = (Element) list.item(0);
+ elem.getFirstChild().setNodeValue(paramValue);
+ } else {
+ Node value = _document.createTextNode(paramValue);
+ Node element = _document.createElement(paramName);
+ element.appendChild(value);
+ _xmlRoot.appendChild(element);
+ }
+ } else {
try {
- builder = factory.newDocumentBuilder();
+ DocumentBuilder builder = getDocumentBuilder();
String configString = "<root><" + paramName + ">"+paramValue+"</" + paramName + "></root>";
- Document domTree = builder.parse(new ByteArrayInputStream(configString.getBytes("UTF-8")));
- _xmlRoot = domTree.getDocumentElement();
+ _document = builder.parse(new ByteArrayInputStream(configString.getBytes("UTF-8")));
+ _xmlRoot = _document.getDocumentElement();
} catch (Exception e) {
throw new DMLRuntimeException("Unable to set config value", e);
}
http://git-wip-us.apache.org/repos/asf/systemml/blob/3fd8e495/src/test/java/org/apache/sysml/test/unit/UtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/sysml/test/unit/UtilsTest.java b/src/test/java/org/apache/sysml/test/unit/UtilsTest.java
index b5dccd0..02c96a0 100644
--- a/src/test/java/org/apache/sysml/test/unit/UtilsTest.java
+++ b/src/test/java/org/apache/sysml/test/unit/UtilsTest.java
@@ -20,8 +20,15 @@
package org.apache.sysml.test.unit;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
import java.util.Arrays;
+import org.apache.sysml.conf.DMLConfig;
+import org.apache.sysml.parser.ParseException;
+import org.apache.sysml.runtime.DMLRuntimeException;
import org.apache.sysml.runtime.instructions.gpu.context.GPUContextPool;
import org.junit.Assert;
import org.junit.Test;
@@ -75,4 +82,57 @@ public class UtilsTest {
public void testParseListStringFail4() {
GPUContextPool.parseListString("-1-4", 6);
}
+
+
+ @Test
+ public void testDMLConfig1() throws DMLRuntimeException{
+ DMLConfig dmlConfig = new DMLConfig();
+ dmlConfig.setTextValue("A", "a");
+ dmlConfig.setTextValue("B", "b");
+ dmlConfig.setTextValue("C", "2");
+ dmlConfig.setTextValue("D", "5");
+ dmlConfig.setTextValue("E", "5.01");
+
+ Assert.assertEquals("a", dmlConfig.getTextValue("A"));
+ Assert.assertEquals("b", dmlConfig.getTextValue("B"));
+ Assert.assertEquals(2, dmlConfig.getIntValue("C"));
+ Assert.assertEquals(5, dmlConfig.getIntValue("D"));
+ Assert.assertEquals(5.01, dmlConfig.getDoubleValue("E"), 1e-15);
+
+ dmlConfig.setTextValue("E", "a");
+ Assert.assertEquals("a", dmlConfig.getTextValue("E"));
+ }
+
+
+
+ @Test
+ public void testDMLConfig2() throws DMLRuntimeException, IOException, ParseException {
+
+ String testStr = "<root>"
+ + "<A>a</A>"
+ + "<B>b</B>"
+ + "<C>2</C>"
+ + "<D>5</D>"
+ + "<E>5.01</E>"
+ + "</root>";
+ File temp = File.createTempFile("tempfile", null);
+ BufferedWriter bw = new BufferedWriter(new FileWriter(temp));
+ bw.write(testStr);
+ bw.close();
+
+ DMLConfig dmlConfig = new DMLConfig(temp.getAbsolutePath());
+
+ Assert.assertEquals("a", dmlConfig.getTextValue("A"));
+ Assert.assertEquals("b", dmlConfig.getTextValue("B"));
+ Assert.assertEquals(2, dmlConfig.getIntValue("C"));
+ Assert.assertEquals(5, dmlConfig.getIntValue("D"));
+ Assert.assertEquals(5.01, dmlConfig.getDoubleValue("E"), 1e-15);
+
+ dmlConfig.setTextValue("E", "a");
+ Assert.assertEquals("a", dmlConfig.getTextValue("E"));
+ }
+
+
+
+
}