1 | |
package org.apache.maven.plugin.doap; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.text.DateFormat; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Date; |
25 | |
import java.util.HashMap; |
26 | |
import java.util.Iterator; |
27 | |
import java.util.List; |
28 | |
import java.util.Locale; |
29 | |
import java.util.Map; |
30 | |
|
31 | |
import org.apache.maven.model.Contributor; |
32 | |
import org.apache.maven.model.Developer; |
33 | |
import org.codehaus.plexus.i18n.I18N; |
34 | |
import org.codehaus.plexus.util.StringUtils; |
35 | |
import org.codehaus.plexus.util.xml.XMLWriter; |
36 | |
import org.codehaus.plexus.util.xml.XmlWriterUtil; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | 0 | public class DoapUtil |
46 | |
{ |
47 | |
|
48 | |
private static final int REPEAT_EQUALS = 21; |
49 | |
|
50 | |
|
51 | |
protected static final String RDF_RESOURCE = "rdf:resource"; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public static void writeHeader( XMLWriter writer ) |
59 | |
{ |
60 | 2 | XmlWriterUtil.writeLineBreak( writer ); |
61 | |
|
62 | 2 | XmlWriterUtil.writeCommentLineBreak( writer ); |
63 | 2 | XmlWriterUtil.writeComment( writer, StringUtils.repeat( "=", REPEAT_EQUALS ) + " - DO NOT EDIT THIS FILE! - " |
64 | |
+ StringUtils.repeat( "=", REPEAT_EQUALS ) ); |
65 | 2 | XmlWriterUtil.writeCommentLineBreak( writer ); |
66 | 2 | XmlWriterUtil.writeComment( writer, " " ); |
67 | 2 | XmlWriterUtil.writeComment( writer, "Any modifications will be overwritten." ); |
68 | 2 | XmlWriterUtil.writeComment( writer, " " ); |
69 | 2 | DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.US ); |
70 | 2 | XmlWriterUtil.writeComment( writer, "Generated by Maven Doap Plugin on " |
71 | |
+ dateFormat.format( new Date( System.currentTimeMillis() ) ) ); |
72 | 2 | XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-doap-plugin/" ); |
73 | 2 | XmlWriterUtil.writeComment( writer, " " ); |
74 | 2 | XmlWriterUtil.writeCommentLineBreak( writer ); |
75 | |
|
76 | 2 | XmlWriterUtil.writeLineBreak( writer ); |
77 | 2 | } |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
public static void writeElement( XMLWriter writer, String name, String value ) |
86 | |
throws IllegalArgumentException |
87 | |
{ |
88 | 2 | if ( StringUtils.isEmpty( name ) ) |
89 | |
{ |
90 | 1 | throw new IllegalArgumentException( "name should be defined" ); |
91 | |
} |
92 | |
|
93 | 1 | if ( value != null ) |
94 | |
{ |
95 | 1 | writer.startElement( name ); |
96 | 1 | writer.writeText( value ); |
97 | 1 | writer.endElement(); |
98 | |
} |
99 | 1 | } |
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public static void writeElement( XMLWriter writer, String name, String value, String lang ) |
109 | |
throws IllegalArgumentException |
110 | |
{ |
111 | 4 | if ( StringUtils.isEmpty( lang ) ) |
112 | |
{ |
113 | 0 | writeElement( writer, name, value ); |
114 | 0 | return; |
115 | |
} |
116 | |
|
117 | 4 | if ( StringUtils.isEmpty( name ) ) |
118 | |
{ |
119 | 0 | throw new IllegalArgumentException( "name should be defined" ); |
120 | |
} |
121 | |
|
122 | 4 | if ( value != null ) |
123 | |
{ |
124 | 4 | writer.startElement( name ); |
125 | 4 | writer.addAttribute( "xml:lang", lang ); |
126 | 4 | writer.writeText( value ); |
127 | 4 | writer.endElement(); |
128 | |
} |
129 | 4 | } |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public static void writeRdfResourceElement( XMLWriter writer, String name, String value ) |
138 | |
throws IllegalArgumentException |
139 | |
{ |
140 | 12 | if ( StringUtils.isEmpty( name ) ) |
141 | |
{ |
142 | 1 | throw new IllegalArgumentException( "name should be defined" ); |
143 | |
} |
144 | |
|
145 | 11 | if ( value != null ) |
146 | |
{ |
147 | 11 | writer.startElement( name ); |
148 | 11 | writer.addAttribute( RDF_RESOURCE, value ); |
149 | 11 | writer.endElement(); |
150 | |
} |
151 | 11 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public static List getDevelopersOrContributorsWithDeveloperRole( I18N i18n, List developersOrContributors ) |
159 | |
{ |
160 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "developers" ); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
public static List getDevelopersOrContributorsWithDocumenterRole( I18N i18n, List developersOrContributors ) |
169 | |
{ |
170 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "documenters" ); |
171 | |
} |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
public static List getDevelopersOrContributorsWithHelperRole( I18N i18n, List developersOrContributors ) |
179 | |
{ |
180 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "helpers" ); |
181 | |
} |
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public static List getDevelopersOrContributorsWithMaintainerRole( I18N i18n, List developersOrContributors ) |
189 | |
{ |
190 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "maintainers" ); |
191 | |
} |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public static List getDevelopersOrContributorsWithTesterRole( I18N i18n, List developersOrContributors ) |
199 | |
{ |
200 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "testers" ); |
201 | |
} |
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public static List getDevelopersOrContributorsWithTranslatorRole( I18N i18n, List developersOrContributors ) |
209 | |
{ |
210 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "translators" ); |
211 | |
} |
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
public static List getDevelopersOrContributorsWithUnknownRole( I18N i18n, List developersOrContributors ) |
219 | |
{ |
220 | 2 | return (List) filterDevelopersOrContributorsByDoapRoles( i18n, developersOrContributors ).get( "unknowns" ); |
221 | |
} |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
private static Map filterDevelopersOrContributorsByDoapRoles( I18N i18n, List developersOrContributors ) |
242 | |
{ |
243 | 14 | Map returnMap = new HashMap( 7 ); |
244 | 14 | returnMap.put( "maintainers", new ArrayList() ); |
245 | 14 | returnMap.put( "developers", new ArrayList() ); |
246 | 14 | returnMap.put( "documenters", new ArrayList() ); |
247 | 14 | returnMap.put( "translators", new ArrayList() ); |
248 | 14 | returnMap.put( "testers", new ArrayList() ); |
249 | 14 | returnMap.put( "helpers", new ArrayList() ); |
250 | 14 | returnMap.put( "unknowns", new ArrayList() ); |
251 | |
|
252 | 14 | if ( developersOrContributors == null || developersOrContributors.isEmpty() ) |
253 | |
{ |
254 | 0 | return returnMap; |
255 | |
} |
256 | |
|
257 | 14 | for ( Iterator it = developersOrContributors.iterator(); it.hasNext(); ) |
258 | |
{ |
259 | 14 | Object obj = it.next(); |
260 | |
|
261 | |
List roles; |
262 | 15 | if ( Developer.class.isAssignableFrom( obj.getClass() ) ) |
263 | |
{ |
264 | 14 | Developer developer = (Developer) obj; |
265 | 14 | roles = developer.getRoles(); |
266 | 14 | } |
267 | |
else |
268 | |
{ |
269 | 0 | Contributor contributor = (Contributor) obj; |
270 | 0 | roles = contributor.getRoles(); |
271 | |
} |
272 | |
|
273 | 14 | if ( roles != null && roles.size() != 0 ) |
274 | |
{ |
275 | 14 | for ( Iterator it2 = roles.iterator(); it2.hasNext(); ) |
276 | |
{ |
277 | 35 | String role = (String) it2.next(); |
278 | |
|
279 | 35 | role = role.toLowerCase( Locale.ENGLISH ); |
280 | 35 | if ( role.indexOf( getLowerCaseString( i18n, "doap.maintainer" ) ) != -1 ) |
281 | |
{ |
282 | 14 | ( (List) returnMap.get( "maintainers" ) ).add( obj ); |
283 | |
} |
284 | 21 | else if ( role.indexOf( getLowerCaseString( i18n, "doap.developer" ) ) != -1 ) |
285 | |
{ |
286 | 0 | ( (List) returnMap.get( "developers" ) ).add( obj ); |
287 | |
} |
288 | 21 | else if ( role.indexOf( getLowerCaseString( i18n, "doap.documenter" ) ) != -1 ) |
289 | |
{ |
290 | 0 | ( (List) returnMap.get( "documenters" ) ).add( obj ); |
291 | |
} |
292 | 21 | else if ( role.indexOf( getLowerCaseString( i18n, "doap.translator" ) ) != -1 ) |
293 | |
{ |
294 | 0 | ( (List) returnMap.get( "translators" ) ).add( obj ); |
295 | |
} |
296 | 21 | else if ( role.indexOf( getLowerCaseString( i18n, "doap.tester" ) ) != -1 ) |
297 | |
{ |
298 | 7 | ( (List) returnMap.get( "testers" ) ).add( obj ); |
299 | |
} |
300 | 14 | else if ( role.indexOf( getLowerCaseString( i18n, "doap.helper" ) ) != -1 ) |
301 | |
{ |
302 | 0 | ( (List) returnMap.get( "helpers" ) ).add( obj ); |
303 | |
} |
304 | |
else |
305 | |
{ |
306 | 14 | ( (List) returnMap.get( "unknowns" ) ).add( obj ); |
307 | |
} |
308 | 35 | } |
309 | |
} |
310 | |
else |
311 | |
{ |
312 | 0 | ( (List) returnMap.get( "unknowns" ) ).add( obj ); |
313 | |
} |
314 | 14 | } |
315 | |
|
316 | 14 | return returnMap; |
317 | |
} |
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
|
324 | |
private static String getLowerCaseString( I18N i18n, String key ) |
325 | |
{ |
326 | 133 | return i18n.getString( "doap-person", Locale.ENGLISH, key ).toLowerCase( Locale.ENGLISH ); |
327 | |
} |
328 | |
} |