1 /*
2 * #%L
3 * Maven helper plugin
4 *
5 * $Id: SortedProperties.java 13522 2011-02-05 09:43:24Z tchemit $
6 * $HeadURL: http://svn.codehaus.org/mojo/tags/license-maven-plugin-1.0/src/main/java/org/codehaus/mojo/license/SortedProperties.java $
7 * %%
8 * Copyright (C) 2009 - 2010 Tony Chemit, CodeLutin
9 * %%
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Lesser Public License for more details.
19 *
20 * You should have received a copy of the GNU General Lesser Public
21 * License along with this program. If not, see
22 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
23 * #L%
24 */
25
26 package org.codehaus.mojo.license;
27
28
29 import java.io.*;
30 import java.util.*;
31
32 /**
33 * Permet d'avoir les fichiers de proprietes tries.
34 *
35 * @author ruchaud <ruchaud@codelutin.com>
36 * @author tchemit <chemit@codelutin.com>
37 * @since 1.0
38 */
39 public class SortedProperties
40 extends Properties
41 {
42
43 private static final long serialVersionUID = -1147150444452577558L;
44
45 /**
46 * l'encoding a utiliser pour lire et ecrire le properties.
47 */
48 protected String encoding;
49
50 /**
51 * un drapeau pour savoir s'il faut enlever l'entete generere
52 */
53 protected boolean removeHeader;
54
55
56 public SortedProperties( String encoding )
57 {
58 this( encoding, true );
59 }
60
61
62 public SortedProperties( String encoding, boolean removeHeader )
63 {
64 this.encoding = encoding;
65 this.removeHeader = removeHeader;
66 }
67
68 public SortedProperties( Properties defaults )
69 {
70 super( defaults );
71 }
72
73 @Override
74 public Enumeration<Object> keys()
75 {
76 List<Object> objects = Collections.list( super.keys() );
77 Vector<Object> result;
78 try
79 {
80 // Attention, si les clef ne sont pas des string, ca ne marchera pas
81 List<String> list = toGenericList( objects, String.class );
82 Collections.sort( list );
83 result = new Vector<Object>( list );
84 }
85 catch ( IllegalArgumentException e )
86 {
87 // keys are not string !!!
88 // can not sort keys
89 result = new Vector<Object>( objects );
90 }
91 return result.elements();
92 }
93
94 /**
95 * Charge le properties a partir d'un fichier.
96 *
97 * @param src le fichier src a charger en utilisant l'encoding declare
98 * @return l'instance du properties
99 * @throws java.io.IOException if any io pb
100 */
101 public SortedProperties load( File src )
102 throws IOException
103 {
104 FileInputStream reader = new FileInputStream( src );
105 try
106 {
107 load( reader );
108 }
109 finally
110 {
111 reader.close();
112 }
113 return this;
114 }
115
116 /**
117 * Sauvegarde le properties dans un fichier, sans commentaire et en utilisant l'encoding declare.
118 *
119 * @param dst the fichier de destination
120 * @throws java.io.IOException if any io pb
121 */
122 public void store( File dst )
123 throws IOException
124 {
125 OutputStream writer = new FileOutputStream( dst );
126 try
127 {
128 store( writer, null );
129 }
130 finally
131 {
132 writer.close();
133 }
134 }
135
136 /**
137 * Permet de convertir une liste non typee, en une liste typee.
138 * <p/>
139 * La liste en entree en juste bien castee.
140 * <p/>
141 * On effectue une verification sur le typage des elements de la liste.
142 * <p/>
143 * Note : <b>Aucune liste n'est creee, ni recopiee</b>
144 *
145 * @param <O> le type des objets de la liste
146 * @param list la liste a convertir
147 * @param type le type des elements de la liste
148 * @return la liste typee
149 * @throws IllegalArgumentException si un element de la liste en entree
150 * n'est pas en adequation avec le type
151 * voulue.
152 */
153 @SuppressWarnings( { "unchecked" } )
154 static public <O> List<O> toGenericList( List<?> list, Class<O> type )
155 throws IllegalArgumentException
156 {
157 if ( list.isEmpty() )
158 {
159 return (List<O>) list;
160 }
161 for ( Object o : list )
162 {
163 if ( !type.isAssignableFrom( o.getClass() ) )
164 {
165 throw new IllegalArgumentException(
166 "can not cast List with object of type " + o.getClass() + " to " + type + " type!" );
167 }
168 }
169 return (List<O>) list;
170 }
171
172 }