View Javadoc

    1   /***
    2    * @(#)PropertiesHelper.java
    3    *
    4    * JFoxSOAF, Service-Oriented Application Framework
    5    *
    6    * Copyright(c) JFoxSOAF Team
    7    *
    8    * Licensed under the GNU LGPL, Version 2.1 (the "License");
    9    * you may not use this file except in compliance with the License.
    10   * You may obtain a copy of the License at
    11   *
    12   * http://www.gnu.org/copyleft/lesser.html
    13   *
    14   * Unless required by applicable law or agreed to in writing, software
    15   * distributed under the License is distributed on an "AS IS" BASIS,
    16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17   * See the License for the specific language governing permissions and
    18   * limitations under the License.
    19   *
    20   * For more information, please visit:
    21   * http://www.jfox.cn/confluence/display/JFoxSOAF/Home
    22   * http://www.huihoo.org/jfox/jfoxsoaf
    23   */
    24
    25  package org.huihoo.jfox.soaf.util.resource;
    26
    27  import java.util.HashMap;
    28  import java.util.Map;
    29  import java.util.Properties;
    30  import java.util.StringTokenizer;
    31
    32
    33
    34  /***
    35   * A help class to access to property file.
    36   *
    37   * @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
    38   * @version $Revision: 1.2 $ $Date: 2006/02/15 08:45:46 $
    39   * @version Revision: 1.0
    40   */
    41
    42  public class PropertiesHelper {
    43
    44      public PropertiesHelper() {
    45      }
    46
    47      public static boolean getBoolean(String property, Properties properties) {
    48          return Boolean.valueOf(properties.getProperty(property)).booleanValue();
    49      }
    50
    51      public static boolean getBoolean(String property, Properties properties,
    52              boolean defaultValue) {
    53          String setting = properties.getProperty(property);
    54          return (setting == null) ? defaultValue : Boolean.valueOf(setting)
    55                  .booleanValue();
    56      }
    57
    58      public static int getInt(String property, Properties properties,
    59              int defaultValue) {
    60          String propValue = properties.getProperty(property);
    61          return (propValue == null) ? defaultValue : Integer.parseInt(propValue);
    62      }
    63
    64      public static String getString(String property, Properties properties,
    65              String defaultValue) {
    66          String propValue = properties.getProperty(property);
    67          return (propValue == null) ? defaultValue : propValue;
    68      }
    69
    70      public static Integer getInteger(String property, Properties properties) {
    71          String propValue = properties.getProperty(property);
    72          return (propValue == null) ? null : Integer.valueOf(propValue);
    73      }
    74
    75      public static Map toMap(String property, String delim, Properties properties) {
    76          Map map = new HashMap();
    77          String propValue = properties.getProperty(property);
    78          if (propValue != null) {
    79              StringTokenizer tokens = new StringTokenizer(propValue, delim);
    80              while (tokens.hasMoreTokens()) {
    81                  map.put(tokens.nextToken(), tokens.hasMoreElements() ? tokens
    82                          .nextToken() : StringHelper.EMPTY_STRING);
    83              }
    84          }
    85          return map;
    86      }
    87
    88      public static String[] toStringArray(String property, String delim,
    89              Properties properties) {
    90          return toStringArray(properties.getProperty(property), delim);
    91      }
    92
    93      public static String[] toStringArray(String propValue, String delim) {
    94          return StringHelper.split(delim, propValue);
    95      }
    96
    97  }