[LIB-4] Transition to typed properties
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / XSPFProperties.java
diff --git a/src/main/java/org/hedgecode/xml/xspf/XSPFProperties.java b/src/main/java/org/hedgecode/xml/xspf/XSPFProperties.java
new file mode 100644 (file)
index 0000000..965cd81
--- /dev/null
@@ -0,0 +1,178 @@
+/*
+ * Copyright (c) 2015-2019. Developed by Hedgecode.
+ *
+ * Licensed 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.hedgecode.xml.xspf;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.Charset;
+
+/**
+ * XSPF API properties holder.
+ *
+ * @author Dmitry Samoshin aka gotty
+ */
+public final class XSPFProperties {
+
+    /** Charset. */
+    public static final String CHARSET = "xspf.charset";
+
+    /** Formatted output. */
+    public static final String FORMATTED = "xspf.formatted.output";
+
+    /** Standalone. */
+    public static final String STANDALONE = "xspf.standalone";
+
+    /** Version. */
+    public static final String VERSION = "xspf.version";
+
+    /** Inception year. */
+    public static final String INCEPTION_YEAR = "xspf.inception.year";
+
+    /** Properties file. */
+    private static final String PROPERTIES_FILE = "xspf.properties";
+
+    /** Properties. */
+    private static final TypedProperties PROPERTIES;
+
+    /**
+     * Static properties initialization.
+     */
+    static {
+        TypedProperties defaults = new TypedProperties() {
+            {
+                setProperty(CHARSET, XSPFConstants.DEF_CHARSET.name());
+                setProperty(FORMATTED, Boolean.TRUE);
+                setProperty(STANDALONE, Boolean.FALSE);
+            }
+        };
+        PROPERTIES = new TypedProperties(defaults);
+        readProperties(PROPERTIES_FILE, PROPERTIES, true);
+    }
+
+    /**
+     * Read settings from properties file.
+     *
+     * @param propsFile properties file name.
+     * @param props properties.
+     * @param throwExc flag indicating whether to throw an exception or not.
+     */
+    private static void readProperties(String propsFile, TypedProperties props, boolean throwExc) {
+        try (InputStream is = XSPFProperties.class.getClassLoader().getResourceAsStream(propsFile)) {
+            if (is == null) {
+                if (throwExc)
+                    throw new RuntimeException("Failed to find properties file: " + propsFile);
+                else
+                    return;
+            }
+            props.load(is);
+        }
+        catch (IOException e) {
+            throw new RuntimeException("Failed to read properties file: " + propsFile, e);
+        }
+    }
+
+    /**
+     * Empty string for not found properties.
+     */
+    private static final String EMPTY = "";
+
+    /**
+     * Get string property value.
+     *
+     * @param key property key.
+     * @return string value in the property list with the specified key value or empty string.
+     */
+    public static String getString(String key) {
+        return PROPERTIES.getProperty(key, String.class, EMPTY);
+    }
+
+    /**
+     * Get boolean property value or null if key not found.
+     *
+     * @param key property key.
+     * @return boolean value in the property list with the specified key value.
+     */
+    public static Boolean getBoolean(String key) {
+        return PROPERTIES.getBoolean(key);
+    }
+
+    /**
+     * Get boolean property value or default value if key not found.
+     *
+     * @param key property key.
+     * @param defaultValue default value if key not found.
+     * @return boolean value in the property list with the specified key value.
+     */
+    public static Boolean getBoolean(String key, boolean defaultValue) {
+        return PROPERTIES.getProperty(key, Boolean.class, defaultValue);
+    }
+
+    /**
+     * Get integer property value or null if key not found..
+     *
+     * @param key property key.
+     * @return integer value in the property list with the specified key value.
+     */
+    public static Integer getInteger(String key) {
+        return PROPERTIES.getInteger(key);
+    }
+
+    /**
+     * Get integer property value or default value if key not found..
+     *
+     * @param key property key.
+     * @param defaultValue default value if key not found.
+     * @return integer value in the property list with the specified key value.
+     */
+    public static Integer getInteger(String key, int defaultValue) {
+        return PROPERTIES.getProperty(key, Integer.class, defaultValue);
+    }
+
+    /**
+     * Get charset.
+     *
+     * @return charset value in the property list or default charset value.
+     */
+    public static Charset getCharset() {
+        return Charset.forName(
+                PROPERTIES.getString(CHARSET)
+        );
+    }
+
+    /**
+     * Set property value.
+     *
+     * @param key key to be placed into this property list.
+     * @param value value corresponding to key.
+     */
+    public static void setProperty(String key, Object value) {
+        if (PROPERTIES.containsKey(key)) {
+            if (PROPERTIES.get(key).getClass().isInstance(value)) {
+                PROPERTIES.replace(key, value);
+            }
+        } else {
+            PROPERTIES.setProperty(key, value);
+        }
+    }
+
+    /**
+     * Private constructor.
+     */
+    private XSPFProperties() {
+    }
+
+}