1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.jdtaus.mojo.resource.model;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.Map;
28 import java.util.Properties;
29 import javax.xml.bind.JAXBContext;
30 import javax.xml.bind.JAXBException;
31 import javax.xml.bind.Unmarshaller;
32 import org.xml.sax.SAXException;
33
34
35
36
37
38
39
40
41
42 public class ModelManager
43 {
44
45
46 public ModelManager()
47 {
48 super();
49 }
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public Module getModule( final File moduleDescriptor )
64 throws JAXBException, SAXException, IOException
65 {
66 Module module = null;
67 final JAXBContext ctx = JAXBContext.newInstance(
68 "org.jdtaus.mojo.resource.model" );
69
70 final Unmarshaller unmarshaller = ctx.createUnmarshaller();
71 final Object contentTree = unmarshaller.unmarshal( moduleDescriptor );
72 if ( contentTree instanceof Module )
73 {
74 module = (Module) contentTree;
75 }
76
77 return module;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91
92 public Message getMessage( final Module module,
93 final String messageName )
94 {
95 if ( module == null )
96 {
97 throw new NullPointerException( "module" );
98 }
99 if ( messageName == null )
100 {
101 throw new NullPointerException( "messageName" );
102 }
103
104 Message message = null;
105
106 if ( module.getMessages() != null )
107 {
108 for ( final Iterator it = module.getMessages().getMessage().
109 iterator(); it.hasNext(); )
110 {
111 final Message current = (Message) it.next();
112 if ( current.getName().equals( messageName ) )
113 {
114 message = current;
115 break;
116 }
117 }
118 }
119
120 return message;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public int getHashCode( final Module module,
136 final Implementation implementation )
137 {
138 if ( module == null )
139 {
140 throw new NullPointerException( "module" );
141 }
142 if ( implementation == null )
143 {
144 throw new NullPointerException( "implementation" );
145 }
146
147 int bundleHash = 23;
148
149 if ( implementation.getMessages() != null )
150 {
151 for ( final Iterator it = implementation.getMessages().getMessage().
152 iterator(); it.hasNext(); )
153 {
154 final Message message = (Message) it.next();
155 bundleHash = 37 * bundleHash + message.getName().hashCode();
156
157 for ( final Iterator t = message.getTemplate().getText().
158 iterator(); t.hasNext(); )
159 {
160 final Text text = (Text) t.next();
161 bundleHash = 37 * bundleHash + text.getLanguage().hashCode();
162 bundleHash = 37 * bundleHash + text.getValue().hashCode();
163 }
164
165 if ( message.getArguments() != null )
166 {
167 for ( final Iterator a = message.getArguments().
168 getArgument().iterator(); a.hasNext(); )
169 {
170 final Argument argument = (Argument) a.next();
171 bundleHash = 37 * bundleHash + argument.getName().
172 hashCode();
173
174 bundleHash = 37 * bundleHash + argument.getType().
175 toString().hashCode();
176
177 }
178 }
179 }
180
181 for ( final Iterator it = implementation.getMessages().
182 getReference().iterator(); it.hasNext(); )
183 {
184 final MessageReference messageReference =
185 (MessageReference) it.next();
186
187 final Message message =
188 this.getMessage( module, messageReference.getName() );
189
190 for ( final Iterator t = message.getTemplate().getText().
191 iterator(); t.hasNext(); )
192 {
193 final Text text = (Text) t.next();
194 bundleHash = 37 * bundleHash + text.getLanguage().hashCode();
195 bundleHash = 37 * bundleHash + text.getValue().hashCode();
196 }
197
198 if ( message.getArguments() != null )
199 {
200 for ( final Iterator a = message.getArguments().
201 getArgument().iterator(); a.hasNext(); )
202 {
203 final Argument argument = (Argument) a.next();
204 bundleHash = 37 * bundleHash + argument.getName().
205 hashCode();
206
207 bundleHash = 37 * bundleHash + argument.getType().
208 toString().hashCode();
209
210 }
211 }
212 }
213 }
214
215 return bundleHash;
216 }
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231 public Map
232 final Module module, final Implementation implementation )
233 {
234 if ( module == null )
235 {
236 throw new NullPointerException( "module" );
237 }
238 if ( implementation == null )
239 {
240 throw new NullPointerException( "implementation" );
241 }
242
243 final Map properties = new HashMap( 10 );
244
245 if ( implementation.getMessages() != null )
246 {
247 for ( final Iterator it = implementation.getMessages().getMessage().
248 iterator(); it.hasNext(); )
249 {
250 final Message message = (Message) it.next();
251 for ( final Iterator t = message.getTemplate().getText().
252 iterator(); t.hasNext(); )
253 {
254 final Text text = (Text) t.next();
255 final String language = text.getLanguage().toLowerCase();
256
257 Properties bundleProperties =
258 (Properties) properties.get( language );
259
260 if ( bundleProperties == null )
261 {
262 bundleProperties = new Properties();
263 properties.put( language, bundleProperties );
264 }
265
266 bundleProperties.setProperty( message.getName(),
267 text.getValue() );
268
269 }
270 }
271
272 for ( final Iterator it = implementation.getMessages().
273 getReference().iterator(); it.hasNext(); )
274 {
275 final MessageReference messageReference =
276 (MessageReference) it.next();
277
278 final Message message =
279 this.getMessage( module, messageReference.getName() );
280
281 for ( final Iterator t = message.getTemplate().getText().
282 iterator(); t.hasNext(); )
283 {
284 final Text text = (Text) t.next();
285 final String language = text.getLanguage().toLowerCase();
286
287 Properties bundleProperties =
288 (Properties) properties.get( language );
289
290 if ( bundleProperties == null )
291 {
292 bundleProperties = new Properties();
293 properties.put( language, bundleProperties );
294 }
295
296 bundleProperties.setProperty( message.getName(),
297 text.getValue() );
298
299 }
300 }
301 }
302
303 return properties;
304 }
305
306
307
308
309
310
311
312
313
314
315 public String getJavaPackageName( final Implementation implementation )
316 {
317 if ( implementation == null )
318 {
319 throw new NullPointerException( "implementation" );
320 }
321
322 return implementation.getIdentifier().
323 substring( 0, implementation.getIdentifier().lastIndexOf( '.' ) );
324
325 }
326
327
328
329
330
331
332
333
334
335
336 public String getJavaTypeName( final Implementation implementation )
337 {
338 if ( implementation == null )
339 {
340 throw new NullPointerException( "implementation" );
341 }
342
343 return implementation.getIdentifier().
344 substring( implementation.getIdentifier().lastIndexOf( '.' ) + 1 ) +
345 "Bundle";
346
347 }
348
349
350
351
352
353
354
355
356
357
358 public String getJavadocComment( final Text text )
359 {
360 if ( text == null )
361 {
362 throw new NullPointerException( "text" );
363 }
364
365 String normalized = text.getValue();
366 normalized = normalized.replaceAll( "\\/\\*\\*", "/*" );
367 normalized = normalized.replaceAll( "\\*/", "/" );
368 normalized = normalized.replaceAll( "\n", "\n *" );
369 return normalized;
370 }
371
372
373
374
375
376
377
378
379
380
381 public String getJavaAccessorMethodName( final Message message )
382 {
383 if ( message == null )
384 {
385 throw new NullPointerException( "message" );
386 }
387
388 final char[] c = message.getName().toCharArray();
389 c[0] = Character.toUpperCase( c[0] );
390
391 return new StringBuffer( 255 ).append( "get" ).
392 append( String.valueOf( c ) ).
393 append( "Message" ).toString();
394
395 }
396
397
398
399
400
401
402
403
404
405
406
407
408 public String getJavaClasspathLocation( final Implementation implementation )
409 {
410 if ( implementation == null )
411 {
412 throw new NullPointerException( "implementation" );
413 }
414
415 return ( this.getJavaPackageName( implementation ) + '.' +
416 this.getJavaTypeName( implementation ) ).replace( '.', '/' );
417
418 }
419
420 }