[LIB-4] Transition to typed properties
[hespiff.git] / src / main / java / org / hedgecode / xml / xspf / XSPFProperties.java
1 /*
2  * Copyright (c) 2015-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.xml.xspf;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.charset.Charset;
22
23 /**
24  * XSPF API properties holder.
25  *
26  * @author Dmitry Samoshin aka gotty
27  */
28 public final class XSPFProperties {
29
30     /** Charset. */
31     public static final String CHARSET = "xspf.charset";
32
33     /** Formatted output. */
34     public static final String FORMATTED = "xspf.formatted.output";
35
36     /** Standalone. */
37     public static final String STANDALONE = "xspf.standalone";
38
39     /** Version. */
40     public static final String VERSION = "xspf.version";
41
42     /** Inception year. */
43     public static final String INCEPTION_YEAR = "xspf.inception.year";
44
45     /** Properties file. */
46     private static final String PROPERTIES_FILE = "xspf.properties";
47
48     /** Properties. */
49     private static final TypedProperties PROPERTIES;
50
51     /**
52      * Static properties initialization.
53      */
54     static {
55         TypedProperties defaults = new TypedProperties() {
56             {
57                 setProperty(CHARSET, XSPFConstants.DEF_CHARSET.name());
58                 setProperty(FORMATTED, Boolean.TRUE);
59                 setProperty(STANDALONE, Boolean.FALSE);
60             }
61         };
62         PROPERTIES = new TypedProperties(defaults);
63         readProperties(PROPERTIES_FILE, PROPERTIES, true);
64     }
65
66     /**
67      * Read settings from properties file.
68      *
69      * @param propsFile properties file name.
70      * @param props properties.
71      * @param throwExc flag indicating whether to throw an exception or not.
72      */
73     private static void readProperties(String propsFile, TypedProperties props, boolean throwExc) {
74         try (InputStream is = XSPFProperties.class.getClassLoader().getResourceAsStream(propsFile)) {
75             if (is == null) {
76                 if (throwExc)
77                     throw new RuntimeException("Failed to find properties file: " + propsFile);
78                 else
79                     return;
80             }
81             props.load(is);
82         }
83         catch (IOException e) {
84             throw new RuntimeException("Failed to read properties file: " + propsFile, e);
85         }
86     }
87
88     /**
89      * Empty string for not found properties.
90      */
91     private static final String EMPTY = "";
92
93     /**
94      * Get string property value.
95      *
96      * @param key property key.
97      * @return string value in the property list with the specified key value or empty string.
98      */
99     public static String getString(String key) {
100         return PROPERTIES.getProperty(key, String.class, EMPTY);
101     }
102
103     /**
104      * Get boolean property value or null if key not found.
105      *
106      * @param key property key.
107      * @return boolean value in the property list with the specified key value.
108      */
109     public static Boolean getBoolean(String key) {
110         return PROPERTIES.getBoolean(key);
111     }
112
113     /**
114      * Get boolean property value or default value if key not found.
115      *
116      * @param key property key.
117      * @param defaultValue default value if key not found.
118      * @return boolean value in the property list with the specified key value.
119      */
120     public static Boolean getBoolean(String key, boolean defaultValue) {
121         return PROPERTIES.getProperty(key, Boolean.class, defaultValue);
122     }
123
124     /**
125      * Get integer property value or null if key not found..
126      *
127      * @param key property key.
128      * @return integer value in the property list with the specified key value.
129      */
130     public static Integer getInteger(String key) {
131         return PROPERTIES.getInteger(key);
132     }
133
134     /**
135      * Get integer property value or default value if key not found..
136      *
137      * @param key property key.
138      * @param defaultValue default value if key not found.
139      * @return integer value in the property list with the specified key value.
140      */
141     public static Integer getInteger(String key, int defaultValue) {
142         return PROPERTIES.getProperty(key, Integer.class, defaultValue);
143     }
144
145     /**
146      * Get charset.
147      *
148      * @return charset value in the property list or default charset value.
149      */
150     public static Charset getCharset() {
151         return Charset.forName(
152                 PROPERTIES.getString(CHARSET)
153         );
154     }
155
156     /**
157      * Set property value.
158      *
159      * @param key key to be placed into this property list.
160      * @param value value corresponding to key.
161      */
162     public static void setProperty(String key, Object value) {
163         if (PROPERTIES.containsKey(key)) {
164             if (PROPERTIES.get(key).getClass().isInstance(value)) {
165                 PROPERTIES.replace(key, value);
166             }
167         } else {
168             PROPERTIES.setProperty(key, value);
169         }
170     }
171
172     /**
173      * Private constructor.
174      */
175     private XSPFProperties() {
176     }
177
178 }