Example usage for com.google.common.base CharEscapers xmlEscaper

List of usage examples for com.google.common.base CharEscapers xmlEscaper

Introduction

In this page you can find the example usage for com.google.common.base CharEscapers xmlEscaper.

Prototype

public static CharEscaper xmlEscaper() 

Source Link

Document

Returns a CharEscaper instance that escapes special characters in a string so it can safely be included in an XML document in either element content or attribute values.

Usage

From source file:com.google.gxp.base.MarkupAppender.java

/**
 * Subclasses should override this method if they want to escape in a non
 * standard way.//  w  w  w  .j a  v a 2s . c o  m
 *
 * @return a {@code CharEscaper} to use for escaping unescaped character
 * sequences.
 */
protected CharEscaper getCharEscaper(GxpContext gxpContext) {
    return CharEscapers.xmlEscaper();
}

From source file:com.google.transconsole.common.messages.BaseMessage.java

protected void appendAttribute(StringBuilder sb, String name, String value) {
    if (value == null) {
        return;/*from  w w  w.  j ava 2  s . com*/
    }

    sb.append(" ");
    sb.append(name);
    sb.append("=\"");
    sb.append(CharEscapers.xmlEscaper().escape(value));
    sb.append("\"");
}

From source file:com.google.transconsole.common.messages.Placeholder.java

/**
 * Returns an XML fragment representing the placeholder in the XMB format.
 *
 * If the placeholder was constructed with only its presentation, a
 * placeholder in the XTB format is returned instead.
 *
 * The format is described in/*from w w w .  ja  v  a  2 s  .c o m*/
 *
 * /home/build/nonconf/google3/i18n/messagebundle.dtd.
 * /home/build/nonconf/google3/i18n/translationbundle.dtd.
 *
 * @return xml fragment
 */
public String toXml(BundleFormat format) {
    StringBuilder sb = new StringBuilder();

    sb.append("<ph name=\"");
    sb.append(CharEscapers.xmlEscaper().escape(presentation));
    sb.append("\"");
    if (format.equals(BundleFormat.XMB)) {
        sb.append(">");
        if (example != null) {
            sb.append("<ex>");
            sb.append(CharEscapers.xmlContentEscaper().escape(example));
            sb.append("</ex>");
        }
        if (original != null) {
            sb.append(CharEscapers.xmlContentEscaper().escape(original));
        }
        sb.append("</ph>");
    } else {
        sb.append("/>");
    }

    return sb.toString();
}

From source file:com.google.gxp.compiler.errortests.BaseTestCase.java

/**
 * Givin a prefix and suffix, constructs and compiles GXPs containing the
 * prefix followed by an (XML-escaped) expression, followed by the suffix.
 * Several legal and illegal expressions are attempted. Legal expressions
 * should generete no alerts, while illegal expressions should generate
 * specific alerts./*from   w w w  .j a va 2 s.co m*/
 */
public final void assertIllegalExpressionDetected(String prefix, String suffix, int errorLine, int errorColumn)
        throws Exception {
    for (String legalExpr : LEGAL_EXPRESSIONS) {
        compile(prefix + CharEscapers.xmlEscaper().escape(legalExpr) + suffix);
        assertNoUnexpectedAlerts();
    }

    SourcePosition errorPos = pos(errorLine, errorColumn);

    for (IllegalOperatorExpression illegalOp : ILLEGAL_OPERATORS) {
        compile(prefix + CharEscapers.xmlEscaper().escape(illegalOp.getExpression()) + suffix);
        for (OutputLanguage outputLanguage : illegalOp.getOutputLanguages()) {
            assertAlert(
                    new IllegalOperatorError(errorPos, outputLanguage.getDisplay(), illegalOp.getOperator()));
        }
        assertNoUnexpectedAlerts();
    }

    for (Map.Entry<String, Collection<OutputLanguage>> illegalExpr : ILLEGAL_EXPRESSIONS.entrySet()) {
        compile(prefix + CharEscapers.xmlEscaper().escape(illegalExpr.getKey()) + suffix);
        for (OutputLanguage outputLanguage : illegalExpr.getValue()) {
            assertAlert(
                    new IllegalExpressionError(errorPos, outputLanguage.getDisplay(), illegalExpr.getKey()));
        }
        assertNoUnexpectedAlerts();
    }
}

From source file:com.google.gxp.compiler.errortests.BaseTestCase.java

/**
 * Givin a prefix and suffix, constructs and compiles GXPs containing the
 * prefix followed by an (XML-escaped) name, followed by the suffix.
 * Several legal and illegal names are attempted. Legal names
 * should generetae no alerts, while illegal names should generate
 * specific alerts./*from   w  ww .  j  a  v  a  2 s . co m*/
 */
public final void assertIllegalVariableNameDetected(String attrName, String prefix, String suffix)
        throws Exception {
    for (String legalName : LEGAL_VAR_NAMES) {
        compile(prefix + CharEscapers.xmlEscaper().escape(legalName) + suffix);
        assertNoUnexpectedAlerts();
    }

    for (String illegalName : ILLEGAL_VAR_NAMES) {
        compile(prefix + CharEscapers.xmlEscaper().escape(illegalName) + suffix);
        SourcePosition errorPos = pos(2, 1);
        assertAlert(new IllegalVariableNameError(errorPos, illegalName));
        assertNoUnexpectedAlerts();
    }

    for (Map.Entry<String, Collection<OutputLanguage>> illegalName : ILLEGAL_OUTPUT_LANGUAGE_NAMES.entrySet()) {
        compile(prefix + CharEscapers.xmlEscaper().escape(illegalName.getKey()) + suffix);
        SourcePosition errorPos = pos(2, 1);
        for (OutputLanguage outputLanguage : illegalName.getValue()) {
            assertAlert(new IllegalNameError(errorPos, outputLanguage.getDisplay(), illegalName.getKey()));
        }
        assertNoUnexpectedAlerts();
    }
}

From source file:com.google.gxp.compiler.errortests.BaseTestCase.java

/**
 * Givin a prefix and suffix, constructs and compiles GXPs containing the
 * prefix followed by an (XML-escaped) type, followed by the suffix.
 * Several legal and illegal types are attempted. Legal types
 * should generetae no alerts, while illegal types should generate
 * specific alerts.//from  w  ww  . j  a v a 2  s .c  o  m
 */
public final void assertIllegalTypeDetected(String prefix, String suffix) throws Exception {
    for (TestType testType : TEST_TYPES) {
        compile(prefix + CharEscapers.xmlEscaper().escape(testType.getType()) + suffix);
        SourcePosition errorPos = pos(2, 1);
        for (OutputLanguage outputLanguage : testType.getIllegalIn()) {
            assertAlert(new IllegalTypeError(errorPos, outputLanguage.getDisplay(), testType.getType()));
        }
        assertNoUnexpectedAlerts();
    }
}