KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > JacPropTools


1 /*
2   Copyright (C) 2002 Fabrice Legond-Aubry.
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public
15   License along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17   USA */

18
19 package org.objectweb.jac.core;
20
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.util.*;
25 import org.apache.log4j.Logger;
26
27 /**
28  * This class provides useful primitives to parse Java property
29  * files. */

30
31 public abstract class JacPropTools {
32     static Logger logger = Logger.getLogger("props");
33
34    /**
35     * Extract a specific vector (ordered) property from a property list.
36     * Then parse it and split it into tokens that will be
37     * added to the "vector".
38     *
39     * @param vector the vetor where all extracted tokens will be
40     * stored
41     * @param pList the list of all properties from where we will
42     * search the property
43     * @param propertyName the name of the property to parse */

44    public static void fillListStringProps(List vector,
45                                           Properties pList,
46                                           String JavaDoc propertyName,
47                                           boolean force)
48    {
49       String JavaDoc prop = pList.getProperty(propertyName);
50       if (prop == null) {
51          logger.warn("\tNo property '"+propertyName +"' found.");
52       }
53       else {
54          logger.debug("\tVECTOR Property '"+propertyName+"' found.");
55          StringTokenizer st = new StringTokenizer( prop );
56          logger.debug("\tProperty elements added:");
57          String JavaDoc tmp;
58          while ( st.hasMoreElements() ) {
59             try {
60                tmp = (String JavaDoc)st.nextElement() ;
61                logger.debug("\t\tElement: " + tmp);
62                vector.add ( tmp );
63             }
64             catch (Exception JavaDoc e) {
65                logger.debug("\t\tCan not get a reference for a class.");
66                e.printStackTrace();
67             }
68
69          }
70       }
71    }
72
73    /**
74     * Extract a specific hash set property from a property list. Then
75     * parse it and split it into tokens that will be added to the
76     * "hashSet".
77     *
78     * @param set the set where all extracted tokens will be stored
79     * @param pList the list of all properties from where we will
80     * search the property
81     * @param propertyName the name of the property to parse
82     * @param trim wether to trim ending ".*"
83     */

84    public static void fillSetProps(Set set,
85                                    Properties pList,
86                                    String JavaDoc propertyName,
87                                    boolean trim)
88    {
89       String JavaDoc prop = pList.getProperty(propertyName);
90       if (prop == null) {
91          logger.debug("\t-- WARNING: no property '"+
92                    propertyName +"' found.");
93       } else {
94          logger.debug("\tSET Property '"+
95                    propertyName+"' found.");
96          StringTokenizer st = new StringTokenizer( prop );
97          logger.debug("\tProperty tokens added:");
98          while ( st.hasMoreElements() ) {
99             String JavaDoc element = (String JavaDoc)st.nextElement();
100             String JavaDoc tmp;
101         // I remove this to implement a more general wildcard
102
// policy (RP)
103
//if (element.endsWith(".*") && trim) tmp =
104
//element.substring(0,element.length()-2); else
105
tmp = element;
106             tmp = tmp.trim();
107             set.add(tmp);
108             logger.debug("\t\tToken: " + tmp);
109          }
110       }
111    }
112     
113
114    /**
115     * Extract a specific hashTable property from a property list.
116     * Then parse it and split it into tokens that will be added to the
117     * "hashTable".
118     *
119     * @param hashTable the hash table where all extracted tokens will
120     * be stored
121     * @param pList the list of all properties from where we will
122     * search the property
123     * @param propertyName the name of the property to parse
124     * @param nElements the number of elements attached to a key (the
125     * key is the first element). If nElements==0, the number of
126     * elements attached to the key is variant and must be ending
127     * with a '.'. */

128    public static void fillMapProps(Map hashTable,
129                                    Properties pList,
130                                    String JavaDoc propertyName,
131                                    int nElements,
132                                    boolean force)
133    {
134       String JavaDoc prop = pList.getProperty(propertyName);
135       if (prop == null) {
136          logger.debug("\t-- WARNING: no property '"+propertyName+"' found.");
137       }
138       else {
139          logger.debug("\tHASHTABLE Property '"+propertyName+"' found.");
140          StringTokenizer st = new StringTokenizer( prop );
141          logger.debug("\tProperty couple added:");
142          while ( st.hasMoreElements() ) {
143             String JavaDoc key = (String JavaDoc) st.nextElement();
144             Vector vvalue=new Vector();
145             String JavaDoc value=null;
146             if (nElements>1) {
147                for(int i=0;i<nElements;i++) {
148                   vvalue.add(((String JavaDoc)st.nextElement()).trim());
149                }
150             } else if(nElements==0) {
151                String JavaDoc tmpvalue="";
152                while(st.hasMoreElements()) {
153                   tmpvalue=((String JavaDoc)st.nextElement()).trim();
154                   if(tmpvalue.equals(".")) break;
155                   vvalue.add(tmpvalue);
156                }
157             } else {
158                value = ((String JavaDoc)st.nextElement()).trim();
159             }
160             if (force) {
161                hashTable.put(key.trim(),value==null?(Object JavaDoc)vvalue:(Object JavaDoc)value);
162                logger.debug("\t\t(key,value): ("+
163                          key+","+(value==null?vvalue.toString():value)+")");
164             } else if (!hashTable.containsKey(key)) {
165                hashTable.put(key.trim(),value==null?(Object JavaDoc)vvalue:(Object JavaDoc)value);
166                logger.debug("\t\t(key,value): ("+
167                          key+","+(value==null?vvalue.toString():value)+")");
168             }
169          }
170       }
171    }
172         
173
174    /**
175     * Extracts a specific String property from a property list.
176     *
177     * @param pList the list of all properties from where we will search the property
178     * @param propertyName the name of the property to parse
179     */

180    public static String JavaDoc fillStringProp(Properties pList, String JavaDoc propertyName)
181    {
182       String JavaDoc tmp = pList.getProperty(propertyName);
183       if ( tmp == null )
184          logger.debug("\t-- WARNING: no property '"+propertyName+"' found.");
185       else
186          logger.debug("\tSTRING Property '"+propertyName+"' found.");
187       if ( tmp == null)
188          return null;
189       logger.debug("\tValue is "+tmp);
190       return tmp.trim();
191    }
192
193    /**
194     * Try to load the property file (propFileName) from the specified directory.
195     *
196     * @param directory the directory where we should, in theory, found the property file
197     * @param name the name of the property file
198     * @return true if the file was found and loaded, false otherwise
199     */

200    public static Properties getPropsFrom(String JavaDoc directory, String JavaDoc name)
201    {
202       Properties pList = new Properties();
203       try {
204          FileInputStream JavaDoc fis = new FileInputStream JavaDoc(directory + name);
205          pList.load(fis);
206          logger.debug("Properties file '"+name+
207                    "' found in '"+directory +"'.");
208          return pList;
209       }
210       catch (FileNotFoundException JavaDoc e) {
211          logger.warn("No property file '"+
212                    name+"' found in the directory: '"+directory+"'.");
213          return null;
214       }
215       catch (IOException JavaDoc e) {
216          logger.warn("Can not load file '"+
217                      name+"' found in the directory: '" +
218                    directory +"'.");
219          e.printStackTrace();
220          return null;
221       }
222    }
223
224
225 }
226
Popular Tags