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 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 XmlWriterUtil.writeLineBreak( writer );
61
62 XmlWriterUtil.writeCommentLineBreak( writer );
63 XmlWriterUtil.writeComment( writer, StringUtils.repeat( "=", REPEAT_EQUALS ) + " - DO NOT EDIT THIS FILE! - "
64 + StringUtils.repeat( "=", REPEAT_EQUALS ) );
65 XmlWriterUtil.writeCommentLineBreak( writer );
66 XmlWriterUtil.writeComment( writer, " " );
67 XmlWriterUtil.writeComment( writer, "Any modifications will be overwritten." );
68 XmlWriterUtil.writeComment( writer, " " );
69 DateFormat dateFormat = DateFormat.getDateTimeInstance( DateFormat.SHORT, DateFormat.SHORT, Locale.US );
70 XmlWriterUtil.writeComment( writer, "Generated by Maven Doap Plugin on "
71 + dateFormat.format( new Date( System.currentTimeMillis() ) ) );
72 XmlWriterUtil.writeComment( writer, "See: http://maven.apache.org/plugins/maven-doap-plugin/" );
73 XmlWriterUtil.writeComment( writer, " " );
74 XmlWriterUtil.writeCommentLineBreak( writer );
75
76 XmlWriterUtil.writeLineBreak( writer );
77 }
78
79
80
81
82
83
84
85 public static void writeElement( XMLWriter writer, String name, String value )
86 throws IllegalArgumentException
87 {
88 if ( StringUtils.isEmpty( name ) )
89 {
90 throw new IllegalArgumentException( "name should be defined" );
91 }
92
93 if ( value != null )
94 {
95 writer.startElement( name );
96 writer.writeText( value );
97 writer.endElement();
98 }
99 }
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 if ( StringUtils.isEmpty( lang ) )
112 {
113 writeElement( writer, name, value );
114 return;
115 }
116
117 if ( StringUtils.isEmpty( name ) )
118 {
119 throw new IllegalArgumentException( "name should be defined" );
120 }
121
122 if ( value != null )
123 {
124 writer.startElement( name );
125 writer.addAttribute( "xml:lang", lang );
126 writer.writeText( value );
127 writer.endElement();
128 }
129 }
130
131
132
133
134
135
136
137 public static void writeRdfResourceElement( XMLWriter writer, String name, String value )
138 throws IllegalArgumentException
139 {
140 if ( StringUtils.isEmpty( name ) )
141 {
142 throw new IllegalArgumentException( "name should be defined" );
143 }
144
145 if ( value != null )
146 {
147 writer.startElement( name );
148 writer.addAttribute( RDF_RESOURCE, value );
149 writer.endElement();
150 }
151 }
152
153
154
155
156
157
158 public static List getDevelopersOrContributorsWithDeveloperRole( I18N i18n, List developersOrContributors )
159 {
160 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 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 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 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 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 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 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 Map returnMap = new HashMap( 7 );
244 returnMap.put( "maintainers", new ArrayList() );
245 returnMap.put( "developers", new ArrayList() );
246 returnMap.put( "documenters", new ArrayList() );
247 returnMap.put( "translators", new ArrayList() );
248 returnMap.put( "testers", new ArrayList() );
249 returnMap.put( "helpers", new ArrayList() );
250 returnMap.put( "unknowns", new ArrayList() );
251
252 if ( developersOrContributors == null || developersOrContributors.isEmpty() )
253 {
254 return returnMap;
255 }
256
257 for ( Iterator it = developersOrContributors.iterator(); it.hasNext(); )
258 {
259 Object obj = it.next();
260
261 List roles;
262 if ( Developer.class.isAssignableFrom( obj.getClass() ) )
263 {
264 Developer developer = (Developer) obj;
265 roles = developer.getRoles();
266 }
267 else
268 {
269 Contributor contributor = (Contributor) obj;
270 roles = contributor.getRoles();
271 }
272
273 if ( roles != null && roles.size() != 0 )
274 {
275 for ( Iterator it2 = roles.iterator(); it2.hasNext(); )
276 {
277 String role = (String) it2.next();
278
279 role = role.toLowerCase( Locale.ENGLISH );
280 if ( role.indexOf( getLowerCaseString( i18n, "doap.maintainer" ) ) != -1 )
281 {
282 ( (List) returnMap.get( "maintainers" ) ).add( obj );
283 }
284 else if ( role.indexOf( getLowerCaseString( i18n, "doap.developer" ) ) != -1 )
285 {
286 ( (List) returnMap.get( "developers" ) ).add( obj );
287 }
288 else if ( role.indexOf( getLowerCaseString( i18n, "doap.documenter" ) ) != -1 )
289 {
290 ( (List) returnMap.get( "documenters" ) ).add( obj );
291 }
292 else if ( role.indexOf( getLowerCaseString( i18n, "doap.translator" ) ) != -1 )
293 {
294 ( (List) returnMap.get( "translators" ) ).add( obj );
295 }
296 else if ( role.indexOf( getLowerCaseString( i18n, "doap.tester" ) ) != -1 )
297 {
298 ( (List) returnMap.get( "testers" ) ).add( obj );
299 }
300 else if ( role.indexOf( getLowerCaseString( i18n, "doap.helper" ) ) != -1 )
301 {
302 ( (List) returnMap.get( "helpers" ) ).add( obj );
303 }
304 else
305 {
306 ( (List) returnMap.get( "unknowns" ) ).add( obj );
307 }
308 }
309 }
310 else
311 {
312 ( (List) returnMap.get( "unknowns" ) ).add( obj );
313 }
314 }
315
316 return returnMap;
317 }
318
319
320
321
322
323
324 private static String getLowerCaseString( I18N i18n, String key )
325 {
326 return i18n.getString( "doap-person", Locale.ENGLISH, key ).toLowerCase( Locale.ENGLISH );
327 }
328 }