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

com.smartlogic.classificationserver.client.Body Maven / Gradle / Ivy

There is a newer version: 5.6.1
Show newest version
package com.smartlogic.classificationserver.client;

/**
 * Class to represent the body of the document sent to classification server
 * 
 * @author Smartlogic Semaphore
 *
 */
public class Body {
	
	private final static int MAX_LENGTH = 1000;
	
	private String value;
	/**
	 * Create the body field from a string
	 * @param value The string representation of the body of the document
	 */
	public Body(String value) {
		StringBuilder sb = new StringBuilder();
		int lastPos = 0;
		int currPos = 0;
		while ((currPos = value.indexOf('\n', lastPos)) != -1) {
			sb.append(breakText(value.substring(lastPos, currPos)));
			sb.append("\n\n");
			lastPos = currPos + 1;
		}
		sb.append(breakText(value.substring(lastPos)));
		
		this.value = sb.toString();
	}
	
	private String breakText(String input) {
		StringBuffer sb = new StringBuffer(); 
		String current = input;
		while (current.length() > MAX_LENGTH) {
			int endPos = MAX_LENGTH;
			while ((!Character.isWhitespace(current.charAt(endPos))) && (endPos > 0)) endPos--;
			if (endPos == 0) endPos = MAX_LENGTH; // No white space in first MAX_LENGTH characters, we cannot break at a whitespace
			sb.append(current.substring(0,endPos).trim());
			sb.append("\n\n");
			current = current.substring(endPos);
		}
		sb.append(current.trim());
		return sb.toString();
	}
	
	protected String getValue() {
		return value;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy