Added logging

This commit is contained in:
Mattrixwv
2022-07-09 16:55:32 -04:00
parent e9c8397b86
commit 2d7382ba8f
44 changed files with 2341 additions and 1265 deletions

View File

@@ -1,17 +1,25 @@
//CipherStreamJava/src/test/java/com/mattrixwv/CipherStreamJava/Baconian.java
//Mattrixwv
// Created: 01-12-22
//Modified: 01-16-22
//Modified: 07-09-22
package com.mattrixwv.cipherstream.monosubstitution;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mattrixwv.cipherstream.exceptions.InvalidCharacterException;
import com.mattrixwv.cipherstream.exceptions.InvalidInputException;
public class Baconian{
private static final Logger logger = LoggerFactory.getLogger(Baconian.class);
//Conversions
private static final ArrayList<String> code = new ArrayList<>(Arrays.asList(
"aaaaa", "aaaab", "aaaba", "aaabb", "aabaa", "aabab", "aabba","aabbb", "abaaa", "abaaa", "abaab", "ababa", "ababb", //A-M
"abbaa", "abbab", "abbba", "abbbb", "baaaa", "baaab", "baaba", "baabb", "baabb", "babaa", "babab", "babba", "babbb" //N-Z
@@ -26,14 +34,20 @@ public class Baconian{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for encoding '{}'", inputString);
//Remove all whitespace and symbols
inputString = inputString.replaceAll("[^A-Za-z]", "");
if(!preserveCapitals){
logger.debug("Removing case");
inputString = inputString.toLowerCase();
}
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input must contain at least 1 letter");
}
@@ -43,17 +57,26 @@ public class Baconian{
throw new NullPointerException("Input cannot be null");
}
logger.debug("Setting input string for decoding '{}'", inputString);
if(!preserveCapitals){
logger.debug("Removing case");
inputString = inputString.toLowerCase();
}
//Check each Baconian Cipher letter for validity
logger.debug("Ensuring all 'letters' contain 5 characters");
for(String str : inputString.split(" ")){
logger.debug("Current 'letter' {}", str);
//Make sure each letter contains 5 characters
if(str.length() != 5){
throw new InvalidCharacterException("All Baconian letters contain exactly 5 characters: " + str);
}
//Make sure the letter contains only a's and b's
logger.debug("Replacing all non-abAB characters");
String temp = str.replaceAll("[^abAB]", "");
if(!temp.equals(str)){
throw new InvalidCharacterException("Baconian letters contain only a's and b's: " + str);
@@ -62,54 +85,76 @@ public class Baconian{
this.inputString = inputString;
logger.debug("Cleaned input string '{}'", inputString);
if(this.inputString.isBlank()){
throw new InvalidInputException("Input cannot be empty");
}
}
//Encodes the inputString and stores the result in outputString
private String encode(){
logger.debug("Encoding");
StringJoiner output = new StringJoiner(" ");
//Go through every character in the inputString and encode it
for(char ch : inputString.toCharArray()){
logger.debug("Working character {}", ch);
//Convert the character to a binary string with A = 0
String binary = null;
if(Character.isUpperCase(ch)){
logger.debug("Encoding uppercase");
binary = code.get(ch - 'A').toUpperCase();
}
else{
logger.debug("Encoding lowercase");
binary = code.get(ch - 'a').toLowerCase();
}
//Add the encoded character to the output
logger.debug("Output 'letter' {}", binary);
output.add(binary);
}
//Save and return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
//Decodes the inputString and stores the result in outputString
private String decode(){
logger.debug("Decoding");
StringBuilder output = new StringBuilder();
//Go through every Baconian Cipher character in the inputString and decode it
for(String baconianCharacter : inputString.split(" ")){
logger.debug("Working letter {}", baconianCharacter);
//Get the location of the Baconian character in the array
int location = code.indexOf(baconianCharacter.toLowerCase());
logger.debug("Location of letter {}", location);
//Convert the Baconian character to an ASCII character
char ch;
if(Character.isUpperCase(baconianCharacter.charAt(0))){
logger.debug("Decoding uppercase");
ch = (char)(location + 'A');
}
else{
logger.debug("Decoding lowercase");
ch = (char)(location + 'a');
}
//Add the decoded character to the output
logger.debug("Decoded character {}", ch);
output.append(ch);
}
//Save and return the output
logger.debug("Saving output string '{}'", output);
outputString = output.toString();
return outputString;
}
@@ -145,6 +190,8 @@ public class Baconian{
}
//Makes sure all of the variables are empty
public void reset(){
logger.debug("Resetting fields");
inputString = "";
outputString = "";
}