EMMA Coverage Report (generated Sun Jan 17 14:36:44 UTC 2010)
[all classes][org.jomc.tools]

COVERAGE SUMMARY FOR SOURCE FILE [JavaBundles.java]

nameclass, %method, %block, %line, %
JavaBundles.java100% (1/1)88%  (14/16)85%  (630/738)87%  (124.2/142)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JavaBundles100% (1/1)88%  (14/16)85%  (630/738)87%  (124.2/142)
JavaBundles (JavaBundles): void 0%   (0/1)0%   (0/8)0%   (0/3)
setDefaultLocale (Locale): void 0%   (0/1)0%   (0/4)0%   (0/2)
writeBundleSources (Implementation, File): void 100% (1/1)76%  (79/104)82%  (13.9/17)
writeBundleResources (Implementation, File): void 100% (1/1)77%  (203/263)84%  (36.3/43)
getResourceBundleSources (Implementation): String 100% (1/1)86%  (37/43)83%  (10/12)
assertValidTemplates (Implementation): void 100% (1/1)90%  (45/50)89%  (8/9)
<static initializer> 100% (1/1)100% (4/4)100% (1/1)
JavaBundles (): void 100% (1/1)100% (3/3)100% (2/2)
getDefaultLocale (): Locale 100% (1/1)100% (28/28)100% (5/5)
getMessage (String, Object): String 100% (1/1)100% (16/16)100% (2/2)
getResourceBundleResources (Implementation): Map 100% (1/1)100% (80/80)100% (16/16)
getVelocityContext (): VelocityContext 100% (1/1)100% (15/15)100% (4/4)
writeBundleResources (File): void 100% (1/1)100% (25/25)100% (5/5)
writeBundleResources (Module, File): void 100% (1/1)100% (35/35)100% (8/8)
writeBundleSources (File): void 100% (1/1)100% (25/25)100% (5/5)
writeBundleSources (Module, File): void 100% (1/1)100% (35/35)100% (8/8)

