1 /*******************************************************************************
2 * Copyright (c) 2016 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * THTH ry - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.debug.browser.utils;
14 public class Escapes {
16 public static String html(String s) {
17 if (!containsAnyOf(s, HTML_CHARS_TO_ESCAPE))
19 return batchReplace(s, HTML_CHARS_TO_ESCAPE, HTML_CHAR_REPLACEMENTS);
22 private static final char[] HTML_CHARS_TO_ESCAPE = { '&', '<', '>', '\n' };
23 private static final String[] HTML_CHAR_REPLACEMENTS = { "&", "<", ">", "<br/>" };
25 private static boolean containsAnyOf(String s, char[] chars) {
27 for (int i = 0; i < l; ++i) {
36 private static String batchReplace(String s, char[] charsToReplace, String[] replacements) {
38 int rl = charsToReplace.length;
39 StringBuilder sb = new StringBuilder(l*2);
41 for (int i = 0; i < l; ++i) {
43 for (int j = 0; j < rl; ++j) {
44 if (p == charsToReplace[j]) {
45 sb.append(replacements[j]);
54 public static void main(String[] args) {
55 System.out.println(html("><&\nfoobar\n<>"));