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: AbstractResourceFileProcessorCommand.java 5299 2016-08-30 01:50:13Z 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.ResourceFileProcessor;
36
37 /**
38 * {@code ResourceFileProcessor} based command implementation.
39 *
40 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
41 */
42 public abstract class AbstractResourceFileProcessorCommand extends AbstractJomcToolCommand
43 {
44
45 /**
46 * Creates a new {@code AbstractResourceFileProcessorCommand} instance.
47 */
48 public AbstractResourceFileProcessorCommand()
49 {
50 super();
51 }
52
53 @Override
54 public org.apache.commons.cli.Options getOptions()
55 {
56 final org.apache.commons.cli.Options options = super.getOptions();
57 options.addOption( Options.RESOURCE_FILE_PROCESSOR_CLASSNAME_OPTION );
58 options.addOption( Options.NO_RESOURCE_PROCESSING_OPTION );
59 options.addOption( Options.RESOURCE_DIRECTORY_OPTION );
60 return options;
61 }
62
63 /**
64 * Creates a new {@code ResourceFileProcessor} instance taking a command line.
65 *
66 * @param commandLine The command line to process.
67 *
68 * @return A new {@code ResourceFileProcessor} instance as specified by the given command line or {@code null}, if
69 * creating a new instance fails.
70 *
71 * @throws NullPointerException if {@code commandLine} is {@code null}.
72 * @throws CommandExecutionException if creating a new instance fails.
73 */
74 protected ResourceFileProcessor createResourceFileProcessor( final CommandLine commandLine )
75 throws CommandExecutionException
76 {
77 if ( commandLine == null )
78 {
79 throw new NullPointerException( "commandLine" );
80 }
81
82 final String className =
83 commandLine.hasOption( Options.RESOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
84 ? commandLine.getOptionValue( Options.RESOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
85 : ResourceFileProcessor.class.getName();
86
87 final ResourceFileProcessor tool = this.createJomcTool( className, ResourceFileProcessor.class, commandLine );
88 tool.setResourceBundleDefaultLocale( this.getLocale( commandLine ) );
89 return tool;
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_RESOURCE_PROCESSING_OPTION.getOpt() ) )
103 {
104 this.log( Level.INFO, Messages.getMessage( "resourceProcessingDisabled" ), null );
105 }
106 else
107 {
108 this.processResourceFiles( commandLine );
109 }
110 }
111
112 /**
113 * Processes resource files.
114 *
115 * @param commandLine The command line to execute.
116 *
117 * @throws CommandExecutionException if processing resource files fails.
118 */
119 protected abstract void processResourceFiles( final CommandLine commandLine ) throws CommandExecutionException;
120
121 }