1/*
2 *   Copyright (c) 2009 The JOMC Project
3 *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
4 *   All rights reserved.
5 *
6 *   Redistribution and use in source and binary forms, with or without
7 *   modification, are permitted provided that the following conditions
8 *   are met:
9 *
10 *     o Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *
13 *     o Redistributions in binary form must reproduce the above copyright
14 *       notice, this list of conditions and the following disclaimer in
15 *       the documentation and/or other materials provided with the
16 *       distribution.
17 *
18 *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
19 *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
22 *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 *   $Id: JavaBundles.java 1237 2010-01-09 20:22:54Z schulte2005 $
31 *
32 */
33package org.jomc.tools;
34 
35import java.io.File;
36import java.io.FileOutputStream;
37import java.io.IOException;
38import java.io.OutputStream;
39import java.io.StringWriter;
40import java.text.MessageFormat;
41import java.util.HashMap;
42import java.util.Locale;
43import java.util.Map;
44import java.util.Properties;
45import java.util.ResourceBundle;
46import java.util.logging.Level;
47import org.apache.commons.io.FileUtils;
48import org.apache.velocity.Template;
49import org.apache.velocity.VelocityContext;
50import org.jomc.model.Implementation;
51import org.jomc.model.Message;
52import org.jomc.model.Messages;
53import org.jomc.model.Module;
54import org.jomc.model.Text;
55 
56/**
57 * Generates Java bundles.
58 *
59 * <p><b>Use cases</b><br/><ul>
60 * <li>{@link #writeBundleResources(java.io.File) }</li>
61 * <li>{@link #writeBundleResources(org.jomc.model.Module, java.io.File) }</li>
62 * <li>{@link #writeBundleResources(org.jomc.model.Implementation, java.io.File) }</li>
63 * <li>{@link #writeBundleSources(java.io.File) }</li>
64 * <li>{@link #writeBundleSources(org.jomc.model.Module, java.io.File) }</li>
65 * <li>{@link #writeBundleSources(org.jomc.model.Implementation, java.io.File) }</li>
66 * </ul></p>
67 *
68 * @author <a href="mailto:cs@jomc.org">Christian Schulte</a>
69 * @version $Id: JavaBundles.java 1237 2010-01-09 20:22:54Z schulte2005 $
70 *
71 * @see #getModules()
72 */
73public class JavaBundles extends JomcTool
74{
75 
76    /** Name of the generator. */
77    private static final String GENERATOR_NAME = JavaBundles.class.getName();
78 
79    /** Constant for the version of the generator. */
80    private static final String GENERATOR_VERSION = "1.0";
81 
82    /** Location of the {@code Bundle.java.vm} template. */
83    private static final String BUNDLE_TEMPLATE = "Bundle.java.vm";
84 
85    /** Constant for the suffix appended to implementation identifiers. */
86    private static final String BUNDLE_SUFFIX = "Bundle";
87 
88    /** The language of the default language properties file of the bundle. */
89    private Locale defaultLocale;
90 
91    /** Creates a new {@code JavaBundles} instance. */
92    public JavaBundles()
93    {
94        super();
95    }
96 
97    /**
98     * Creates a new {@code JavaBundles} instance taking a {@code JavaBundles} instance to initialize the instance with.
99     *
100     * @param tool The instance to initialize the new instance with,
101     */
102    public JavaBundles( final JavaBundles tool )
103    {
104        super( tool );
105        this.setDefaultLocale( tool.getDefaultLocale() );
106    }
107 
108    /**
109     * Gets the language of the default language properties file of the bundle.
110     *
111     * @return The language of the default language properties file of the bundle.
112     *
113     * @see #setDefaultLocale(java.util.Locale)
114     */
115    public Locale getDefaultLocale()
116    {
117        if ( this.defaultLocale == null )
118        {
119            this.defaultLocale = Locale.getDefault();
120            if ( this.isLoggable( Level.FINE ) )
121            {
122                this.log( Level.FINE, this.getMessage( "defaultLocale", new Object[]
123                    {
124                        this.defaultLocale.toString()
125                    } ), null );
126 
127            }
128        }
129 
130        return this.defaultLocale;
131    }
132 
133    /**
134     * Sets the language of the default language properties file of the bundle.
135     *
136     * @param value The language of the default language properties file of the bundle.
137     *
138     * @see #getDefaultLocale()
139     */
140    public void setDefaultLocale( final Locale value )
141    {
142        this.defaultLocale = value;
143    }
144 
145    /**
146     * Writes bundle sources of the modules of the instance to a given directory.
147     *
148     * @param sourcesDirectory The directory to write sources to.
149     *
150     * @throws NullPointerException if {@code sourcesDirectory} is {@code null}.
151     * @throws ToolException if writing fails.
152     *
153     * @see #writeBundleSources(org.jomc.model.Module, java.io.File)
154     */
155    public void writeBundleSources( final File sourcesDirectory ) throws ToolException
156    {
157        if ( sourcesDirectory == null )
158        {
159            throw new NullPointerException( "sourcesDirectory" );
160        }
161 
162        for ( Module m : this.getModules().getModule() )
163        {
164            this.writeBundleSources( m, sourcesDirectory );
165        }
166    }
167 
168    /**
169     * Writes bundle sources of a given module from the modules of the instance to a given directory.
170     *
171     * @param module The module to process.
172     * @param sourcesDirectory The directory to write sources to.
173     *
174     * @throws NullPointerException if {@code module} or {@code sourcesDirectory} is {@code null}.
175     * @throws ToolException if writing fails.
176     *
177     * @see #writeBundleSources(org.jomc.model.Implementation, java.io.File)
178     */
179    public void writeBundleSources( final Module module, final File sourcesDirectory ) throws ToolException
180    {
181        if ( module == null )
182        {
183            throw new NullPointerException( "module" );
184        }
185        if ( sourcesDirectory == null )
186        {
187            throw new NullPointerException( "sourcesDirectory" );
188        }
189 
190        if ( module.getImplementations() != null )
191        {
192            for ( Implementation i : module.getImplementations().getImplementation() )
193            {
194                this.writeBundleSources( i, sourcesDirectory );
195            }
196        }
197    }
198 
199    /**
200     * Writes bundle sources of a given implementation from the modules of the instance to a given directory.
201     *
202     * @param implementation The implementation to process.
203     * @param sourcesDirectory The directory to write sources to.
204     *
205     * @throws NullPointerException if {@code implementation} or {@code sourcesDirectory} is {@code null}.
206     * @throws ToolException if writing fails.
207     *
208     * @see #getResourceBundleSources(org.jomc.model.Implementation)
209     */
210    public void writeBundleSources( final Implementation implementation, final File sourcesDirectory )
211        throws ToolException
212    {
213        if ( implementation == null )
214        {
215            throw new NullPointerException( "implementation" );
216        }
217        if ( sourcesDirectory == null )
218        {
219            throw new NullPointerException( "sourcesDirectory" );
220        }
221 
222        try
223        {
224            if ( implementation.isClassDeclaration() )
225            {
226                this.assertValidTemplates( implementation );
227 
228                final String bundlePath =
229                    ( this.getJavaTypeName( implementation, true ) + BUNDLE_SUFFIX ).replace( '.', File.separatorChar );
230 
231                final File bundleFile = new File( sourcesDirectory, bundlePath + ".java" );
232 
233                if ( !bundleFile.getParentFile().exists() && !bundleFile.getParentFile().mkdirs() )
234                {
235                    throw new ToolException( this.getMessage( "failedCreatingDirectory", new Object[]
236                        {
237                            bundleFile.getParentFile().getAbsolutePath()
238                        } ) );
239 
240                }
241 
242                if ( this.isLoggable( Level.INFO ) )
243                {
244                    this.log( Level.INFO, this.getMessage( "writing", new Object[]
245                        {
246                            bundleFile.getCanonicalPath()
247                        } ), null );
248 
249                }
250 
251                FileUtils.writeStringToFile( bundleFile, this.getResourceBundleSources( implementation ),
252                                             this.getOutputEncoding() );
253 
254            }
255        }
256        catch ( final IOException e )
257        {
258            throw new ToolException( e );
259        }
260    }
261 
262    /**
263     * Writes bundle resources of the modules of the instance to a given directory.
264     *
265     * @param resourcesDirectory The directory to write resources to.
266     *
267     * @throws NullPointerException if {@code resourcesDirectory} is {@code null}.
268     * @throws ToolException if writing fails.
269     *
270     * @see #writeBundleResources(org.jomc.model.Module, java.io.File)
271     */
272    public void writeBundleResources( final File resourcesDirectory ) throws ToolException
273    {
274        if ( resourcesDirectory == null )
275        {
276            throw new NullPointerException( "resourcesDirectory" );
277        }
278 
279        for ( Module m : this.getModules().getModule() )
280        {
281            this.writeBundleResources( m, resourcesDirectory );
282        }
283    }
284 
285    /**
286     * Writes bundle resources of a given module from the modules of the instance to a given directory.
287     *
288     * @param module The module to process.
289     * @param resourcesDirectory The directory to write resources to.
290     *
291     * @throws NullPointerException if {@code module} or {@code resourcesDirectory} is {@code null}.
292     * @throws ToolException if writing fails.
293     *
294     * @see #writeBundleResources(org.jomc.model.Implementation, java.io.File)
295     */
296    public void writeBundleResources( final Module module, final File resourcesDirectory ) throws ToolException
297    {
298        if ( module == null )
299        {
300            throw new NullPointerException( "module" );
301        }
302        if ( resourcesDirectory == null )
303        {
304            throw new NullPointerException( "resourcesDirectory" );
305        }
306 
307        if ( module.getImplementations() != null )
308        {
309            for ( Implementation i : module.getImplementations().getImplementation() )
310            {
311                this.writeBundleResources( i, resourcesDirectory );
312            }
313        }
314    }
315 
316    /**
317     * Writes the bundle resources of a given implementation from the modules of the instance to a directory.
318     *
319     * @param implementation The implementation to process.
320     * @param resourcesDirectory The directory to write resources to.
321     *
322     * @throws NullPointerException if {@code implementation} or {@code resourcesDirectory} is {@code null}.
323     * @throws ToolException if writing fails.
324     *
325     * @see #getResourceBundleResources(org.jomc.model.Implementation)
326     */
327    public void writeBundleResources( final Implementation implementation, final File resourcesDirectory )
328        throws ToolException
329    {
330        if ( implementation == null )
331        {
332            throw new NullPointerException( "implementation" );
333        }
334        if ( resourcesDirectory == null )
335        {
336            throw new NullPointerException( "resourcesDirectory" );
337        }
338 
339        try
340        {
341            if ( implementation.isClassDeclaration() )
342            {
343                this.assertValidTemplates( implementation );
344 
345                final String bundlePath =
346                    ( this.getJavaTypeName( implementation, true ) + BUNDLE_SUFFIX ).replace( '.', File.separatorChar );
347 
348                Properties defProperties = null;
349                Properties fallbackProperties = null;
350 
351                for ( Map.Entry<Locale, Properties> e : this.getResourceBundleResources( implementation ).entrySet() )
352                {
353                    final String language = e.getKey().getLanguage().toLowerCase();
354                    final java.util.Properties p = e.getValue();
355                    final File file = new File( resourcesDirectory, bundlePath + "_" + language + ".properties" );
356 
357                    if ( !file.getParentFile().exists() && !file.getParentFile().mkdirs() )
358                    {
359                        throw new ToolException( this.getMessage( "failedCreatingDirectory", new Object[]
360                            {
361                                file.getParentFile().getAbsolutePath()
362                            } ) );
363 
364                    }
365 
366                    if ( this.isLoggable( Level.INFO ) )
367                    {
368                        this.log( Level.INFO, this.getMessage( "writing", new Object[]
369                            {
370                                file.getCanonicalPath()
371                            } ), null );
372 
373                    }
374 
375                    OutputStream out = null;
376                    try
377                    {
378                        out = new FileOutputStream( file );
379                        p.store( out, GENERATOR_NAME + ' ' + GENERATOR_VERSION );
380                    }
381                    finally
382                    {
383                        if ( out != null )
384                        {
385                            out.close();
386                        }
387                    }
388 
389                    if ( this.getDefaultLocale().getLanguage().equalsIgnoreCase( language ) )
390                    {
391                        defProperties = p;
392                    }
393 
394                    fallbackProperties = p;
395                }
396 
397                if ( defProperties == null )
398                {
399                    defProperties = fallbackProperties;
400                }
401 
402                if ( defProperties != null )
403                {
404                    final File file = new File( resourcesDirectory, bundlePath + ".properties" );
405                    if ( !file.getParentFile().exists() && !file.getParentFile().mkdirs() )
406                    {
407                        throw new ToolException( this.getMessage( "failedCreatingDirectory", new Object[]
408                            {
409                                file.getParentFile().getAbsolutePath()
410                            } ) );
411 
412                    }
413 
414                    if ( this.isLoggable( Level.INFO ) )
415                    {
416                        this.log( Level.INFO, this.getMessage( "writing", new Object[]
417                            {
418                                file.getCanonicalPath()
419                            } ), null );
420 
421                    }
422 
423                    OutputStream out = null;
424                    try
425                    {
426                        out = new FileOutputStream( file );
427                        defProperties.store( out, GENERATOR_NAME + ' ' + GENERATOR_VERSION );
428                    }
429                    finally
430                    {
431                        if ( out != null )
432                        {
433                            out.close();
434                        }
435                    }
436                }
437            }
438        }
439        catch ( final IOException e )
440        {
441            throw new ToolException( e );
442        }
443    }
444 
445    /**
446     * Gets the source code of the Java class for accessing the resource bundle of a given implementation.
447     *
448     * @param implementation The implementation to get the source code of.
449     *
450     * @return The source code of the Java class for accessing the resource bundle of {@code implementation}.
451     *
452     * @throws NullPointerException if {@code implementation} is {@code null}.
453     * @throws ToolException if getting the source code fails.
454     */
455    public String getResourceBundleSources( final Implementation implementation ) throws ToolException
456    {
457        if ( implementation == null )
458        {
459            throw new NullPointerException( "implementation" );
460        }
461 
462        try
463        {
464            final StringWriter writer = new StringWriter();
465            final VelocityContext ctx = this.getVelocityContext();
466            final Template template = this.getVelocityTemplate( BUNDLE_TEMPLATE );
467            ctx.put( "implementation", implementation );
468            ctx.put( "template", template );
469            template.merge( ctx, writer );
470            writer.close();
471            return writer.toString();
472        }
473        catch ( final IOException e )
474        {
475            throw new ToolException( e );
476        }
477    }
478 
479    /**
480     * Gets the resource bundle properties of a given implementation.
481     *
482     * @param implementation The implementation to get resource bundle properties of.
483     *
484     * @return Resource bundle properties of {@code implementation}.
485     *
486     * @throws NullPointerException if {@code implementation} is {@code null}.
487     * @throws ToolException if getting the resources fails.
488     */
489    public Map<Locale, Properties> getResourceBundleResources( final Implementation implementation )
490        throws ToolException
491    {
492        if ( implementation == null )
493        {
494            throw new NullPointerException( "implementation" );
495        }
496 
497        final Map<Locale, java.util.Properties> properties = new HashMap<Locale, java.util.Properties>( 10 );
498        final Messages messages = this.getModules().getMessages( implementation.getIdentifier() );
499 
500        if ( messages != null )
501        {
502            for ( Message message : messages.getMessage() )
503            {
504                if ( message.getTemplate() != null )
505                {
506                    for ( Text text : message.getTemplate().getText() )
507                    {
508                        final Locale locale = new Locale( text.getLanguage().toLowerCase() );
509                        Properties bundleProperties = properties.get( locale );
510 
511                        if ( bundleProperties == null )
512                        {
513                            bundleProperties = new Properties();
514                            properties.put( locale, bundleProperties );
515                        }
516 
517                        bundleProperties.setProperty( message.getName(), text.getValue() );
518                    }
519                }
520            }
521        }
522 
523        return properties;
524    }
525 
526    /**
527     * Gets the velocity context used for merging templates.
528     *
529     * @return The velocity context used for merging templates.
530     */
531    @Override
532    public VelocityContext getVelocityContext()
533    {
534        final VelocityContext ctx = super.getVelocityContext();
535        ctx.put( "classSuffix", BUNDLE_SUFFIX );
536        ctx.put( "comment", Boolean.TRUE );
537        return ctx;
538    }
539 
540    private void assertValidTemplates( final Implementation implementation )
541    {
542        if ( implementation == null )
543        {
544            throw new NullPointerException( "implementation" );
545        }
546 
547        final Messages messages = this.getModules().getMessages( implementation.getIdentifier() );
548        if ( messages != null )
549        {
550            for ( Message m : messages.getMessage() )
551            {
552                if ( m.getTemplate() != null )
553                {
554                    for ( Text t : m.getTemplate().getText() )
555                    {
556                        new MessageFormat( t.getValue() );
557                    }
558                }
559            }
560        }
561    }
562 
563    private String getMessage( final String key, final Object args )
564    {
565        final ResourceBundle b = ResourceBundle.getBundle( JavaBundles.class.getName().replace( '.', '/' ) );
566        return new MessageFormat( b.getString( key ) ).format( args );
567    }
568 
569}

[all classes][org.jomc.tools]
EMMA 2.0.5312 (C) Vladimir Roubtsov