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: AbstractResourceFileProcessorCommand.java 5299 2016-08-30 01:50:13Z schulte $ 029 * 030 */ 031package org.jomc.cli.commands; 032 033import java.util.logging.Level; 034import org.apache.commons.cli.CommandLine; 035import org.jomc.tools.ResourceFileProcessor; 036 037/** 038 * {@code ResourceFileProcessor} based command implementation. 039 * 040 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 041 */ 042public abstract class AbstractResourceFileProcessorCommand extends AbstractJomcToolCommand 043{ 044 045 /** 046 * Creates a new {@code AbstractResourceFileProcessorCommand} instance. 047 */ 048 public AbstractResourceFileProcessorCommand() 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.RESOURCE_FILE_PROCESSOR_CLASSNAME_OPTION ); 058 options.addOption( Options.NO_RESOURCE_PROCESSING_OPTION ); 059 options.addOption( Options.RESOURCE_DIRECTORY_OPTION ); 060 return options; 061 } 062 063 /** 064 * Creates a new {@code ResourceFileProcessor} instance taking a command line. 065 * 066 * @param commandLine The command line to process. 067 * 068 * @return A new {@code ResourceFileProcessor} instance as specified by the given command line or {@code null}, if 069 * creating a new instance fails. 070 * 071 * @throws NullPointerException if {@code commandLine} is {@code null}. 072 * @throws CommandExecutionException if creating a new instance fails. 073 */ 074 protected ResourceFileProcessor createResourceFileProcessor( final CommandLine commandLine ) 075 throws CommandExecutionException 076 { 077 if ( commandLine == null ) 078 { 079 throw new NullPointerException( "commandLine" ); 080 } 081 082 final String className = 083 commandLine.hasOption( Options.RESOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() ) 084 ? commandLine.getOptionValue( Options.RESOURCE_FILE_PROCESSOR_CLASSNAME_OPTION.getOpt() ) 085 : ResourceFileProcessor.class.getName(); 086 087 final ResourceFileProcessor tool = this.createJomcTool( className, ResourceFileProcessor.class, commandLine ); 088 tool.setResourceBundleDefaultLocale( this.getLocale( commandLine ) ); 089 return tool; 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_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}