View Javadoc

1   /*
2    AboutOptionExecution.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.about;
26  
27  import static java.util.logging.Level.INFO;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.HYPHEN_CHAR;
30  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.LINE_CHAR;
31  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
32  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.TAB_CHAR;
33  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
34  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.JAVA_PROGRAM_NULL_ERROR;
35  
36  import java.util.logging.Logger;
37  
38  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
39  import net.sourceforge.plantumldependency.commoncli.option.execution.AbstractOptionExecution;
40  import net.sourceforge.plantumldependency.commoncli.program.JavaProgram;
41  
42  /**
43   * The default option execution associated to the "-about" option, displaying the program authors
44   * information.
45   *
46   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
47   * @since 1.3.0
48   * @version 1.3.0
49   */
50  public class AboutOptionExecution extends AbstractOptionExecution {
51  
52      /** Serial version UID. */
53      private static final long serialVersionUID = 5829000495584908599L;
54  
55      /** The class logger. */
56      private static final transient Logger LOGGER = getLogger(AboutOptionExecution.class.getName());
57  
58      /** The java program to display authors information. */
59      private JavaProgram javaProgram;
60  
61      /**
62       * Default constructor.
63       *
64       * @param program
65       *            the java program to display authors information, mustn't be <code>null</code>.
66       * @param optionPriority
67       *            the option priority as an integer. <i>Note : the priority must be unique amongst
68       *            all options executions</i>.
69       * @since 1.3.0
70       */
71      public AboutOptionExecution(final JavaProgram program, final int optionPriority) {
72          super(optionPriority);
73          setJavaProgram(program);
74      }
75  
76      /**
77       * {@inheritDoc}
78       *
79       * @since 1.3.0
80       */
81      @Override
82      public AboutOptionExecution deepClone() {
83          final AboutOptionExecution a = (AboutOptionExecution) super.deepClone();
84          a.javaProgram = getJavaProgram().deepClone();
85          return a;
86      }
87  
88      /**
89       * {@inheritDoc}
90       *
91       * @since 1.3.0
92       */
93      @Override
94      public void execute() throws CommandLineException {
95          final StringBuilder buffer = new StringBuilder(getJavaProgram().getName());
96          buffer.append(" authors :");
97          buffer.append(LINE_CHAR);
98  
99          for (final String author : getJavaProgram().getAuthors()) {
100             buffer.append(LINE_CHAR);
101             buffer.append(TAB_CHAR);
102             buffer.append(HYPHEN_CHAR);
103             buffer.append(SPACE_CHAR);
104             buffer.append(author);
105         }
106 
107         buffer.append(LINE_CHAR);
108         buffer.append(LINE_CHAR);
109         buffer.append(getJavaProgram().getName());
110         buffer.append(" licenses :");
111         buffer.append(LINE_CHAR);
112 
113         for (final String license : getJavaProgram().getLicenseNames()) {
114             buffer.append(LINE_CHAR);
115             buffer.append(TAB_CHAR);
116             buffer.append(HYPHEN_CHAR);
117             buffer.append(SPACE_CHAR);
118             buffer.append(license);
119         }
120 
121         final String bufferStr = buffer.toString();
122         System.out.println(bufferStr);
123         LOGGER.log(INFO, bufferStr);
124     }
125 
126     /**
127      * Gets the value of <code>javaProgram</code>.
128      *
129      * @return the value of <code>javaProgram</code>.
130      * @see #setJavaProgram(JavaProgram)
131      * @since 1.3.0
132      */
133     private JavaProgram getJavaProgram() {
134         return javaProgram;
135     }
136 
137     /**
138      * Sets the value of <code>javaProgram</code>.
139      *
140      * @param value
141      *            the <code>javaProgram</code> to set, can be <code>null</code>.
142      * @see #getJavaProgram()
143      * @since 1.3.0
144      */
145     private void setJavaProgram(final JavaProgram value) {
146         checkNull(value, JAVA_PROGRAM_NULL_ERROR);
147 
148         javaProgram = value;
149     }
150 
151     /**
152      * {@inheritDoc}
153      *
154      * @since 1.3.0
155      */
156     @Override
157     public String toString() {
158         return getClass().getSimpleName() + " [" + super.toString() + ",javaProgram=" + javaProgram + "]";
159     }
160 }