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

base.text.TextReformat Maven / Gradle / Ivy

/**
 * 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.text;

import java.util.ArrayList;
import java.util.List;

/**
 * Reformats the line length of a text buffer, such as from
 * an email. Each paragraph in the text buffer is reformatted
 * to ensure that each line is no longer than a requested
 * width.
 */
public class TextReformat {

	public String format(String text, int lineLength) {
		List paragraphs = getParagraphs(text);

		StringBuffer result = new StringBuffer();
		for(String paragraph : paragraphs) {
			result.append(resize(paragraph, lineLength));
			result.append("\n");
		}

		return result.toString();
	}

	private List getParagraphs(String text) {
		List paragraphs = new ArrayList();

		StringBuffer paragraph = new StringBuffer();
		for(String line : text.split("\\r")) {
			while(line.endsWith("\r") || line.endsWith("\n") || line.endsWith(" ") || line.endsWith("\t")) {
				line = line.substring(0, line.length()-1);
			}
			while(line.startsWith("\r") || line.startsWith("\n")) {
				line = line.substring(1);
			}
			if(line.length() == 0) {
				if(paragraph.length() > 0) {
					paragraphs.add(paragraph.toString());
					paragraph = new StringBuffer();
				}
				continue;
			}
			paragraph.append(line);
			paragraph.append("\n");
		}
		if(paragraph.length() > 0) {
			paragraphs.add(paragraph.toString());
			paragraph = new StringBuffer();
		}

		return paragraphs;
	}

	private String resize(String paragraph, int lineLength) {
		StringBuffer result = new StringBuffer();

		List lines = new ArrayList();
		for(String line : paragraph.split("\\n")) {
			lines.add(line);
		}

		while(lines.size() > 0) {
			String line = lines.remove(0);

			if(line.length() > lineLength) {
				int breakpoint = findBreakpoint(line, lineLength);
				String part1 = line.substring(0, breakpoint);
				String part2 = line.substring(breakpoint);
				if(part2.charAt(0) == ' ' || part2.charAt(0) == '\t') {
					part2 = part2.substring(1);
				}
				result.append(part1);
				result.append("\n");
				if(lines.size() > 0) {
					lines.add(0, prependText(part2, lines.remove(0)));
				} else {
					int index = firstNonWhitespace(part1);
					int p2index = firstNonWhitespace(part2);
					lines.add(0, part1.substring(0, index)+part2.substring(p2index));
				}
			} else {
				result.append(line);
				result.append("\n");
			}
		}

		return result.toString();
	}

	private int findBreakpoint(String line, int lineLength) {
		int index = lineLength;

		while(line.charAt(index) != ' ' && line.charAt(index) != '\t' && index > 0) {
			index--;
		}

		if(index == 0) {
			return lineLength;
		}

		return index;
	}

	private int firstNonWhitespace(String text) {
		int i = 0;
		for(; i < text.length(); i++) {
			if(text.charAt(i) != ' ' && text.charAt(i) != '\t') {
				return i;
			}
		}
		return i;
	}

	private String prependText(String part1, String part2) {
		int index = firstNonWhitespace(part2);
		//TODO: trim part1
		return part2.substring(0, index) + part1 + " " + part2.substring(index);
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy