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

COVERAGE SUMMARY FOR SOURCE FILE [Jomc.java]

nameclass, %method, %block, %line, %
Jomc.java100% (2/2)92%  (35/38)85%  (993/1163)89%  (178.2/200)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Jomc100% (1/1)92%  (33/36)85%  (980/1150)89%  (175.2/197)
main (String []): void 0%   (0/1)0%   (0/4)0%   (0/2)
setDefaultLogLevel (Level): void 0%   (0/1)0%   (0/3)0%   (0/2)
setPrintWriter (PrintWriter): void 0%   (0/1)0%   (0/4)0%   (0/2)
getMessage (Throwable): String 100% (1/1)53%  (8/15)60%  (3/5)
isLoggable (Level): boolean 100% (1/1)65%  (11/17)63%  (1.9/3)
getDebugOptionLongName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getDebugOptionShortName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getFailOnWarningsOptionLongName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getFailOnWarningsOptionShortName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getHelpCommandName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getLocale (): Locale 100% (1/1)75%  (15/20)91%  (2.7/3)
getVerboseOptionLongName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getVerboseOptionShortName (): String 100% (1/1)75%  (15/20)91%  (2.7/3)
getCommands (): Command [] 100% (1/1)76%  (16/21)91%  (2.7/3)
getDescPad (): int 100% (1/1)76%  (16/21)91%  (2.7/3)
getLeftPad (): int 100% (1/1)76%  (16/21)91%  (2.7/3)
getWidth (): int 100% (1/1)76%  (16/21)91%  (2.7/3)
getDebugOptionMessage (Locale): String 100% (1/1)77%  (17/22)92%  (2.8/3)
getFailOnWarningsOptionMessage (Locale): String 100% (1/1)77%  (17/22)92%  (2.8/3)
getVerboseOptionMessage (Locale): String 100% (1/1)77%  (17/22)92%  (2.8/3)
<static initializer> 100% (1/1)80%  (8/10)90%  (1.8/2)
getCommandLineInfoMessage (Locale, String): String 100% (1/1)81%  (21/26)93%  (2.8/3)
getUsageMessage (Locale, String): String 100% (1/1)81%  (21/26)93%  (2.8/3)
getIllegalArgumentsMessage (Locale, String, String): String 100% (1/1)83%  (25/30)94%  (2.8/3)
formatLogLines (Level, String): String 100% (1/1)86%  (37/43)75%  (6/8)
log (Level, String, Throwable): void 100% (1/1)87%  (86/99)91%  (20/22)
jomc (String []): int 100% (1/1)92%  (420/455)95%  (69.2/73)
Jomc (): void 100% (1/1)100% (6/6)100% (3/3)
getDebugOption (): Option 100% (1/1)100% (20/20)100% (3/3)
getDefaultLogLevel (): Level 100% (1/1)100% (10/10)100% (3/3)
getFailOnWarningsOption (): Option 100% (1/1)100% (20/20)100% (3/3)
getLogLevel (): Level 100% (1/1)100% (9/9)100% (3/3)
getPrintWriter (): PrintWriter 100% (1/1)100% (13/13)100% (3/3)
getVerboseOption (): Option 100% (1/1)100% (20/20)100% (3/3)
run (String []): int 100% (1/1)100% (6/6)100% (1/1)
setLogLevel (Level): void 100% (1/1)100% (4/4)100% (2/2)
     
class Jomc$1100% (1/1)100% (2/2)100% (13/13)100% (3/3)
Jomc$1 (Jomc): void 100% (1/1)100% (6/6)100% (1/1)
onLog (Level, String, Throwable): void 100% (1/1)100% (7/7)100% (2/2)

