Java tutorial
/** * Copyright 2015 J. Patrick Meyer * <p/> * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * <p/> * http://www.apache.org/licenses/LICENSE-2.0 * <p/> * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.itemanalysis.jmetrik.manager; import com.itemanalysis.jmetrik.command.AbstractJmetrikCommand; import com.itemanalysis.jmetrik.command.SelectAllOption; import com.itemanalysis.jmetrik.command.SelectOneOption; import org.apache.commons.io.FilenameUtils; import java.io.File; import java.util.HashSet; import java.util.Iterator; public class ExportDataCommand extends AbstractJmetrikCommand { //data option added in abstract class. The data option is the only required option. //outfile option added in abstract class. private final String optionsSelectAllName = "options"; private final String optionsHeader = "header"; private final String optionsOverwrite = "overwrite"; private final String optionsScored = "scored"; private final String optionsEmpty = "empty"; private SelectAllOption optionsSelectAll = new SelectAllOption(optionsSelectAllName); private final String delimiterOptionName = "delim"; private SelectOneOption delimiterOption = new SelectOneOption(delimiterOptionName); public ExportDataCommand() { super("export", "Export data and convert *.jmetrik file to *.csv file."); setValidOptions(); } private void setValidOptions() { setValidOptionName(optionsSelectAllName); optionsSelectAll.addPermittedArgument(optionsHeader, true); optionsSelectAll.addPermittedArgument(optionsEmpty, true); optionsSelectAll.addPermittedArgument(optionsOverwrite, false); optionsSelectAll.addPermittedArgument(optionsScored, false); setValidOptionName(delimiterOptionName); delimiterOption.addPermittedArgument(DelimiterType.COMMA.toString(), true); delimiterOption.addPermittedArgument(DelimiterType.TAB.toString(), false); delimiterOption.addPermittedArgument(DelimiterType.SEMICOLON.toString(), false); delimiterOption.addPermittedArgument(DelimiterType.COLON.toString(), false); delimiterOption.addPermittedArgument(DelimiterType.WHITESPACE.toString(), false); } //================================================================================================================================================ // GETTERS // Required options should throw an exception if not found in the option map. // Options that are not required should return their default values if not found in the option map. //================================================================================================================================================ /** * Return the type of delimiter * * @return */ public DelimiterType getDelimiterType() { DelimiterType type = DelimiterType.COMMA; if (delimiterOption.isValueSelected(DelimiterType.TAB.toString())) { type = DelimiterType.TAB; } else if (delimiterOption.isValueSelected(DelimiterType.SEMICOLON.toString())) { type = DelimiterType.SEMICOLON; } else if (delimiterOption.isValueSelected(DelimiterType.COLON.toString())) { type = DelimiterType.COLON; } else if (delimiterOption.isValueSelected(DelimiterType.WHITESPACE.toString())) { type = DelimiterType.WHITESPACE; } return type; } /** * Return the delimiter itself * * @return */ public char getDelimiter() { switch (getDelimiterType()) { case COMMA: return ','; case TAB: return '\t'; case SEMICOLON: return ';'; case COLON: return ':'; case WHITESPACE: return ' '; } return ','; } /** * If output file not specified then use the same name as the original file but change the suffix. * @return */ @Override public File getOutputFile() { File f = getOutputFile(); if (null == f) { String s = getDataFile().getAbsolutePath(); s = FilenameUtils.removeExtension(s); s += ".csv"; return new File(s); } else { return f; } } public boolean headerIncluded() { return optionsSelectAll.isValueSelected(optionsHeader); } public boolean overwrite() { return optionsSelectAll.isValueSelected(optionsOverwrite); } public boolean empty() { return optionsSelectAll.isValueSelected(optionsEmpty); } public boolean scored() { return optionsSelectAll.isValueSelected(optionsScored); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(name); sb.append(";\n"); sb.append(dataOption + " = " + getDataFile().getAbsolutePath()); sb.append(";\n"); File f = getOutputFile(); if (null != f && f.exists()) sb.append(outFileOption + " = " + f.getAbsolutePath()); sb.append(";\n"); sb.append(optionsSelectAll.toString()); sb.append(";\n"); sb.append(delimiterOption.toString()); sb.append(";\n"); return sb.toString(); } //================================================================================================================================================ // SETTERS // If option arguments are already proper objects, use setOptions(String, Object); // Otherwise, use the option-specific call to have the correct object created before adding it to the option map. //================================================================================================================================================ public void setDelimiter(String optionArgument) { delimiterOption.setSelectedArguments(optionArgument); } public void setOptionsOption(String[] value) throws IllegalArgumentException { optionsSelectAll.setSelectedArguments(value); } public boolean parse(String text) throws IllegalArgumentException { String[] commandParts = text.split(";"); if (!name.equals(commandParts[0])) return false; String optionName = ""; String optionArgument = ""; //Parse the options that are part of this command and add objects to the HashMap. for (String s : commandParts) { if (s.contains("=")) { String[] opts = s.split("="); optionName = opts[0].trim(); optionArgument = opts[1].trim(); if (!validOption(optionName)) throw new IllegalArgumentException("Option not allowed: " + optionName); //Parse data file if (dataOption.equals(optionName)) { setDataFileOption(optionArgument); } //parse output file option if (outFileOption.equals(optionName)) { setOutfileOption(optionArgument); } //Parse general options if (optionsSelectAll.isOptionName(optionName)) { String[] list = optionArgument.split(","); optionsSelectAll.setSelectedArguments(list); } //Parse delimiter options if (delimiterOption.isOptionName(optionName)) { delimiterOption.setSelectedArguments(optionArgument); } } } return true; } }