1 | /* |
2 | * $Id: FormattedProperties.java,v 1.1.1.5 2004/05/25 20:23:30 hastings Exp $ |
3 | * |
4 | * (c) Copyright, Moebius Solutions, Inc., 2004 |
5 | * |
6 | * All Rights Reserved |
7 | * |
8 | * This material may be reproduced by or for the U. S. Government |
9 | * pursuant to the copyright license under the clause at |
10 | * DFARS 252.227-7014 (OCT 2001). |
11 | */ |
12 | |
13 | package com.moesol.util; |
14 | |
15 | import java.io.IOException; |
16 | import java.io.OutputStream; |
17 | import java.io.PrintStream; |
18 | import java.util.Arrays; |
19 | import java.util.Properties; |
20 | import java.util.Set; |
21 | import java.util.logging.Level; |
22 | import java.util.logging.Logger; |
23 | |
24 | /** |
25 | * @author Adam |
26 | * |
27 | * To change the template for this generated type comment go to |
28 | * Window>Preferences>Java>Code Generation>Code and Comments |
29 | */ |
30 | public class FormattedProperties extends Properties { |
31 | |
32 | public FormattedProperties() { |
33 | super(); |
34 | defaultDelim(); |
35 | } |
36 | |
37 | public FormattedProperties(Properties arg0) { |
38 | super(arg0); |
39 | defaultDelim(); |
40 | } |
41 | |
42 | private void defaultDelim() { |
43 | m_delimeter = DEFAULT_DELIMETER; |
44 | } |
45 | |
46 | public void setFormatDelimeter(char delimeter){ |
47 | if (delimeter == EQUAL_SIGN || |
48 | delimeter == DEFAULT_DELIMETER || |
49 | delimeter == COLON){ |
50 | m_delimeter = delimeter; |
51 | } else { |
52 | m_delimeter = DEFAULT_DELIMETER; |
53 | } |
54 | } |
55 | |
56 | public char getFormatDelimeter(){ |
57 | return m_delimeter; |
58 | } |
59 | |
60 | public void store(OutputStream out, String header) throws IOException{ |
61 | if (m_delimeter == EQUAL_SIGN) { |
62 | super.store(out, header); |
63 | } else { |
64 | writeOut(out, header); |
65 | } |
66 | } |
67 | |
68 | /** |
69 | * @deprecated this mutes IOException use store instead. |
70 | */ |
71 | public void save(OutputStream out, String header){ |
72 | if (m_delimeter == EQUAL_SIGN) { |
73 | super.save(out, header); |
74 | } else { |
75 | try{ |
76 | writeOut(out, header); |
77 | } catch (IOException e) { |
78 | logger.log(Level.FINE, "save", "Threw an IOException that is muted" + |
79 | " by the construct of save. Caller should use store instead."); |
80 | } |
81 | } |
82 | } |
83 | |
84 | private void writeOut(OutputStream out, String header) |
85 | throws IOException { |
86 | PrintStream ps = new PrintStream(out); |
87 | Set keys = keySet(); |
88 | Object[] keys_a = keys.toArray(); |
89 | Arrays.sort(keys_a); |
90 | for (int i = 0; i < keys_a.length; i++) { |
91 | String key = (String)keys_a[i]; |
92 | myStore(key, getProperty(key), ps); |
93 | } |
94 | } |
95 | |
96 | private void myStore(String key, String value, PrintStream ps) throws IOException { |
97 | ps.print(key); |
98 | ps.print(": "); |
99 | ps.print(value); |
100 | ps.println(); |
101 | } |
102 | |
103 | private char m_delimeter; |
104 | |
105 | public static final char EQUAL_SIGN = '='; |
106 | public static final char DEFAULT_DELIMETER = EQUAL_SIGN; |
107 | public static final char COLON = ':'; |
108 | static Logger logger = Logger.getLogger(FormattedProperties.class.getName()); |
109 | } |