GrxConfigBundle.java
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, AIST, the University of Tokyo and General Robotix Inc.
3  * All rights reserved. This program is made available under the terms of the
4  * Eclipse Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/epl-v10.html
6  * Contributors:
7  * General Robotix Inc.
8  * National Institute of Advanced Industrial Science and Technology (AIST)
9  */
10 /*
11  * GrxConfigBundle.java
12  *
13  * Copyright (C) 2007 GeneralRobotix, Inc.
14  * All Rights Reserved
15  *
16  * @author Yuichiro Kawasumi (General Robotix, Inc.)
17  * 2004/04/19
18  */
19 
20 package com.generalrobotix.ui.util;
21 
22 import java.io.FileInputStream;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.text.NumberFormat;
26 import java.util.Properties;
27 
29 
30 @SuppressWarnings("serial")
34 public class GrxConfigBundle extends Properties {
38  public GrxConfigBundle() {
39 
40  }
41 
47  public GrxConfigBundle(String fname) throws IOException{
48  load(fname);
49  }
50 
56  public void load(String fname) throws IOException {
57  FileInputStream in = new FileInputStream(fname);
58  load(in);
59  in.close();
60  }
61 
68  public void store(String fname,String comments) throws IOException{
69  FileOutputStream out = new FileOutputStream(fname);
70  store(out,comments);
71  }
72 
78  public final String getStr(String key){
79  String str = null;
80  StringBuffer buf = null;
81  try {
82  str = getProperty(key, System.getenv(key));
83  buf = new StringBuffer(str);
84  } catch (java.util.MissingResourceException ex){
85  GrxDebugUtil.println("ConfigBundle.getStr: missingResouce("+key+")");
86  return null;
87  } catch (NullPointerException e){
88  GrxDebugUtil.printErr("ConfigBundle.getStr: The key '"+key+"' does not exist.");
89  return null;
90  }
91 
92  for (int i=0;;i++){
93  int index1,index2;
94  if ((index1 = buf.indexOf("$(")) == -1)
95  break;
96  if ((index2 = buf.indexOf(")" ,index1)) != -1){
97  // in case there is no environment value,
98  // check the property file
99  String keyword = buf.substring(index1+2,index2);
100  String prop = System.getProperty(keyword);
101  if(prop==null){
102  prop = Activator.getDefault().getPreferenceStore().getString(keyword);
103  if(prop.equals("")){
104  prop = System.getenv(keyword);
105  if (prop == null)
106  prop = getStr(keyword,"");
107  }
108  }
109  buf.replace(index1,index2+1,prop);
110  }
111  }
112  return buf.toString();
113  }
114 
121  public final String getStr(String key, String defaultVal) {
122  String ret = null;
123  if ((ret= getStr(key)) == null)
124  ret = defaultVal;
125  return ret;
126  }
127 
134  public final Integer getInt(String key, Integer defaultVal) {
135  Integer ret;
136  try {
137  ret = Integer.parseInt(getStr(key));
138  } catch(Exception e){
139  ret = defaultVal;
140  }
141  return ret;
142  }
143 
150  public final Short getShort(String key, Short defaultVal) {
151  Short ret = getShort(getStr(key));
152  if (ret != null){
153  return ret;
154  }else{
155  return defaultVal;
156  }
157  }
158 
164  public Short getShort(String value){
165  Short ret;
166  try {
167  ret = Short.parseShort(value);
168  } catch(Exception e){
169  return null;
170  }
171  return ret;
172  }
173 
179  public final void setShort(String key, short value) {
180  String val = new String();
181  val += String.valueOf(value)+" ";
182  setProperty(key,val);
183  }
184 
190  public final int[] getIntAry(String key){
191  String s = getStr(key);
192  if (s==null)
193  return null;
194  String[] str = s.split(" ");
195  int[] ret = null;
196  ret = new int[str.length];
197  try{
198  for (int i=0;i<str.length;i++)
199  ret[i] = Integer.parseInt(str[i]);
200  }catch(Exception e){
201  return null;
202  }
203  return ret;
204  }
205 
212  public final Double getDbl(String key, Double defaultVal) {
213  Double ret = getDbl(getStr(key));
214  if (ret != null){
215  return ret;
216  }else{
217  return defaultVal;
218  }
219  }
220 
226  public Double getDbl(String value){
227  Double ret;
228  try {
229  ret = Double.parseDouble(value);
230  } catch(Exception e){
231  return null;
232  }
233  return ret;
234  }
235 
242  public final Float getFlt(String key, Float defaultVal) {
243  Float ret;
244  try {
245  ret = Float.parseFloat(getStr(key));
246  } catch(Exception e){
247  ret = defaultVal;
248  }
249  return ret;
250  }
251 
252  public final Float getFlt(String value) {
253  Float ret;
254  try {
255  ret = Float.parseFloat(value);
256  } catch(Exception e){
257  return null;
258  }
259  return ret;
260  }
261 
268  public final double[] getDblAry(String key, double[] defaultVal){
269  String s = getStr(key);
270  double[] ret = getDblAry(s);
271  if (ret != null){
272  return ret;
273  }else{
274  return defaultVal;
275  }
276  }
277 
283  public double[] getDblAry(String value){
284  if (value == null) return null;
285  String[] str = value.split(" ");
286  double[] ret = new double[str.length];
287  try {
288  for (int i=0;i<str.length;i++)
289  ret[i] = Double.parseDouble(str[i]);
290  } catch(Exception e){
291  return null;
292  }
293  return ret;
294  }
295 
302  public final float[] getFltAry(String key, float[] defaultVal){
303  String s = getStr(key);
304  if (s==null)
305  return defaultVal;
306  String[] str = s.split(" ");
307  float[] ret = new float[str.length];
308  try {
309  for (int i=0;i<str.length;i++)
310  ret[i] = Float.parseFloat(str[i]);
311  } catch(Exception e){
312  return defaultVal;
313  }
314 
315  return ret;
316  }
317 
318  public float[] getFltAry(String value){
319  if (value == null) return null;
320  String[] str = value.split(" ");
321  float[] ret = new float[str.length];
322  try {
323  for (int i=0;i<str.length;i++)
324  ret[i] = Float.parseFloat(str[i]);
325  } catch(Exception e){
326  return null;
327  }
328  return ret;
329  }
330 
337  public final void setDblAry(String key, double[] value, int digits) {
338  String val = new String();
339  NumberFormat nf = NumberFormat.getInstance();
340  nf.setMaximumFractionDigits(digits);
341  for (int i=0; i<value.length; i++) {
342  if (Double.isNaN(value[i])){
343  val += "NaN ";
344  }else{
345  val += nf.format(value[i])+" ";
346  }
347  }
348  setProperty(key,val);
349  }
350 
356  public final void setDblAry(String key, double[] value) {
357  String val = new String();
358  for (int i=0; i<value.length; i++) {
359  val += String.valueOf(value[i])+" ";
360  }
361  setProperty(key,val);
362  }
363 
369  public final void setFlt(String key, float value) {
370  String val = new String();
371  val += String.valueOf(value)+" ";
372  setProperty(key,val);
373  }
374 
380  public final void setFltAry(String key, float[] value) {
381  String val = new String();
382  for (int i=0; i<value.length; i++) {
383  val += String.valueOf(value[i])+" ";
384  }
385  setProperty(key,val);
386  }
387 
393  public final void setDbl(String key, double value) {
394  String val = new String();
395  val += String.valueOf(value)+" ";
396  setProperty(key,val);
397  }
398 
405  public final void setDbl(String key, double value, int digits) {
406  String val = new String();
407  NumberFormat nf = NumberFormat.getInstance();
408  nf.setMaximumFractionDigits(digits);
409  val += nf.format(value)+" ";
410  setProperty(key,val);
411  }
412 
418  public final void setInt(String key, int value) {
419  String val = new String();
420  val += String.valueOf(value)+" ";
421  setProperty(key,val);
422  }
423 
424  public final void setBool(String key, boolean value){
425  if(value)
426  setProperty(key,"true");
427  else
428  setProperty(key,"false");
429  }
430 
436  public final boolean isTrue(String key){
437  return isTrue(key,false);
438  }
439 
446  public final boolean isTrue(String key,boolean defaultVal){
447  Boolean b = new Boolean(defaultVal);
448  String str = getStr(key,b.toString()).toLowerCase();
449  if (str.indexOf("true")!=-1)
450  return true;
451  return false;
452  }
453 
459  public final boolean isFalse(String key){
460  return isFalse(key,false);
461  }
462 
468  public final boolean isFalse(String key,boolean defaultVal){
469  Boolean b = new Boolean(defaultVal);
470  String str = getStr(key,b.toString()).toLowerCase();
471  if (str.indexOf("false")!=-1)
472  return true;
473  return false;
474  }
475 }
const char * keyword
Definition: cdjpeg.h:149
final float [] getFltAry(String key, float[] defaultVal)
get float array associated to key
final boolean isTrue(String key)
check whether value associated to key includes a word "true"
final Integer getInt(String key, Integer defaultVal)
get integer value associated to key
final double [] getDblAry(String key, double[] defaultVal)
get double array associated to key
#define null
our own NULL pointer
Definition: IceTypes.h:57
final void setDbl(String key, double value, int digits)
associate double value to key
final void setBool(String key, boolean value)
final Short getShort(String key, Short defaultVal)
get short value associated to key
png_voidp int value
Definition: png.h:2113
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
final Float getFlt(String key, Float defaultVal)
get float value associated to key
final boolean isFalse(String key, boolean defaultVal)
check whether value associated to key includes a word "false"
final void setFltAry(String key, float[] value)
associate float array to key
png_uint_32 i
Definition: png.h:2735
long b
Definition: jpegint.h:371
final void setDbl(String key, double value)
associate double value to key
final void setDblAry(String key, double[] value)
associate double array to key
final void setInt(String key, int value)
associate int value to key
final void setFlt(String key, float value)
associate float value to key
int val
Definition: jpeglib.h:956
png_bytep buf
Definition: png.h:2729
final void setShort(String key, short value)
associate short value to key
prop
final Double getDbl(String key, Double defaultVal)
get double value associated to key
final int [] getIntAry(String key)
get integer array associated to key
final String getStr(String key)
get value associated to keyword
double [] getDblAry(String value)
get array of double from String
void load(String fname)
load config from a file
void store(String fname, String comments)
store this config
GrxConfigBundle(String fname)
construct from a file
final boolean isTrue(String key, boolean defaultVal)
check whether value associated to key includes a word "true"
final void setDblAry(String key, double[] value, int digits)
associate double array to key
final boolean isFalse(String key)
check whether value associated to key includes a word "false"
final String getStr(String key, String defaultVal)
get value associated to key


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Sep 8 2022 02:24:03