View Javadoc
1   /*
2    * Copyright (C) 2009 Christian Schulte <cs@schulte.it>
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *   o Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the following disclaimer.
11   *
12   *   o Redistributions in binary form must reproduce the above copyright
13   *     notice, this list of conditions and the following disclaimer in
14   *     the documentation and/or other materials provided with the
15   *     distribution.
16   *
17   * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
18   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
19   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
20   * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $JOMC: AbstractSourceFileProcessorCommand.java 5215 2016-04-24 06:54:04Z schulte $
29   *
30   */
31  package org.jomc.cli.commands;
32  
33  import java.util.logging.Level;
34  import org.apache.commons.cli.CommandLine;
35  import org.jomc.tools.SourceFileProcessor;
36  
37  // SECTION-START[Documentation]
38  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
39  /**
40   * {@code SourceFileProcessor} based command implementation.
41   *
42   * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
43   */
44  public abstract class AbstractSourceFileProcessorCommand extends AbstractJomcToolCommand
45  {
46  
47      /**
48       * Creates a new {@code AbstractSourceFileProcessorCommand} instance.
49       */
50      public AbstractSourceFileProcessorCommand()
51      {
52          super();
53      }
54  
55      @Override
56      public org.apache.commons.cli.Options getOptions()
57      {
58          final org.apache.commons.cli.Options options = super.getOptions();
59          options.addOption( Options.SOURCE_FILE_PROCESSOR_CLASSNAME_OPTION );
60          options.addOption( Options.NO_SOURCE_PROCESSING_OPTION );
61          options.addOption( Options.SOURCE_DIRECTORY_OPTION );
62          return options;
63      }
64  
65      /**
66       * Creates a new {@code SourceFileProcessor} instance taking a command line.
67       *
68       * @param commandLine The command line to process.
69       *
70       * @return A new {@code SourceFileProcessor} instance as specified by the given command line or {@code null}, if
71       * creating a new instance fails.
72       *
73       * @throws NullPointerException if {@code commandLine} is {@code null}.
74       * @throws CommandExecutionException if creating a new instance fails.
75       */
76      protected SourceFileProcessor createSourceFileProcessor( final CommandLine commandLine )
77          throws CommandExecutionException
78      {
79          if ( commandLine == null )
80          {
81              throw new NullPointerException( "commandLine" );
82          }
83  
84          final String className =
85              commandLine.hasOption( Options.SOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
86                  ? commandLine.getOptionValue( Options.SOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
87                  : SourceFileProcessor.class.getName();
88  
89          return this.createJomcTool( className, SourceFileProcessor.class, commandLine );
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      protected final void executeCommand( final CommandLine commandLine ) throws CommandExecutionException
96      {
97          if ( commandLine == null )
98          {
99              throw new NullPointerException( "commandLine" );
100         }
101 
102         if ( commandLine.hasOption( Options.NO_SOURCE_PROCESSING_OPTION.getOpt() ) )
103         {
104             this.log( Level.INFO, Messages.getMessage( "sourceProcessingDisabled" ), null );
105         }
106         else
107         {
108             this.processSourceFiles( commandLine );
109         }
110     }
111 
112     /**
113      * Processes source files.
114      *
115      * @param commandLine The command line to execute.
116      *
117      * @throws CommandExecutionException if processing source files fails.
118      */
119     protected abstract void processSourceFiles( final CommandLine commandLine ) throws CommandExecutionException;
120 
121 }