View Javadoc

1   /*
2    PlantUMLDependencyOutputOption.java
3    Creation date : 2/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.cli.main.option.output;
26  
27  import static java.util.logging.Level.FINE;
28  import static java.util.logging.Logger.getLogger;
29  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.BASE_DIRECTORY_OPTION_NULL_ERROR;
30  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.DISPLAY_NAME_OPTION_NULL_ERROR;
31  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.DISPLAY_PACKAGE_NAME_OPTION_NULL_ERROR;
32  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.DISPLAY_TYPE_OPTION_NULL_ERROR;
33  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.EXCLUDE_OPTION_NULL_ERROR;
34  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.INCLUDE_OPTION_NULL_ERROR;
35  import static net.sourceforge.plantumldependency.cli.constants.log.ErrorConstants.PROGRAMMING_LANGUAGE_OPTION_NULL_ERROR;
36  import static net.sourceforge.plantumldependency.common.constants.CharacterConstants.SPACE_CHAR;
37  import static net.sourceforge.plantumldependency.common.utils.check.ParameterCheckerUtils.checkNull;
38  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.buildLogString;
39  import static net.sourceforge.plantumldependency.common.utils.log.LogUtils.setLevelOfAllLoggersAndHandlers;
40  import static net.sourceforge.plantumldependency.commoncli.constants.log.ErrorConstants.VERBOSE_OPTION_NULL_ERROR;
41  import static net.sourceforge.plantumldependency.commoncli.constants.log.FineConstants.OPTION_NOT_SPECIFIED_FINE;
42  import static net.sourceforge.plantumldependency.commoncli.option.status.OptionStatus.ACTIVE_OPTIONAL_OPTION_STATUS;
43  
44  import java.io.File;
45  import java.util.Set;
46  import java.util.logging.Level;
47  import java.util.logging.Logger;
48  import java.util.regex.Pattern;
49  
50  import net.sourceforge.plantumldependency.cli.main.option.basedirectory.PlantUMLDependencyBaseDirectoryOption;
51  import net.sourceforge.plantumldependency.cli.main.option.display.name.PlantUMLDependencyDisplayNameOption;
52  import net.sourceforge.plantumldependency.cli.main.option.display.packagename.PlantUMLDependencyDisplayPackageNameOption;
53  import net.sourceforge.plantumldependency.cli.main.option.display.type.PlantUMLDependencyDisplayTypeOption;
54  import net.sourceforge.plantumldependency.cli.main.option.display.type.argument.DisplayType;
55  import net.sourceforge.plantumldependency.cli.main.option.exclude.PlantUMLDependencyExcludeOption;
56  import net.sourceforge.plantumldependency.cli.main.option.include.PlantUMLDependencyIncludeOption;
57  import net.sourceforge.plantumldependency.cli.main.option.programminglanguage.PlantUMLDependencyProgrammingLanguageOption;
58  import net.sourceforge.plantumldependency.cli.main.option.programminglanguage.argument.ProgrammingLanguage;
59  import net.sourceforge.plantumldependency.commoncli.command.CommandLine;
60  import net.sourceforge.plantumldependency.commoncli.exception.CommandLineException;
61  import net.sourceforge.plantumldependency.commoncli.option.argument.impl.file.ExistingOrNotFileOptionArgumentImpl;
62  import net.sourceforge.plantumldependency.commoncli.option.execution.ExecutableOption;
63  import net.sourceforge.plantumldependency.commoncli.option.execution.OptionExecution;
64  import net.sourceforge.plantumldependency.commoncli.option.impl.output.OutputOption;
65  import net.sourceforge.plantumldependency.commoncli.option.impl.verbose.VerboseLevelOption;
66  
67  import org.apache.tools.ant.types.FileSet;
68  
69  /**
70   * The output option, allowing to specify an output file, processing the input source files and
71   * generating the plantUML description. <i>Note : no option should have the same main or secondary
72   * names</i>.
73   *
74   * @author Benjamin Croizet (<a href="mailto:graffity2199@yahoo.fr>graffity2199@yahoo.fr</a>)
75   * @since 1.0.0
76   * @version 1.4.0
77   */
78  public class PlantUMLDependencyOutputOption extends OutputOption implements ExecutableOption {
79  
80      /** Serial version UID. */
81      private static final long serialVersionUID = -1629517125349614965L;
82  
83      /** The class logger. */
84      private static final transient Logger LOGGER = getLogger(PlantUMLDependencyOutputOption.class.getName());
85  
86      /** The verbose option. */
87      private VerboseLevelOption verboseLevelOption;
88  
89      /** The programming language option. */
90      private PlantUMLDependencyProgrammingLanguageOption programmingLanguageOption;
91  
92      /** The include option. */
93      private PlantUMLDependencyIncludeOption includeOption;
94  
95      /** The exclude option. */
96      private PlantUMLDependencyExcludeOption excludeOption;
97  
98      /** The display type option. */
99      private PlantUMLDependencyDisplayTypeOption displayTypeOption;
100 
101     /** The display package name option. */
102     private PlantUMLDependencyDisplayPackageNameOption displayPackageNameOption;
103 
104     /** The display name option. */
105     private PlantUMLDependencyDisplayNameOption displayNameOption;
106 
107     /** The base directory option. */
108     private PlantUMLDependencyBaseDirectoryOption baseDirOption;
109 
110     /** The option priority. */
111     private int priority;
112 
113     /**
114      * Default constructor.
115      *
116      * @param verboseLvlOpt
117      *            the {@link VerboseLevelOption} instance, to display log information, mustn't be
118      *            <code>null</code>.
119      * @param programmingLanguageOpt
120      *            the {@link PlantUMLDependencyProgrammingLanguageOption} instance, to know what is
121      *            the programming language to parse, mustn't be <code>null</code>.
122      * @param includeOpt
123      *            the {@link PlantUMLDependencyIncludeOption} instance, to know input files to
124      *            include for parsing, mustn't be <code>null</code>.
125      * @param excludeOpt
126      *            the {@link PlantUMLDependencyExcludeOption} instance, to know exclude files which
127      *            mustn't be parsed, mustn't be <code>null</code>.
128      * @param displayTypeOpt
129      *            the {@link PlantUMLDependencyDisplayTypeOption} instance, to know which types to
130      *            display in the plantUML generated file, mustn't be <code>null</code>.
131      * @param displayPackageNameOpt
132      *            the {@link PlantUMLDependencyDisplayTypeOption} instance, to know which package
133      *            name to display in the plantUML generated file, mustn't be <code>null</code>.
134      * @param displayNameOpt
135      *            the {@link PlantUMLDependencyDisplayTypeOption} instance, to know which package
136      *            name to display in the plantUML generated file, mustn't be <code>null</code>.
137      * @param baseDirOpt
138      *            the {@link PlantUMLDependencyBaseDirectoryOption} instance, to know the base
139      *            directory where to look for source files, mustn't be <code>null</code>.
140      * @param optionPriority
141      *            the option priority as an integer. <i>Note : the priority must be unique amongst
142      *            all options</i>.
143      * @since 1.0.0
144      */
145     public PlantUMLDependencyOutputOption(final VerboseLevelOption verboseLvlOpt,
146             final PlantUMLDependencyProgrammingLanguageOption programmingLanguageOpt,
147             final PlantUMLDependencyIncludeOption includeOpt, final PlantUMLDependencyExcludeOption excludeOpt,
148             final PlantUMLDependencyDisplayTypeOption displayTypeOpt,
149             final PlantUMLDependencyDisplayPackageNameOption displayPackageNameOpt,
150             final PlantUMLDependencyDisplayNameOption displayNameOpt,
151             final PlantUMLDependencyBaseDirectoryOption baseDirOpt, final int optionPriority) {
152         super(new ExistingOrNotFileOptionArgumentImpl(true), new StringBuilder(
153                 "To specify the output file path where to generate the PlantUML description."), SPACE_CHAR,
154                 ACTIVE_OPTIONAL_OPTION_STATUS);
155         setVerboseLevelOption(verboseLvlOpt);
156         setProgrammingLanguageOption(programmingLanguageOpt);
157         setIncludeOption(includeOpt);
158         setExcludeOption(excludeOpt);
159         setDisplayTypeOption(displayTypeOpt);
160         setDisplayPackageNameOption(displayPackageNameOpt);
161         setDisplayNameOption(displayNameOpt);
162         setBaseDirOption(baseDirOpt);
163         setPriority(optionPriority);
164     }
165 
166     /**
167      * Gets the value of <code>baseDirOption</code>.
168      *
169      * @return the value of <code>baseDirOption</code>.
170      * @see #setBaseDirOption(PlantUMLDependencyBaseDirectoryOption)
171      * @since 1.0.0
172      */
173     private PlantUMLDependencyBaseDirectoryOption getBaseDirOption() {
174         return baseDirOption;
175     }
176 
177     /**
178      * Gets the value of <code>displayNameOption</code>.
179      *
180      * @return the value of <code>displayNameOption</code>.
181      * @see #setDisplayNameOption(PlantUMLDependencyDisplayNameOption)
182      * @since 1.4.0
183      */
184     private PlantUMLDependencyDisplayNameOption getDisplayNameOption() {
185         return displayNameOption;
186     }
187 
188     /**
189      * Gets the value of <code>displayPackageNameOption</code>.
190      *
191      * @return the value of <code>displayPackageNameOption</code>.
192      * @see #setDisplayPackageNameOption(PlantUMLDependencyDisplayPackageNameOption)
193      * @since 1.4.0
194      */
195     private PlantUMLDependencyDisplayPackageNameOption getDisplayPackageNameOption() {
196         return displayPackageNameOption;
197     }
198 
199     /**
200      * Gets the value of <code>displayTypeOption</code>.
201      *
202      * @return the value of <code>displayTypeOption</code>.
203      * @see #setDisplayTypeOption(PlantUMLDependencyDisplayTypeOption)
204      * @since 1.0.0
205      */
206     private PlantUMLDependencyDisplayTypeOption getDisplayTypeOption() {
207         return displayTypeOption;
208     }
209 
210     /**
211      * Gets the value of <code>excludeOption</code>.
212      *
213      * @return the value of <code>excludeOption</code>.
214      * @see #setExcludeOption(PlantUMLDependencyExcludeOption)
215      * @since 1.0.0
216      */
217     private PlantUMLDependencyExcludeOption getExcludeOption() {
218         return excludeOption;
219     }
220 
221     /**
222      * Gets the value of <code>includeOption</code>.
223      *
224      * @return the value of <code>includeOption</code>.
225      * @see #setIncludeOption(PlantUMLDependencyIncludeOption)
226      * @since 1.0.0
227      */
228     private PlantUMLDependencyIncludeOption getIncludeOption() {
229         return includeOption;
230     }
231 
232     /**
233      * {@inheritDoc}
234      *
235      * @since 1.0.0
236      */
237     @Override
238     public int getPriority() {
239         return priority;
240     }
241 
242     /**
243      * Gets the value of <code>programmingLanguageOption</code>.
244      *
245      * @return the value of <code>programmingLanguageOption</code>.
246      * @see #setProgrammingLanguageOption(PlantUMLDependencyProgrammingLanguageOption)
247      * @since 1.0.0
248      */
249     private PlantUMLDependencyProgrammingLanguageOption getProgrammingLanguageOption() {
250         return programmingLanguageOption;
251     }
252 
253     /**
254      * Gets the value of <code>verboseLevelOption</code>.
255      *
256      * @return the value of <code>verboseLevelOption</code>.
257      * @see #setVerboseLevelOption(VerboseLevelOption)
258      * @since 1.0.0
259      */
260     private VerboseLevelOption getVerboseLevelOption() {
261         return verboseLevelOption;
262     }
263 
264     /**
265      * {@inheritDoc}
266      *
267      * @since 1.0.0
268      */
269     @Override
270     public OptionExecution parseCommandLine(final CommandLine commandLine) throws CommandLineException {
271         OptionExecution optionExecution = null;
272 
273         final Level verboseLevel = getVerboseLevelOption().findAndParseArgumentOrGetDefaultArgument(commandLine);
274         setLevelOfAllLoggersAndHandlers(verboseLevel);
275 
276         if (commandLine.isOptionSpecified(this)) {
277             final File outputFile = findAndParseArgumentOrGetDefaultArgument(commandLine);
278             final String excludes = getExcludeOption().findAndParseArgumentAsStringOrGetDefaultArgument(commandLine);
279             final FileSet includesExcludes = getIncludeOption().findAndParseArgumentOrGetDefaultArgument(commandLine);
280             includesExcludes.setExcludes(excludes);
281             final File baseDirectory = getBaseDirOption().findAndParseArgumentOrGetDefaultArgument(commandLine);
282             includesExcludes.setDir(baseDirectory);
283             final Set < DisplayType > displayTypesOptions = getDisplayTypeOption()
284                     .findAndParseArgumentOrGetDefaultArgument(commandLine);
285             final Pattern displayPackageNamePatternOpt = getDisplayPackageNameOption()
286                     .findAndParseArgumentOrGetDefaultArgument(commandLine);
287             final Pattern displayNamePatternOpt = getDisplayNameOption().findAndParseArgumentOrGetDefaultArgument(
288                     commandLine);
289             final ProgrammingLanguage programmingLanguage = getProgrammingLanguageOption()
290                     .findAndParseArgumentOrGetDefaultArgument(commandLine);
291             optionExecution = new PlantUMLDependencyOutputOptionExecution(outputFile, programmingLanguage,
292                     includesExcludes, displayTypesOptions, displayPackageNamePatternOpt, displayNamePatternOpt,
293                     getPriority());
294         } else {
295             LOGGER.log(FINE, buildLogString(OPTION_NOT_SPECIFIED_FINE, getAllNames()));
296         }
297 
298         return optionExecution;
299     }
300 
301     /**
302      * Sets the value of <code>baseDirOption</code>.
303      *
304      * @param value
305      *            the <code>baseDirOption</code> to set, can be <code>null</code>.
306      * @see #getBaseDirOption()
307      * @since 1.0.0
308      */
309     private void setBaseDirOption(final PlantUMLDependencyBaseDirectoryOption value) {
310         checkNull(value, BASE_DIRECTORY_OPTION_NULL_ERROR);
311 
312         baseDirOption = value;
313     }
314 
315     /**
316      * Sets the value of <code>displayNameOption</code>.
317      *
318      * @param value
319      *            the <code>displayNameOption</code> to set, can be <code>null</code>.
320      * @see #getDisplayNameOption()
321      * @since 1.4.0
322      */
323     private void setDisplayNameOption(final PlantUMLDependencyDisplayNameOption value) {
324         checkNull(value, DISPLAY_NAME_OPTION_NULL_ERROR);
325 
326         displayNameOption = value;
327     }
328 
329     /**
330      * Sets the value of <code>displayPackageNameOption</code>.
331      *
332      * @param value
333      *            the <code>displayPackageNameOption</code> to set, can be <code>null</code>.
334      * @see #getDisplayPackageNameOption()
335      * @since 1.4.0
336      */
337     private void setDisplayPackageNameOption(final PlantUMLDependencyDisplayPackageNameOption value) {
338         checkNull(value, DISPLAY_PACKAGE_NAME_OPTION_NULL_ERROR);
339 
340         displayPackageNameOption = value;
341     }
342 
343     /**
344      * Sets the value of <code>displayTypeOption</code>.
345      *
346      * @param value
347      *            the <code>displayTypeOption</code> to set, can be <code>null</code>.
348      * @see #getDisplayTypeOption()
349      * @since 1.0.0
350      */
351     private void setDisplayTypeOption(final PlantUMLDependencyDisplayTypeOption value) {
352         checkNull(value, DISPLAY_TYPE_OPTION_NULL_ERROR);
353 
354         displayTypeOption = value;
355     }
356 
357     /**
358      * Sets the value of <code>excludeOption</code>.
359      *
360      * @param value
361      *            the <code>excludeOption</code> to set, can be <code>null</code>.
362      * @see #getExcludeOption()
363      * @since 1.0.0
364      */
365     private void setExcludeOption(final PlantUMLDependencyExcludeOption value) {
366         checkNull(value, EXCLUDE_OPTION_NULL_ERROR);
367 
368         excludeOption = value;
369     }
370 
371     /**
372      * Sets the value of <code>includeOption</code>.
373      *
374      * @param value
375      *            the <code>includeOption</code> to set, can be <code>null</code>.
376      * @see #getIncludeOption()
377      * @since 1.0.0
378      */
379     private void setIncludeOption(final PlantUMLDependencyIncludeOption value) {
380         checkNull(value, INCLUDE_OPTION_NULL_ERROR);
381 
382         includeOption = value;
383     }
384 
385     /**
386      * Sets the value of <code>priority</code>.
387      *
388      * @param value
389      *            the <code>priority</code> to set.
390      * @see #getPriority()
391      * @since 1.0.0
392      */
393     private void setPriority(final int value) {
394         priority = value;
395     }
396 
397     /**
398      * Sets the value of <code>programmingLanguageOption</code>.
399      *
400      * @param value
401      *            the <code>programmingLanguageOption</code> to set, can be <code>null</code>.
402      * @see #getProgrammingLanguageOption()
403      * @since 1.0.0
404      */
405     private void setProgrammingLanguageOption(final PlantUMLDependencyProgrammingLanguageOption value) {
406         checkNull(value, PROGRAMMING_LANGUAGE_OPTION_NULL_ERROR);
407 
408         programmingLanguageOption = value;
409     }
410 
411     /**
412      * Sets the value of <code>verboseLevelOption</code>.
413      *
414      * @param value
415      *            the <code>verboseLevelOption</code> to set, can be <code>null</code>.
416      * @see #getVerboseLevelOption()
417      * @since 1.0.0
418      */
419     private void setVerboseLevelOption(final VerboseLevelOption value) {
420         checkNull(value, VERBOSE_OPTION_NULL_ERROR);
421 
422         verboseLevelOption = value;
423     }
424 }