2 * Copyright (c) 2015-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.xml.xspf;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.nio.charset.Charset;
24 * XSPF API properties holder.
26 * @author Dmitry Samoshin aka gotty
28 public final class XSPFProperties {
31 public static final String CHARSET = "xspf.charset";
33 /** Formatted output. */
34 public static final String FORMATTED = "xspf.formatted.output";
37 public static final String STANDALONE = "xspf.standalone";
40 public static final String VERSION = "xspf.version";
42 /** Inception year. */
43 public static final String INCEPTION_YEAR = "xspf.inception.year";
45 /** Properties file. */
46 private static final String PROPERTIES_FILE = "xspf.properties";
49 private static final TypedProperties PROPERTIES;
52 * Static properties initialization.
55 TypedProperties defaults = new TypedProperties() {
57 setProperty(CHARSET, XSPFConstants.DEF_CHARSET.name());
58 setProperty(FORMATTED, Boolean.TRUE);
59 setProperty(STANDALONE, Boolean.FALSE);
62 PROPERTIES = new TypedProperties(defaults);
63 readProperties(PROPERTIES_FILE, PROPERTIES, true);
67 * Read settings from properties file.
69 * @param propsFile properties file name.
70 * @param props properties.
71 * @param throwExc flag indicating whether to throw an exception or not.
73 private static void readProperties(String propsFile, TypedProperties props, boolean throwExc) {
74 try (InputStream is = XSPFProperties.class.getClassLoader().getResourceAsStream(propsFile)) {
77 throw new RuntimeException("Failed to find properties file: " + propsFile);
83 catch (IOException e) {
84 throw new RuntimeException("Failed to read properties file: " + propsFile, e);
89 * Empty string for not found properties.
91 private static final String EMPTY = "";
94 * Get string property value.
96 * @param key property key.
97 * @return string value in the property list with the specified key value or empty string.
99 public static String getString(String key) {
100 return PROPERTIES.getProperty(key, String.class, EMPTY);
104 * Get boolean property value or null if key not found.
106 * @param key property key.
107 * @return boolean value in the property list with the specified key value.
109 public static Boolean getBoolean(String key) {
110 return PROPERTIES.getBoolean(key);
114 * Get boolean property value or default value if key not found.
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.
120 public static Boolean getBoolean(String key, boolean defaultValue) {
121 return PROPERTIES.getProperty(key, Boolean.class, defaultValue);
125 * Get integer property value or null if key not found..
127 * @param key property key.
128 * @return integer value in the property list with the specified key value.
130 public static Integer getInteger(String key) {
131 return PROPERTIES.getInteger(key);
135 * Get integer property value or default value if key not found..
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.
141 public static Integer getInteger(String key, int defaultValue) {
142 return PROPERTIES.getProperty(key, Integer.class, defaultValue);
148 * @return charset value in the property list or default charset value.
150 public static Charset getCharset() {
151 return Charset.forName(
152 PROPERTIES.getString(CHARSET)
157 * Set property value.
159 * @param key key to be placed into this property list.
160 * @param value value corresponding to key.
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);
168 PROPERTIES.setProperty(key, value);
173 * Private constructor.
175 private XSPFProperties() {