org.apache.commons.text.StringEscapeUtils类的使用及代码示例

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

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

StringEscapeUtils介绍

暂无

代码示例

代码示例来源:origin: jeremylong/DependencyCheck

/**
 * HTML Encodes the provided text.
 *
 * @param text the text to encode
 * @return the HTML encoded text
 */
public String html(String text) {
  if (text == null || text.isEmpty()) {
    return text;
  }
  return StringEscapeUtils.escapeHtml4(text);
}

代码示例来源:origin: jeremylong/DependencyCheck

/**
 * JSON Encodes the provided text.
 *
 * @param text the text to encode
 * @return the JSON encoded text
 */
public String json(String text) {
  if (text == null || text.isEmpty()) {
    return text;
  }
  return StringEscapeUtils.escapeJson(text);
}

代码示例来源:origin: jamesdbloom/mockserver

public static String serializeNottableString(NottableString nottableString) {
    if (nottableString.isNot()) {
      return "not(\"" + StringEscapeUtils.escapeJava(nottableString.getValue()) + "\")";
    } else {
      return "\"" + StringEscapeUtils.escapeJava(nottableString.getValue()) + "\"";
    }
  }
}

代码示例来源:origin: org.apache.hadoop/hadoop-common

while (params.hasMoreElements()) {
 String rawParam = params.nextElement();
 String param = StringEscapeUtils.unescapeHtml4(rawParam);
 String value =
  StringEscapeUtils.unescapeHtml4(req.getParameter(rawParam));
 if (value != null) {
  if (value.equals(newConf.getRaw(param)) || value.equals("default") ||
     oldConf.getRaw(param) != null) {
    out.println("<p>Changed \"" + 
          StringEscapeUtils.escapeHtml4(param) + "\" from \"" +
          StringEscapeUtils.escapeHtml4(oldConf.getRaw(param)) +
          "\" to default</p>");
    reconf.reconfigureProperty(param, null);
           StringEscapeUtils.escapeHtml4(param) +
           "\" from default to \"" +
           StringEscapeUtils.escapeHtml4(value) + "\"</p>");
    } else {
     out.println("<p>Changed \"" + 
           StringEscapeUtils.escapeHtml4(param) + "\" from \"" +
           StringEscapeUtils.escapeHtml4(oldConf.
                          getRaw(param)) +
           "\" to \"" +
           StringEscapeUtils.escapeHtml4(value) + "\"</p>");
   out.println("<p>\"" + StringEscapeUtils.escapeHtml4(param) +
         "\" not changed because value has changed from \"" +
         StringEscapeUtils.escapeHtml4(value) + "\" to \"" +

代码示例来源:origin: com.infotel.seleniumRobot/core

protected String encodeString(String message, String format) {
  if (encoded) {
    return message;
  }
  String newMessage;
  switch (format) {
  case "xml":
    newMessage = StringEscapeUtils.escapeXml11(message);
    break;
  case "csv":
    newMessage = StringEscapeUtils.escapeCsv(message);
    break;
  case "html":
    newMessage = StringEscapeUtils.escapeHtml4(message);
    break;
  case "json":
    newMessage = StringEscapeUtils.escapeJson(message);
    break;
  default:
    throw new CustomSeleniumTestsException("only escaping of 'xml', 'html', 'csv', 'json' is allowed");
  }
  return newMessage;
}

代码示例来源:origin: org.apache.hadoop/hadoop-yarn-server-router

private static String escape(String str) {
  return escapeEcmaScript(escapeHtml4(str));
 }
}

代码示例来源:origin: igniterealtime/Openfire

public BasicStreamID(String id) {
  if ( id == null || id.isEmpty() ) {
    throw new IllegalArgumentException( "Argument 'id' cannot be null." );
  }
  this.id = StringEscapeUtils.escapeXml10( id );
}

代码示例来源:origin: igniterealtime/Openfire

response.addHeader("Pragma", "no-cache");
content = "_BOSH_(\"" + StringEscapeUtils.escapeEcmaScript(content) + "\")";

代码示例来源:origin: jeremylong/DependencyCheck

/**
 * XML Encodes the provided text.
 *
 * @param text the text to encode
 * @return the XML encoded text
 */
public String xml(String text) {
  if (text == null || text.isEmpty()) {
    return text;
  }
  return StringEscapeUtils.escapeXml11(text);
}

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

/**
 * Tests https://issues.apache.org/jira/browse/LANG-911
 */
@Test
public void testLang911() {
  final String bellsTest = "\ud83d\udc80\ud83d\udd14";
  final String value = StringEscapeUtils.escapeJava(bellsTest);
  final String valueTest = StringEscapeUtils.unescapeJava(value);
  assertEquals(bellsTest, valueTest);
}

代码示例来源:origin: jeremylong/DependencyCheck

/**
 * Formats text for CSV format. This includes trimming whitespace, replace
 * line breaks with spaces, and if necessary quotes the text and/or escapes
 * contained quotes.
 *
 * @param text the text to escape and quote
 * @return the escaped and quoted text
 */
public String csv(String text) {
  if (text == null || text.isEmpty()) {
    return "\"\"";
  }
  final String str = text.trim().replace("\n", " ");
  if (str.length() == 0) {
    return "\"\"";
  }
  return StringEscapeUtils.escapeCsv(str);
}

代码示例来源:origin: wso2/wso2-synapse

/**
 * Escapes XML special characters
 *
 * @param msgCtx Message Context
 * @param value XML String which needs to be escaped
 * @return XML special char escaped string
 */
private String escapeXMLEnvelope(MessageContext msgCtx, String value) {
  String xmlVersion = "1.0"; //Default is set to 1.0
  try {
    xmlVersion = checkXMLVersion(msgCtx);
  } catch (IOException e) {
    log.error("Error reading message envelope", e);
  } catch (SAXException e) {
    log.error("Error parsing message envelope", e);
  } catch (ParserConfigurationException e) {
    log.error("Error building message envelope document", e);
  }
  if("1.1".equals(xmlVersion)) {
    return org.apache.commons.text.StringEscapeUtils.escapeXml11(value);
  } else {
    return org.apache.commons.text.StringEscapeUtils.escapeXml10(value);
  }
}

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

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

代码示例来源:origin: org.apache.hadoop/hadoop-yarn-common

/**
 * Print strings escaping html.
 * @param args the strings to print
 */
public void echo(Object... args) {
 PrintWriter out = writer();
 for (Object s : args) {
  String escapedString = StringEscapeUtils.escapeEcmaScript(
    StringEscapeUtils.escapeHtml4(s.toString()));
  out.print(escapedString);
 }
}

代码示例来源:origin: apache/nifi

String attName = atts.getQName(i);
attributeNames.add(attName);
String attValue = StringEscapeUtils.escapeXml10(atts.getValue(i));
sb.append(" ").append(attName).append("=").append("\"").append(attValue).append("\"");

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

/**
 * Tests https://issues.apache.org/jira/browse/LANG-708
 *
 * @throws IOException
 *             if an I/O error occurs
 */
@Test
public void testLang708() throws IOException {
  final byte[] inputBytes = Files.readAllBytes(Paths.get("src/test/resources/stringEscapeUtilsTestData.txt"));
  final String input = new String(inputBytes, StandardCharsets.UTF_8);
  final String escaped = StringEscapeUtils.escapeEcmaScript(input);
  // just the end:
  assertTrue(escaped.endsWith("}]"), escaped);
  // a little more:
  assertTrue(escaped.endsWith("\"valueCode\\\":\\\"\\\"}]"), escaped);
}

代码示例来源:origin: apache/nifi

private void writeAttributesEntry(final Map<String, String> attributes, final TarArchiveOutputStream tout) throws IOException {
  final StringBuilder sb = new StringBuilder();
  sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE properties\n  SYSTEM \"http://java.sun.com/dtd/properties.dtd\">\n");
  sb.append("<properties>");
  for (final Map.Entry<String, String> entry : attributes.entrySet()) {
    final String escapedKey = StringEscapeUtils.escapeXml11(entry.getKey());
    final String escapedValue = StringEscapeUtils.escapeXml11(entry.getValue());
    sb.append("\n  <entry key=\"").append(escapedKey).append("\">").append(escapedValue).append("</entry>");
  }
  sb.append("</properties>");
  final byte[] metaBytes = sb.toString().getBytes(StandardCharsets.UTF_8);
  final TarArchiveEntry attribEntry = new TarArchiveEntry(FILENAME_ATTRIBUTES);
  attribEntry.setMode(tarPermissions);
  attribEntry.setSize(metaBytes.length);
  tout.putArchiveEntry(attribEntry);
  tout.write(metaBytes);
  tout.closeArchiveEntry();
}

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

private void assertUnescapeJava(final String unescaped, final String original, final String message)
    throws IOException {
  final String expected = unescaped;
  final String actual = StringEscapeUtils.unescapeJava(original);
  assertEquals(expected, actual, "unescape(String) failed"
          + (message == null ? "" : (": " + message))
          + ": expected '" + StringEscapeUtils.escapeJava(expected)
          // we escape this so we can see it in the error message
          + "' actual '" + StringEscapeUtils.escapeJava(actual) + "'");
  final StringWriter writer = new StringWriter();
  StringEscapeUtils.UNESCAPE_JAVA.translate(original, writer);
  assertEquals(unescaped, writer.toString());
}

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

sb.append( escapeCsv( "Process PID" ) ).append( ',' )
    .append( escapeCsv( "Process Name" ) ).append( ',' )
    .append( escapeCsv( "Process Time" ) ).append( ',' )
    .append( escapeCsv( "User" ) ).append( ',' )
    .append( escapeCsv( "Virtual Memory" ) ).append( ',' )
    .append( escapeCsv( "Physical Memory" ) ).append( ',' )
    .append( escapeCsv( "CPU usage" ) ).append( ',' )
    .append( escapeCsv( "Start Time" ) ).append( ',' )
    .append( escapeCsv( "Priority" ) ).append( ',' )
    .append( escapeCsv( "Full command" ) ).append( '\n' );
      .append( escapeCsv( processInfo.getName() ) ).append( ',' )
      .append( processInfo.getTime() ).append( ',' )
      .append( escapeCsv( processInfo.getUser() ) ).append( ',' )
      .append( processInfo.getVirtualMemory() ).append( ',' )
      .append( processInfo.getPhysicalMemory() ).append( ',' )
      .append( processInfo.getStartTime() ).append( ',' )
      .append( processInfo.getStartTime() ).append( ',' )
      .append( escapeCsv( processInfo.getCommand() ) ).append( '\n' );

代码示例来源:origin: org.apache.hadoop/hadoop-common

private void printHeader(PrintWriter out, String nodeName) {
 out.print("<html><head>");
 out.printf("<title>%s Reconfiguration Utility</title>%n",
       StringEscapeUtils.escapeHtml4(nodeName));
 out.print("</head><body>\n");
 out.printf("<h1>%s Reconfiguration Utility</h1>%n",
       StringEscapeUtils.escapeHtml4(nodeName));
}

相关文章