Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.33 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package os.assignment;
  7.  
  8. import java.nio.file.InvalidPathException;
  9. import java.nio.file.Paths;
  10. import java.util.ArrayList;
  11. import java.util.Scanner;
  12. import java.lang.NullPointerException;
  13. /**
  14.  *
  15.  * @author DELL
  16.  */
  17. public class Parser {
  18.  
  19.     String[] args=new String[2];
  20.     String cmd;
  21.  
  22.     public boolean parse(String input) throws NullPointerException
  23.     {
  24.         Scanner In = new Scanner(System.in);
  25.         input = In.nextLine();
  26.         ArrayList<String> Command_by2par = new ArrayList<String>();
  27.         ArrayList<String> Command_by1par = new ArrayList<String>();
  28.         ArrayList<String> Command_nopar = new ArrayList<String>();
  29.         Command_by2par.add("$cp");
  30.         Command_by2par.add("$mv");
  31.         Command_by1par.add("$cat");
  32.         Command_by1par.add("$more");
  33.         Command_by1par.add("$mkdir");
  34.         Command_by1par.add("$rmdir");
  35.         Command_by1par.add("$rm");
  36.         Command_by1par.add("$cd");
  37.         Command_nopar.add("$Is");
  38.         Command_nopar.add("$date");
  39.         Command_nopar.add("$pwd");
  40.         Command_nopar.add("$clear");
  41.         Command_nopar.add("$args");
  42.         Command_nopar.add("$help");
  43.         String[] token = input.split(" ");
  44.         int choice = 3;
  45.         for (int i = 0; i < 2; i++)
  46.             if (Command_by2par.get(i).equals(token[0])) {
  47.                 choice = 2;
  48.                 break;
  49.             }
  50.         for (int i = 0; i < 6; i++)
  51.             if (Command_by1par.get(i).equals(token[0])) {
  52.                 choice = 1;
  53.                 break;
  54.             }
  55.         for (int i = 0; i < 6; i++)
  56.             if (Command_nopar.get(i).equals(token[0])) {
  57.                 choice = 0;
  58.                 break;
  59.             }
  60.         if (choice==3)
  61.                 System.out.println("Yoy Write Wrong Command");
  62.            
  63.        
  64.         if (choice == 1 && token[1] != null) {
  65.             if(ValidPath(token[1]))
  66.                     cmd=token[0];
  67.                     args[0]=token[1];
  68.         }
  69.         else if (token[1] == null) {
  70.             System.out.println(token[0] + " command should have one parameter");
  71.         }
  72.             if (choice == 2 && token[1] != null && token[2] != null) {
  73.                 if(ValidPath(token[1]))
  74.                     cmd=token[0];
  75.                     args[0]=token[1];
  76.                     args[1]=token[2];
  77.         }
  78.             else if (token[1] == null || token[2] == null) {
  79.                 System.out.println(token[0] + " command should have two parameter");
  80.         }
  81.         if (choice == 0 && token[1] == null && token[2] == null) {
  82.                     cmd=token[0];
  83.         }
  84.         else if (token[1] == null) {
  85.             System.out.println(token[0] + " command should not have parameter");
  86.         }
  87.         System.out.println(cmd+args[0]+args[1]);
  88.         return false;
  89.     }
  90.    
  91.     public String getCmd() {
  92.         return cmd;
  93.     }
  94.  
  95.     public String[] getArguments() {
  96.         return args;
  97.     }
  98.     public static boolean ValidPath(String path) {
  99.     try {
  100.         Paths.get(path);
  101.     } catch (InvalidPathException | NullPointerException ex) {
  102.         return false;
  103.     }
  104.     return true;
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement