Added logging
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
//MattrixwvWebsite/src/main/java/com/mattrixwv/CipherStreamJava/polySubstitution/Columnar.java
|
||||
//Mattrixwv
|
||||
// Created: 01-16-22
|
||||
//Modified: 03-03-22
|
||||
//Modified: 07-09-22
|
||||
package com.mattrixwv.cipherstream.polysubstitution;
|
||||
|
||||
|
||||
@@ -9,12 +9,18 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
public class Columnar{
|
||||
private static final Logger logger = LoggerFactory.getLogger(Columnar.class);
|
||||
|
||||
//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
|
||||
@@ -28,10 +34,13 @@ public class Columnar{
|
||||
|
||||
//Strip the inputString of all non-letter characters and change them to capitals
|
||||
private String getCleanInputString(){
|
||||
logger.debug("Cleaning input string");
|
||||
return inputString.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
}
|
||||
//Create the grid from the keyword
|
||||
private void createGridEncode(){
|
||||
logger.debug("Creating grid for encoding");
|
||||
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<>();
|
||||
grid.add(new ArrayList<>(Arrays.asList(keyword.chars().mapToObj(c -> (char)c).toArray(Character[]::new))));
|
||||
@@ -49,6 +58,8 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
private void createGridDecode(){
|
||||
logger.debug("Creating grid for decoding");
|
||||
|
||||
//Add the keyword to the first row in the array
|
||||
grid = new ArrayList<>();
|
||||
StringBuilder orderedKeyword = new StringBuilder();
|
||||
@@ -76,19 +87,29 @@ public class Columnar{
|
||||
}
|
||||
//Strips invalid characters from the string that needs encoded/decoded
|
||||
private void setInputStringEncode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for encoding");
|
||||
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing case");
|
||||
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("\\s", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Remoing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
@@ -100,6 +121,8 @@ public class Columnar{
|
||||
int charsToAdd = (cleanLength % keyword.length());
|
||||
if(charsToAdd != 0){
|
||||
charsToAdd = keyword.length() - charsToAdd;
|
||||
|
||||
logger.debug("Appending {} characters", charsToAdd);
|
||||
}
|
||||
for(int cnt = 0;cnt < charsToAdd;++cnt){
|
||||
inputStringBuilder.append(characterToAdd);
|
||||
@@ -108,6 +131,7 @@ public class Columnar{
|
||||
inputString = inputStringBuilder.toString();
|
||||
|
||||
//Save the string
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
@@ -116,19 +140,29 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
private void setInputStringDecode(String inputString) throws InvalidInputException{
|
||||
logger.debug("Setting input string for decoding");
|
||||
|
||||
//Ensure the input isn't null
|
||||
if(inputString == null){
|
||||
throw new InvalidInputException("Input must not be null");
|
||||
}
|
||||
|
||||
logger.debug("Original input string '{}'", inputString);
|
||||
|
||||
//Apply removal options
|
||||
if(!preserveCapitals){
|
||||
logger.debug("Removing case");
|
||||
|
||||
inputString = inputString.toUpperCase();
|
||||
}
|
||||
if(!preserveWhitespace){
|
||||
logger.debug("Removing whitespace");
|
||||
|
||||
inputString = inputString.replaceAll("[\\s]", "");
|
||||
}
|
||||
if(!preserveSymbols){
|
||||
logger.debug("Removing symbols");
|
||||
|
||||
inputString = inputString.replaceAll("[^a-zA-Z\\s]", "");
|
||||
}
|
||||
|
||||
@@ -182,6 +216,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save the input
|
||||
logger.debug("Cleaned input string '{}'", inputString);
|
||||
this.inputString = inputString;
|
||||
|
||||
//Ensure the string isn't blank
|
||||
@@ -191,7 +226,10 @@ public class Columnar{
|
||||
}
|
||||
//Creates the output string from the grid
|
||||
private void createOutputStringFromColumns(){
|
||||
logger.debug("Creating output string for encoding");
|
||||
|
||||
//Get the current rows of any characters that you added
|
||||
logger.debug("Getting added characters");
|
||||
ArrayList<Integer> colsAddedTo = new ArrayList<>();
|
||||
if(removePadding){
|
||||
ArrayList<Integer> cols = getKeywordOriginalLocations();
|
||||
@@ -205,6 +243,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Turn the grid into a string
|
||||
logger.debug("Turning grid into string");
|
||||
StringBuilder gridOutput = new StringBuilder();
|
||||
for(int col = 0;col < grid.get(0).size();++col){
|
||||
for(int row = 1;row < grid.size();++row){
|
||||
@@ -216,6 +255,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Preserve any remaining symbols in the string
|
||||
logger.debug("Formatting output string");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int outputLoc = 0, inputLoc = 0;inputLoc < (inputString.length() - charsAdded);){
|
||||
char inputChar = inputString.charAt(inputLoc++);
|
||||
@@ -231,10 +271,14 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
}
|
||||
private void createOutputStringFromRows(){
|
||||
logger.debug("Creating output string for decoding");
|
||||
|
||||
//Turn the grid into a string
|
||||
logger.debug("Transforming grid to a string");
|
||||
StringBuilder gridOutput = new StringBuilder();
|
||||
for(int row = 1;row < grid.size();++row){
|
||||
for(int col = 0;col < grid.get(row).size();++col){
|
||||
@@ -242,6 +286,7 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
//Remove any added characters
|
||||
logger.debug("Removing padding");
|
||||
if(removePadding){
|
||||
for(int cnt = 0;cnt < charsAdded;++cnt){
|
||||
gridOutput.deleteCharAt(gridOutput.length() - 1);
|
||||
@@ -260,6 +305,7 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
//Preserve any remaining symbols in the string
|
||||
logger.debug("Formatting output string");
|
||||
StringBuilder output = new StringBuilder();
|
||||
for(int outputLoc = 0, inputLoc = 0, row = 1, col = 0;inputLoc < inputString.length();){
|
||||
char inputChar = inputString.charAt(inputLoc++);
|
||||
@@ -288,6 +334,7 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save and return the output
|
||||
logger.debug("Decoded output string '{}'", output);
|
||||
outputString = output.toString();
|
||||
}
|
||||
//Strips invalid characters from the keyword and creates the grid
|
||||
@@ -297,9 +344,13 @@ public class Columnar{
|
||||
throw new NullPointerException("Keyword cannot be null");
|
||||
}
|
||||
|
||||
logger.debug("Original keyword {}", keyword);
|
||||
|
||||
//Strip all non-letter characters and change them to uppercase
|
||||
this.keyword = keyword.toUpperCase().replaceAll("[^A-Z]", "");
|
||||
|
||||
logger.debug("Cleaned keyword {}", keyword);
|
||||
|
||||
//Make sure a valid keyword is present
|
||||
if(this.keyword.length() < 2){
|
||||
throw new InvalidKeywordException("The keyword must contain at least 2 letters");
|
||||
@@ -311,17 +362,23 @@ public class Columnar{
|
||||
throw new InvalidCharacterException("Character to add must be a letter");
|
||||
}
|
||||
|
||||
logger.debug("Setting character to add");
|
||||
|
||||
if(!preserveCapitals){
|
||||
this.characterToAdd = Character.toUpperCase(characterToAdd);
|
||||
}
|
||||
else{
|
||||
this.characterToAdd = characterToAdd;
|
||||
}
|
||||
|
||||
logger.debug("Character to add for padding {}", characterToAdd);
|
||||
}
|
||||
//Returns a list of integers that represents the location of the characters of the keyword in alphabetic order
|
||||
private ArrayList<Integer> getKeywordAlphaLocations(){
|
||||
logger.debug("Creating an array of keyword letter locations");
|
||||
|
||||
ArrayList<Integer> orderedLocations = new ArrayList<>();
|
||||
//go through every letter and check it against the keyword
|
||||
//Go through every letter and check it against the keyword
|
||||
for(char ch = 'A';ch <= 'Z';++ch){
|
||||
for(int cnt = 0;cnt < keyword.length();++cnt){
|
||||
//If the current letter is the same as the letter we are looking for add the location to the array
|
||||
@@ -332,9 +389,12 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Return the alphabetic locations
|
||||
logger.debug("Array of keyword letters {}", orderedLocations);
|
||||
return orderedLocations;
|
||||
}
|
||||
private ArrayList<Integer> getKeywordOriginalLocations(){
|
||||
logger.debug("Creating array of original keyword locations");
|
||||
|
||||
//Figure out the order the columns are in
|
||||
ArrayList<Integer> orderedLocations = getKeywordAlphaLocations();
|
||||
//Figure out what order the columns need rearanged to
|
||||
@@ -347,10 +407,15 @@ public class Columnar{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Returning the locations
|
||||
logger.debug("Array of keyword letters {}", orderedLocations);
|
||||
return originalOrder;
|
||||
}
|
||||
//Rearanges the grid based on the list of numbers given
|
||||
private void rearangeGrid(ArrayList<Integer> listOrder){
|
||||
logger.debug("Rearanging grid");
|
||||
|
||||
//Create a new grid and make sure it is the same size as the original grid
|
||||
int numCol = grid.get(0).size();
|
||||
ArrayList<ArrayList<Character>> newGrid = new ArrayList<>(grid.size());
|
||||
@@ -366,10 +431,13 @@ public class Columnar{
|
||||
}
|
||||
|
||||
//Save the new grid
|
||||
logger.debug("New grid {}", newGrid);
|
||||
grid = newGrid;
|
||||
}
|
||||
//Encodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private void encode(){
|
||||
logger.debug("Encoding");
|
||||
|
||||
//Create the grid
|
||||
createGridEncode();
|
||||
//Figure out the new column order
|
||||
@@ -381,6 +449,8 @@ public class Columnar{
|
||||
}
|
||||
//Decodes inputString using the Columnar cipher and stores the result in outputString
|
||||
private void decode(){
|
||||
logger.debug("Decoding");
|
||||
|
||||
//Create the grid
|
||||
createGridDecode();
|
||||
ArrayList<Integer> originalOrder = getKeywordOriginalLocations();
|
||||
@@ -440,6 +510,8 @@ public class Columnar{
|
||||
|
||||
//Makes sure all variables are empty
|
||||
public void reset(){
|
||||
logger.debug("Resetting fields");
|
||||
|
||||
inputString = "";
|
||||
outputString = "";
|
||||
keyword = "";
|
||||
|
||||
Reference in New Issue
Block a user