Добавил:
north memphis Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
МБКС_лаб_3.pdf
Скачиваний:
0
Добавлен:
26.01.2026
Размер:
861.01 Кб
Скачать

Приложение Б. Исходный код программы работника

public class Main{

public static void main(String[] args) { Interface.show();

}

}

import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Group;

import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.image.Image;

import javafx.scene.image.ImageView; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.stage.Stage;

public class Interface extends Application {

@Override

public void start(Stage stage){

Group mainGroup = new Group();

Group textFieldGroup = new Group();

Group buttonGroup = new Group();

TextField employeeNameTF = new TextField(); employeeNameTF.setLayoutX(10); employeeNameTF.setLayoutY(10+15); employeeNameTF.setPrefSize(120,20); employeeNameTF.setPromptText("Enter name here..."); employeeNameTF.setFocusTraversable(false); mainGroup.getChildren().add(employeeNameTF);

TextField filesTF = new TextField(); filesTF.setLayoutX(10+120+10); filesTF.setLayoutY(10+15); filesTF.setPrefSize(120,20); filesTF.setPromptText("Enter files here..."); filesTF.setFocusTraversable(false); mainGroup.getChildren().add(filesTF);

TextArea outputTextArea = new TextArea(); outputTextArea.setLayoutX(10); outputTextArea.setLayoutY(70); outputTextArea.setMinHeight(400); outputTextArea.setMaxHeight(400); outputTextArea.setMinWidth(765); outputTextArea.setMaxWidth(765); outputTextArea.setWrapText(true); outputTextArea.setEditable(false); outputTextArea.setFont(Font.font("Consolas", 28)); outputTextArea.end(); mainGroup.getChildren().add(outputTextArea);

try {

new Thread(new Runnable() { public void run() { while(true) {

try { Thread.sleep(500);

} catch (InterruptedException e) { System.out.println("Thread error "+e);

}

48

Backend a = new Backend(); a.importMatrix("Matrix.txt");

outputTextArea.setText(a.getUserInfo(employeeNameTF.getText(), filesTF.getText()));

}

}

}).start();

} catch (Exception e) {

Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Error"); alert.setHeaderText("Error"); alert.setContentText("Unknown error occurred."); alert.showAndWait().ifPresent(rs -> {

if (rs == ButtonType.OK) { System.out.println("Pressed OK.");

}

});

}

Image gifImage = new Image("file:user2.gif");//or file:doc2.gif ImageView imageView = new ImageView(gifImage); imageView.setLayoutX(375);

imageView.setLayoutY(5); mainGroup.getChildren().add(imageView);

mainGroup.getChildren().addAll(buttonGroup,textFieldGroup); Scene scene = new Scene(mainGroup, Color.rgb(245,245,245)); stage.setScene(scene);

stage.setTitle("Employee");

stage.setWidth(800);

stage.setHeight(600);

stage.setResizable(false);

stage.show();

}

public static void show(){ Application.launch();

}

}

import javafx.scene.control.*; import java.util.*;

public class Backend extends Interface {

private static final Matrix matrix = new Matrix();

private static final FileNames allFileNames = new FileNames(); public void importMatrix(String path){

Backend a = new Backend(); FileWorker fw = new FileWorker(); int result = fw.importMatrix(path);

try{

if(result==0) {

if(Objects.equals(fw.parseObjects(path),null) || Objects.equals(fw.parseSubjects(path),null)){ throw new Exception();

}

matrix.clearEmployees();

allFileNames.clearFileNames();

a.parseMatrix(path);

}else{

throw new Exception();

}

}catch(Exception e){

Alert alert = new Alert(Alert.AlertType.ERROR); alert.setTitle("Error"); alert.setHeaderText("Error");

49

alert.setContentText("Cannot read file from given path."); alert.showAndWait().ifPresent(rs -> {

if (rs == ButtonType.OK) { System.out.println("Pressed OK.");

}

});

}

}

public String getUserInfo(String name, String files){ Backend a = new Backend();

String output=""; String employee="";

String accessibleFiles="";

String[] filesArray = a.removeDuplicates(files.split("(?!^)")); for(int i=0;i<matrix.matrixLength();i++){

if(Objects.equals(matrix.getEmployees().get(i).getName(),name)){ employee+=matrix.getEmployees().get(i).getName();

for(int n=0;n<matrix.getEmployees().get(i).getFileNames().size();n++){ for(int m=0;m<filesArray.length;m++){

if(Objects.equals(matrix.getEmployees().get(i).getFileNames().get(n),filesArray[m])){ accessibleFiles+=matrix.getEmployees().get(i).getFileNames().get(n)+" ";

}

}

}

}

}

if(Objects.equals(employee,"")){

output="User with name \""+name+"\" cannot be found."; }else{

if(Objects.equals(accessibleFiles,"")){

output="User \""+employee+"\" have no access to listed file(s)"; }else{

output="User \""+employee+"\" have access to file(s) \""+accessibleFiles+"\"";

}

}

return output;

}

public String[] removeDuplicates(String[] array){ Arrays.sort(array);

String current = array[0]; boolean found = false; String str="";

for(int i = 0; i < array.length; i++) {

if (Objects.equals(current, array[i]) && !found) { found = true;

} else if (!Objects.equals(current, array[i])) { str+=current+" ";

current = array[i]; found = false;

}

}

str+=current+" ";

return str.split(" ");

}

public static ArrayList removeArrayListDuplicates(ArrayList array){ Collections.sort(array);

String current = (String) array.get(0); boolean found = false;

String str="";

for(int i = 0; i < array.size(); i++) {

if (Objects.equals(current, array.get(i)) && !found) { found = true;

50

} else if (!Objects.equals(current, array.get(i))) { str+=current+" ";

current = (String) array.get(i); found = false;

}

}

str+=current+" ";

ArrayList<String> result = new ArrayList<>(); Collections.addAll(result,str.split(" ")); return result;

}

public void parseMatrix(String path){

//парсить текстовый файл, заносить содержимое в объекты matrix и allFileNames FileWorker fw = new FileWorker();

ArrayList<String> subjects = fw.parseSubjects(path); ArrayList<String> objects = fw.parseObjects(path);

if(Objects.equals(subjects,null) || Objects.equals(objects,null)) return;

for(int i=0;i<subjects.size();i++){

matrix.addEmployee(new Employee(subjects.get(i),objects.get(i).split("(?!^)")));

}

String str = "";

for(int i=0; i<objects.size(); i++){ str+=objects.get(i);

}

allFileNames.addFiles(removeDuplicates(str.split("(?!^)")));

}

}

