001/*
002 * Copyright (C) 2009 Christian Schulte <cs@schulte.it>
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without
006 * modification, are permitted provided that the following conditions
007 * are met:
008 *
009 *   o Redistributions of source code must retain the above copyright
010 *     notice, this list of conditions and the following disclaimer.
011 *
012 *   o Redistributions in binary form must reproduce the above copyright
013 *     notice, this list of conditions and the following disclaimer in
014 *     the documentation and/or other materials provided with the
015 *     distribution.
016 *
017 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 * $JOMC: AbstractSourceFileProcessorCommand.java 5215 2016-04-24 06:54:04Z schulte $
029 *
030 */
031package org.jomc.cli.commands;
032
033import java.util.logging.Level;
034import org.apache.commons.cli.CommandLine;
035import org.jomc.tools.SourceFileProcessor;
036
037// SECTION-START[Documentation]
038// <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
039/**
040 * {@code SourceFileProcessor} based command implementation.
041 *
042 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
043 */
044public abstract class AbstractSourceFileProcessorCommand extends AbstractJomcToolCommand
045{
046
047    /**
048     * Creates a new {@code AbstractSourceFileProcessorCommand} instance.
049     */
050    public AbstractSourceFileProcessorCommand()
051    {
052        super();
053    }
054
055    @Override
056    public org.apache.commons.cli.Options getOptions()
057    {
058        final org.apache.commons.cli.Options options = super.getOptions();
059        options.addOption( Options.SOURCE_FILE_PROCESSOR_CLASSNAME_OPTION );
060        options.addOption( Options.NO_SOURCE_PROCESSING_OPTION );
061        options.addOption( Options.SOURCE_DIRECTORY_OPTION );
062        return options;
063    }
064
065    /**
066     * Creates a new {@code SourceFileProcessor} instance taking a command line.
067     *
068     * @param commandLine The command line to process.
069     *
070     * @return A new {@code SourceFileProcessor} instance as specified by the given command line or {@code null}, if
071     * creating a new instance fails.
072     *
073     * @throws NullPointerException if {@code commandLine} is {@code null}.
074     * @throws CommandExecutionException if creating a new instance fails.
075     */
076    protected SourceFileProcessor createSourceFileProcessor( final CommandLine commandLine )
077        throws CommandExecutionException
078    {
079        if ( commandLine == null )
080        {
081            throw new NullPointerException( "commandLine" );
082        }
083
084        final String className =
085            commandLine.hasOption( Options.SOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
086                ? commandLine.getOptionValue( Options.SOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
087                : SourceFileProcessor.class.getName();
088
089        return this.createJomcTool( className, SourceFileProcessor.class, commandLine );
090    }
091
092    /**
093     * {@inheritDoc}
094     */
095    protected final void executeCommand( final CommandLine commandLine ) throws CommandExecutionException
096    {
097        if ( commandLine == null )
098        {
099            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}