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: AbstractClassFileProcessorCommand.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.ClassFileProcessor;
036
037/**
038 * {@code ClassFileProcessor} based command implementation.
039 *
040 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
041 */
042public abstract class AbstractClassFileProcessorCommand extends AbstractJomcToolCommand
043{
044
045    /**
046     * Creates a new {@code AbstractClassFileProcessorCommand} instance.
047     */
048    public AbstractClassFileProcessorCommand()
049    {
050        super();
051    }
052
053    @Override
054    public org.apache.commons.cli.Options getOptions()
055    {
056        final org.apache.commons.cli.Options options = super.getOptions();
057        options.addOption( Options.CLASS_FILE_PROCESSOR_CLASSNAME_OPTION );
058        options.addOption( Options.NO_CLASS_PROCESSING_OPTION );
059        return options;
060    }
061
062    /**
063     * Creates a new {@code ClassFileProcessor} instance taking a command line.
064     *
065     * @param commandLine The command line to process.
066     *
067     * @return A new {@code ClassFileProcessor} instance as specified by the given command line.
068     *
069     * @throws NullPointerException if {@code commandLine} is {@code null}.
070     * @throws CommandExecutionException if creating a new instance fails.
071     */
072    protected ClassFileProcessor createClassFileProcessor( final CommandLine commandLine )
073        throws CommandExecutionException
074    {
075        if ( commandLine == null )
076        {
077            throw new NullPointerException( "commandLine" );
078        }
079
080        final String className =
081            commandLine.hasOption( Options.CLASS_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
082                ? commandLine.getOptionValue( Options.CLASS_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() )
083                : ClassFileProcessor.class.getName();
084
085        final ClassFileProcessor tool = this.createJomcTool( className, ClassFileProcessor.class, commandLine );
086        return tool;
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    protected final void executeCommand( final CommandLine commandLine ) throws CommandExecutionException
093    {
094        if ( commandLine == null )
095        {
096            throw new NullPointerException( "commandLine" );
097        }
098
099        if ( commandLine.hasOption( Options.NO_CLASS_PROCESSING_OPTION.getOpt() ) )
100        {
101            this.log( Level.INFO, Messages.getMessage( "classProcessingDisabled" ), null );
102        }
103        else
104        {
105            this.processClassFiles( commandLine );
106        }
107    }
108
109    /**
110     * Processes class files.
111     *
112     * @param commandLine The command line to execute.
113     *
114     * @throws CommandExecutionException if processing class files fails.
115     */
116    protected abstract void processClassFiles( final CommandLine commandLine ) throws CommandExecutionException;
117
118}