Java tutorial
/* * Copyright (c) 2014 Patrick Meyer * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.itemanalysis.jmetrik.manager; import com.itemanalysis.jmetrik.command.*; import com.itemanalysis.jmetrik.data.SpecialCodesString; import org.apache.commons.io.FilenameUtils; import java.io.File; /** * Obtains options that allows the program to convert a delimited file to *.jmetrik format. */ public class ImportDataCommand 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 SelectAllOption optionsSelectAll = new SelectAllOption(optionsSelectAllName); private final String spcodesOption = "spcodes"; private final String spScoresOption = "spscores"; private final String delimiterOptionName = "delim"; private SelectOneOption delimiterOption = new SelectOneOption(delimiterOptionName); private SpecialCodesString specialCodesString = new SpecialCodesString(); public ImportDataCommand() { super("import", "Import data and convert to *.jmetrik format."); setValidOptions(); } private void setValidOptions() { setValidOptionName(optionsSelectAllName); optionsSelectAll.addPermittedArgument(optionsHeader, true); optionsSelectAll.addPermittedArgument(optionsOverwrite, 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); setValidOptionName(spcodesOption); setValidOptionName(spScoresOption); } //================================================================================================================================================ // 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 String 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() { if (null == outputFile) { String s = getDataFile().getAbsolutePath(); s = FilenameUtils.removeExtension(s); s += ".jmetrik"; return new File(s); } else { return outputFile; } } public boolean headerIncluded() { return optionsSelectAll.isValueSelected(optionsHeader); } public boolean overwrite() { return optionsSelectAll.isValueSelected(optionsOverwrite); } public SpecialCodesString getSpecialCodesString() { return specialCodesString; } @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"); if (specialCodesString != null) { sb.append(spcodesOption + " = " + specialCodesString.getCodesText()); sb.append(";\n"); sb.append(spScoresOption + " = " + specialCodesString.getScoringText()); 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); } //Set special codes if (spcodesOption.equals(optionName)) { String[] list = optionArgument.split(","); specialCodesString.setCodes(list); } //Set scoring values if (spScoresOption.equals(optionName)) { String[] list = optionArgument.split(","); specialCodesString.setScores(list); } } } return true; } }