1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codehaus.mojo.license;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.List;
26 import java.util.Properties;
27 import java.util.Vector;
28
29
30
31
32
33
34
35
36 public class SortedProperties extends Properties {
37
38 private static final long serialVersionUID = -1147150444452577558L;
39
40
41
42
43 protected String encoding;
44
45
46
47
48 protected boolean removeHeader;
49
50 public SortedProperties(String encoding) {
51 this(encoding, true);
52 }
53
54 public SortedProperties(String encoding, boolean removeHeader) {
55 this.encoding = encoding;
56 this.removeHeader = removeHeader;
57 }
58
59 public SortedProperties(Properties defaults) {
60 super(defaults);
61 }
62
63 @Override
64 public Enumeration<Object> keys() {
65 List<Object> objects = Collections.list(super.keys());
66 Vector<Object> result;
67 try {
68
69 List<String> list = toGenericList(objects, String.class);
70 Collections.sort(list);
71 result = new Vector<Object>(list);
72 } catch (IllegalArgumentException e) {
73
74
75 result = new Vector<Object>(objects);
76 }
77 return result.elements();
78 }
79
80
81
82
83
84
85
86
87
88
89 public SortedProperties load(File src) throws IOException {
90 FileInputStream reader = new FileInputStream(src);
91 try {
92 load(reader);
93 } finally {
94 reader.close();
95 }
96 return this;
97 }
98
99
100
101
102
103
104
105
106
107 public void store(File dst) throws IOException {
108 OutputStream writer = new FileOutputStream(dst);
109 try {
110 store(writer, null);
111 } finally {
112 writer.close();
113 }
114 }
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135 @SuppressWarnings({ "unchecked" })
136 static public <O> List<O> toGenericList(List<?> list, Class<O> type) throws IllegalArgumentException {
137 if (list.isEmpty()) {
138 return (List<O>) list;
139 }
140 for (Object o : list) {
141 if (!type.isAssignableFrom(o.getClass())) {
142 throw new IllegalArgumentException("can not cast List with object of type " + o.getClass() + " to "
143 + type + " type!");
144 }
145 }
146 return (List<O>) list;
147 }
148
149 }