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

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

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

StringEscapeUtils.escapeEcmaScript介绍

[英]Escapes the characters in a String using EcmaScript String rules.

Escapes any values it finds into their EcmaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.)

So a tab becomes the characters '\' and 't'.

The only difference between Java strings and EcmaScript strings is that in EcmaScript, a single quote and forward-slash (/) are escaped.

Note that EcmaScript is best known by the JavaScript and ActionScript dialects.

Example:

input string: He didn't say, "Stop!" 
output string: He didn\'t say, \"Stop!\"

[中]使用EcmaScript字符串规则转义字符串中的字符。
将找到的任何值转义到EcmaScript字符串形式中。正确处理引号和控制字符(制表符、反斜杠、cr、ff等)
所以一个标签变成了字符“\”和“t”。
Java字符串和EcmaScript字符串之间的唯一区别在于,在EcmaScript中,单引号和正斜杠(/)被转义。
请注意,EcmaScript最为人所知的是JavaScript和ActionScript方言。
例子:

input string: He didn't say, "Stop!" 
output string: He didn\'t say, \"Stop!\"

代码示例

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

/**
 * 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/lang-708-input.txt"));
  final String input = new String(inputBytes, StandardCharsets.UTF_8);
  final String escaped = StringEscapeUtils.escapeEcmaScript(input);
  // just the end:
  assertTrue(escaped, escaped.endsWith("}]"));
  // a little more:
  assertTrue(escaped, escaped.endsWith("\"valueCode\\\":\\\"\\\"}]"));
}

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

/**
 * Tests LANG-858.
 */
@Test
public void testEscapeSurrogatePairsLang858() {
  assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeJava("\uDBFF\uDFFD"));       //fail LANG-858
  assertEquals("\\uDBFF\\uDFFD", StringEscapeUtils.escapeEcmaScript("\uDBFF\uDFFD")); //fail LANG-858
}

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

@Test
public void testEscapeEcmaScript() {
  assertNull(StringEscapeUtils.escapeEcmaScript(null));
  try {
    StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(null, null);
    fail();
  } catch (final IOException ex) {
    fail();
  } catch (final IllegalArgumentException ex) {
  }
  try {
    StringEscapeUtils.ESCAPE_ECMASCRIPT.translate("", null);
    fail();
  } catch (final IOException ex) {
    fail();
  } catch (final IllegalArgumentException ex) {
  }
  assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeEcmaScript("He didn't say, \"stop!\""));
  assertEquals("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';",
      StringEscapeUtils.escapeEcmaScript("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';"));
}

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

@Test
public void testUnescapeEcmaScript() {
  assertNull(StringEscapeUtils.escapeEcmaScript(null));
  try {
    StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate(null, null);
    fail();
  } catch (final IOException ex) {
    fail();
  } catch (final IllegalArgumentException ex) {
  }
  try {
    StringEscapeUtils.UNESCAPE_ECMASCRIPT.translate("", null);
    fail();
  } catch (final IOException ex) {
    fail();
  } catch (final IllegalArgumentException ex) {
  }
  assertEquals("He didn't say, \"stop!\"", StringEscapeUtils.unescapeEcmaScript("He didn\\'t say, \\\"stop!\\\""));
  assertEquals("document.getElementById(\"test\").value = '<script>alert('aaa');</script>';",
      StringEscapeUtils.unescapeEcmaScript("document.getElementById(\\\"test\\\").value = \\'<script>alert(\\'aaa\\');<\\/script>\\';"));
}

代码示例来源:origin: cn.dreampie/jfinal-web

public String escapeScript(String text) {

  String value = text;
  if (text == null) {
   return text;
  } else {
   value = StringEscapeUtils.escapeEcmaScript(value);
  }
  return value;
 }
//  public String escapeFile(String text) {

代码示例来源:origin: org.jtwig/jtwig-core

@Override
  public String escape(String input) {
    return StringEscapeUtils.escapeEcmaScript(input);
  }
}

代码示例来源:origin: org.apache.velocity/velocity-engine-core

/**
 * Escapes the characters in a String to be suitable for use in JavaScript.
 *
 * @param text
 * @return An escaped String.
 * @see <a href="http://commons.apache.org/proper/commons-lang/javadocs/api-release/org/apache/commons/lang3/StringEscapeUtils.html#escapeEcmaScript%28java.lang.String%29">StringEscapeUtils</a>
 */
protected String escape(Object text)
{
  return StringEscapeUtils.escapeEcmaScript(text.toString());
}

代码示例来源:origin: org.jboss.windup.config/windup-config-api

