TAMAYA-240: ConfigFormat. readXX should throw IOException instead of 
ConfigException


Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/commit/1598ccd0
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/tree/1598ccd0
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/diff/1598ccd0

Branch: refs/heads/master
Commit: 1598ccd063cea9a09d98afa0a612edb983080ddb
Parents: 63b77be
Author: anatole <anat...@apache.org>
Authored: Thu Feb 23 01:15:24 2017 +0100
Committer: anatole <anat...@apache.org>
Committed: Mon Feb 27 00:05:00 2017 +0100

----------------------------------------------------------------------
 .../tamaya/commons/IniConfigurationFormat.java  | 28 ++++-----
 .../tamaya/commons/XmlConfigurationFormat.java  | 63 ++++++++++++++++++++
 ...org.apache.tamaya.format.ConfigurationFormat | 20 +++++++
 3 files changed, 95 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1598ccd0/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java
----------------------------------------------------------------------
diff --git 
a/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java
 
b/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java
index 916d4ed..de1a35b 100644
--- 
a/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java
+++ 
b/apache-commons/src/main/java/org/apache/tamaya/commons/IniConfigurationFormat.java
@@ -26,7 +26,6 @@ import org.apache.tamaya.format.ConfigurationData;
 import org.apache.tamaya.format.ConfigurationDataBuilder;
 import org.apache.tamaya.format.ConfigurationFormat;
 
-import java.io.InputStream;
 import java.net.URL;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -38,6 +37,18 @@ import java.util.Map;
  */
 public class IniConfigurationFormat implements ConfigurationFormat {
 
+    @Override
+    public String getName() {
+        return "ini";
+    }
+
+    @Override
+    public boolean accepts(URL url) {
+        String fileName = url.getFile();
+        return fileName.endsWith(".ini") || fileName.endsWith(".INI");
+    }
+
+    @Override
     public ConfigurationData readConfiguration(URL url) {
         ConfigurationDataBuilder builder = 
ConfigurationDataBuilder.of(url.toString(), this);
         try {
@@ -57,19 +68,4 @@ public class IniConfigurationFormat implements 
ConfigurationFormat {
         }
         return builder.build();
     }
-
-    @Override
-    public String getName() {
-        throw new RuntimeException("Not implemented yet!");
-    }
-
-    @Override
-    public boolean accepts(URL url) {
-        throw new RuntimeException("Not implemented yet!");
-    }
-
-    @Override
-    public ConfigurationData readConfiguration(String resource, InputStream 
inputStream) {
-        throw new RuntimeException("Not implemented yet!");
-    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1598ccd0/apache-commons/src/main/java/org/apache/tamaya/commons/XmlConfigurationFormat.java
----------------------------------------------------------------------
diff --git 
a/apache-commons/src/main/java/org/apache/tamaya/commons/XmlConfigurationFormat.java
 
b/apache-commons/src/main/java/org/apache/tamaya/commons/XmlConfigurationFormat.java
new file mode 100644
index 0000000..9f3a503
--- /dev/null
+++ 
b/apache-commons/src/main/java/org/apache/tamaya/commons/XmlConfigurationFormat.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.tamaya.commons;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.XMLConfiguration;
+import org.apache.tamaya.ConfigException;
+import org.apache.tamaya.format.ConfigurationData;
+import org.apache.tamaya.format.ConfigurationDataBuilder;
+import org.apache.tamaya.format.ConfigurationFormat;
+
+import java.net.URL;
+import java.util.Iterator;
+
+/**
+ * Implements a ini file format based on the APache Commons
+ * {@link XMLConfiguration}.
+ */
+public class XmlConfigurationFormat implements ConfigurationFormat {
+
+    @Override
+    public String getName() {
+        return "xml";
+    }
+
+    @Override
+    public boolean accepts(URL url) {
+        String fileName = url.getFile().toLowerCase();
+        return fileName.endsWith(".xml");
+    }
+
+    @Override
+    public ConfigurationData readConfiguration(URL url) {
+        ConfigurationDataBuilder builder = 
ConfigurationDataBuilder.of(url.toString(), this);
+        try {
+            XMLConfiguration commonXmlConfiguration = new 
XMLConfiguration(url);
+                Iterator<String> keyIter = commonXmlConfiguration.getKeys();
+                while(keyIter.hasNext()){
+                    String key = keyIter.next();
+                    builder.addDefaultProperty(key, 
commonXmlConfiguration.getString(key));
+            }
+        } catch (ConfigurationException e) {
+            throw new ConfigException("Failed to parse xml-file format from " 
+ url, e);
+        }
+        return builder.build();
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tamaya-sandbox/blob/1598ccd0/apache-commons/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
----------------------------------------------------------------------
diff --git 
a/apache-commons/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
 
b/apache-commons/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
new file mode 100644
index 0000000..af86833
--- /dev/null
+++ 
b/apache-commons/src/main/resources/META-INF/services/org.apache.tamaya.format.ConfigurationFormat
@@ -0,0 +1,20 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy current the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+org.apache.tamaya.commons.IniConfigurationFormat
+org.apache.tamaya.commons.XmlConfigurationFormat
\ No newline at end of file

Reply via email to