Example usage for com.google.common.xml XmlEscapers xmlAttributeEscaper

List of usage examples for com.google.common.xml XmlEscapers xmlAttributeEscaper

Introduction

In this page you can find the example usage for com.google.common.xml XmlEscapers xmlAttributeEscaper.

Prototype

public static Escaper xmlAttributeEscaper() 

Source Link

Document

Returns an Escaper instance that escapes special characters in a string so it can safely be included in XML document as an attribute value.

Usage

From source file:org.liberty.android.fantastischmemo.downloader.google.DocumentFactory.java

public static Document createSpreadsheet(String title, String authToken)
        throws XmlPullParserException, IOException {
    URL url = new URL("https://docs.google.com/feeds/default/private/full?access_token=" + authToken);

    String payload = "<?xml version='1.0' encoding='UTF-8'?>" + "<entry xmlns='http://www.w3.org/2005/Atom'>"
            + "<category scheme='http://schemas.google.com/g/2005#kind'"
            + " term='http://schemas.google.com/docs/2007#spreadsheet'/>" + "<title>"
            + XmlEscapers.xmlAttributeEscaper().escape(title) + "</title>" + "</entry>";

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoInput(true);//  www.j av  a 2s  . c  o  m
    conn.setDoOutput(true);
    //conn.addRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.addRequestProperty("GData-Version", "3.0");
    conn.addRequestProperty("Content-Type", "application/atom+xml");
    conn.setRequestProperty("Content-Length", Integer.toString(payload.getBytes("UTF-8").length));

    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
    out.write(payload);
    out.close();

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new RuntimeException(s);
    }

    List<Document> documentList = EntryFactory.getEntries(Document.class, conn.getInputStream());

    return documentList.get(0);
}

From source file:com.bsiag.geneclipsetoc.internal.contexts.ContextUtility.java

public static String toXml(List<Context> contexts) {
    StringBuilder sb = new StringBuilder();
    sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    sb.append(NEW_LINE);/*w ww.j  a v a  2s. co m*/
    sb.append("<?NLS TYPE=\"org.eclipse.help.contexts\"?>");
    sb.append(NEW_LINE);
    sb.append("<contexts>");
    if (contexts != null) {
        for (Context context : contexts) {
            sb.append(NEW_LINE);
            sb.append(INDENTATION);
            sb.append("<context");
            appendAttr(sb, "id", context.getId());
            appendAttr(sb, "title", context.getTitle());
            sb.append(">");
            if (context.getDescription() != null && context.getDescription().length() > 0) {
                sb.append(NEW_LINE);
                sb.append(INDENTATION);
                sb.append(INDENTATION);
                sb.append("<description>");
                Splitter splitter = Splitter.onPattern("\r?\n");
                boolean needNewLine = false;
                for (String line : splitter.split(context.getDescription())) {
                    if (needNewLine) {
                        sb.append(NEW_LINE);
                    }
                    sb.append(XmlEscapers.xmlAttributeEscaper().escape(line));
                    needNewLine = true;
                }
                sb.append("</description>");
            }
            if (context.getTopics() != null) {
                for (Topic topic : context.getTopics()) {
                    sb.append(NEW_LINE);
                    sb.append(INDENTATION);
                    sb.append(INDENTATION);
                    sb.append("<topic");
                    appendAttr(sb, "label", topic.getLabel());
                    appendAttr(sb, "href", topic.getHref());
                    sb.append("/>");
                }
            }
            sb.append(NEW_LINE);
            sb.append(INDENTATION);
            sb.append("</context>");
        }
    }
    sb.append(NEW_LINE);
    sb.append("</contexts>");
    return sb.toString();
}

From source file:forms.api.RockerRaw.java

/** Appends HTML attributes.  Pass null as the value for a boolean attribute. */
public RockerRaw appendAttr(String... attributes) {
    Preconditions.checkArgument(attributes.length % 2 == 0);
    for (int i = 0; i < attributes.length / 2; ++i) {
        String key = attributes[2 * i];
        String value = attributes[2 * i + 1];
        appendRaw(" ");
        appendRaw(key);/*from   w w  w . j  ava 2 s .  c o  m*/
        if (value != null) {
            appendRaw("=\"");
            appendRaw(XmlEscapers.xmlAttributeEscaper().escape(value));
            appendRaw("\"");
        }
    }
    return this;
}

From source file:com.google.template.soy.xliffmsgplugin.XliffGenerator.java

/**
 * Generates the output XLIFF file content for a given SoyMsgBundle.
 *
 * @param msgBundle The SoyMsgBundle to process.
 * @param sourceLocaleString The source language/locale string of the messages.
 * @param targetLocaleString The target language/locale string of the messages (optional). If
 *     specified, the resulting XLIFF file will specify this target language and will contain
 *     empty 'target' tags. If not specified, the resulting XLIFF file will not contain target
 *     language and will not contain 'target' tags.
 * @return The generated XLIFF file content.
 *//*w  w w. j  a va  2  s  .co m*/
static CharSequence generateXliff(SoyMsgBundle msgBundle, String sourceLocaleString,
        @Nullable String targetLocaleString) {

    Escaper attributeEscaper = XmlEscapers.xmlAttributeEscaper();
    Escaper contentEscaper = XmlEscapers.xmlContentEscaper();

    boolean hasTarget = targetLocaleString != null && targetLocaleString.length() > 0;

    IndentedLinesBuilder ilb = new IndentedLinesBuilder(2);
    ilb.appendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
    ilb.appendLine("<xliff version=\"1.2\" xmlns=\"urn:oasis:names:tc:xliff:document:1.2\">");
    ilb.increaseIndent();
    ilb.appendLineStart("<file original=\"SoyMsgBundle\" datatype=\"x-soy-msg-bundle\"",
            " xml:space=\"preserve\"", " source-language=\"", attributeEscaper.escape(sourceLocaleString),
            "\"");
    if (hasTarget) {
        ilb.appendParts(" target-language=\"", attributeEscaper.escape(targetLocaleString), "\"");
    }
    ilb.appendLineEnd(">");
    ilb.increaseIndent();
    ilb.appendLine("<body>");
    ilb.increaseIndent();

    for (SoyMsg msg : msgBundle) {

        // Begin 'trans-unit'.
        ilb.appendLineStart("<trans-unit id=\"", Long.toString(msg.getId()), "\"");
        String contentType = msg.getContentType();
        if (contentType != null && contentType.length() > 0) {
            String xliffDatatype = CONTENT_TYPE_TO_XLIFF_DATATYPE_MAP.get(contentType);
            if (xliffDatatype == null) {
                xliffDatatype = contentType; // just use the contentType string
            }
            ilb.appendParts(" datatype=\"", attributeEscaper.escape(xliffDatatype), "\"");
        }
        ilb.appendLineEnd(">");
        ilb.increaseIndent();

        // Source.
        ilb.appendLineStart("<source>");
        for (SoyMsgPart msgPart : msg.getParts()) {
            if (msgPart instanceof SoyMsgRawTextPart) {
                String rawText = ((SoyMsgRawTextPart) msgPart).getRawText();
                ilb.append(contentEscaper.escape(rawText));
            } else {
                String placeholderName = ((SoyMsgPlaceholderPart) msgPart).getPlaceholderName();
                ilb.appendParts("<x id=\"", attributeEscaper.escape(placeholderName), "\"/>");
            }
        }
        ilb.appendLineEnd("</source>");

        // Target.
        if (hasTarget) {
            ilb.appendLine("<target/>");
        }

        // Description and meaning.
        String desc = msg.getDesc();
        if (desc != null && desc.length() > 0) {
            ilb.appendLine("<note priority=\"1\" from=\"description\">", contentEscaper.escape(desc),
                    "</note>");
        }
        String meaning = msg.getMeaning();
        if (meaning != null && meaning.length() > 0) {
            ilb.appendLine("<note priority=\"1\" from=\"meaning\">", contentEscaper.escape(meaning), "</note>");
        }

        // End 'trans-unit'.
        ilb.decreaseIndent();
        ilb.appendLine("</trans-unit>");
    }

    ilb.decreaseIndent();
    ilb.appendLine("</body>");
    ilb.decreaseIndent();
    ilb.appendLine("</file>");
    ilb.decreaseIndent();
    ilb.appendLine("</xliff>");

    return ilb;
}