private void escapeOrNull(final String string, Writer writer) throws IOException
{
  if (string == null)
    writer.append("null");
  else
    writer.append('"').append(StringEscapeUtils.escapeEcmaScript(string)).append('"');
}

代码示例来源:origin: addthis/hydra

private static Response buildServerError(Exception exception) {
  log.warn("", exception);
  String message = exception.getMessage();
  if (message == null) {
    message = exception.toString();
  }
  final String response = "{" +
              "\"error\": \"A java exception was thrown.\", " +
              "\"message\": \"" + StringEscapeUtils.escapeEcmaScript(message) + "\"" +
              "}";
  return Response.serverError().entity(response).build();
}

代码示例来源:origin: org.apache.shindig/shindig-gadgets

private String toArrayString(Set<String> bundles) {
  StringBuilder builder = new StringBuilder();
  for (String bundle : bundles) {
   if (builder.length() > 0) builder.append(',');
   builder.append('\'').append(StringEscapeUtils.escapeEcmaScript(bundle)).append('\'');
  }
  return '[' + builder.toString() + ']';
 }
}

代码示例来源:origin: vakinge/jeesuite-libs

@Override    
public String getParameter(String name) {    
  String value = super.getParameter(name);    
   if (value != null) {    
     value = StringEscapeUtils.escapeEcmaScript(value);    
   }    
   return value;    
}

代码示例来源:origin: vakinge/jeesuite-libs

@Override  
public String[] getParameterValues(String name) {  
   String[] values = super.getParameterValues(name);  
   if(values != null && values.length > 0){  
     for(int i =0; i< values.length ;i++){  
       values[i] = StringEscapeUtils.escapeEcmaScript(values[i]);  
     }  
   }  
  return values;  
 }

代码示例来源:origin: vakinge/jeesuite-libs

@Override    
   public String getHeader(String name) {    
    
      String value = super.getHeader(name);    
      if (value != null) {    
        value = StringEscapeUtils.escapeEcmaScript(value);    
      }    
      return value;    
    }  
}

代码示例来源:origin: org.jasig.portal/uPortal-security-xslt

@Override
  public String getMessageForEmacsScript(
      String code, String language, String arg1, String arg2, String arg3) {
    return StringEscapeUtils.escapeEcmaScript(
        this.getMessage(code, language, arg1, arg2, arg3));
  }
}

代码示例来源:origin: com.typesafe.play/play-java_2.10

/**
 * Send a message on this socket (will be received as String in the Javascript callback method).
 */
public void sendMessage(String message) {
  out.write("<script type=\"text/javascript\">" + callbackMethod + "('" + org.apache.commons.lang3.StringEscapeUtils.escapeEcmaScript(message) + "');</script>");
}

代码示例来源:origin: com.atlassian.soy/soy-template-plugin

@Override
public JsExpression generate(JsExpression... args) {
  Matcher matcher = SOY_STRING_PATTERN.matcher(args[0].getText());
  checkArgument(matcher.matches(), "The help key name should be a string literal");
  String helpUrl = getHelpUrl(matcher.group(1));
  return new JsExpression('"' + StringEscapeUtils.escapeEcmaScript(helpUrl) + '"');
}

代码示例来源:origin: org.xworker/xworker_core

public static String escapeEcmaScript(ActionContext actionContext){
  Thing self = actionContext.getObject("self");
  String str  = (String) self.doAction("getStr", actionContext);
  return StringEscapeUtils.escapeEcmaScript(str);
}

代码示例来源:origin: org.xwiki.platform/xwiki-platform-test-ui

/**
 * @return if the livetable has finished displaying and is ready for service
 */
public boolean isReady()
{
  Object result = getDriver().executeJavascript("return Element.hasClassName('"
    + StringEscapeUtils.escapeEcmaScript(livetableId) + "-ajax-loader','hidden')");
  return result instanceof Boolean ? (Boolean) result : false;
}

代码示例来源:origin: org.wso2.org.apache.shindig/shindig-gadgets

@Test
public void jsEvalSnippet() {
 assertEquals("eval(_js['" + StringEscapeUtils.escapeEcmaScript(RESOURCE_1.toString()) + "']);",
   DefaultConcatUriManager.getJsSnippet("_js", RESOURCE_1));
}

代码示例来源:origin: org.apache.shindig/shindig-gadgets

@Test
public void jsEvalSnippet() {
 assertEquals("eval(_js['" + StringEscapeUtils.escapeEcmaScript(RESOURCE_1.toString()) + "']);",
   DefaultConcatUriManager.getJsSnippet("_js", RESOURCE_1));
}

相关文章