aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/util/Properties.java
blob: 2f7a25159b7543d914af9ee928c03d8900388c85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Properties - Property list representation.

/* Copyright (C) 1998, 1999, 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.util;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.PushbackReader;

/**
 * @author Tom Tromey <tromey@cygnus.com>
 * @date October 26, 1998.
 */

/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * Status: Complete to JDK 1.2.
 */

public class Properties extends Hashtable
{
  protected Properties defaults;

  public String getProperty (String propName)
    {
      return getProperty (propName, null);
    }

  public String getProperty (String propName, String defVal)
    {
      String r = (String) get (propName);
      if (r == null)
	{
	  if (defaults != null)
	    r = defaults.getProperty(propName, defVal);
	  else
	    r = defVal;
	}
      return r;
    }

  public Object setProperty (String key, String value)
  {
    return put (key, value);
  }

  public void list (PrintStream out)
    {
      Enumeration e = propertyNames ();
      while (e.hasMoreElements())
	{
	  String key = (String) e.nextElement();
	  String value = getProperty(key);
	  if (value != null)
	    {
	      if (value.length() > 40)
		{
		  // JDK compatibility.
		  value = value.substring(0, 37) + "...";
		}
	      out.print(key);
	      out.print("=");
	      out.println(value);
	    }
	}
    }

  public void list (PrintWriter writer)
    {
      Enumeration e = propertyNames ();
      while (e.hasMoreElements())
	{
	  String key = (String) e.nextElement();
	  String value = getProperty(key);
	  if (value != null)
	    {
	      if (value.length() > 40)
		{
		  // JDK compatibility.
		  value = value.substring(0, 37) + "...";
		}
	      writer.print(key);
	      writer.print("=");
	      writer.println(value);
	    }
	}
    }

  private final boolean skip_ws (PushbackReader reader) throws IOException
    {
      while (true)
	{
	  int c = reader.read();
	  if (c == -1)
	    return false;
	  // FIXME: we use our own definition of whitespace.
	  // Character.isWhitespace includes newlines, which we don't
	  // want.  Character.isSpaceChar doesn't include \t.
	  if (c != ' ' && c != '\t')
	    {
	      reader.unread(c);
	      return true;
	    }
	}
    }

  // Note: this method needs to be rewritten for JDK 1.2.
  // We rather arbitrarily decide that an EOF in the middle of a line
  // means that the whole line should be ignored.  The spec doesn't
  // specifically address this, but this interpretation seems valid.
  public synchronized void load (InputStream in) throws IOException
    {
      PushbackReader reader = new PushbackReader (new InputStreamReader (in));

      StringBuffer key = new StringBuffer ();
      StringBuffer value = new StringBuffer ();

    nextLine:
      while (true)
	{
	  key.setLength(0);
	  value.setLength(0);

	  // Skip leading whitespace.
	  if (! skip_ws (reader))
	    return;

	  // Read key until key terminator.
	  boolean first_char = true;
	  int c;
	  while (true)
	    {
	      c = reader.read();
	      if (c == -1)
		return;
	      if (c == '\\')
		{
		  first_char = false;
		  c = reader.read();
		  if (c == -1)
		    return;
		}

	      // If we found a comment, just read to end of line and
	      // then keep going.
	      if (first_char == true && (c == '#' || c == '!'))
		{
		  while (c != -1 && c != '\r' && c != '\n')
		    c = reader.read();
		  if (c == -1)
		    return;
		  continue nextLine;
		}

	      if (c == '\r' || c == '\n')
		{
		  if (first_char)
		    continue nextLine;
		  reader.unread(c);
		  break;
		}
	      // FIXME: again, our own definition of whitespace.
	      if (c == ' ' || c == '\t' || c == ':' || c == '=')
		break;

	      first_char = false;
	      key.append((char) c);
	    }

	  // Found end of key.  Skip whitespace.  If the terminator
	  // was whitespace, also skip a single instance of a "real"
	  // terminator, and then more whitespace.
	  if (! skip_ws (reader))
	    return;
	  if (c != ':' && c != '=')
	    {
	      c = reader.read();
	      if (c == -1)
		return;
	      if (c == ':' || c == '=')
		{
		  // Skip more whitespace.
		  if (! skip_ws (reader))
		    return;
		}
	      else
		reader.unread(c);
	    }

	  // Now read the value.
	  while (true)
	    {
	      c = reader.read();
	      if (c == -1)
		return;
	      if (c == '\r' || c == '\n')
		break;
	      if (c == '\\')
		{
		  c = reader.read();
		  switch (c)
		    {
		    case -1:
		      return;
		    case 't':
		      c = '\t';
		      break;
		    case 'r':
		      c = '\r';
		      break;
		    case 'n':
		      c = '\n';
		      break;
		    case 'u':
		      c = 0;
		      for (int i = 0; i < 4; ++i)
			{
			  int x = reader.read();
			  if (x == -1)
			    return;
			  int d = Character.digit((char) x, 16);
			  // We follow JDK here: invalid characters
			  // are treated as terminators.
			  if (d == -1)
			    {
			      value.append((char) c);
			      c = x;
			      break;
			    }
			  c <<= 4;
			  c |= d;
			}
		      break;
		    default:
		      // Nothing.
		    }
		}
	      value.append((char) c);
	    }

	  put (key.toString(), value.toString());
	}
    }

