001    /*
002      Copyright (C) 2002 Fabrice Legond-Aubry.
003    
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful,
010      but WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012      GNU Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.core;
020    
021    import java.io.FileInputStream;
022    import java.io.FileNotFoundException;
023    import java.io.IOException;
024    import java.util.*;
025    import org.apache.log4j.Logger;
026    
027    /**
028     * This class provides useful primitives to parse Java property
029     * files. */
030    
031    public abstract class JacPropTools {
032        static Logger logger = Logger.getLogger("props");
033    
034       /**
035        * Extract a specific vector (ordered) property from a property list.
036        * Then parse it and split it into tokens that will be
037        * added to the "vector".
038        *
039        * @param vector the vetor where all extracted tokens will be
040        * stored
041        * @param pList the list of all properties from where we will
042        * search the property
043        * @param propertyName the name of the property to parse */
044       public static void fillListStringProps(List vector,
045                                              Properties pList,
046                                              String propertyName,
047                                              boolean force)
048       {
049          String prop = pList.getProperty(propertyName);
050          if (prop == null) {
051             logger.warn("\tNo property '"+propertyName +"' found.");
052          }
053          else {
054             logger.debug("\tVECTOR Property '"+propertyName+"' found."); 
055             StringTokenizer st = new StringTokenizer( prop );
056             logger.debug("\tProperty elements added:");
057             String tmp;
058             while ( st.hasMoreElements() ) {
059                try {
060                   tmp = (String)st.nextElement() ;
061                   logger.debug("\t\tElement: " + tmp);
062                   vector.add ( tmp );
063                }
064                catch (Exception e) {
065                   logger.debug("\t\tCan not get a reference for a class."); 
066                   e.printStackTrace(); 
067                }                              
068    
069             }
070          }
071       }
072    
073       /**
074        * Extract a specific hash set property from a property list. Then
075        * parse it and split it into tokens that will be added to the
076        * "hashSet".
077        *
078        * @param set the set where all extracted tokens will be stored
079        * @param pList the list of all properties from where we will
080        * search the property
081        * @param propertyName the name of the property to parse 
082        * @param trim wether to trim ending ".*"
083        */
084       public static void fillSetProps(Set set, 
085                                       Properties pList, 
086                                       String propertyName,
087                                       boolean trim)
088       { 
089          String prop = pList.getProperty(propertyName);
090          if (prop == null) {
091             logger.debug("\t-- WARNING: no property '"+ 
092                       propertyName +"' found.");
093          } else {
094             logger.debug("\tSET Property '"+
095                       propertyName+"' found.");
096             StringTokenizer st = new StringTokenizer( prop );
097             logger.debug("\tProperty tokens added:");
098             while ( st.hasMoreElements() ) {
099                String element = (String)st.nextElement();
100                String 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 propertyName,
131                                       int nElements,
132                                       boolean force)
133       { 
134          String 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 key = (String) st.nextElement();
144                Vector vvalue=new Vector();
145                String value=null;
146                if (nElements>1) {
147                   for(int i=0;i<nElements;i++) {
148                      vvalue.add(((String)st.nextElement()).trim());
149                   }
150                } else if(nElements==0) {
151                   String tmpvalue="";
152                   while(st.hasMoreElements()) {
153                      tmpvalue=((String)st.nextElement()).trim();
154                      if(tmpvalue.equals(".")) break;
155                      vvalue.add(tmpvalue);
156                   }
157                } else {
158                   value = ((String)st.nextElement()).trim();
159                }
160                if (force) {
161                   hashTable.put(key.trim(),value==null?(Object)vvalue:(Object)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)vvalue:(Object)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 fillStringProp(Properties pList, String propertyName)
181       {
182          String 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 directory, String name)
201       {
202          Properties pList = new Properties();
203          try {
204             FileInputStream fis = new FileInputStream(directory + name);
205             pList.load(fis);
206             logger.debug("Properties file '"+name+
207                       "' found in '"+directory +"'.");
208             return pList;
209          }
210          catch (FileNotFoundException e) {
211             logger.warn("No property file '"+
212                       name+"' found in the directory: '"+directory+"'.");
213             return null;
214          }
215          catch (IOException 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    }