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