< prev index next >

src/com/sun/interview/Properties2.java

Print this page
rev 145 : 7902237: Fixing raw use of parameterized class
Reviewed-by: jjg


  54      * found in this property list.
  55      */
  56     protected Properties2 defaults;
  57 
  58     /**
  59      * Creates an empty property list with no default values.
  60      */
  61     public Properties2() {
  62         this(null);
  63     }
  64 
  65     /**
  66      * Creates an empty property list with the specified defaults.
  67      * @param   defaults   the defaults.
  68      */
  69     public Properties2(Properties2 defaults) {
  70         this.defaults = defaults;
  71     }
  72 
  73     public void load(java.util.Properties source) {
  74         Enumeration e = source.propertyNames();
  75         while(e.hasMoreElements()) {
  76             Object next = e.nextElement();
  77             put( ((String)next), source.get(next) );
  78         }   // while
  79     }
  80 
  81 
  82     /**
  83      * Reads a property list from an input stream.
  84      *
  85      * @param      in   the input stream.
  86      * @exception  IOException  if an error occurred when reading from the
  87      *               input stream.
  88      */
  89     public synchronized void load(Reader in) throws IOException {
  90         int ch = in.read();
  91         while (true) {
  92             switch (ch) {
  93               case -1:
  94                 return;


 186      * string header is printed as a comment at the beginning of the stream.
 187      *
 188      * @param   out      an output stream.
 189      * @param   header   a description of the property list.
 190      */
 191     public synchronized void save(Writer out, String header) {
 192         save(out, header, false);
 193     }
 194 
 195     private void save(Writer out, String header, boolean localize) {
 196         PrintWriter prnt = (out instanceof PrintWriter ? (PrintWriter)out :
 197                             new PrintWriter(out, false));
 198 
 199         if (header != null) {
 200             prnt.write('#');
 201             prnt.println(header);
 202         }
 203         prnt.write('#');
 204         prnt.println(new Date());
 205 
 206         for (Enumeration e = keys() ; e.hasMoreElements() ;) {
 207             String key = (String)e.nextElement();
 208             prnt.print(key);
 209             prnt.write('=');
 210 
 211             String val = (String)get(key);
 212             int len = val.length();
 213             boolean empty = false;
 214 
 215             for (int i = 0 ; i < len ; i++) {
 216                 int ch = val.charAt(i);
 217 
 218                 switch (ch) {
 219                   case '\\': prnt.write('\\'); prnt.write('\\'); break;
 220                   case '\t': prnt.write('\\'); prnt.write('t'); break;
 221                   case '\n': prnt.write('\\'); prnt.write('n'); break;
 222                   case '\r': prnt.write('\\'); prnt.write('r'); break;
 223 
 224                   default:
 225                     if ((ch < ' ') || (ch >= 127) || (empty && (ch == ' '))) {
 226                         if ((ch > 255) && localize) {
 227                             prnt.write(ch);


 268      * @param   key            the hashtable key.
 269      * @param   defaultValue   a default value.
 270      *
 271      * @return  the value in this property list with the specified key value.
 272      * @see     com.sun.interview.Properties2#defaults
 273      */
 274     public String getProperty(String key, String defaultValue) {
 275         String val = getProperty(key);
 276         return (val == null) ? defaultValue : val;
 277     }
 278 
 279     /**
 280      * Returns an enumeration of all the keys in this property list, including
 281      * the keys in the default property list.
 282      *
 283      * @return  an enumeration of all the keys in this property list, including
 284      *          the keys in the default property list.
 285      * @see     java.util.Enumeration
 286      * @see     com.sun.interview.Properties2#defaults
 287      */
 288     public Enumeration propertyNames() {
 289         Hashtable<String, Object> h = new Hashtable<>();
 290         enumerate(h);
 291         return h.keys();
 292     }
 293 
 294 
 295     /**
 296      * Prints this property list out to the specified output stream.
 297      * This method is useful for debugging.
 298      *
 299      * @param   out   an output stream.
 300      */
 301     /*
 302      * Rather than use an anonymous inner class to share common code, this
 303      * method is duplicated in order to ensure that a non-1.1 compiler can
 304      * compile this file.
 305      */
 306     public void list(PrintWriter out) {
 307         out.println("-- listing properties --");
 308         Hashtable<String, Object> h = new Hashtable<>();
 309         enumerate(h);
 310         for (Enumeration e = h.keys() ; e.hasMoreElements() ;) {
 311             String key = (String)e.nextElement();
 312             String val = (String)h.get(key);
 313             if (val.length() > 40) {
 314                 val = val.substring(0, 37) + "...";
 315             }
 316             out.println(key + "=" + val);
 317         }
 318     }
 319 
 320     /**
 321      * Enumerates all key/value pairs in the specified hastable.
 322      * @param h the hashtable
 323      */
 324     private synchronized void enumerate(Hashtable<String, Object> h) {
 325         if (defaults != null) {
 326             defaults.enumerate(h);
 327         }
 328         for (Enumeration e = keys() ; e.hasMoreElements() ;) {
 329             String key = (String)e.nextElement();
 330             h.put(key, get(key));
 331         }
 332     }
 333 
 334     /**
 335      * Convert a nibble to a hex character
 336      * @param   nibble  the nibble to convert.
 337      */
 338     private static char toHex(int nibble) {
 339         return hexDigit[(nibble & 0xF)];
 340     }
 341 
 342     /** A table of hex digits */
 343     private static char[] hexDigit = {
 344         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
 345     };
 346 }


  54      * found in this property list.
  55      */
  56     protected Properties2 defaults;
  57 
  58     /**
  59      * Creates an empty property list with no default values.
  60      */
  61     public Properties2() {
  62         this(null);
  63     }
  64 
  65     /**
  66      * Creates an empty property list with the specified defaults.
  67      * @param   defaults   the defaults.
  68      */
  69     public Properties2(Properties2 defaults) {
  70         this.defaults = defaults;
  71     }
  72 
  73     public void load(java.util.Properties source) {
  74         Enumeration<?> e = source.propertyNames();
  75         while(e.hasMoreElements()) {
  76             Object next = e.nextElement();
  77             put( ((String)next), source.get(next) );
  78         }   // while
  79     }
  80 
  81 
  82     /**
  83      * Reads a property list from an input stream.
  84      *
  85      * @param      in   the input stream.
  86      * @exception  IOException  if an error occurred when reading from the
  87      *               input stream.
  88      */
  89     public synchronized void load(Reader in) throws IOException {
  90         int ch = in.read();
  91         while (true) {
  92             switch (ch) {
  93               case -1:
  94                 return;


 186      * string header is printed as a comment at the beginning of the stream.
 187      *
 188      * @param   out      an output stream.
 189      * @param   header   a description of the property list.
 190      */
 191     public synchronized void save(Writer out, String header) {
 192         save(out, header, false);
 193     }
 194 
 195     private void save(Writer out, String header, boolean localize) {
 196         PrintWriter prnt = (out instanceof PrintWriter ? (PrintWriter)out :
 197                             new PrintWriter(out, false));
 198 
 199         if (header != null) {
 200             prnt.write('#');
 201             prnt.println(header);
 202         }
 203         prnt.write('#');
 204         prnt.println(new Date());
 205 
 206         for (Enumeration<String> e = keys() ; e.hasMoreElements() ;) {
 207             String key = e.nextElement();
 208             prnt.print(key);
 209             prnt.write('=');
 210 
 211             String val = (String)get(key);
 212             int len = val.length();
 213             boolean empty = false;
 214 
 215             for (int i = 0 ; i < len ; i++) {
 216                 int ch = val.charAt(i);
 217 
 218                 switch (ch) {
 219                   case '\\': prnt.write('\\'); prnt.write('\\'); break;
 220                   case '\t': prnt.write('\\'); prnt.write('t'); break;
 221                   case '\n': prnt.write('\\'); prnt.write('n'); break;
 222                   case '\r': prnt.write('\\'); prnt.write('r'); break;
 223 
 224                   default:
 225                     if ((ch < ' ') || (ch >= 127) || (empty && (ch == ' '))) {
 226                         if ((ch > 255) && localize) {
 227                             prnt.write(ch);


 268      * @param   key            the hashtable key.
 269      * @param   defaultValue   a default value.
 270      *
 271      * @return  the value in this property list with the specified key value.
 272      * @see     com.sun.interview.Properties2#defaults
 273      */
 274     public String getProperty(String key, String defaultValue) {
 275         String val = getProperty(key);
 276         return (val == null) ? defaultValue : val;
 277     }
 278 
 279     /**
 280      * Returns an enumeration of all the keys in this property list, including
 281      * the keys in the default property list.
 282      *
 283      * @return  an enumeration of all the keys in this property list, including
 284      *          the keys in the default property list.
 285      * @see     java.util.Enumeration
 286      * @see     com.sun.interview.Properties2#defaults
 287      */
 288     public Enumeration<String> propertyNames() {
 289         Hashtable<String, Object> h = new Hashtable<>();
 290         enumerate(h);
 291         return h.keys();
 292     }
 293 
 294 
 295     /**
 296      * Prints this property list out to the specified output stream.
 297      * This method is useful for debugging.
 298      *
 299      * @param   out   an output stream.
 300      */
 301     /*
 302      * Rather than use an anonymous inner class to share common code, this
 303      * method is duplicated in order to ensure that a non-1.1 compiler can
 304      * compile this file.
 305      */
 306     public void list(PrintWriter out) {
 307         out.println("-- listing properties --");
 308         Hashtable<String, Object> h = new Hashtable<>();
 309         enumerate(h);
 310         for (Enumeration<String> e = h.keys() ; e.hasMoreElements() ;) {
 311             String key = e.nextElement();
 312             String val = (String)h.get(key);
 313             if (val.length() > 40) {
 314                 val = val.substring(0, 37) + "...";
 315             }
 316             out.println(key + "=" + val);
 317         }
 318     }
 319 
 320     /**
 321      * Enumerates all key/value pairs in the specified hastable.
 322      * @param h the hashtable
 323      */
 324     private synchronized void enumerate(Hashtable<String, Object> h) {
 325         if (defaults != null) {
 326             defaults.enumerate(h);
 327         }
 328         for (Enumeration<String> e = keys() ; e.hasMoreElements() ;) {
 329             String key = e.nextElement();
 330             h.put(key, get(key));
 331         }
 332     }
 333 
 334     /**
 335      * Convert a nibble to a hex character
 336      * @param   nibble  the nibble to convert.
 337      */
 338     private static char toHex(int nibble) {
 339         return hexDigit[(nibble & 0xF)];
 340     }
 341 
 342     /** A table of hex digits */
 343     private static char[] hexDigit = {
 344         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
 345     };
 346 }
< prev index next >