Question
refactor this code? package edu.gatech.seclass.edtext; import org.apache.commons.cli.*; import org.apache.commons.lang3.StringUtils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths;
refactor this code?
package edu.gatech.seclass.edtext; import org.apache.commons.cli.*; import org.apache.commons.lang3.StringUtils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { // Empty Main class for compiling Individual Project. // During Deliverable 1 and Deliverable 2, DO NOT ALTER THIS CLASS or implement it private static CommandLine cmdline = null; private static List readLine; private static String contents; public static void main(String[] args) throws FileNotFoundException { // Empty Skeleton Method Options opt = new Options(); CommandLineParser parser = new DefaultParser(); opt.addOption("g", false, "replaceall"); Option r = Option.builder("r").numberOfArgs(2).hasArg(true).desc("string").build(); r.setArgs(2); opt.addOption(r); opt.addOption("f", false, "string, Specify Filename"); opt.addOption("d", true, "integer, Create duplicate as specified"); opt.addOption("a", false, "Converts to ASCII characters"); opt.addOption("n", true, "integer, To add line number to the string"); opt.addOption("p", true, "string, Append prefix to the string"); try { cmdline = parser.parse(opt, args); } catch (Exception e) { usage(); return; } List arguments_list = new ArrayList<>(cmdline.getArgList()); if ((cmdline.getOptions().length) == 0 && (arguments_list.size() > 1)) { usage(); return; } if (arguments_list.size() == 0 || (cmdline.getOptions().length) == 0) { usage(); return; } int sizeofargument = arguments_list.size()-1; String file = arguments_list.get(sizeofargument); Path pathoffile = Paths.get(file); if (pathoffile==null){ usage(); return; } Scanner reader = new Scanner(file); try { readLine = Files.readAllLines(pathoffile); } catch (InvalidPathException | IOException e) { usage(); return; } try { contents = new String(Files.readAllBytes(pathoffile)); if(!contents.isEmpty()){ } if(contents.isEmpty()){ System.out.print(""); } if(contents == null ){ System.out.print(""); } } catch (InvalidPathException | FileNotFoundException e) { usage(); return; } catch (IOException e) { usage(); return; } if (contents.length() == 0 && !cmdline.hasOption("f")) { System.out.print(""); return; } if (cmdline.hasOption("g")) { if( !cmdline.hasOption("r")){ usage(); return; } } if (cmdline.hasOption("r")) { if (cmdline.getOptionValues("r").length == 0) { usage(); return; } int a1Position = cmdline.getOptionValues("r").length - 2; int a2Position = cmdline.getOptionValues("r").length - 1; String a1 = cmdline.getOptionValues("r")[a1Position]; String a2 = cmdline.getOptionValues("r")[a2Position]; if (a1 == "") { usage(); return; } else { readLine = optionReplace(a1, a2); } } if (cmdline.hasOption("a")) { readLine = optionAscii(); } if (cmdline.hasOption("p")) { int prefixPosition = cmdline.getOptionValues("p").length - 1; String pre = cmdline.getOptionValues("p")[prefixPosition]; if (pre == "") { usage(); return; } else { readLine = optionPrefix(pre); } } if (cmdline.hasOption("d")) { int dPosition = cmdline.getOptionValues("d").length-1; String arg = cmdline.getOptionValues("d")[dPosition]; try{ int d = Integer.parseInt(arg); if (d < 0||d>10) { usage(); return; } readLine = optionDuplicate(d); }catch(NumberFormatException e){ usage(); }} if (cmdline.hasOption("n")) { int nPosition = cmdline.getOptionValues("n").length-1; String nValue = cmdline.getOptionValues("n")[nPosition]; try{ int n = Integer.parseInt(nValue); if (n < 1||n>5) { usage(); return; } readLine = optionNumbering(n); }catch(NumberFormatException e){ usage(); return; }} if (cmdline.hasOption("f")) { PrintWriter pw = new PrintWriter(file); for (String l : readLine) { pw.write(l + System.getProperty("line.separator")); } pw.close(); pw.flush(); } else { for (String l : readLine) { System.out.println(l); return; } } } private static List optionNumbering(int n) { List temp = new ArrayList<>(); int i = 1; String space = " "; for (String rl : readLine) { if (n == 0) { temp.add(space + rl); } else if (String.valueOf(i).length() <= n) { String iValue = String.valueOf(i); String ln = StringUtils.leftPad(iValue, n, "0"); temp.add(ln + space + rl); } else { String iValue = String.valueOf(i); String ln = iValue.substring(iValue.length() - n); temp.add(ln + space + rl); } i++; } return temp; } private static List optionDuplicate(int d) { List temp = new ArrayList<>(); for (String dl : readLine) { for (int i=0; i optionReplace(String arg1, String arg2) { ArrayList temp = new ArrayList<>(); if (cmdline.hasOption("g")) { for (String line : readLine) { temp.add(line.replace(arg1, arg2)); } } else { for (String line : readLine) { temp.add(line.replaceFirst(Pattern.quote(arg1), Matcher.quoteReplacement(arg2))); } } return temp; } private static List optionPrefix(String arg) { ArrayList temp = new ArrayList<>(); for (String rl : readLine) { temp.add(arg + rl); } return temp; } private static List optionAscii() { ArrayList temp = new ArrayList<>(); for (String xl : readLine) { if(!xl.matches("[^x00-x7F]")){ byte[] bytes = xl.getBytes(StandardCharsets.US_ASCII); List result = new ArrayList<>(); for (byte aByte : bytes) { int ascii = (int) aByte; if (ascii < 0 || ascii >= 127) { usage(); } // byte -> int if(ascii >= 32 && asciipackage edu.gatech.seclass.edtext; import org.apache.commons.cli.*; import org.apache.commons.lang3.StringUtils; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintWriter; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { // Empty Main class for compiling Individual Project. // During Deliverable 1 and Deliverable 2, DO NOT ALTER THIS CLASS or implement it private static CommandLine cmdline = null; private static List readLine; private static String contents; public static void main(String[] args) throws FileNotFoundException { // Empty Skeleton Method Options opt = new Options(); CommandLineParser parser = new DefaultParser(); opt.addOption("g", false, "replaceall"); Option r = Option.builder("r").numberOfArgs(2).hasArg(true).desc("string").build(); r.setArgs(2); opt.addOption(r); opt.addOption("f", false, "string, Specify Filename"); opt.addOption("d", true, "integer, Create duplicate as specified"); opt.addOption("a", false, "Converts to ASCII characters"); opt.addOption("n", true, "integer, To add line number to the string"); opt.addOption("p", true, "string, Append prefix to the string"); try { cmdline = parser.parse(opt, args); } catch (Exception e) { usage(); return; } List arguments_list = new ArrayList<>(cmdline.getArgList()); if ((cmdline.getOptions().length) == 0 && (arguments_list.size() > 1)) { usage(); return; } if (arguments_list.size() == 0 || (cmdline.getOptions().length) == 0) { usage(); return; } int sizeofargument = arguments_list.size()-1; String file = arguments_list.get(sizeofargument); Path pathoffile = Paths.get(file); if (pathoffile==null){ usage(); return; } Scanner reader = new Scanner(file); try { readLine = Files.readAllLines(pathoffile); } catch (InvalidPathException | IOException e) { usage(); return; } try { contents = new String(Files.readAllBytes(pathoffile)); if(!contents.isEmpty()){ } if(contents.isEmpty()){ System.out.print(""); } if(contents == null ){ System.out.print(""); } } catch (InvalidPathException | FileNotFoundException e) { usage(); return; } catch (IOException e) { usage(); return; } if (contents.length() == 0 && !cmdline.hasOption("f")) { System.out.print(""); return; } if (cmdline.hasOption("g")) { if( !cmdline.hasOption("r")){ usage(); return; } } if (cmdline.hasOption("r")) { if (cmdline.getOptionValues("r").length == 0) { usage(); return; } int a1Position = cmdline.getOptionValues("r").length - 2; int a2Position = cmdline.getOptionValues("r").length - 1; String a1 = cmdline.getOptionValues("r")[a1Position]; String a2 = cmdline.getOptionValues("r")[a2Position]; if (a1 == "") { usage(); return; } else { readLine = optionReplace(a1, a2); } } if (cmdline.hasOption("a")) { readLine = optionAscii(); } if (cmdline.hasOption("p")) { int prefixPosition = cmdline.getOptionValues("p").length - 1; String pre = cmdline.getOptionValues("p")[prefixPosition]; if (pre == "") { usage(); return; } else { readLine = optionPrefix(pre); } } if (cmdline.hasOption("d")) { int dPosition = cmdline.getOptionValues("d").length-1; String arg = cmdline.getOptionValues("d")[dPosition]; try{ int d = Integer.parseInt(arg); if (d < 0||d>10) { usage(); return; } readLine = optionDuplicate(d); }catch(NumberFormatException e){ usage(); }} if (cmdline.hasOption("n")) { int nPosition = cmdline.getOptionValues("n").length-1; String nValue = cmdline.getOptionValues("n")[nPosition]; try{ int n = Integer.parseInt(nValue); if (n < 1||n>5) { usage(); return; } readLine = optionNumbering(n); }catch(NumberFormatException e){ usage(); return; }} if (cmdline.hasOption("f")) { PrintWriter pw = new PrintWriter(file); for (String l : readLine) { pw.write(l + System.getProperty("line.separator")); } pw.close(); pw.flush(); } else { for (String l : readLine) { System.out.println(l); return; } } } private static List optionNumbering(int n) { List temp = new ArrayList<>(); int i = 1; String space = " "; for (String rl : readLine) { if (n == 0) { temp.add(space + rl); } else if (String.valueOf(i).length() <= n) { String iValue = String.valueOf(i); String ln = StringUtils.leftPad(iValue, n, "0"); temp.add(ln + space + rl); } else { String iValue = String.valueOf(i); String ln = iValue.substring(iValue.length() - n); temp.add(ln + space + rl); } i++; } return temp; } private static List optionDuplicate(int d) { List temp = new ArrayList<>(); for (String dl : readLine) { for (int i=0; i optionReplace(String arg1, String arg2) { ArrayList temp = new ArrayList<>(); if (cmdline.hasOption("g")) { for (String line : readLine) { temp.add(line.replace(arg1, arg2)); } } else { for (String line : readLine) { temp.add(line.replaceFirst(Pattern.quote(arg1), Matcher.quoteReplacement(arg2))); } } return temp; } private static List optionPrefix(String arg) { ArrayList temp = new ArrayList<>(); for (String rl : readLine) { temp.add(arg + rl); } return temp; } private static List optionAscii() { ArrayList temp = new ArrayList<>(); for (String xl : readLine) { if(!xl.matches("[^x00-x7F]")){ byte[] bytes = xl.getBytes(StandardCharsets.US_ASCII); List result = new ArrayList<>(); for (byte aByte : bytes) { int ascii = (int) aByte; if (ascii < 0 || ascii >= 127) { usage(); } // byte -> int if(ascii >= 32 && ascii <= 126) { result.add(ascii); }else{ } } String asciiString = result.toString() .replace(",", "") .replace("[", "") .replace("]", "") ;; temp.add(asciiString + " " ); }} return temp; } private static void usage() { System.err.println("Usage: edtext [ -f | -r old new | -g | -a | -p prefix | -d n | -n width ] FILE"); } } <= 126) { result.add(ascii); }else{ } } String asciiString = result.toString() .replace(",", "") .replace("[", "") .replace("]", "") ;; temp.add(asciiString + " " ); }} return temp; } private static void usage() { System.err.println("Usage: edtext [ -f | -r old new | -g | -a | -p prefix | -d n | -n width ] FILE"); } }
Step by Step Solution
3.44 Rating (154 Votes )
There are 3 Steps involved in it
Step: 1
To refactor the code lets focus on improving readability modularity and adhering to coding best practices Heres a refactored version of the code impor...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started