View Javadoc

1   /*
2    HelpOptionExecution.java
3    Creation date : 9/06/2010
4    Copyright © Benjamin Croizet (graffity2199@yahoo.fr)
5   
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License
8    or GNU Lesser General Public License as published by the
9    Free Software Foundation; either version 3 of the License,
10   or (at your option) any later version.
11  
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16  
17   You should have received copies of the GNU General Public License
18   and GNU Lesser General Public License along with this program;
19   if not, write to the Free Software Foundation, Inc.,
20   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21   http://www.fsf.org/licensing/licenses/gpl.html
22   http://www.gnu.org/licenses/lgpl.html
23   */
24  
25  package net.sourceforge.plantumldependency.commoncli.option.impl.help;
26  
27  import static java.util.logging.Level.INFO;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
30  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.JAVA_PROGRAM_NULL_ERROR;
31  
32  import java.util.logging.Logger;
33  
34  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
35  import net.sourceforge.plantumldependency.commoncli.option.execution.AbstractOptionExecution;
36  import net.sourceforge.plantumldependency.commoncli.program.JavaProgram;
37  
38  /**
39   * The default option execution associated to the "-help" option, displaying the program usage
40   * information.
41   *
42   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
43   * @since 1.3.0
44   * @version 1.3.0
45   */
46  public class HelpOptionExecution extends AbstractOptionExecution {
47  
48      /** Serial version UID. */
49      private static final long serialVersionUID = 7861414955826853050L;
50  
51      /** The class logger. */
52      private static final transient Logger LOGGER = getLogger(HelpOptionExecution.class.getName());
53  
54      /** The java program to display authors information. */
55      private JavaProgram javaProgram;
56  
57      /**
58       * Default constructor.
59       *
60       * @param program
61       *            the java program to display authors information, mustn't be <code>null</code>.
62       * @param optionPriority
63       *            the option priority as an integer. <i>Note : the priority must be unique amongst
64       *            all options executions</i>.
65       * @since 1.3.0
66       */
67      public HelpOptionExecution(final JavaProgram program, final int optionPriority) {
68          super(optionPriority);
69          setJavaProgram(program);
70      }
71  
72      /**
73       * {@inheritDoc}
74       *
75       * @since 1.3.0
76       */
77      @Override
78      public HelpOptionExecution deepClone() {
79          final HelpOptionExecution a = (HelpOptionExecution) super.deepClone();
80          a.javaProgram = getJavaProgram().deepClone();
81          return a;
82      }
83  
84      /**
85       * {@inheritDoc}
86       *
87       * @since 1.3.0
88       */
89      @Override
90      public void execute() throws CommandLineException {
91          final StringBuilder buffer = getJavaProgram().getFullUsage();
92  
93          final String bufferStr = buffer.toString();
94          System.out.println(bufferStr);
95          LOGGER.log(INFO, bufferStr);
96      }
97  
98      /**
99       * Gets the value of <code>javaProgram</code>.
100      *
101      * @return the value of <code>javaProgram</code>.
102      * @see #setJavaProgram(JavaProgram)
103      * @since 1.3.0
104      */
105     private JavaProgram getJavaProgram() {
106         return javaProgram;
107     }
108 
109     /**
110      * Sets the value of <code>javaProgram</code>.
111      *
112      * @param value
113      *            the <code>javaProgram</code> to set, can be <code>null</code>.
114      * @see #getJavaProgram()
115      * @since 1.3.0
116      */
117     private void setJavaProgram(final JavaProgram value) {
118         checkNull(value, JAVA_PROGRAM_NULL_ERROR);
119 
120         javaProgram = value;
121     }
122 
123     /**
124      * {@inheritDoc}
125      *
126      * @since 1.3.0
127      */
128     @Override
129     public String toString() {
130         return getClass().getSimpleName() + " [" + super.toString() + ", javaProgram=" + javaProgram + "]";
131     }
132 }