1// SECTION-START[License Header]
2// <editor-fold defaultstate="collapsed" desc=" Generated License ">
3/*
4 *   Copyright (c) 2010 The JOMC Project
5 *   Copyright (c) 2005 Christian Schulte <cs@jomc.org>
6 *   All rights reserved.
7 *
8 *   Redistribution and use in source and binary forms, with or without
9 *   modification, are permitted provided that the following conditions
10 *   are met:
11 *
12 *     o Redistributions of source code must retain the above copyright
13 *       notice, this list of conditions and the following disclaimer.
14 *
15 *     o Redistributions in binary form must reproduce the above copyright
16 *       notice, this list of conditions and the following disclaimer in
17 *       the documentation and/or other materials provided with the
18 *       distribution.
19 *
20 *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
21 *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
24 *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 *   $Id: Jomc.java 1306 2010-01-16 14:28:11Z schulte2005 $
33 *
34 */
35// </editor-fold>
36// SECTION-END
37package org.jomc.cli;
38 
39import java.io.BufferedReader;
40import java.io.IOException;
41import java.io.PrintWriter;
42import java.io.StringReader;
43import java.io.StringWriter;
44import java.util.logging.Level;
45import org.apache.commons.cli.CommandLine;
46import org.apache.commons.cli.GnuParser;
47import org.apache.commons.cli.HelpFormatter;
48import org.apache.commons.cli.Option;
49import org.apache.commons.cli.Options;
50import org.apache.commons.cli.ParseException;
51import org.apache.commons.lang.StringUtils;
52import org.jomc.model.DefaultModelProcessor;
53import org.jomc.model.DefaultModelProvider;
54import org.jomc.model.bootstrap.DefaultSchemaProvider;
55 
56// SECTION-START[Documentation]
57// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
58/**
59 * JOMC command line interface.
60 * <p><b>Properties</b><ul>
61 * <li>"{@link #getDebugOptionLongName debugOptionLongName}"
62 * <blockquote>Property of type {@code java.lang.String}.
63 * <p>Long name of the 'debug' option.</p>
64 * </blockquote></li>
65 * <li>"{@link #getDebugOptionShortName debugOptionShortName}"
66 * <blockquote>Property of type {@code java.lang.String}.
67 * <p>Name of the 'debug' option.</p>
68 * </blockquote></li>
69 * <li>"{@link #getDescPad descPad}"
70 * <blockquote>Property of type {@code int}.
71 * <p>The number of characters of padding to be prefixed to each description line.</p>
72 * </blockquote></li>
73 * <li>"{@link #getFailOnWarningsOptionLongName failOnWarningsOptionLongName}"
74 * <blockquote>Property of type {@code java.lang.String}.
75 * <p>Long name of the 'fail-on-warnings' option.</p>
76 * </blockquote></li>
77 * <li>"{@link #getFailOnWarningsOptionShortName failOnWarningsOptionShortName}"
78 * <blockquote>Property of type {@code java.lang.String}.
79 * <p>Name of the 'fail-on-warnings' option.</p>
80 * </blockquote></li>
81 * <li>"{@link #getHelpCommandName helpCommandName}"
82 * <blockquote>Property of type {@code java.lang.String}.
83 * <p>The name of the command used to request help.</p>
84 * </blockquote></li>
85 * <li>"{@link #getLeftPad leftPad}"
86 * <blockquote>Property of type {@code int}.
87 * <p>The number of characters of padding to be prefixed to each line.</p>
88 * </blockquote></li>
89 * <li>"{@link #getVerboseOptionLongName verboseOptionLongName}"
90 * <blockquote>Property of type {@code java.lang.String}.
91 * <p>Long name of the 'verbose' option.</p>
92 * </blockquote></li>
93 * <li>"{@link #getVerboseOptionShortName verboseOptionShortName}"
94 * <blockquote>Property of type {@code java.lang.String}.
95 * <p>Name of the 'verbose' option.</p>
96 * </blockquote></li>
97 * <li>"{@link #getWidth width}"
98 * <blockquote>Property of type {@code int}.
99 * <p>The number of characters per line for the usage statement.</p>
100 * </blockquote></li>
101 * </ul></p>
102 * <p><b>Dependencies</b><ul>
103 * <li>"{@link #getCommands Commands}"<blockquote>
104 * Dependency on {@code org.jomc.cli.Command} at specification level 1.0-alpha-14.</blockquote></li>
105 * <li>"{@link #getLocale Locale}"<blockquote>
106 * Dependency on {@code java.util.Locale} at specification level 1.1 bound to an instance.</blockquote></li>
107 * </ul></p>
108 * <p><b>Messages</b><ul>
109 * <li>"{@link #getCommandLineInfoMessage commandLineInfo}"<table>
110 * <tr><td valign="top">English:</td><td valign="top"><pre>Command line:
111 * {0}</pre></td></tr>
112 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Kommandozeile:
113 * {0}</pre></td></tr>
114 * </table>
115 * <li>"{@link #getDebugOptionMessage debugOption}"<table>
116 * <tr><td valign="top">English:</td><td valign="top"><pre>Enables debug output.</pre></td></tr>
117 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Aktiviert Diagnose-Ausgaben.</pre></td></tr>
118 * </table>
119 * <li>"{@link #getFailOnWarningsOptionMessage failOnWarningsOption}"<table>
120 * <tr><td valign="top">English:</td><td valign="top"><pre>Exit with failure on warnings.</pre></td></tr>
121 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Bei Warnungen Fehler melden.</pre></td></tr>
122 * </table>
123 * <li>"{@link #getIllegalArgumentsMessage illegalArguments}"<table>
124 * <tr><td valign="top">English:</td><td valign="top"><pre>Illegal arguments. Type &raquo;jomc {0} {1}&laquo; for further information.</pre></td></tr>
125 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Ung&uuml;ltige Argumente. Geben Sie &raquo;jomc {0} {1}&laquo; f&uuml;r weitere Informationen ein.</pre></td></tr>
126 * </table>
127 * <li>"{@link #getUsageMessage usage}"<table>
128 * <tr><td valign="top">English:</td><td valign="top"><pre>Type &raquo;jomc &lt;command&gt; {0}&laquo; for further information.</pre></td></tr>
129 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Geben Sie &raquo;jomc &lt;Befehl&gt; {0}&laquo; f&uuml;r weitere Informationen ein.</pre></td></tr>
130 * </table>
131 * <li>"{@link #getVerboseOptionMessage verboseOption}"<table>
132 * <tr><td valign="top">English:</td><td valign="top"><pre>Enables verbose output.</pre></td></tr>
133 * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Aktiviert ausf&uuml;hrliche Ausgaben.</pre></td></tr>
134 * </table>
135 * </ul></p>
136 *
137 * @author <a href="mailto:cs@jomc.org">Christian Schulte</a> 1.0
138 * @version $Id: Jomc.java 1306 2010-01-16 14:28:11Z schulte2005 $
139 */
140// </editor-fold>
141// SECTION-END
142// SECTION-START[Annotations]
143// <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
144@javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
145                             comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
146// </editor-fold>
147// SECTION-END
148public class Jomc
149{
150    // SECTION-START[Jomc]
151 
152    /**
153     * Log level events are logged at by default.
154     * @see #getDefaultLogLevel()
155     */
156    private static final Level DEFAULT_LOG_LEVEL = Level.WARNING;
157 
158    /** Default log level. */
159    private static volatile Level defaultLogLevel;
160 
161    /** 'verbose' option of the instance. */
162    private Option verboseOption;
163 
164    /** 'debug' option of the instance. */
165    private Option debugOption;
166 
167    /** 'fail-on-warnings' option of the instance. */
168    private Option failOnWarningsOption;
169 
170    /** Print writer of the instance. */
171    private PrintWriter printWriter;
172 
173    /** Log level of the instance. */
174    private Level logLevel;
175 
176    /** Greatest severity logged by the command. */
177    private Level severity = Level.ALL;
178 
179    /**
180     * Gets the print writer of the instance.
181     *
182     * @return The print writer of the instance.
183     */
184    public PrintWriter getPrintWriter()
185    {
186        if ( this.printWriter == null )
187        {
188            this.printWriter = new PrintWriter( System.out, true );
189        }
190 
191        return this.printWriter;
192    }
193 
194    /**
195     * Sets the print writer of the instance.
196     *
197     * @param value The new print writer of the instance or {@code null}.
198     */
199    public void setPrintWriter( final PrintWriter value )
200    {
201        this.printWriter = value;
202    }
203 
204    /**
205     * Gets the default log level events are logged at.
206     * <p>The default log level is controlled by system property {@code org.jomc.cli.Jomc.defaultLogLevel} holding the
207     * log level to log events at by default. If that property is not set, the {@code WARNING} default is returned.</p>
208     *
209     * @return The log level events are logged at by default.
210     *
211     * @see #getLogLevel()
212     * @see Level#parse(java.lang.String)
213     */
214    public static Level getDefaultLogLevel()
215    {
216        if ( defaultLogLevel == null )
217        {
218            defaultLogLevel = Level.parse( System.getProperty(
219                "org.jomc.cli.Jomc.defaultLogLevel", DEFAULT_LOG_LEVEL.getName() ) );
220 
221        }
222 
223        return defaultLogLevel;
224    }
225 
226    /**
227     * Sets the default log level events are logged at.
228     *
229     * @param value The new default level events are logged at or {@code null}.
230     *
231     * @see #getDefaultLogLevel()
232     */
233    public static void setDefaultLogLevel( final Level value )
234    {
235        defaultLogLevel = value;
236    }
237 
238    /**
239     * Gets the log level of the instance.
240     *
241     * @return The log level of the instance.
242     *
243     * @see #getDefaultLogLevel()
244     * @see #setLogLevel(java.util.logging.Level)
245     * @see #isLoggable(java.util.logging.Level)
246     */
247    public Level getLogLevel()
248    {
249        if ( this.logLevel == null )
250        {
251            this.logLevel = getDefaultLogLevel();
252        }
253 
254        return this.logLevel;
255    }
256 
257    /**
258     * Sets the log level of the instance.
259     *
260     * @param value The new log level of the instance or {@code null}.
261     *
262     * @see #getLogLevel()
263     * @see #isLoggable(java.util.logging.Level)
264     */
265    public void setLogLevel( final Level value )
266    {
267        this.logLevel = value;
268    }
269 
270    /**
271     * Checks if a message at a given level is provided to the listeners of the instance.
272     *
273     * @param level The level to test.
274     *
275     * @return {@code true} if messages at {@code level} are provided to the listeners of the instance;
276     * {@code false} if messages at {@code level} are not provided to the listeners of the instance.
277     *
278     * @throws NullPointerException if {@code level} is {@code null}.
279     *
280     * @see #getLogLevel()
281     * @see #setLogLevel(java.util.logging.Level)
282     */
283    public boolean isLoggable( final Level level )
284    {
285        if ( level == null )
286        {
287            throw new NullPointerException( "level" );
288        }
289 
290        return level.intValue() >= this.getLogLevel().intValue();
291    }
292 
293    public Option getVerboseOption()
294    {
295        if ( this.verboseOption == null )
296        {
297            this.verboseOption = new Option( this.getVerboseOptionShortName(), this.getVerboseOptionLongName(),
298                                             false, this.getVerboseOptionMessage( this.getLocale() ) );
299 
300        }
301 
302        return this.verboseOption;
303    }
304 
305    public Option getDebugOption()
306    {
307        if ( this.debugOption == null )
308        {
309            this.debugOption = new Option( this.getDebugOptionShortName(), this.getDebugOptionLongName(),
310                                           false, this.getDebugOptionMessage( this.getLocale() ) );
311 
312        }
313 
314        return this.debugOption;
315    }
316 
317    public Option getFailOnWarningsOption()
318    {
319        if ( this.failOnWarningsOption == null )
320        {
321            this.failOnWarningsOption = new Option( this.getFailOnWarningsOptionShortName(),
322                                                    this.getFailOnWarningsOptionLongName(),
323                                                    false, this.getFailOnWarningsOptionMessage( this.getLocale() ) );
324 
325        }
326 
327        return this.failOnWarningsOption;
328    }
329 
330    /**
331     * Processes the given arguments and executes the corresponding command.
332     *
333     * @param args Arguments to process.
334     *
335     * @return Status code.
336     *
337     * @see Command#STATUS_SUCCESS
338     * @see Command#STATUS_FAILURE
339     */
340    public int jomc( final String[] args )
341    {
342        Command cmd = null;
343        this.severity = Level.ALL;
344 
345        try
346        {
347            DefaultModelProvider.setDefaultModuleLocation( "META-INF/jomc-cli.xml" );
348            DefaultModelProcessor.setDefaultTransformerLocation( "META-INF/jomc-cli.xslt" );
349            DefaultSchemaProvider.setDefaultSchemaLocation( "META-INF/jomc-bootstrap.xml" );
350 
351            final StringBuilder commandInfo = new StringBuilder();
352 
353            for ( Command c : this.getCommands() )
354            {
355                if ( cmd == null && args != null && args.length > 0 &&
356                     ( args[0].equals( c.getName() ) || args[0].equals( c.getAbbreviatedName() ) ) )
357                {
358                    cmd = c;
359                }
360 
361                commandInfo.append( StringUtils.rightPad( c.getName(), 25 ) ).append( " : " ).
362                    append( c.getShortDescription( this.getLocale() ) ).append( " (" ).append( c.getAbbreviatedName() ).
363                    append( ")" ).append( System.getProperty( "line.separator" ) );
364 
365            }
366 
367            if ( cmd == null )
368            {
369                this.getPrintWriter().println( this.getUsageMessage( this.getLocale(), this.getHelpCommandName() ) );
370                this.getPrintWriter().println();
371                this.getPrintWriter().println( commandInfo.toString() );
372                return Command.STATUS_FAILURE;
373            }
374 
375            final String[] commandArguments = new String[ args.length - 1 ];
376            System.arraycopy( args, 1, commandArguments, 0, commandArguments.length );
377 
378            if ( commandArguments.length > 0 && this.getHelpCommandName().equals( commandArguments[0] ) )
379            {
380                final StringWriter usage = new StringWriter();
381                final StringWriter opts = new StringWriter();
382                final HelpFormatter formatter = new HelpFormatter();
383                final Options options = cmd.getOptions();
384                options.addOption( this.getDebugOption() );
385                options.addOption( this.getVerboseOption() );
386                options.addOption( this.getFailOnWarningsOption() );
387 
388                PrintWriter pw = new PrintWriter( usage );
389                formatter.printUsage( pw, this.getWidth(), cmd.getName(), options );
390                pw.close();
391 
392                pw = new PrintWriter( opts );
393                formatter.printOptions( pw, this.getWidth(), options, this.getLeftPad(), this.getDescPad() );
394                pw.close();
395 
396                this.getPrintWriter().println( cmd.getShortDescription( this.getLocale() ) );
397                this.getPrintWriter().println();
398                this.getPrintWriter().println( usage.toString() );
399                this.getPrintWriter().println( opts.toString() );
400                this.getPrintWriter().println();
401                this.getPrintWriter().println( cmd.getLongDescription( this.getLocale() ) );
402                this.getPrintWriter().println();
403                return Command.STATUS_SUCCESS;
404            }
405 
406            cmd.getListeners().add( new Command.Listener()
407            {
408 
409                public void onLog( final Level level, final String message, final Throwable t )
410                {
411                    log( level, message, t );
412                }
413 
414            } );
415 
416            DefaultModelProvider.setDefaultModuleLocation( null );
417            DefaultModelProcessor.setDefaultTransformerLocation( null );
418            DefaultSchemaProvider.setDefaultSchemaLocation( null );
419 
420            final Options options = cmd.getOptions();
421            options.addOption( this.getDebugOption() );
422            options.addOption( this.getVerboseOption() );
423            options.addOption( this.getFailOnWarningsOption() );
424 
425            final CommandLine commandLine = new GnuParser().parse( options, commandArguments );
426            final boolean debug = commandLine.hasOption( this.getDebugOption().getOpt() );
427            final boolean verbose = commandLine.hasOption( this.getVerboseOption().getOpt() );
428 
429            if ( debug || verbose )
430            {
431                this.setLogLevel( debug ? Level.ALL : Level.INFO );
432            }
433 
434            cmd.setLogLevel( this.getLogLevel() );
435 
436            if ( this.isLoggable( Level.FINE ) )
437            {
438                final StringBuilder argumentInfo = new StringBuilder();
439 
440                for ( int i = 0; i < args.length; i++ )
441                {
442                    argumentInfo.append( "\t[" ).append( i ).append( "]='" ).append( args[i] ).append( "'" ).
443                        append( System.getProperty( "line.separator" ) );
444 
445                }
446 
447                this.log( Level.FINE, this.getCommandLineInfoMessage(
448                    this.getLocale(), argumentInfo.toString() ), null );
449 
450            }
451 
452            final boolean failOnWarnings = commandLine.hasOption( this.getFailOnWarningsOption().getOpt() );
453 
454            final int status = cmd.execute( commandLine );
455            if ( status == Command.STATUS_SUCCESS && failOnWarnings &&
456                 this.severity.intValue() >= Level.WARNING.intValue() )
457            {
458                return Command.STATUS_FAILURE;
459            }
460 
461            return status;
462        }
463        catch ( final ParseException e )
464        {
465            this.log( Level.SEVERE, this.getIllegalArgumentsMessage(
466                this.getLocale(), cmd.getName(), this.getHelpCommandName() ), e );
467 
468            return Command.STATUS_FAILURE;
469        }
470        catch ( final Throwable t )
471        {
472            this.log( Level.SEVERE, t.getMessage(), t );
473            return Command.STATUS_FAILURE;
474        }
475        finally
476        {
477            DefaultModelProvider.setDefaultModuleLocation( null );
478            DefaultModelProcessor.setDefaultTransformerLocation( null );
479            DefaultSchemaProvider.setDefaultSchemaLocation( null );
480            this.getPrintWriter().flush();
481        }
482    }
483 
484    /**
485     * Main entry point.
486     *
487     * @param args The application arguments.
488     */
489    public static void main( final String[] args )
490    {
491        System.exit( run( args ) );
492    }
493 
494    /**
495     * Main entry point without exiting the VM.
496     *
497     * @param args The application arguments.
498     *
499     * @return Status code.
500     *
501     * @see Command#STATUS_SUCCESS
502     * @see Command#STATUS_FAILURE
503     */
504    public static int run( final String[] args )
505    {
506        return new Jomc().jomc( args );
507    }
508 
509    /**
510     * Logs to the print writer of the instance.
511     *
512     * @param level The level of the event.
513     * @param message The message of the event or {@code null}.
514     * @param throwable The throwable of the event {@code null}.
515     *
516     * @throws NullPointerException if {@code level} is {@code null}.
517     */
518    protected void log( final Level level, final String message, final Throwable throwable )
519    {
520        if ( level == null )
521        {
522            throw new NullPointerException( "level" );
523        }
524 
525        if ( this.severity.intValue() < level.intValue() )
526        {
527            this.severity = level;
528        }
529 
530        if ( this.isLoggable( level ) )
531        {
532            if ( message != null )
533            {
534                this.getPrintWriter().print( this.formatLogLines( level, "" ) );
535                this.getPrintWriter().print( this.formatLogLines( level, message ) );
536            }
537 
538            if ( throwable != null )
539            {
540                this.getPrintWriter().print( this.formatLogLines( level, "" ) );
541                final String m = this.getMessage( throwable );
542 
543                if ( m != null )
544                {
545                    this.getPrintWriter().print( this.formatLogLines( level, m ) );
546                }
547                else
548                {
549                    this.getPrintWriter().print( this.formatLogLines( level, throwable.toString() ) );
550                }
551 
552                if ( this.getLogLevel().intValue() < Level.INFO.intValue() )
553                {
554                    final StringWriter stackTrace = new StringWriter();
555                    final PrintWriter pw = new PrintWriter( stackTrace );
556                    throwable.printStackTrace( pw );
557                    pw.flush();
558                    this.getPrintWriter().print( this.formatLogLines( level, stackTrace.toString() ) );
559                }
560            }
561        }
562 
563        this.getPrintWriter().flush();
564    }
565 
566    private String formatLogLines( final Level level, final String text )
567    {
568        try
569        {
570            final StringBuilder lines = new StringBuilder();
571            final BufferedReader reader = new BufferedReader( new StringReader( text ) );
572 
573            String line;
574            while ( ( line = reader.readLine() ) != null )
575            {
576                lines.append( "[" ).append( level.getLocalizedName() ).append( "] " );
577                lines.append( line ).append( System.getProperty( "line.separator" ) );
578            }
579 
580            return lines.toString();
581        }
582        catch ( final IOException e )
583        {
584            throw new AssertionError( e );
585        }
586    }
587 
588    private String getMessage( final Throwable throwable )
589    {
590        if ( throwable != null )
591        {
592            if ( throwable.getMessage() != null )
593            {
594                return throwable.getMessage();
595            }
596 
597            return this.getMessage( throwable.getCause() );
598        }
599 
600        return null;
601    }
602 
603    // SECTION-END
604    // SECTION-START[Constructors]
605    // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
606 
607    /** Creates a new {@code Jomc} instance. */
608    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
609                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
610    public Jomc()
611    {
612        // SECTION-START[Default Constructor]
613        super();
614        // SECTION-END
615    }
616    // </editor-fold>
617    // SECTION-END
618    // SECTION-START[Dependencies]
619    // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
620 
621    /**
622     * Gets the {@code Commands} dependency.
623     * <p>This method returns any available object of the {@code org.jomc.cli.Command} specification at specification level 1.0-alpha-14.</p>
624     * <p>That specification does not apply to any scope. A new object is returned whenever requested.</p>
625     * @return The {@code Commands} dependency.
626     * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
627     */
628    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
629                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
630    private org.jomc.cli.Command[] getCommands()
631    {
632        final org.jomc.cli.Command[] _d = (org.jomc.cli.Command[]) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Commands" );
633        assert _d != null : "'Commands' dependency not found.";
634        return _d;
635    }
636 
637    /**
638     * Gets the {@code Locale} dependency.
639     * <p>This method returns the "{@code default}" object of the {@code java.util.Locale} specification at specification level 1.1.</p>
640     * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
641     * @return The {@code Locale} dependency.
642     * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
643     */
644    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
645                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
646    private java.util.Locale getLocale()
647    {
648        final java.util.Locale _d = (java.util.Locale) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "Locale" );
649        assert _d != null : "'Locale' dependency not found.";
650        return _d;
651    }
652    // </editor-fold>
653    // SECTION-END
654    // SECTION-START[Properties]
655    // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
656 
657    /**
658     * Gets the value of the {@code debugOptionLongName} property.
659     * @return Long name of the 'debug' option.
660     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
661     */
662    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
663                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
664    private java.lang.String getDebugOptionLongName()
665    {
666        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "debugOptionLongName" );
667        assert _p != null : "'debugOptionLongName' property not found.";
668        return _p;
669    }
670 
671    /**
672     * Gets the value of the {@code debugOptionShortName} property.
673     * @return Name of the 'debug' option.
674     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
675     */
676    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
677                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
678    private java.lang.String getDebugOptionShortName()
679    {
680        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "debugOptionShortName" );
681        assert _p != null : "'debugOptionShortName' property not found.";
682        return _p;
683    }
684 
685    /**
686     * Gets the value of the {@code descPad} property.
687     * @return The number of characters of padding to be prefixed to each description line.
688     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
689     */
690    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
691                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
692    private int getDescPad()
693    {
694        final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "descPad" );
695        assert _p != null : "'descPad' property not found.";
696        return _p.intValue();
697    }
698 
699    /**
700     * Gets the value of the {@code failOnWarningsOptionLongName} property.
701     * @return Long name of the 'fail-on-warnings' option.
702     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
703     */
704    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
705                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
706    private java.lang.String getFailOnWarningsOptionLongName()
707    {
708        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "failOnWarningsOptionLongName" );
709        assert _p != null : "'failOnWarningsOptionLongName' property not found.";
710        return _p;
711    }
712 
713    /**
714     * Gets the value of the {@code failOnWarningsOptionShortName} property.
715     * @return Name of the 'fail-on-warnings' option.
716     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
717     */
718    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
719                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
720    private java.lang.String getFailOnWarningsOptionShortName()
721    {
722        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "failOnWarningsOptionShortName" );
723        assert _p != null : "'failOnWarningsOptionShortName' property not found.";
724        return _p;
725    }
726 
727    /**
728     * Gets the value of the {@code helpCommandName} property.
729     * @return The name of the command used to request help.
730     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
731     */
732    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
733                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
734    private java.lang.String getHelpCommandName()
735    {
736        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "helpCommandName" );
737        assert _p != null : "'helpCommandName' property not found.";
738        return _p;
739    }
740 
741    /**
742     * Gets the value of the {@code leftPad} property.
743     * @return The number of characters of padding to be prefixed to each line.
744     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
745     */
746    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
747                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
748    private int getLeftPad()
749    {
750        final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "leftPad" );
751        assert _p != null : "'leftPad' property not found.";
752        return _p.intValue();
753    }
754 
755    /**
756     * Gets the value of the {@code verboseOptionLongName} property.
757     * @return Long name of the 'verbose' option.
758     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
759     */
760    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
761                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
762    private java.lang.String getVerboseOptionLongName()
763    {
764        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "verboseOptionLongName" );
765        assert _p != null : "'verboseOptionLongName' property not found.";
766        return _p;
767    }
768 
769    /**
770     * Gets the value of the {@code verboseOptionShortName} property.
771     * @return Name of the 'verbose' option.
772     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
773     */
774    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
775                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
776    private java.lang.String getVerboseOptionShortName()
777    {
778        final java.lang.String _p = (java.lang.String) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "verboseOptionShortName" );
779        assert _p != null : "'verboseOptionShortName' property not found.";
780        return _p;
781    }
782 
783    /**
784     * Gets the value of the {@code width} property.
785     * @return The number of characters per line for the usage statement.
786     * @throws org.jomc.ObjectManagementException if getting the property instance fails.
787     */
788    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
789                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
790    private int getWidth()
791    {
792        final java.lang.Integer _p = (java.lang.Integer) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "width" );
793        assert _p != null : "'width' property not found.";
794        return _p.intValue();
795    }
796    // </editor-fold>
797    // SECTION-END
798    // SECTION-START[Messages]
799    // <editor-fold defaultstate="collapsed" desc=" Generated Messages ">
800 
801    /**
802     * Gets the text of the {@code commandLineInfo} message.
803     * <p><b>Templates</b><br/><table>
804     * <tr><td valign="top">English:</td><td valign="top"><pre>Command line:
805     * {0}</pre></td></tr>
806     * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Kommandozeile:
807     * {0}</pre></td></tr>
808     * </table></p>
809     * @param locale The locale of the message to return.
810     * @param commandLine Format argument.
811     * @return The text of the {@code commandLineInfo} message.
812     *
813     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
814     */
815    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
816                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
817    private String getCommandLineInfoMessage( final java.util.Locale locale, final java.lang.String commandLine )
818    {
819        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "commandLineInfo", locale, commandLine );
820        assert _m != null : "'commandLineInfo' message not found.";
821        return _m;
822    }
823 
824    /**
825     * Gets the text of the {@code debugOption} message.
826     * <p><b>Templates</b><br/><table>
827     * <tr><td valign="top">English:</td><td valign="top"><pre>Enables debug output.</pre></td></tr>
828     * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Aktiviert Diagnose-Ausgaben.</pre></td></tr>
829     * </table></p>
830     * @param locale The locale of the message to return.
831     * @return The text of the {@code debugOption} message.
832     *
833     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
834     */
835    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
836                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
837    private String getDebugOptionMessage( final java.util.Locale locale )
838    {
839        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "debugOption", locale );
840        assert _m != null : "'debugOption' message not found.";
841        return _m;
842    }
843 
844    /**
845     * Gets the text of the {@code failOnWarningsOption} message.
846     * <p><b>Templates</b><br/><table>
847     * <tr><td valign="top">English:</td><td valign="top"><pre>Exit with failure on warnings.</pre></td></tr>
848     * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Bei Warnungen Fehler melden.</pre></td></tr>
849     * </table></p>
850     * @param locale The locale of the message to return.
851     * @return The text of the {@code failOnWarningsOption} message.
852     *
853     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
854     */
855    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
856                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
857    private String getFailOnWarningsOptionMessage( final java.util.Locale locale )
858    {
859        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "failOnWarningsOption", locale );
860        assert _m != null : "'failOnWarningsOption' message not found.";
861        return _m;
862    }
863 
864    /**
865     * Gets the text of the {@code illegalArguments} message.
866     * <p><b>Templates</b><br/><table>
867     * <tr><td valign="top">English:</td><td valign="top"><pre>Illegal arguments. Type &raquo;jomc {0} {1}&laquo; for further information.</pre></td></tr>
868     * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Ung&uuml;ltige Argumente. Geben Sie &raquo;jomc {0} {1}&laquo; f&uuml;r weitere Informationen ein.</pre></td></tr>
869     * </table></p>
870     * @param locale The locale of the message to return.
871     * @param command Format argument.
872     * @param helpCommandName Format argument.
873     * @return The text of the {@code illegalArguments} message.
874     *
875     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
876     */
877    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
878                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
879    private String getIllegalArgumentsMessage( final java.util.Locale locale, final java.lang.String command, final java.lang.String helpCommandName )
880    {
881        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "illegalArguments", locale, command, helpCommandName );
882        assert _m != null : "'illegalArguments' message not found.";
883        return _m;
884    }
885 
886    /**
887     * Gets the text of the {@code usage} message.
888     * <p><b>Templates</b><br/><table>
889     * <tr><td valign="top">English:</td><td valign="top"><pre>Type &raquo;jomc &lt;command&gt; {0}&laquo; for further information.</pre></td></tr>
890     * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Geben Sie &raquo;jomc &lt;Befehl&gt; {0}&laquo; f&uuml;r weitere Informationen ein.</pre></td></tr>
891     * </table></p>
892     * @param locale The locale of the message to return.
893     * @param helpCommandName Format argument.
894     * @return The text of the {@code usage} message.
895     *
896     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
897     */
898    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
899                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
900    private String getUsageMessage( final java.util.Locale locale, final java.lang.String helpCommandName )
901    {
902        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "usage", locale, helpCommandName );
903        assert _m != null : "'usage' message not found.";
904        return _m;
905    }
906 
907    /**
908     * Gets the text of the {@code verboseOption} message.
909     * <p><b>Templates</b><br/><table>
910     * <tr><td valign="top">English:</td><td valign="top"><pre>Enables verbose output.</pre></td></tr>
911     * <tr><td valign="top">Deutsch:</td><td valign="top"><pre>Aktiviert ausf&uuml;hrliche Ausgaben.</pre></td></tr>
912     * </table></p>
913     * @param locale The locale of the message to return.
914     * @return The text of the {@code verboseOption} message.
915     *
916     * @throws org.jomc.ObjectManagementException if getting the message instance fails.
917     */
918    @javax.annotation.Generated( value = "org.jomc.tools.JavaSources",
919                                 comments = "See http://jomc.sourceforge.net/jomc/1.0-alpha-14/jomc-tools" )
920    private String getVerboseOptionMessage( final java.util.Locale locale )
921    {
922        final String _m = org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getMessage( this, "verboseOption", locale );
923        assert _m != null : "'verboseOption' message not found.";
924        return _m;
925    }
926    // </editor-fold>
927    // SECTION-END
928}

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