use of com.google.common.escape.Escaper in project ANNIS by korpling.
the class GuavaEscaperMarshallerProvider method getContext.
@Override
public Marshaller getContext(Class<?> type) {
JAXBContext context = contextCache.computeIfAbsent(type, new Function<Class<?>, JAXBContext>() {
@Override
public JAXBContext apply(Class<?> t) {
try {
return JAXBContext.newInstance(t);
} catch (JAXBException ex) {
log.error("Can't create JAXB context", ex);
}
return null;
}
});
if (context != null) {
try {
Marshaller m = context.createMarshaller();
m.setProperty("com.sun.xml.bind.characterEscapeHandler", new CharacterEscapeHandler() {
@Override
public void escape(char[] ch, int start, int length, boolean isAttVal, Writer out) throws IOException {
String asString = new String(ch, start, length);
Escaper e = isAttVal ? XmlEscapers.xmlAttributeEscaper() : XmlEscapers.xmlContentEscaper();
out.write(e.escape(asString));
}
});
return m;
} catch (JAXBException ex) {
log.error("Can't create XML marshaller", ex);
}
}
return null;
}
use of com.google.common.escape.Escaper in project ANNIS by korpling.
the class AnnotationConditionProvider method addAnnotationConditions.
/**
* Adds annotation conditions for a single node.
* @param conditions Condition list where the conditions should be added to
* @param index Index for a specific annotation
* @param annotation The annotation to add
* @param table Table to operate on
* @param tas {@link TableAccessStrategy} for the given node.
*/
public void addAnnotationConditions(Collection<String> conditions, int index, QueryAnnotation annotation, String table, TableAccessStrategy tas) {
TextMatching tm = annotation.getTextMatching();
String column = annotation.getNamespace() == null ? "annotext" : "qannotext";
Escaper escaper = tm != null && tm.isRegex() ? regexEscaper : likeEscaper;
String val;
if (tm == null) {
val = "%";
} else {
val = escaper.escape(annotation.getValue());
}
String prefix;
if (annotation.getNamespace() == null) {
prefix = escaper.escape(annotation.getName()) + ":";
} else {
prefix = escaper.escape(annotation.getNamespace()) + ":" + escaper.escape(annotation.getName()) + ":";
}
if (tm == null || tm == TextMatching.EXACT_EQUAL) {
conditions.add(tas.aliasedColumn(table, column, index) + " LIKE '" + prefix + val + "'");
} else if (tm == TextMatching.EXACT_NOT_EQUAL) {
conditions.add(tas.aliasedColumn(table, column, index) + " LIKE '" + prefix + "%'");
conditions.add(tas.aliasedColumn(table, column, index) + " NOT LIKE '" + prefix + val + "'");
} else if (tm == TextMatching.REGEXP_EQUAL) {
conditions.add(tas.aliasedColumn(table, column, index) + " ~ '^(" + prefix + "(" + val + "))$'");
} else if (tm == TextMatching.REGEXP_NOT_EQUAL) {
conditions.add(tas.aliasedColumn(table, column, index) + " LIKE '" + prefix + "%'");
conditions.add(tas.aliasedColumn(table, column, index) + " !~ '^(" + prefix + "(" + val + "))$'");
}
}
use of com.google.common.escape.Escaper in project helios by spotify.
the class HeliosClient method path.
private String path(final String resource, final Object... params) {
final String path;
final Escaper escaper = UrlEscapers.urlPathSegmentEscaper();
if (params.length == 0) {
path = resource;
} else {
final List<String> encodedParams = Lists.newArrayList();
for (final Object param : params) {
encodedParams.add(escaper.escape(param.toString()));
}
path = format(resource, encodedParams.toArray());
}
return path;
}
use of com.google.common.escape.Escaper in project oap by oaplatform.
the class UrlPerformance method encode.
@Test
public void encode() {
String value = "sdihgjf sdkgh dsfkjgh?&skfjh ?&. \tdkjhgf&";
Escaper escaper = UrlEscapers.urlFormParameterEscaper();
assertThat(URLEncoder.encode(value, UTF_8)).isEqualTo(escaper.escape(value));
benchmark("URLEncoder.encode", SAMPLES, () -> URLEncoder.encode(value, UTF_8)).run();
// benchmark( "CharEscapers.escapeUri", SAMPLES,
// () -> CharEscapers.escapeUriConformant( value )
// ).run();
benchmark("UrlEscaper.escape", SAMPLES, () -> escaper.escape(value)).run();
}
use of com.google.common.escape.Escaper in project mylyn.docs by eclipse.
the class PotentialBracketEndDelimiterTest method replaceHtmlEntities.
@Test
public void replaceHtmlEntities() {
PotentialBracketEndDelimiter delimiter = new PotentialBracketEndDelimiter(line, 0);
Escaper escaper = UrlEscapers.urlFormParameterEscaper();
assertEquals("asf", delimiter.replaceHtmlEntities("asf", escaper));
assertEquals("&", delimiter.replaceHtmlEntities("&", escaper));
assertEquals("& ;", delimiter.replaceHtmlEntities("& ;", escaper));
assertEquals("%26", delimiter.replaceHtmlEntities("&", escaper));
assertEquals("a%26", delimiter.replaceHtmlEntities("a&", escaper));
assertEquals("a%26b", delimiter.replaceHtmlEntities("a&b", escaper));
assertEquals("%C3%A4", delimiter.replaceHtmlEntities("ä", escaper));
assertEquals("&", delimiter.replaceHtmlEntities("&", null));
assertEquals("\"", delimiter.replaceHtmlEntities(""", null));
assertEquals("\u00e4", delimiter.replaceHtmlEntities("ä", null));
assertEquals("&xdfsldk;", delimiter.replaceHtmlEntities("&xdfsldk;", null));
assertEquals("&0;", delimiter.replaceHtmlEntities("&0;", null));
}
Aggregations