Search in sources :

Example 11 with Escaper

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;
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) CharacterEscapeHandler(com.sun.xml.bind.marshaller.CharacterEscapeHandler) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) Escaper(com.google.common.escape.Escaper) Writer(java.io.Writer)

Example 12 with Escaper

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 + "))$'");
    }
}
Also used : TextMatching(annis.model.QueryNode.TextMatching) Escaper(com.google.common.escape.Escaper)

Example 13 with Escaper

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;
}
Also used : Escaper(com.google.common.escape.Escaper)

Example 14 with Escaper

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&amp;";
    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();
}
Also used : Escaper(com.google.common.escape.Escaper) Test(org.testng.annotations.Test)

Example 15 with Escaper

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("&amp", delimiter.replaceHtmlEntities("&amp", escaper));
    assertEquals("&amp ;", delimiter.replaceHtmlEntities("&amp ;", escaper));
    assertEquals("%26", delimiter.replaceHtmlEntities("&amp;", escaper));
    assertEquals("a%26", delimiter.replaceHtmlEntities("a&amp;", escaper));
    assertEquals("a%26b", delimiter.replaceHtmlEntities("a&amp;b", escaper));
    assertEquals("%C3%A4", delimiter.replaceHtmlEntities("&auml;", escaper));
    assertEquals("&", delimiter.replaceHtmlEntities("&amp;", null));
    assertEquals("\"", delimiter.replaceHtmlEntities("&quot;", null));
    assertEquals("\u00e4", delimiter.replaceHtmlEntities("&auml;", null));
    assertEquals("&xdfsldk;", delimiter.replaceHtmlEntities("&xdfsldk;", null));
    assertEquals("&0;", delimiter.replaceHtmlEntities("&0;", null));
}
Also used : Escaper(com.google.common.escape.Escaper) Test(org.junit.Test)

Aggregations

Escaper (com.google.common.escape.Escaper)17 Map (java.util.Map)2 TextMatching (annis.model.QueryNode.TextMatching)1 IndentedLinesBuilder (com.google.template.soy.base.internal.IndentedLinesBuilder)1 SoyMsg (com.google.template.soy.msgs.restricted.SoyMsg)1 SoyMsgPart (com.google.template.soy.msgs.restricted.SoyMsgPart)1 SoyMsgPlaceholderPart (com.google.template.soy.msgs.restricted.SoyMsgPlaceholderPart)1 SoyMsgRawTextPart (com.google.template.soy.msgs.restricted.SoyMsgRawTextPart)1 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)1 Task (com.intellij.openapi.progress.Task)1 Project (com.intellij.openapi.project.Project)1 CharacterEscapeHandler (com.sun.xml.bind.marshaller.CharacterEscapeHandler)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 CancellationException (java.util.concurrent.CancellationException)1 CompletionException (java.util.concurrent.CompletionException)1 JAXBContext (javax.xml.bind.JAXBContext)1 JAXBException (javax.xml.bind.JAXBException)1 Marshaller (javax.xml.bind.Marshaller)1 Test (org.junit.Test)1