org.apache.commons.lang3.StringEscapeUtils.escapeHtml4()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(8.8k)|赞(0)|评价(0)|浏览(747)

本文整理了Java中org.apache.commons.lang3.StringEscapeUtils.escapeHtml4()方法的一些代码示例,展示了StringEscapeUtils.escapeHtml4()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringEscapeUtils.escapeHtml4()方法的具体详情如下:
包路径:org.apache.commons.lang3.StringEscapeUtils
类名称:StringEscapeUtils
方法名:escapeHtml4

StringEscapeUtils.escapeHtml4介绍

[英]Escapes the characters in a String using HTML entities.

For example:

"bread" & "butter"
becomes:

"bread" & "butter".

Supports all known HTML 4.0 entities, including funky accents. Note that the commonly used apostrophe escape character (') is not a legal entity and so is not supported).
[中]使用HTML实体转义字符串中的字符。
例如:
"bread" & "butter"
变成:
"bread" & "butter".
支持所有已知的HTML4.0实体,包括时髦的口音。请注意,常用的撇号转义字符(')不是法律实体,因此不受支持)。

代码示例

代码示例来源:origin: springside/springside4

/**
 * Html转码,将字符串转码为符合HTML4格式的字符串.
 * 
 * 比如 "bread" & "butter" 转化为 "bread" & "butter"
 */
public static String escapeHtml(String html) {
  return StringEscapeUtils.escapeHtml4(html);
}

代码示例来源:origin: Javen205/IJPay

/**
 * 转义HTML用于安全过滤
 * @param html
 * @return {String}
 */
public static String escapeHtml(String html) {
  return StringEscapeUtils.escapeHtml4(html);
}

代码示例来源:origin: ninjaframework/ninja

private String escape(String value) {
  return StringEscapeUtils.escapeHtml4(value);
}

代码示例来源:origin: gocd/gocd

public void escapeAndAdd(String text) {
  buffer.append(StringEscapeUtils.escapeHtml4(text));
}

代码示例来源:origin: gocd/gocd

private String textOnLink(Matcher matcher) {
  return StringEscapeUtils.escapeHtml4(matcher.group());
}

代码示例来源:origin: gocd/gocd

public static ServerHealthState error(String message, String description, HealthStateType type) {
  return new ServerHealthState(HealthStateLevel.ERROR, type, escapeHtml4(message), escapeHtml4(description));
}

代码示例来源:origin: gocd/gocd

public static ServerHealthState warning(String message, String description, HealthStateType healthStateType, long milliSeconds) {
  return new ServerHealthState(HealthStateLevel.WARNING, healthStateType, escapeHtml4(message), escapeHtml4(description), milliSeconds);
}

代码示例来源:origin: gocd/gocd

public static ServerHealthState warning(String message, String description, HealthStateType healthStateType) {
  return new ServerHealthState(HealthStateLevel.WARNING, healthStateType, escapeHtml4(message), escapeHtml4(description));
}

代码示例来源:origin: stanfordnlp/CoreNLP

private static void outputByWriter(Consumer<StringWriter> printer, PrintWriter out) {
 StringWriter output = new StringWriter();
 printer.accept(output);
 output.flush();
 String escapedXml = StringEscapeUtils.escapeHtml4(output.toString());
 String[] lines = escapedXml.split("\n");
 out.print("<div><pre>");
 for (String line : lines) {
  int numSpaces = 0;
  while (numSpaces < line.length() && line.charAt(numSpaces) == ' ') {
   out.print("&nbsp;");
   ++numSpaces;
  }
  out.print(line.substring(numSpaces));
  out.print("\n");
 }
 out.print("</pre></div>");
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
  public String generate(Request request, int status, String reasonPhrase, String description, Throwable exception) {
    final ImmutableMap.Builder<String, Object> modelBuilder = ImmutableMap.builder();
    modelBuilder.put("reason", StringEscapeUtils.escapeHtml4(reasonPhrase));

    if (description != null) {
      modelBuilder.put("description", StringEscapeUtils.escapeHtml4(description));
    }

    if (exception != null) {
      String stacktrace = "";
      try (final StringWriter stringWriter = new StringWriter();
         final PrintWriter printWriter = new PrintWriter(stringWriter)) {
        exception.printStackTrace(printWriter);
        printWriter.flush();
        stringWriter.flush();
        stacktrace = stringWriter.toString();
      } catch (IOException ignored) {
        // Ignore
      }
      modelBuilder
          .put("exception", StringEscapeUtils.escapeHtml4(exception.getMessage()))
          .put("stacktrace", StringEscapeUtils.escapeHtml4(stacktrace));
    }

    return engine.transform(template, modelBuilder.build());
  }
}

代码示例来源:origin: gocd/gocd

private String dynamicLink(Matcher matcher) {
  String linkWithRealId = StringEscapeUtils.escapeHtml4(link.replace("${ID}", id(matcher)));
  return String.format("<a href=\"%s\" target=\"story_tracker\">%s</a>", linkWithRealId, textOnLink(matcher));
}

代码示例来源:origin: kiegroup/optaplanner

public String getSolverConfigAsHtmlEscapedXml() {
  // TODO reuse a single XStream instance for the entire report
  XStream xStream = XStreamXmlSolverFactory.buildXStream();
  xStream.setMode(XStream.NO_REFERENCES);
  String xml = xStream.toXML(solverConfig);
  return StringEscapeUtils.escapeHtml4(xml);
}

代码示例来源:origin: stanfordnlp/CoreNLP

if (dateError) {
 out.println("<br><br>Warning: unparseable date " +
       StringEscapeUtils.escapeHtml4(dateString));
  displayAnnotation(out, query, anno, includeOffsets);
 } else {
  out.println("<br><br>Error creating annotator for " + StringEscapeUtils.escapeHtml4(annotatorType));

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEscapeHtml() {
  for (final String[] element : HTML_ESCAPES) {
    final String message = element[0];
    final String expected = element[1];
    final String original = element[2];
    assertEquals(message, expected, StringEscapeUtils.escapeHtml4(original));
    final StringWriter sw = new StringWriter();
    try {
      StringEscapeUtils.ESCAPE_HTML4.translate(original, sw);
    } catch (final IOException e) {
    }
    final String actual = original == null ? null : sw.toString();
    assertEquals(message, expected, actual);
  }
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
   * Tests // https://issues.apache.org/jira/browse/LANG-480
   */
  @Test
  public void testEscapeHtmlHighUnicode() {
    // this is the utf8 representation of the character:
    // COUNTING ROD UNIT DIGIT THREE
    // in Unicode
    // codepoint: U+1D362
    final byte[] data = new byte[] { (byte)0xF0, (byte)0x9D, (byte)0x8D, (byte)0xA2 };

    final String original = new String(data, Charset.forName("UTF8"));

    final String escaped = StringEscapeUtils.escapeHtml4( original );
    assertEquals( "High Unicode should not have been escaped", original, escaped);

    final String unescaped = StringEscapeUtils.unescapeHtml4( escaped );
    assertEquals( "High Unicode should have been unchanged", original, unescaped);

// TODO: I think this should hold, needs further investigation
//        String unescapedFromEntity = StringEscapeUtils.unescapeHtml4( "&#119650;" );
//        assertEquals( "High Unicode should have been unescaped", original, unescapedFromEntity);
  }

代码示例来源:origin: gocd/gocd

@Test
public void shouldEscapeTheWholeCommentIfNoneIsMatched() {
  trackingTool = new DefaultCommentRenderer("", "");
  String toRender = "some <string>";
  String result = trackingTool.render(toRender);
  assertThat(result, is(StringEscapeUtils.escapeHtml4(toRender)));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEscapeHtmlVersions() throws Exception {
  assertEquals("&Beta;", StringEscapeUtils.escapeHtml4("\u0392"));
  assertEquals("\u0392", StringEscapeUtils.unescapeHtml4("&Beta;"));
  // TODO: refine API for escaping/unescaping specific HTML versions
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Tests https://issues.apache.org/jira/browse/LANG-339
 */
@Test
public void testEscapeHiragana() {
  // Some random Japanese Unicode characters
  final String original = "\u304B\u304C\u3068";
  final String escaped = StringEscapeUtils.escapeHtml4(original);
  assertEquals( "Hiragana character Unicode behaviour should not be being escaped by escapeHtml4",
  original, escaped);
  final String unescaped = StringEscapeUtils.unescapeHtml4( escaped );
  assertEquals( "Hiragana character Unicode behaviour has changed - expected no unescaping", escaped, unescaped);
}

代码示例来源:origin: gocd/gocd

@Test
public void shouldSupportUTF8() throws Exception {
  String link = "http://mingle05/projects/cce/cards/${ID}";
  String regex = "#(\\d+)";
  trackingTool = new DefaultCommentRenderer(link, regex);
  String result = trackingTool.render("The story #111 is fixed by 德里克. #122 is also related to this");
  assertThat(result,
      is("The story " + dynamicLink("111") + " is fixed by " + StringEscapeUtils.escapeHtml4("德里克") + ". "
          + dynamicLink("122") + " is also related to this"));
}

代码示例来源:origin: org.apache.commons/commons-lang3

@Test
public void testEscapeSurrogatePairs() throws Exception {
  assertEquals("\uD83D\uDE30", StringEscapeUtils.escapeCsv("\uD83D\uDE30"));
  // Examples from https://en.wikipedia.org/wiki/UTF-16
  assertEquals("\uD800\uDC00", StringEscapeUtils.escapeCsv("\uD800\uDC00"));
  assertEquals("\uD834\uDD1E", StringEscapeUtils.escapeCsv("\uD834\uDD1E"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeCsv("\uDBFF\uDFFD"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml3("\uDBFF\uDFFD"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeHtml4("\uDBFF\uDFFD"));
  assertEquals("\uDBFF\uDFFD", StringEscapeUtils.escapeXml("\uDBFF\uDFFD"));
}

相关文章