Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package os.assignment;
- import java.nio.file.InvalidPathException;
- import java.nio.file.Paths;
- import java.util.ArrayList;
- import java.util.Scanner;
- import java.lang.NullPointerException;
- /**
- *
- * @author DELL
- */
- public class Parser {
- String[] args=new String[2];
- String cmd;
- public boolean parse(String input) throws NullPointerException
- {
- Scanner In = new Scanner(System.in);
- input = In.nextLine();
- ArrayList<String> Command_by2par = new ArrayList<String>();
- ArrayList<String> Command_by1par = new ArrayList<String>();
- ArrayList<String> Command_nopar = new ArrayList<String>();
- Command_by2par.add("$cp");
- Command_by2par.add("$mv");
- Command_by1par.add("$cat");
- Command_by1par.add("$more");
- Command_by1par.add("$mkdir");
- Command_by1par.add("$rmdir");
- Command_by1par.add("$rm");
- Command_by1par.add("$cd");
- Command_nopar.add("$Is");
- Command_nopar.add("$date");
- Command_nopar.add("$pwd");
- Command_nopar.add("$clear");
- Command_nopar.add("$args");
- Command_nopar.add("$help");
- String[] token = input.split(" ");
- int choice = 3;
- for (int i = 0; i < 2; i++)
- if (Command_by2par.get(i).equals(token[0])) {
- choice = 2;
- break;
- }
- for (int i = 0; i < 6; i++)
- if (Command_by1par.get(i).equals(token[0])) {
- choice = 1;
- break;
- }
- for (int i = 0; i < 6; i++)
- if (Command_nopar.get(i).equals(token[0])) {
- choice = 0;
- break;
- }
- if (choice==3)
- System.out.println("Yoy Write Wrong Command");
- if (choice == 1 && token[1] != null) {
- if(ValidPath(token[1]))
- cmd=token[0];
- args[0]=token[1];
- }
- else if (token[1] == null) {
- System.out.println(token[0] + " command should have one parameter");
- }
- if (choice == 2 && token[1] != null && token[2] != null) {
- if(ValidPath(token[1]))
- cmd=token[0];
- args[0]=token[1];
- args[1]=token[2];
- }
- else if (token[1] == null || token[2] == null) {
- System.out.println(token[0] + " command should have two parameter");
- }
- if (choice == 0 && token[1] == null && token[2] == null) {
- cmd=token[0];
- }
- else if (token[1] == null) {
- System.out.println(token[0] + " command should not have parameter");
- }
- System.out.println(cmd+args[0]+args[1]);
- return false;
- }
- public String getCmd() {
- return cmd;
- }
- public String[] getArguments() {
- return args;
- }
- public static boolean ValidPath(String path) {
- try {
- Paths.get(path);
- } catch (InvalidPathException | NullPointerException ex) {
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement