87 lines
2.9 KiB
Java
87 lines
2.9 KiB
Java
package envoy.client.util;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
import envoy.event.IssueProposal;
|
|
|
|
/**
|
|
* Provides methods to handle outgoing issues.
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
public final class IssueUtil {
|
|
|
|
private static final Pattern removeBackslashes = Pattern.compile("\\\\");
|
|
private static final Pattern escapeQuotes = Pattern.compile("\"");
|
|
|
|
private IssueUtil() {}
|
|
|
|
/**
|
|
* Creates a new {@code IssueProposal} from the given data.
|
|
*
|
|
* @param title the proposed title of the issue
|
|
* @param description the proposed description of the issue
|
|
* @param username the user who submitted the issue. Should be
|
|
* {@code null} if he does not want to be named. * @param
|
|
* isBug
|
|
* @param isBug whether this issue is a bug or a feature
|
|
* @return a sanitized IssueProposal that should not fail to be sent
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
public static IssueProposal createIssueProposal(String title, String description, String username, boolean isBug) {
|
|
title = sanitizeIssueTitle(title);
|
|
description = sanitizeIssueDescription(description, username);
|
|
return new IssueProposal(title, description, isBug);
|
|
|
|
}
|
|
|
|
/**
|
|
* Escapes quotes and removes backslashes for a suggested issue title.
|
|
*
|
|
* @param title the title to sanitize
|
|
* @return the sanitized title
|
|
* @since Envoy Client v0.2-beta
|
|
*/
|
|
private static String sanitizeIssueTitle(String title) {
|
|
|
|
// Remove ALL backslashes as they are only error prone
|
|
title = removeBackslashes.matcher(title).replaceAll("");
|
|
|
|
// Escape quotes
|
|
title = escapeQuotes.matcher(title).replaceAll("\\\\\"");
|
|
return title;
|
|
}
|
|
|
|
/**
|
|
* Normalizes line breaks, <br>
|
|
* removes all backslashes, <br>
|
|
* escapes quotes and<br>
|
|
* appends the user name to the issue description if requested.
|
|
*
|
|
* @param description the description to sanitize
|
|
* @param username the user who submitted the issue. Should be
|
|
* {@code null} if he does not want to be named.
|
|
* @return the sanitized description
|
|
* @since Envoy Client v0.2-beta
|
|
* @apiNote the String returned might not be sanitized in case multiple
|
|
* backslashes are preceding a quote.
|
|
*/
|
|
public static String sanitizeIssueDescription(String description, String username) {
|
|
|
|
// Trim and replace line breaks by <br> tags
|
|
description = description.trim().replaceAll(System.getProperty("line.separator"), "<br>");
|
|
|
|
// Append user name if requested
|
|
if (username != null) description += String.format("<br>Submitted by user %s.", username);
|
|
|
|
// Remove ALL backslashes as they are only error prone
|
|
description = removeBackslashes.matcher(description).replaceAll("");
|
|
|
|
// Escape all quotes to avoid prematurely ending the string
|
|
description = escapeQuotes.matcher(description).replaceAll("\\\\\"");
|
|
return description;
|
|
}
|
|
}
|