From source file:ch.acanda.eclipse.pmd.repository.RuleSetConfigurationToXMLTag.java

@Override
public String apply(final RuleSetModel config) {
    final Escaper escaper = XmlEscapers.xmlAttributeEscaper();
    final String name = escaper.escape(nullToEmpty(config.getName()));
    final String ref = escaper.escape(nullToEmpty(config.getLocation().getPath()));
    final String refcontext = getContext(config);
    return String.format(Locale.ENGLISH, "<%s %s=\"%s\" %s=\"%s\" %s=\"%s\" />", TAG_NAME_RULESET,
            ATTRIBUTE_NAME_NAME, name, ATTRIBUTE_NAME_REF, ref, ATTRIBUTE_NAME_REFCONTEXT, refcontext);
}

From source file:org.liberty.android.fantastischmemo.downloader.google.FolderFactory.java

public static Folder createFolder(String title, String authToken) throws XmlPullParserException, IOException {
    URL url = new URL("https://docs.google.com/feeds/default/private/full?access_token=" + authToken);

    String payload = "<?xml version='1.0' encoding='UTF-8'?>" + "<entry xmlns='http://www.w3.org/2005/Atom'>"
            + "<category scheme='http://schemas.google.com/g/2005#kind'"
            + " term='http://schemas.google.com/docs/2007#folder'/>" + "<title>"
            + XmlEscapers.xmlAttributeEscaper().escape(title) + "</title>" + "</entry>";

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoInput(true);/*from   www. ja  v a 2  s.c o  m*/
    conn.setDoOutput(true);
    //conn.addRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.addRequestProperty("GData-Version", "3.0");
    conn.addRequestProperty("Content-Type", "application/atom+xml");
    conn.setRequestProperty("Content-Length", Integer.toString(payload.getBytes("UTF-8").length));

    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
    out.write(payload);
    out.close();

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new RuntimeException(s);
    }

    List<Folder> folderList = EntryFactory.getEntries(Folder.class, conn.getInputStream());

    return folderList.get(0);
}

From source file:io.sarl.lang.mwe2.externalspec.AbstractXmlHighlightingFragment2.java

/** Open the tag.
 *
 * @param tag the name of the tag.// w  w  w  . j  a v  a 2s .  c  om
 * @param nameValuePairs the parameters of the tag (name-value pairs).
 * @see #close()
 */
protected void open(String tag, String... nameValuePairs) {
    StringBuilder line = new StringBuilder();
    line.append(indent());
    line.append("<").append(tag); //$NON-NLS-1$
    for (int i = 0; i < nameValuePairs.length; i = i + 2) {
        line.append(" "); //$NON-NLS-1$
        line.append(nameValuePairs[i]);
        line.append("=\""); //$NON-NLS-1$
        line.append(XmlEscapers.xmlAttributeEscaper().escape(nameValuePairs[i + 1]));
        line.append("\""); //$NON-NLS-1$
    }
    line.append(">"); //$NON-NLS-1$
    this.openedContexts.add(tag);
    this.lines.add(line.toString());
}

From source file:com.bsiag.geneclipsetoc.internal.contexts.ContextUtility.java

private static void appendAttr(StringBuilder sb, String attribute, String value) {
    if (value != null && value.length() > 0) {
        sb.append(" ");
        sb.append(attribute);/*from w  w  w  . j a  v a  2 s .com*/
        sb.append("=\"");
        sb.append(XmlEscapers.xmlAttributeEscaper().escape(value));
        sb.append("\"");
    }
}

From source file:org.eobjects.analyzer.beans.codec.XmlEncoderTransformer.java

@Override
public String[] transform(final InputRow inputRow) {
    final String value = inputRow.getValue(column);
    if (value == null) {
        return new String[1];
    }/*w  w  w .  jav a2 s. c o m*/
    final Escaper escaper;
    if (targetFormat == TargetFormat.Content) {
        escaper = XmlEscapers.xmlContentEscaper();
    } else {
        escaper = XmlEscapers.xmlAttributeEscaper();
    }
    final String escaped = escaper.escape(value);
    return new String[] { escaped };
}

From source file:org.roda.core.common.MetadataFileUtils.java

public static String escapeAttribute(String value) {
    return XmlEscapers.xmlAttributeEscaper().escape(value);
}