• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java CharEscaper类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中com.google.common.escape.CharEscaper的典型用法代码示例。如果您正苦于以下问题:Java CharEscaper类的具体用法?Java CharEscaper怎么用?Java CharEscaper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



CharEscaper类属于com.google.common.escape包,在下文中一共展示了CharEscaper类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: testXmlContentEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
public void testXmlContentEscaper() throws Exception {
  CharEscaper xmlContentEscaper = (CharEscaper) XmlEscapers.xmlContentEscaper();
  assertBasicXmlEscaper(xmlContentEscaper, false, false);
  // Test quotes are not escaped.
  assertEquals("\"test\"", xmlContentEscaper.escape("\"test\""));
  assertEquals("'test'", xmlContentEscaper.escape("'test'"));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:8,代码来源:XmlEscapersTest.java


示例2: testXmlAttributeEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
public void testXmlAttributeEscaper() throws Exception {
  CharEscaper xmlAttributeEscaper = (CharEscaper) XmlEscapers.xmlAttributeEscaper();
  assertBasicXmlEscaper(xmlAttributeEscaper, true, true);
  // Test quotes are escaped.
  assertEquals(""test"", xmlAttributeEscaper.escape("\"test\""));
  assertEquals("'test'", xmlAttributeEscaper.escape("\'test'"));
  // Test all escapes
  assertEquals("a"b<c>d&e"f'",
      xmlAttributeEscaper.escape("a\"b<c>d&e\"f'"));
  // Test '\t', '\n' and '\r' are escaped.
  assertEquals("a&#x9;b&#xA;c&#xD;d", xmlAttributeEscaper.escape("a\tb\nc\rd"));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:13,代码来源:XmlEscapersTest.java


示例3: testXmlAttributeEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
public void testXmlAttributeEscaper() throws Exception {
  CharEscaper xmlAttributeEscaper = (CharEscaper) XmlEscapers.xmlAttributeEscaper();
  assertBasicXmlEscaper(xmlAttributeEscaper, true, true);
  // Test quotes are escaped.
  assertEquals("&quot;test&quot;", xmlAttributeEscaper.escape("\"test\""));
  assertEquals("&apos;test&apos;", xmlAttributeEscaper.escape("\'test'"));
  // Test all escapes
  assertEquals(
      "a&quot;b&lt;c&gt;d&amp;e&quot;f&apos;", xmlAttributeEscaper.escape("a\"b<c>d&e\"f'"));
  // Test '\t', '\n' and '\r' are escaped.
  assertEquals("a&#x9;b&#xA;c&#xD;d", xmlAttributeEscaper.escape("a\tb\nc\rd"));
}
 
开发者ID:google,项目名称:guava,代码行数:13,代码来源:XmlEscapersTest.java


示例4: assertBasicXmlEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
static void assertBasicXmlEscaper(CharEscaper xmlEscaper,
    boolean shouldEscapeQuotes, boolean shouldEscapeWhitespaceChars) {
  // Simple examples (smoke tests)
  assertEquals("xxx", xmlEscaper.escape("xxx"));
  assertEquals("test &amp; test &amp; test",
      xmlEscaper.escape("test & test & test"));
  assertEquals("test &lt;&lt; 1", xmlEscaper.escape("test << 1"));
  assertEquals("test &gt;&gt; 1", xmlEscaper.escape("test >> 1"));
  assertEquals("&lt;tab&gt;", xmlEscaper.escape("<tab>"));

  // Test all non-escaped ASCII characters.
  String s = "[email protected]#$%^*()_+=-/?\\|]}[{,.;:" +
      "abcdefghijklmnopqrstuvwxyz" +
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
      "1234567890";
  assertEquals(s, xmlEscaper.escape(s));

  // Test ASCII control characters.
  for (char ch = 0; ch < 0x20; ch++) {
    if (ch == '\t' || ch == '\n' || ch == '\r') {
      // Only these whitespace chars are permitted in XML,
      if (shouldEscapeWhitespaceChars) {
        assertEscaping(xmlEscaper, "&#x" + Integer.toHexString(ch).toUpperCase() + ";", ch);
      } else {
        assertUnescaped(xmlEscaper, ch);
      }
    } else {
      // and everything else is replaced with FFFD.
      assertEscaping(xmlEscaper, "\uFFFD", ch);
    }
  }

  // Test _all_ allowed characters (including surrogate values).
  for (char ch = 0x20; ch <= 0xFFFD; ch++) {
    // There are a small number of cases to consider, so just do it manually.
    if (ch == '&') {
      assertEscaping(xmlEscaper, "&amp;", ch);
    } else if (ch == '<') {
      assertEscaping(xmlEscaper, "&lt;", ch);
    } else if (ch == '>') {
      assertEscaping(xmlEscaper, "&gt;", ch);
    } else if (shouldEscapeQuotes && ch == '\'') {
      assertEscaping(xmlEscaper, "&apos;", ch);
    } else if (shouldEscapeQuotes && ch == '"') {
      assertEscaping(xmlEscaper, "&quot;", ch);
    } else {
      String input = String.valueOf(ch);
      String escaped = xmlEscaper.escape(input);
      assertEquals(
          "char 0x" + Integer.toString(ch, 16) + " should not be escaped",
          input, escaped);
    }
  }

  // Test that 0xFFFE and 0xFFFF are replaced with 0xFFFD
  assertEscaping(xmlEscaper, "\uFFFD", '\uFFFE');
  assertEscaping(xmlEscaper, "\uFFFD", '\uFFFF');

  assertEquals("0xFFFE is forbidden and should be replaced during escaping",
      "[\uFFFD]", xmlEscaper.escape("[\ufffe]"));
  assertEquals("0xFFFF is forbidden and should be replaced during escaping",
      "[\uFFFD]", xmlEscaper.escape("[\uffff]"));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:64,代码来源:XmlEscapersTest.java


示例5: assertBasicXmlEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
private void assertBasicXmlEscaper(CharEscaper xmlEscaper,
    boolean shouldEscapeQuotes, boolean shouldEscapeWhitespaceChars) {
  // Simple examples (smoke tests)
  assertEquals("xxx", xmlEscaper.escape("xxx"));
  assertEquals("test &amp; test &amp; test",
      xmlEscaper.escape("test & test & test"));
  assertEquals("test &lt;&lt; 1", xmlEscaper.escape("test << 1"));
  assertEquals("test &gt;&gt; 1", xmlEscaper.escape("test >> 1"));
  assertEquals("&lt;tab&gt;", xmlEscaper.escape("<tab>"));

  // Test all non-escaped ASCII characters.
  String s = "[email protected]#$%^*()_+=-/?\\|]}[{,.;:" +
      "abcdefghijklmnopqrstuvwxyz" +
      "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
      "1234567890";
  assertEquals(s, xmlEscaper.escape(s));

  // Test ASCII control characters.
  for (char ch = 0; ch < 0x20; ch++) {
    if (ch == '\t' || ch == '\n' || ch == '\r') {
      // Only these whitespace chars are permitted in XML,
      if (shouldEscapeWhitespaceChars) {
        assertEscaping(xmlEscaper, "&#x" + Integer.toHexString(ch).toUpperCase() + ";", ch);
      } else {
        assertUnescaped(xmlEscaper, ch);
      }
    } else {
      // and everything else is replaced with FFFD.
      assertEscaping(xmlEscaper, "\uFFFD", ch);
    }
  }

  // Test _all_ allowed characters (including surrogate values).
  for (char ch = 0x20; ch <= 0xFFFD; ch++) {
    // There are a small number of cases to consider, so just do it manually.
    if (ch == '&') {
      assertEscaping(xmlEscaper, "&amp;", ch);
    } else if (ch == '<') {
      assertEscaping(xmlEscaper, "&lt;", ch);
    } else if (ch == '>') {
      assertEscaping(xmlEscaper, "&gt;", ch);
    } else if (shouldEscapeQuotes && ch == '\'') {
      assertEscaping(xmlEscaper, "&apos;", ch);
    } else if (shouldEscapeQuotes && ch == '"') {
      assertEscaping(xmlEscaper, "&quot;", ch);
    } else {
      String input = String.valueOf(ch);
      String escaped = xmlEscaper.escape(input);
      assertEquals(
          "char 0x" + Integer.toString(ch, 16) + " should not be escaped",
          input, escaped);
    }
  }

  // Test that 0xFFFE and 0xFFFF are replaced with 0xFFFD
  assertEscaping(xmlEscaper, "\uFFFD", '\uFFFE');
  assertEscaping(xmlEscaper, "\uFFFD", '\uFFFF');

  assertEquals("0xFFFE is forbidden and should be replaced during escaping",
      "[\uFFFD]", xmlEscaper.escape("[\ufffe]"));
  assertEquals("0xFFFF is forbidden and should be replaced during escaping",
      "[\uFFFD]", xmlEscaper.escape("[\uffff]"));
}
 
开发者ID:sander120786,项目名称:guava-libraries,代码行数:64,代码来源:XmlEscapersTest.java


示例6: assertBasicXmlEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
static void assertBasicXmlEscaper(
    CharEscaper xmlEscaper, boolean shouldEscapeQuotes, boolean shouldEscapeWhitespaceChars) {
  // Simple examples (smoke tests)
  assertEquals("xxx", xmlEscaper.escape("xxx"));
  assertEquals("test &amp; test &amp; test", xmlEscaper.escape("test & test & test"));
  assertEquals("test &lt;&lt; 1", xmlEscaper.escape("test << 1"));
  assertEquals("test &gt;&gt; 1", xmlEscaper.escape("test >> 1"));
  assertEquals("&lt;tab&gt;", xmlEscaper.escape("<tab>"));

  // Test all non-escaped ASCII characters.
  String s =
      "[email protected]#$%^*()_+=-/?\\|]}[{,.;:"
          + "abcdefghijklmnopqrstuvwxyz"
          + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
          + "1234567890";
  assertEquals(s, xmlEscaper.escape(s));

  // Test ASCII control characters.
  for (char ch = 0; ch < 0x20; ch++) {
    if (ch == '\t' || ch == '\n' || ch == '\r') {
      // Only these whitespace chars are permitted in XML,
      if (shouldEscapeWhitespaceChars) {
        assertEscaping(xmlEscaper, "&#x" + Integer.toHexString(ch).toUpperCase() + ";", ch);
      } else {
        assertUnescaped(xmlEscaper, ch);
      }
    } else {
      // and everything else is replaced with FFFD.
      assertEscaping(xmlEscaper, "\uFFFD", ch);
    }
  }

  // Test _all_ allowed characters (including surrogate values).
  for (char ch = 0x20; ch <= 0xFFFD; ch++) {
    // There are a small number of cases to consider, so just do it manually.
    if (ch == '&') {
      assertEscaping(xmlEscaper, "&amp;", ch);
    } else if (ch == '<') {
      assertEscaping(xmlEscaper, "&lt;", ch);
    } else if (ch == '>') {
      assertEscaping(xmlEscaper, "&gt;", ch);
    } else if (shouldEscapeQuotes && ch == '\'') {
      assertEscaping(xmlEscaper, "&apos;", ch);
    } else if (shouldEscapeQuotes && ch == '"') {
      assertEscaping(xmlEscaper, "&quot;", ch);
    } else {
      String input = String.valueOf(ch);
      String escaped = xmlEscaper.escape(input);
      assertEquals(
          "char 0x" + Integer.toString(ch, 16) + " should not be escaped", input, escaped);
    }
  }

  // Test that 0xFFFE and 0xFFFF are replaced with 0xFFFD
  assertEscaping(xmlEscaper, "\uFFFD", '\uFFFE');
  assertEscaping(xmlEscaper, "\uFFFD", '\uFFFF');

  assertEquals(
      "0xFFFE is forbidden and should be replaced during escaping",
      "[\uFFFD]",
      xmlEscaper.escape("[\ufffe]"));
  assertEquals(
      "0xFFFF is forbidden and should be replaced during escaping",
      "[\uFFFD]",
      xmlEscaper.escape("[\uffff]"));
}
 
开发者ID:google,项目名称:guava,代码行数:67,代码来源:XmlEscapersTest.java


示例7: assertEscaping

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
/**
 * Asserts that an escaper escapes the given character into the expected
 * string.
 *
 * @param escaper the non-null escaper to test
 * @param expected the expected output string
 * @param c the character to escape
 */
public static void assertEscaping(CharEscaper escaper, String expected,
    char c) {

  String escaped = computeReplacement(escaper, c);
  Assert.assertNotNull(escaped);
  Assert.assertEquals(expected, escaped);
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:16,代码来源:EscaperAsserts.java


示例8: assertEscaping

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
/**
 * Asserts that an escaper escapes the given character into the expected string.
 *
 * @param escaper the non-null escaper to test
 * @param expected the expected output string
 * @param c the character to escape
 */
public static void assertEscaping(CharEscaper escaper, String expected, char c) {

  String escaped = computeReplacement(escaper, c);
  Assert.assertNotNull(escaped);
  Assert.assertEquals(expected, escaped);
}
 
开发者ID:google,项目名称:guava,代码行数:14,代码来源:EscaperAsserts.java


示例9: assertUnescaped

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
/**
 * Asserts that an escaper does not escape the given character.
 *
 * @param escaper the non-null escaper to test
 * @param c the character to test
 */
public static void assertUnescaped(CharEscaper escaper, char c) {
  Assert.assertNull(computeReplacement(escaper, c));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:10,代码来源:EscaperAsserts.java


示例10: javaCharEscaper

import com.google.common.escape.CharEscaper; //导入依赖的package包/类
/**
 * Returns an {@link Escaper} instance that escapes special characters in a string so it can
 * safely be included in either a Java character literal or string literal. This is the preferred
 * way to escape Java characters for use in String or character literals.
 *
 * <p>See: <a href= "http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#101089"
 * >The Java Language Specification</a> for more details.
 */
public static CharEscaper javaCharEscaper() {
  return JAVA_CHAR_ESCAPER;
}
 
开发者ID:google,项目名称:error-prone,代码行数:12,代码来源:SourceCodeEscapers.java



注:本文中的com.google.common.escape.CharEscaper类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java RrdGraph类代码示例发布时间:2022-05-23
下一篇:
Java DJISDKError类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap