本文整理了Java中org.apache.commons.lang3.StringEscapeUtils.escapeJava()
方法的一些代码示例,展示了StringEscapeUtils.escapeJava()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。StringEscapeUtils.escapeJava()
方法的具体详情如下:
包路径:org.apache.commons.lang3.StringEscapeUtils
类名称:StringEscapeUtils
方法名:escapeJava
[英]Escapes the characters in a String using Java String rules.
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 JavaScript strings is that in JavaScript, a single quote and forward-slash (/) are escaped.
Example:
input string: He didn't say, "Stop!"
output string: He didn't say, \"Stop!\"
[中]使用Java字符串规则转义字符串中的字符。
正确处理引号和控制字符(制表符、反斜杠、cr、ff等)
所以一个标签变成了字符“\”和“t”。
Java字符串和JavaScript字符串之间的唯一区别在于,在JavaScript中,单引号和正斜杠(/)被转义。
例子:
input string: He didn't say, "Stop!"
output string: He didn't say, \"Stop!\"
代码示例来源:origin: gocd/gocd
public static String quoteJavascriptString(String s) {
return "\"" + StringEscapeUtils.escapeJava(s) + "\"";
}
代码示例来源:origin: apache/drill
/**
* Escapes the characters in a {@code String} according to Java string literal
* rules.
*
* Deals correctly with quotes and control-chars (tab, backslash, cr, ff,
* etc.) so, for example, a tab becomes the characters {@code '\\'} and
* {@code 't'}.
*
* Example:
* <pre>
* input string: He didn't say, "Stop!"
* output string: He didn't say, \"Stop!\"
* </pre>
*
* @param input String to escape values in, may be null
* @return String with escaped values, {@code null} if null string input
*/
public static final String escapeJava(String input) {
return StringEscapeUtils.escapeJava(input);
}
代码示例来源:origin: aws/aws-sdk-java
public String getExpectedAsEscapedJson() {
return StringEscapeUtils.escapeJava(expected.toString());
}
代码示例来源:origin: galenframework/galen
private static String convertArgumentToString(Object argument) {
if (argument == null) {
return "null";
} else if (argument instanceof String) {
return "\"" + StringEscapeUtils.escapeJava(argument.toString()) + "\"";
} else if (argument instanceof Boolean) {
return Boolean.toString((Boolean)argument);
} else {
return argument.toString();
}
}
}
代码示例来源:origin: aws/aws-sdk-java
/**
* Generates the code for a new JmesPathLiteral.
*
* @param literal JmesPath literal type
* @param aVoid void
* @return String that represents a call to
* the new literal expression
*/
@Override
public String visit(final JmesPathLiteral literal, final Void aVoid) {
return "new JmesPathLiteral(\"" + StringEscapeUtils
.escapeJava(literal.getValue().toString()) + "\")";
}
代码示例来源:origin: apache/hive
/**
* Set context for this fetch operator in to the jobconf.
* This helps InputFormats make decisions based on the scope of the complete
* operation.
* @param conf the configuration to modify
* @param paths the list of input directories
*/
static void setFetchOperatorContext(JobConf conf, List<Path> paths) {
if (paths != null) {
StringBuilder buff = new StringBuilder();
for (Path path : paths) {
if (buff.length() > 0) {
buff.append('\t');
}
buff.append(StringEscapeUtils.escapeJava(path.toString()));
}
conf.set(FETCH_OPERATOR_DIRECTORY_LIST, buff.toString());
}
}
代码示例来源:origin: galenframework/galen
public static void cookie(WebDriver driver, String cookie) {
String script = "document.cookie=\"" + StringEscapeUtils.escapeJava(cookie) + "\";";
injectJavascript(driver, script);
}
代码示例来源:origin: apache/drill
/**
* Set context for this fetch operator in to the jobconf.
* This helps InputFormats make decisions based on the scope of the complete
* operation.
* @param conf the configuration to modify
* @param paths the list of input directories
*/
static void setFetchOperatorContext(JobConf conf, List<Path> paths) {
if (paths != null) {
StringBuilder buff = new StringBuilder();
for (Path path : paths) {
if (buff.length() > 0) {
buff.append('\t');
}
buff.append(StringEscapeUtils.escapeJava(path.toString()));
}
conf.set(FETCH_OPERATOR_DIRECTORY_LIST, buff.toString());
}
}
代码示例来源:origin: galenframework/galen
@Override
public void execute(TestReport report, Browser browser, GalenPageTest pageTest, ValidationListener validationListener) throws Exception {
if (cookies != null && cookies.size() > 0) {
StringBuilder js = new StringBuilder();
for (String cookie : cookies) {
js.append("document.cookie=\"" + StringEscapeUtils.escapeJava(cookie) + "\";");
}
browser.executeJavascript(js.toString());
browser.refresh();
}
}
代码示例来源:origin: apache/hive
/**
* Copies the storage handler properties configured for a table descriptor to a runtime job
* configuration. This differs from {@link #copyTablePropertiesToConf(org.apache.hadoop.hive.ql.plan.TableDesc, org.apache.hadoop.mapred.JobConf)}
* in that it does not allow parameters already set in the job to override the values from the
* table. This is important for setting the config up for reading,
* as the job may already have values in it from another table.
* @param tbl
* @param job
*/
public static void copyTablePropertiesToConf(TableDesc tbl, JobConf job) throws HiveException {
Properties tblProperties = tbl.getProperties();
for(String name: tblProperties.stringPropertyNames()) {
String val = (String) tblProperties.get(name);
if (val != null) {
job.set(name, StringEscapeUtils.escapeJava(val));
}
}
Map<String, String> jobProperties = tbl.getJobProperties();
if (jobProperties != null) {
for (Map.Entry<String, String> entry : jobProperties.entrySet()) {
job.set(entry.getKey(), entry.getValue());
}
}
}
代码示例来源:origin: apache/hive
/**
* Copies the storage handler properties configured for a table descriptor to a runtime job
* configuration.
*
* @param tbl
* table descriptor from which to read
*
* @param job
* configuration which receives configured properties
*/
public static void copyTableJobPropertiesToConf(TableDesc tbl, JobConf job) throws HiveException {
Properties tblProperties = tbl.getProperties();
for(String name: tblProperties.stringPropertyNames()) {
if (job.get(name) == null) {
String val = (String) tblProperties.get(name);
if (val != null) {
job.set(name, StringEscapeUtils.escapeJava(val));
}
}
}
Map<String, String> jobProperties = tbl.getJobProperties();
if (jobProperties != null) {
for (Map.Entry<String, String> entry : jobProperties.entrySet()) {
job.set(entry.getKey(), entry.getValue());
}
}
}
代码示例来源:origin: apache/drill
/**
* Copies the storage handler proeprites configured for a table descriptor to a runtime job
* configuration. This differs from {@link #copyTablePropertiesToConf(org.apache.hadoop.hive.ql.plan.TableDesc, org.apache.hadoop.mapred.JobConf)}
* in that it does not allow parameters already set in the job to override the values from the
* table. This is important for setting the config up for reading,
* as the job may already have values in it from another table.
* @param tbl
* @param job
*/
public static void copyTablePropertiesToConf(TableDesc tbl, JobConf job) {
Properties tblProperties = tbl.getProperties();
for(String name: tblProperties.stringPropertyNames()) {
String val = (String) tblProperties.get(name);
if (val != null) {
job.set(name, StringEscapeUtils.escapeJava(val));
}
}
Map<String, String> jobProperties = tbl.getJobProperties();
if (jobProperties == null) {
return;
}
for (Map.Entry<String, String> entry : jobProperties.entrySet()) {
job.set(entry.getKey(), entry.getValue());
}
}
代码示例来源:origin: org.apache.commons/commons-lang3
private void assertUnescapeJava(final String message, final String unescaped, final String original) throws IOException {
final String expected = unescaped;
final String actual = StringEscapeUtils.unescapeJava(original);
assertEquals("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) + "'",
expected, actual);
final StringWriter writer = new StringWriter();
StringEscapeUtils.UNESCAPE_JAVA.translate(original, writer);
assertEquals(unescaped, writer.toString());
}
代码示例来源:origin: org.apache.commons/commons-lang3
private void assertEscapeJava(String message, final String expected, final String original) throws IOException {
final String converted = StringEscapeUtils.escapeJava(original);
message = "escapeJava(String) failed" + (message == null ? "" : (": " + message));
assertEquals(message, expected, converted);
final StringWriter writer = new StringWriter();
StringEscapeUtils.ESCAPE_JAVA.translate(original, writer);
assertEquals(expected, writer.toString());
}
代码示例来源:origin: org.apache.commons/commons-lang3
/**
* Tests https://issues.apache.org/jira/browse/LANG-421
*/
@Test
public void testEscapeJavaWithSlash() {
final String input = "String with a slash (/) in it";
final String expected = input;
final String actual = StringEscapeUtils.escapeJava(input);
/**
* In 2.4 StringEscapeUtils.escapeJava(String) escapes '/' characters, which are not a valid character to escape
* in a Java string.
*/
assertEquals(expected, actual);
}
代码示例来源:origin: org.apache.commons/commons-lang3
/**
* 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: 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: neo4j/neo4j
@Test
public void shouldLogHelpfullyWhenPluginJarIsCorrupt() throws Exception
{
// given
URL theJar = createJarFor( ClassWithOneProcedure.class, ClassWithAnotherProcedure.class, ClassWithNoProcedureAtAll.class );
corruptJar( theJar );
AssertableLogProvider logProvider = new AssertableLogProvider( true );
ProcedureJarLoader jarloader = new ProcedureJarLoader(
new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), registryWithUnsafeAPI(), log, procedureConfig() ),
logProvider.getLog( ProcedureJarLoader.class ) );
// when
try
{
jarloader.loadProceduresFromDir( parentDir( theJar ) );
fail( "Should have logged and thrown exception." );
}
catch ( ZipException expected )
{
// then
logProvider.assertContainsLogCallContaining(
escapeJava( String.format( "Plugin jar file: %s corrupted.", new File( theJar.toURI() ).toPath() ) ) );
}
}
代码示例来源:origin: neo4j/neo4j
@Test
public void shouldWorkOnPathsWithSpaces() throws Exception
{
// given
File fileWithSpacesInName = tmpdir.newFile( new Random().nextInt() + " some spaces in the filename" + ".jar" );
URL theJar = new JarBuilder().createJarFor( fileWithSpacesInName, ClassWithOneProcedure.class );
corruptJar( theJar );
AssertableLogProvider logProvider = new AssertableLogProvider( true );
ProcedureJarLoader jarloader = new ProcedureJarLoader(
new ReflectiveProcedureCompiler( new TypeMappers(), new ComponentRegistry(), registryWithUnsafeAPI(), log, procedureConfig() ),
logProvider.getLog( ProcedureJarLoader.class ) );
// when
try
{
jarloader.loadProceduresFromDir( parentDir( theJar ) );
fail( "Should have logged and thrown exception." );
}
catch ( ZipException expected )
{
// then
logProvider.assertContainsLogCallContaining(
escapeJava( String.format( "Plugin jar file: %s corrupted.", fileWithSpacesInName.toPath() ) ) );
}
}
代码示例来源:origin: org.apache.commons/commons-lang3
@Test
public void testEscapeJava() throws IOException {
assertNull(StringEscapeUtils.escapeJava(null));
try {
StringEscapeUtils.ESCAPE_JAVA.translate(null, null);
内容来源于网络,如有侵权,请联系作者删除!