Skip to main content
summaryrefslogtreecommitdiffstats
blob: d887d821ddbe8b71cdfc5d9b5668739051c20bda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*******************************************************************************
 * Copyright (c) 2004, 2007 Mylyn project committers and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.core;

/**
 * Utility class to handle many text quoting scenarios
 * <p>
 * Each line of text is quoted individually and wrapped, according to the {@link lineSize} attribute
 * <p>
 * The wrapping policy is the following:
 * <p>
 * <ol>
 * <li>A substring of {@link lineSize} characters is extracted and examined
 * <li>If the next character after the substring is a blank space, the substring is quoted
 * <li>If don't, the substring is searched backwards for a blank space; if one is found, the substring until the blank
 * space is quoted
 * <li>If no blank space is found, the entire substring is quoted
 * <li>The remaining of substring + line are reevaluated on step 1
 * </ol>
 * 
 * @author Willian Mitsuda
 */
public class CommentQuoter {

	public static final int DEFAULT_WRAP_SIZE = 80;

	private final int lineSize;

	public CommentQuoter() {
		this(DEFAULT_WRAP_SIZE);
	}

	public CommentQuoter(int lineSize) {
		this.lineSize = lineSize;
	}

	/**
	 * Quote a text, wrapping if necessary
	 */
	public String quote(String text) {
		StringBuilder sb = new StringBuilder(text.length() + text.length() / lineSize);

		String[] lines = text.split("\n");
		for (String line : lines) {
			if (line.trim().equals("")) {
				sb.append("> \n");
				continue;
			}

			int pos = 0;
			while (pos < line.length()) {
				int wrapPos = pos + lineSize;
				if (wrapPos < line.length()) {
					// Try to find a space to wrap the line
					while (wrapPos > pos) {
						char wrapChar = line.charAt(wrapPos);
						if (Character.isSpaceChar(wrapChar)) {
							break;
						}
						wrapPos--;
					}
					if (wrapPos == pos) {
						// There is no space; don't break the line and find the
						// next space after the limit...
						wrapPos = pos + lineSize;
						while (wrapPos < line.length()) {
							if (Character.isSpaceChar(line.charAt(wrapPos))) {
								break;
							}
							wrapPos++;
						}
					}

					// Extract the substring and recalculate the next search
					// start point
					String wrappedLine = line.substring(pos, wrapPos).trim();
					sb.append("> " + wrappedLine + "\n");
					pos = wrapPos + 1;
				} else {
					sb.append("> " + line.substring(pos).trim() + "\n");
					break;
				}
			}
		}

		return sb.toString();
	}

}

Back to the top