import java.io.*;

import java.util.ArrayList;

public class FileWorker {

public ArrayList<String> parseSubjects(String path){ ArrayList<String> result = new ArrayList<>();

try(BufferedReader br = new BufferedReader(new FileReader(path)))

{

String s; while((s=br.readLine())!=null){

result.add(s.split("-")[0]);

}

}

catch(IOException ex){ System.out.println(ex.getMessage()); return null;

}

return result;

}

public ArrayList<String> parseObjects(String path){

//must include ONLY english letters (no numbers and etc.) - done ArrayList<String> result = new ArrayList<>();

try(BufferedReader br = new BufferedReader(new FileReader(path)))

{

String s; while((s=br.readLine())!=null){

String temp=""; try {

temp = s.split("-")[1];

String[] splitted = temp.split("(?!^)"); ArrayList<String> tempEngOnly = new ArrayList<>(); for(int i=0;i<splitted.length;i++){

51

if((splitted[i]+splitted[i]).matches("^[a-zA-Z][a-zA-Z\\s]+$")){ tempEngOnly.add(splitted[i]);

}

}

temp="";

for(int i=0; i<tempEngOnly.size();i++){ temp+=tempEngOnly.get(i);

}

result.add(temp);

}catch (ArrayIndexOutOfBoundsException e){ System.out.println(e+": this user have no file accesses."); result.add("");

}

}

}

catch(IOException ex){ System.out.println(ex.getMessage()); return null;

}

return result;

}

public int importMatrix(String path){

try(BufferedReader br = new BufferedReader(new FileReader(path)))

{

//чтение строки

String s; if((s=br.readLine())!=null){

;

}

}

catch(IOException ex){ System.out.println(ex.getMessage()); return 1;

}

return 0;

}

}

import java.util.ArrayList; import java.util.Collections; public class FileNames {

private ArrayList<String> fileNames = new ArrayList<>();

public void addFiles(String[] array){ if(array.length==0) return; Collections.addAll(this.fileNames, array);

this.fileNames=Backend.removeArrayListDuplicates(this.fileNames);

}

public int size(){

return this.fileNames.size();

}

public String get(int index){

return this.fileNames.get(index);

}

public void clearFileNames(){ this.fileNames=new ArrayList<>();

}

}

public class Employee { private String name;

private FileNames fileNames = new FileNames();

public Employee(String name, String[] files){ this.name=name;

52

if(files!=null) { this.fileNames.addFiles(files);

}else{

this.fileNames.addFiles(new String[]{""});

}

}

public String getName(){ return this.name;

}

public FileNames getFileNames(){ return this.fileNames;

}

}

import java.util.ArrayList;

public class Matrix {

private ArrayList<Employee> employees = new ArrayList<>();

public ArrayList<Employee> getEmployees(){ return employees;

}

public void addEmployee(Employee employee){ this.employees.add(employee);

}

public int matrixLength(){ return employees.size();

}

public void clearEmployees(){ this.employees=new ArrayList<>();

}

}

53

Соседние файлы в предмете Модели безопасности компьютерных систем