X-Git-Url: https://git.hedgecode.org/?p=hespiff.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fxml%2Fxspf%2FXSPFProperties.java;fp=src%2Fmain%2Fjava%2Forg%2Fhedgecode%2Fxml%2Fxspf%2FXSPFProperties.java;h=965cd815e96e4be3061dfa27b8c8ddaf67059511;hp=0000000000000000000000000000000000000000;hb=d50a7fa6952079a2685cf740cf221b69a4e5fafa;hpb=ad6e6a2426c97aa14868d9bd2d279efc84c890d8 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 index 0000000..965cd81 --- /dev/null +++ b/src/main/java/org/hedgecode/xml/xspf/XSPFProperties.java @@ -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() { + } + +}