View Javadoc

1   /*
2    *  jDTAUS Core Resource Mojo
3    *  Copyright (C) 2005 Christian Schulte
4    *  <cs@schulte.it>
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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   * Manages the {@code http://jdtaus.org/core/model/container} model.
36   *
37   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
38   * @version $JDTAUS: ModelManager.java 8743 2012-10-07 03:06:20Z schulte $
39   * @plexus.component role="org.jdtaus.mojo.resource.model.ModelManager"
40   *                   role-hint="default"
41   */
42  public class ModelManager
43  {
44  
45      /** Creates a new {@code ModelManager} instance. */
46      public ModelManager()
47      {
48          super();
49      }
50  
51      /**
52       * Unmarshals a given module descriptor.
53       *
54       * @param moduleDescriptor module descriptor to unmarshall.
55       *
56       * @return the content tree unmarshalled from {@code moduleDescriptor}.
57       *
58       * @throws NullPointerException if {@code moduleDescriptor} is {@code null}.
59       * @throws JAXBException if unmarshalling fails.
60       * @throws SAXException if parsing fails.
61       * @throws IOException if reading fails.
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       * Gets a message.
82       *
83       * @param module the module to search.
84       * @param messageName the name of the message to search.
85       *
86       * @return the message with name {@code name} from {@code module} or
87       * {@code null} if no message exists.
88       *
89       * @throws NullPointerException if {@code module} or {@code messageName} is
90       * {@code null}.
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      * Computes the hashcode of an implementation bundle.
125      *
126      * @param module the module containing {@code implementation}.
127      * @param implementation the implementation to compute the bundle hashcode
128      * of.
129      *
130      * @return the bundle hashcode of {@code implementation}.
131      *
132      * @throws NullPointerException if {@code module} or {@code implementation}
133      * is {@code null}.
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      * Builds a mapping of language codes to {@code Properties} instances
220      * holding the messages for the language.
221      *
222      * @param module the module containing {@code implementation}.
223      * @param implementation the implementation to get the properties for.
224      *
225      * @return mapping of language codes to {@code Properties} instances
226      * holding the messages for the language.
227      *
228      * @throws NullPointerException if {@code module} or {@code implementation}
229      * is {@code null}.
230      */
231     public Map/*<String,Properties>*/ getBundleProperties(
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      * Gets the java package name of an implementation.
308      *
309      * @param implementation the implementation to get the java package name of.
310      *
311      * @return the java package name of {@code implementation}.
312      *
313      * @throws NullPointerException if {@code implementation} is {@code null}.
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      * Gets the java type name of an implementation.
329      *
330      * @param implementation the implementation to get the java type name of.
331      *
332      * @return the java type name of {@code implementation}.
333      *
334      * @throws NullPointerException if {@code implementation} is {@code null}.
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      * Formats a text to a javadoc comment.
351      *
352      * @param text the text to nformat to a javadoc comment.
353      *
354      * @return {@code text} formatted as a javadoc comment.
355      *
356      * @throws NullPointerException if {@code text} is {@code null}.
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      * Gets the method name of a Java accessor method for a given message.
374      *
375      * @param message the message to return the accessor method name for.
376      *
377      * @return The method name of a Java accessor method for {@code message}.
378      *
379      * @throws NullPointerException if {@code text} is {@code null}.
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      * Gets the java classpath location of an implementation bundle.
399      *
400      * @param implementation the implementation to return the bundle's java
401      * classpath location of.
402      *
403      * @return the java classpath location of the bundle of
404      * {@code implementation}.
405      *
406      * @throws NullPointerException if {@code implementation} is {@code null}.
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 }