  public Properties ()
    {
      defaults = null;
    }

  public Properties (Properties defs)
    {
      defaults = defs;
    }

  private final void addHashEntries (Hashtable base)
    {
      if (defaults != null)
	defaults.addHashEntries(base);
      Enumeration keys = keys ();
      while (keys.hasMoreElements())
	base.put(keys.nextElement(), base);
    }

  public Enumeration propertyNames ()
    {
      // We make a new Hashtable that holds all the keys.  Then we
      // return an enumeration for this hash.  We do this because we
      // don't want modifications to be reflected in the enumeration
      // (per JCL), and because there doesn't seem to be a
      // particularly better way to ensure that duplicates are
      // ignored.
      Hashtable t = new Hashtable ();
      addHashEntries (t);
      return t.keys();
    }

  public synchronized void save (OutputStream out, String comment)
  {
    try
      {
	store (out, comment);
      }
    catch (IOException _)
      {
      }
  }

  public synchronized void store (OutputStream out, String comment)
    throws IOException
  {
      // Use a buffer because writing a single string through
      // OutputStreamWriter is fairly expensive.
      BufferedWriter output
	= new BufferedWriter (new OutputStreamWriter (out));
      String newline = System.getProperty("line.separator");

      if (comment != null)
	{
	  // We just lose if COMMENT contains a newline.  This is
	  // what JDK 1.1 does.
	  output.write("#");
	  output.write(comment);
	  output.write(newline);
	}
      output.write("# ");
      output.write(new Date().toString());
      output.write(newline);

      Enumeration keys = keys ();
      while (keys.hasMoreElements())
	{
	  String key = (String) keys.nextElement();
	  String value = (String) get (key);

	  // FIXME: JCL says that the key can contain many Unicode
	  // characters.  But it also doesn't say we should encode
	  // it in any way.
	  // FIXME: if key contains ':', '=', or whitespace, must
	  // quote it here.  Note that JDK 1.1 does not do this.
	  output.write(key);
	  output.write("=");

	  boolean leading = true;
	  for (int i = 0; i < value.length(); ++i)
	    {
	      boolean new_lead = false;
	      char c = value.charAt(i);
	      switch (c)
		{
		case '\n':
		  output.write("\\n");
		  break;
		case '\r':
		  output.write("\\r");
		  break;
		case '\t':
		  output.write("\\t");
		  break;
		case '\\':
		  output.write("\\\\");
		  break;

		case '#':
		case '!':
		case '=':
		case ':':
		  output.write("\\");
		  output.write(c);
		  break;

		case ' ':
		  new_lead = leading;
		  if (leading)
		    output.write("\\");
		  output.write(c);
		  break;

		default:
		  if (c < '\u0020' || c > '\u007e')
		    {
		      output.write("\\u");
		      output.write(Character.forDigit(c >>> 12, 16));
		      output.write(Character.forDigit((c >>> 8) & 0xff,
						      16));
		      output.write(Character.forDigit((c >>> 4) & 0xff,
						      16));
		      output.write(Character.forDigit(c & 0xff, 16));
		    }
		  else
		    output.write(c);
		}
	      leading = new_lead;
	    }
	  output.write(newline);
	}

      output.flush();
  }
}