GrxConfigBundle.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
00003  * All rights reserved. This program is made available under the terms of the
00004  * Eclipse Public License v1.0 which accompanies this distribution, and is
00005  * available at http://www.eclipse.org/legal/epl-v10.html
00006  * Contributors:
00007  * General Robotix Inc.
00008  * National Institute of Advanced Industrial Science and Technology (AIST) 
00009  */
00010 /*
00011  *  GrxConfigBundle.java
00012  *
00013  *  Copyright (C) 2007 GeneralRobotix, Inc.
00014  *  All Rights Reserved
00015  *
00016  *  @author Yuichiro Kawasumi (General Robotix, Inc.)
00017  *  2004/04/19
00018  */
00019 
00020 package com.generalrobotix.ui.util;
00021 
00022 import java.io.FileInputStream;
00023 import java.io.FileOutputStream;
00024 import java.io.IOException;
00025 import java.text.NumberFormat;
00026 import java.util.Properties;
00027 
00028 import com.generalrobotix.ui.grxui.Activator;
00029 
00030 @SuppressWarnings("serial")
00034 public class GrxConfigBundle extends Properties {       
00038         public GrxConfigBundle() {
00039                 
00040         }
00041         
00047         public GrxConfigBundle(String fname) throws IOException{
00048                 load(fname);
00049         }
00050         
00056         public void load(String fname) throws IOException {
00057                 FileInputStream in = new FileInputStream(fname);
00058                 load(in);
00059                 in.close();
00060         }
00061         
00068         public void store(String fname,String comments) throws IOException{
00069                 FileOutputStream out = new FileOutputStream(fname);
00070                 store(out,comments);
00071         }
00072         
00078         public final String getStr(String key){ 
00079                 String str = null;
00080                 StringBuffer buf = null;
00081                 try {
00082                         str = getProperty(key, System.getenv(key));
00083                         buf = new StringBuffer(str);
00084                 } catch (java.util.MissingResourceException ex){
00085                         GrxDebugUtil.println("ConfigBundle.getStr: missingResouce("+key+")");
00086                         return null;
00087                 } catch (NullPointerException e){
00088                         GrxDebugUtil.printErr("ConfigBundle.getStr: The key '"+key+"' does not exist.");
00089                         return null;
00090                 }
00091                 
00092                 for (int i=0;;i++){
00093                         int index1,index2;
00094                         if ((index1 = buf.indexOf("$(")) == -1)
00095                                 break;
00096                         if ((index2 = buf.indexOf(")" ,index1)) != -1){
00097                                 // in case there is no environment value,
00098                                 // check the property file 
00099                                 String keyword = buf.substring(index1+2,index2);
00100                                 String prop = System.getProperty(keyword);
00101                                 if(prop==null){
00102                                         prop = Activator.getDefault().getPreferenceStore().getString(keyword);
00103                                         if(prop.equals("")){
00104                                                 prop = System.getenv(keyword);                  
00105                                                 if (prop == null)
00106                                                         prop = getStr(keyword,"");
00107                                         }
00108                                 }
00109                                 buf.replace(index1,index2+1,prop);
00110                         }
00111                 }
00112                 return buf.toString();
00113         }
00114         
00121         public final String getStr(String key, String defaultVal) {
00122                 String ret = null;
00123                 if ((ret= getStr(key)) == null)
00124                         ret = defaultVal;
00125                 return ret;
00126         }
00127 
00134         public final Integer getInt(String key, Integer defaultVal) {
00135                 Integer ret;
00136                 try {
00137                         ret = Integer.parseInt(getStr(key));
00138                 } catch(Exception e){
00139                         ret = defaultVal;       
00140                 }
00141                 return ret;
00142         }
00143         
00150         public final Short getShort(String key, Short defaultVal) {
00151                 Short ret = getShort(getStr(key));
00152                 if (ret != null){
00153                         return ret;
00154                 }else{
00155                         return defaultVal;
00156                 }
00157         }
00158 
00164         public Short getShort(String value){
00165                 Short ret;
00166                 try {
00167                         ret = Short.parseShort(value);
00168                 } catch(Exception e){
00169                         return null;
00170                 }
00171                 return ret;
00172         }
00173 
00179         public final void setShort(String key, short value) {
00180                 String val = new String();
00181                 val += String.valueOf(value)+" ";
00182                 setProperty(key,val);
00183         }
00184         
00190         public final int[] getIntAry(String key){
00191                 String s = getStr(key);
00192                 if (s==null)
00193                         return null;
00194                 String[] str = s.split(" ");
00195                 int[] ret = null;
00196                 ret = new int[str.length];
00197                 try{
00198                         for (int i=0;i<str.length;i++)
00199                                 ret[i] = Integer.parseInt(str[i]);
00200                 }catch(Exception e){
00201                         return null;
00202                 }
00203                 return ret;
00204         }
00205         
00212         public final Double getDbl(String key, Double defaultVal) {
00213                 Double ret = getDbl(getStr(key));
00214                 if (ret != null){
00215                         return ret;
00216                 }else{
00217                         return defaultVal;
00218                 }
00219         }
00220         
00226         public Double getDbl(String value){
00227                 Double ret;
00228                 try {
00229                         ret = Double.parseDouble(value);
00230                 } catch(Exception e){
00231                         return null;
00232                 }
00233                 return ret;
00234         }
00235 
00242         public final Float getFlt(String key, Float defaultVal) {
00243                 Float ret;
00244                 try {
00245                         ret = Float.parseFloat(getStr(key));
00246                 } catch(Exception e){
00247                         ret = defaultVal;
00248                 }
00249                 return ret;
00250         }
00251         
00252         public final Float getFlt(String value) {
00253                 Float ret;
00254                 try {
00255                         ret = Float.parseFloat(value);
00256                 } catch(Exception e){
00257                         return null;
00258                 }
00259                 return ret;
00260         }
00261         
00268         public final double[] getDblAry(String key, double[] defaultVal){
00269                 String s = getStr(key);
00270                 double[] ret = getDblAry(s);
00271                 if (ret != null){
00272                         return ret;
00273                 }else{
00274                         return defaultVal;
00275                 }
00276         }
00277         
00283         public double[] getDblAry(String value){
00284                 if (value == null) return null;
00285                 String[] str = value.split(" ");
00286                 double[] ret = new double[str.length];
00287                 try {
00288                         for (int i=0;i<str.length;i++)
00289                                 ret[i] = Double.parseDouble(str[i]);
00290                 } catch(Exception e){
00291                         return null;
00292                 }
00293                 return ret;
00294         }
00295         
00302         public final float[] getFltAry(String key, float[] defaultVal){
00303                 String s = getStr(key);
00304                 if (s==null)
00305                         return defaultVal;
00306                 String[] str = s.split(" ");
00307                 float[] ret = new float[str.length];
00308                 try {
00309                         for (int i=0;i<str.length;i++)
00310                                 ret[i] = Float.parseFloat(str[i]);
00311                 } catch(Exception e){
00312                         return defaultVal;
00313                 }
00314                 
00315                 return ret;
00316         }
00317         
00318         public float[] getFltAry(String value){
00319                 if (value == null) return null;
00320                 String[] str = value.split(" ");
00321                 float[] ret = new float[str.length];
00322                 try {
00323                         for (int i=0;i<str.length;i++)
00324                                 ret[i] = Float.parseFloat(str[i]);
00325                 } catch(Exception e){
00326                         return null;
00327                 }
00328                 return ret;
00329         }
00330         
00337         public final void setDblAry(String key, double[] value, int digits) {
00338                 String val = new String();
00339                 NumberFormat nf = NumberFormat.getInstance();
00340                 nf.setMaximumFractionDigits(digits);
00341                 for (int i=0; i<value.length; i++) {
00342                         if (Double.isNaN(value[i])){
00343                                 val += "NaN ";
00344                         }else{
00345                                 val += nf.format(value[i])+" ";
00346                         }
00347                 }
00348                 setProperty(key,val);
00349         }
00350         
00356         public final void setDblAry(String key, double[] value) {
00357                 String val = new String();
00358                 for (int i=0; i<value.length; i++) {
00359                         val += String.valueOf(value[i])+" ";
00360                 }
00361                 setProperty(key,val);
00362         }
00363         
00369         public final void setFlt(String key, float value) {
00370                 String val = new String();
00371                 val += String.valueOf(value)+" ";
00372                 setProperty(key,val);
00373         }
00374         
00380         public final void setFltAry(String key, float[] value) {
00381                 String val = new String();
00382                 for (int i=0; i<value.length; i++) {
00383                         val += String.valueOf(value[i])+" ";
00384                 }
00385                 setProperty(key,val);
00386         }
00387         
00393         public final void setDbl(String key, double value) {
00394                 String val = new String();
00395                 val += String.valueOf(value)+" ";
00396                 setProperty(key,val);
00397         }
00398         
00405         public final void setDbl(String key, double value, int digits) {
00406                 String val = new String();
00407                 NumberFormat nf = NumberFormat.getInstance();
00408                 nf.setMaximumFractionDigits(digits);
00409                 val += nf.format(value)+" ";
00410                 setProperty(key,val);
00411         }
00412         
00418         public final void setInt(String key, int value) {
00419                 String val = new String();
00420                 val += String.valueOf(value)+" ";
00421                 setProperty(key,val);
00422         }
00423         
00424         public final void setBool(String key, boolean value){
00425                 if(value)
00426                         setProperty(key,"true");
00427                 else
00428                         setProperty(key,"false");
00429         }
00430         
00436         public final boolean isTrue(String key){
00437                 return isTrue(key,false);
00438         }
00439         
00446         public final boolean isTrue(String key,boolean defaultVal){
00447                 Boolean b = new Boolean(defaultVal);
00448                 String str = getStr(key,b.toString()).toLowerCase();
00449                 if (str.indexOf("true")!=-1)
00450                         return true;
00451                 return false;
00452         }
00453 
00459         public final boolean isFalse(String key){
00460                 return isFalse(key,false);
00461         }
00462 
00468         public final boolean isFalse(String key,boolean defaultVal){
00469                 Boolean b = new Boolean(defaultVal);
00470                 String str = getStr(key,b.toString()).toLowerCase();
00471                 if (str.indexOf("false")!=-1)
00472                         return true;
00473                 return false;
00474         }
00475 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:16