Added logging
This commit is contained in:
@@ -8,6 +8,9 @@ package com.mattrixwv.cipherstream.polysubstitution;
|
||||
import java.util.ArrayList;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
|
||||
import com.mattrixwv.cipherstream.exceptions.InvalidKeywordException;
|
||||
@@ -15,6 +18,8 @@ import com.mattrixwv.cipherstream.exceptions.InvalidBaseException;
|
||||
|
||||
|
||||
public class Trifid{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Trifid.class);
|
||||
|
||||
//A class representing the location of a character in the grid
|
||||
private class CharLocation{
|
||||
private int x;
|
||||
@@ -39,7 +44,7 @@ public class Trifid{
|
||||
}
|
||||
}
|
||||
|
||||
//Variables
|
||||
//Fields
|
||||
private String inputString; //The message that needs to be encoded/decoded
|
||||
private String outputString; //The encoded/decoded message
|
||||
private String keyword; //The keyword used to create the grid
|
||||
@@ -61,6 +66,8 @@ public class Trifid{
|
||||
throw new InvalidCharacterException("Fill in must not be a letter");
|
||||
}
|
||||
|
||||
logger.debug("Setting fill in {}", fillIn);
|
||||
|
||||
//Save the fillIn character
|
||||
this.fillIn = fillIn;
|
||||
}
|
||||
@@ -71,25 +78,35 @@ public class Trifid{
|
||||
throw new InvalidKeywordException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
|
||||
//Change everything to uppercase
|
||||
logger.debug("Removing case");
|
||||
keyword = keyword.toUpperCase();
|
||||
|
||||
//Remove everything except capital letters
|
||||
logger.debug("Removing all invalid characters");
|
||||
keyword = keyword.replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
|
||||
//Add all letters in the alphabet and fillIn to the key
|
||||
logger.debug("Appending entire alphabet to keyword");
|
||||
keyword += "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + fillIn;
|
||||
|
||||
//Remove all duplicate characters
|
||||
logger.debug("Removing duplicated characters");
|
||||
StringBuilder uniqueKey = new StringBuilder();
|
||||
keyword.chars().distinct().forEach(c -> uniqueKey.append((char)c));
|
||||
this.keyword = uniqueKey.toString();
|
||||
|
||||
logger.debug("Cleaned keyword {}", this.keyword);
|
||||
|
||||
//Create the grid from the sanitized keyword
|
||||
createGrid();
|
||||
}
|
||||
//Creates the grid from the keyword
|
||||
private void createGrid(){
|
||||
logger.debug("Creating grid from keyword");
|
||||
|
||||
for(int layerCnt = 0;layerCnt < grid.length;++layerCnt){
|
||||
for(int rowCnt = 0;rowCnt < grid[layerCnt].length;++rowCnt){
|
||||
for(int colCnt = 0;colCnt < grid[layerCnt][rowCnt].length;++colCnt){
|
||||
@@ -99,6 +116,8 @@ public class Trifid{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logger.debug("Completed grid\n{}", getGrid());
|
||||
}
|
||||
//Ensures groupSize constraints
|
||||
private void setGroupSize(int groupSize) throws InvalidBaseException{
|
||||
@@ -106,6 +125,8 @@ public class Trifid{
|
||||
throw new InvalidBaseException("Group size must be > 0");
|
||||
}
|
||||
|
||||
logger.debug("Setting group size");
|
||||
|
||||
this.groupSize = groupSize;
|
||||
}
|
||||
//Ensures inputString constraints
|
||||
@@ -115,21 +136,27 @@ public class Trifid{
|
||||
throw new InvalidInputException("Input cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing case");
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
if(Character.isWhitespace(fillIn)){
|
||||
throw new InvalidInputException("If fillIn is whitespace, whitespace must be preserved");
|
||||
}
|
||||
logger.debug("Removing whitespace");
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
inputString = inputString.replaceAll("[^a-zA-Z" + fillIn + "\\s]", "");
|
||||
}
|
||||
|
||||
//Save the string
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
@@ -139,14 +166,19 @@ public class Trifid{
|
||||
}
|
||||
//Returns the inputString with only letters
|
||||
private String getCleanInputString(){
|
||||
logger.debug("Cleaning input string for encoding");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z" + fillIn + "]", "");
|
||||
}
|
||||
//Returns the location of the given character in the grid
|
||||
private CharLocation findChar(char letter) throws InvalidCharacterException{
|
||||
logger.debug("Finding character {} in grid", letter);
|
||||
|
||||
for(int layer = 0;layer < grid.length;++layer){
|
||||
for(int row = 0;row < grid[layer].length;++row){
|
||||
for(int col = 0;col < grid[layer][row].length;++col){
|
||||
if(grid[layer][row][col] == letter){
|
||||
logger.debug("Found at {} {} {}", layer, row, col);
|
||||
|
||||
return new CharLocation(row, col, layer);
|
||||
}
|
||||
}
|
||||
@@ -168,32 +200,44 @@ public class Trifid{
|
||||
throw new InvalidCharacterException("z cannot be larget than 2");
|
||||
}
|
||||
|
||||
logger.debug("Getting character at {} {} {}", location.getZ(), location.getX(), location.getY());
|
||||
|
||||
return grid[location.getZ()][location.getX()][location.getY()];
|
||||
}
|
||||
//Adds all non-letter characters back to the output string
|
||||
private void formatOutput(String outputString){
|
||||
logger.debug("Formatting output");
|
||||
|
||||
//Keep track of where you are in the output
|
||||
int outputCnt = 0;
|
||||
StringBuilder output = new StringBuilder();
|
||||
//Check every character in the input and apply the correct rules to the output
|
||||
for(char ch : inputString.toCharArray()){
|
||||
logger.debug("Working character {}", ch);
|
||||
if(Character.isUpperCase(ch)){
|
||||
logger.debug("Formatting uppercase");
|
||||
output.append(Character.toUpperCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else if(Character.isLowerCase(ch)){
|
||||
logger.debug("Formatting lowercase");
|
||||
output.append(Character.toLowerCase(outputString.charAt(outputCnt++)));
|
||||
}
|
||||
else{
|
||||
logger.debug("Appending symbol");
|
||||
output.append(ch);
|
||||
}
|
||||
}
|
||||
|
||||
//Save the output
|
||||
logger.debug("Formatted output '{}'", output);
|
||||
this.outputString = output.toString();
|
||||
}
|
||||
//Encodes inputString using a polybius square and stores the result in outputString
|
||||
private void encode() throws InvalidCharacterException{
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
logger.debug("Conveting letters to coordinates");
|
||||
ArrayList<CharLocation> locations = new ArrayList<>();
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
//Get the location of the char in the grid
|
||||
@@ -202,6 +246,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the locations up by group
|
||||
logger.debug("Splitting locations into groups");
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
@@ -220,6 +265,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the coordinates into rows
|
||||
logger.debug("Splitting groups into rows");
|
||||
ArrayList<Integer> coordinates = new ArrayList<>(locations.size() * 3);
|
||||
for(ArrayList<CharLocation> group : groups){
|
||||
//Split the coordinates up into 3 rows
|
||||
@@ -236,6 +282,7 @@ public class Trifid{
|
||||
coordinates.addAll(cols);
|
||||
}
|
||||
//Create new locations from the rows of coordinates
|
||||
logger.debug("Converting split locations into new locations");
|
||||
ArrayList<CharLocation> newLocations = new ArrayList<>(locations.size());
|
||||
for(int cnt = 0;cnt < coordinates.size();){
|
||||
int z = coordinates.get(cnt++);
|
||||
@@ -245,6 +292,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Get the new letters from the grid
|
||||
logger.debug("Converting new locations into characters");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(CharLocation loc : newLocations){
|
||||
output.append(getChar(loc));
|
||||
@@ -255,7 +303,10 @@ public class Trifid{
|
||||
}
|
||||
//Decodes inputString using a polybius square and stores the result in outputString
|
||||
private void decode() throws InvalidCharacterException{
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Step through every element in the sanitized inputString encoding the letters
|
||||
logger.debug("Converting letters to coordinates");
|
||||
ArrayList<CharLocation> locations = new ArrayList<>();
|
||||
for(char ch : getCleanInputString().toCharArray()){
|
||||
//Get the location of the char in the grid
|
||||
@@ -264,6 +315,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the locations up by group
|
||||
logger.debug("Splitting locations into groups");
|
||||
int numGroups = inputString.length() / groupSize;
|
||||
if(numGroups == 0){
|
||||
numGroups = 1;
|
||||
@@ -282,6 +334,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Split the coordinates into rows by group and create the original grid locations
|
||||
logger.debug("Putting locations into rows");
|
||||
ArrayList<CharLocation> originalLocations = new ArrayList<>(locations.size());
|
||||
for(ArrayList<CharLocation> group : groups){
|
||||
//Read all of the coordinates from the group out into a row
|
||||
@@ -293,6 +346,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Read out the coordinates into new locations
|
||||
logger.debug("Converting locations into new locations");
|
||||
ArrayList<CharLocation> originalGroup = new ArrayList<>(group.size());
|
||||
for(int cnt = 0;cnt < group.size();++cnt){
|
||||
originalGroup.add(new CharLocation(0, 0, 0));
|
||||
@@ -311,6 +365,7 @@ public class Trifid{
|
||||
}
|
||||
|
||||
//Get the original letters from the grid
|
||||
logger.debug("Converting new locations into letters");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(CharLocation loc : originalLocations){
|
||||
output.append(getChar(loc));
|
||||
@@ -374,6 +429,8 @@ public class Trifid{
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
@@ -397,6 +454,8 @@ public class Trifid{
|
||||
return fillIn;
|
||||
}
|
||||
public String getGrid(){
|
||||
logger.debug("Creating string from grid");
|
||||
|
||||
StringJoiner layers = new StringJoiner("\n\n");
|
||||
|
||||
for(char[][] layer : grid){
|
||||
|
||||
Reference in New Issue
Block a user