All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.email.encoding.PlainTextEncoding Maven / Gradle / Ivy

Go to download

A collection of basic java utility classes that provide basic features for a standalone/simple JEE application. Backed by a Cassandra, MySQL, or SQLite database, it provides, web page templates, user and group management, and a searchable online audit log of all user activity.

There is a newer version: 1.5.4
Show newest version
/**
 * Creative commons Attribution-NonCommercial license.
 *
 * http://creativecommons.org/licenses/by-nc/2.5/au/deed.en_GB
 *
 * NO WARRANTY IS GIVEN OR IMPLIED, USE AT YOUR OWN RISK.
 */
package base.email.encoding;

public class PlainTextEncoding extends Encoding{

	/**
	 * Remove CR and LF characters from the start and end of a string.
	 *
	 * @param line String to trim
	 * @return String with all CR and LF's removed from the start and end
	 */
	private String trimCrlf(String line) {
		while(line.endsWith("\n") || line.endsWith("\r")) {
			line = line.substring(0, line.length()-1);
		}
		while(line.startsWith("\n") || line.startsWith("\r")) {
			line = line.substring(1, line.length()-1);
		}
		return line;
	}

	/**
	 * Ensure all lines consistently end with CRLF, and ensure the full
	 * stop (period) character is escaped properly.
	 */
	public String encode(String message) {
		if(message == null || message == "") {
			return message;
		}

		int crlfCount = countSubstring(message, "\r\n");
		int crCount = countSubstring(message, "\r");
		int lfCount = countSubstring(message, "\n");
		String splitBy = "\n";

		if(crlfCount > crCount && crlfCount > lfCount) {
			splitBy = "\r\n";
		}
		if(crCount > crlfCount && crCount > lfCount) {
			splitBy = "\r";
		}
		if(lfCount > crCount && lfCount > crlfCount) {
			splitBy = "\n";
		}

		StringBuffer result = new StringBuffer();
		for(String line : message.split(splitBy)) {
			line = trimCrlf(line);
			if(line.matches("^\\.+$")) {
				line = line + ".";
			}
			result.append(line);
			result.append("\r\n");
		}

		return result.toString();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy