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

org.fife.ui.rsyntaxtextarea.HtmlUtil Maven / Gradle / Ivy

Go to download

RSyntaxTextArea is the syntax highlighting text editor for Swing applications. Features include syntax highlighting for 40+ languages, code folding, code completion, regex find and replace, macros, code templates, undo/redo, line numbering and bracket matching.

The newest version!
/*
 * This library is distributed under a modified BSD license.  See the included
 * LICENSE file for details.
 */
package org.fife.ui.rsyntaxtextarea;

import java.awt.*;

/**
 * Utility methods useful when generating HTML representations of RSTA content.
 */
public final class HtmlUtil {

	private HtmlUtil() {
		// Do nothing (comment for Sonar)
	}


	/**
	 * Returns a string with characters that are special to HTML (such as
	 * <, > and &) replaced
	 * by their HTML escape sequences.
	 *
	 * @param s The input string.
	 * @param newlineReplacement What to replace newline characters with.
	 *        If this is null, they are simply removed.
	 * @param inPreBlock Whether this HTML will be in within pre
	 *        tags.  If this is true, spaces will be kept as-is;
	 *        otherwise, they will be converted to " ".
	 * @return The escaped version of s.
	 */
	public static String escapeForHtml(String s, String newlineReplacement,
									   boolean inPreBlock) {

		if (s==null) {
			return null;
		}
		if (newlineReplacement==null) {
			newlineReplacement = "";
		}

		// Always all   instead of an initial ' ' so we always play
		// nice when there's intermixed spaces and tabs
		String tabString = inPreBlock ? "    " : "    ";
		boolean lastWasSpace = false;

		StringBuilder sb = new StringBuilder();

		for (int i=0; i':
					sb.append(">");
					lastWasSpace = false;
					break;
				case '\'':
					sb.append("'");
					lastWasSpace = false;
					break;
				case '"':
					sb.append(""");
					lastWasSpace = false;
					break;
				case '/': // OWASP-recommended even though unnecessary
					sb.append("/");
					lastWasSpace = false;
					break;
				default:
					sb.append(ch);
					lastWasSpace = false;
					break;
			}
		}

		return sb.toString();
	}


	/**
	 * Returns a hex string for the specified color, suitable for HTML.
	 *
	 * @param c The color.
	 * @return The string representation, in the form "#rrggbb",
	 *         or null if c is null.
	 */
	public static String getHexString(Color c) {

		if (c == null) {
			return null;
		}

		StringBuilder sb = new StringBuilder("#");

		int r = c.getRed();
		if (r<16) {
			sb.append('0');
		}
		sb.append(Integer.toHexString(r));
		int g = c.getGreen();
		if (g<16) {
			sb.append('0');
		}
		sb.append(Integer.toHexString(g));
		int b = c.getBlue();
		if (b<16) {
			sb.append('0');
		}
		sb.append(Integer.toHexString(b));

		return sb.toString();
	}

	/**
	 * Returns text from a text area as HTML.  Markup is added so that the
	 * HTML represents the syntax highlighting in the editor.
	 *
	 * @param textArea The text area.
	 * @param start The start offset.
	 * @param end The end offset.
	 * @return The HTML.
	 */
	public static String getTextAsHtml(RSyntaxTextArea textArea, int start, int end) {

		// Create the selection as HTML
		StringBuilder sb = new StringBuilder("
");

		Token token = textArea.getTokenListFor(start, end);
		for (Token t = token; t != null; t = t.getNextToken()) {

			if (t.isPaintable()) {

				if (t.isSingleChar('\n')) {
					sb.append("
"); } else { sb.append(TokenUtils.tokenToHtml(textArea, t)); } } } sb.append("
"); return sb.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy