1 /**
2 * Copyright 2010-2012 The Kuali Foundation
3 *
4 * Licensed under the Educational Community License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.opensource.org/licenses/ecl2.php
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Permet d'avoir les fichiers de proprietes tries.
31 *
32 * @author ruchaud <ruchaud@codelutin.com>
33 * @author tchemit <chemit@codelutin.com>
34 * @since 1.0
35 */
36 public class SortedProperties extends Properties {
37
38 private static final long serialVersionUID = -1147150444452577558L;
39
40 /**
41 * l'encoding a utiliser pour lire et ecrire le properties.
42 */
43 protected String encoding;
44
45 /**
46 * un drapeau pour savoir s'il faut enlever l'entete generere
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 // Attention, si les clef ne sont pas des string, ca ne marchera pas
69 List<String> list = toGenericList(objects, String.class);
70 Collections.sort(list);
71 result = new Vector<Object>(list);
72 } catch (IllegalArgumentException e) {
73 // keys are not string !!!
74 // can not sort keys
75 result = new Vector<Object>(objects);
76 }
77 return result.elements();
78 }
79
80 /**
81 * Charge le properties a partir d'un fichier.
82 *
83 * @param src
84 * le fichier src a charger en utilisant l'encoding declare
85 * @return l'instance du properties
86 * @throws java.io.IOException
87 * if any io pb
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 * Sauvegarde le properties dans un fichier, sans commentaire et en utilisant l'encoding declare.
101 *
102 * @param dst
103 * the fichier de destination
104 * @throws java.io.IOException
105 * if any io pb
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 * Permet de convertir une liste non typee, en une liste typee.
118 * <p/>
119 * La liste en entree en juste bien castee.
120 * <p/>
121 * On effectue une verification sur le typage des elements de la liste.
122 * <p/>
123 * Note : <b>Aucune liste n'est creee, ni recopiee</b>
124 *
125 * @param <O>
126 * le type des objets de la liste
127 * @param list
128 * la liste a convertir
129 * @param type
130 * le type des elements de la liste
131 * @return la liste typee
132 * @throws IllegalArgumentException
133 * si un element de la liste en entree n'est pas en adequation avec le type voulue.
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 }