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

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

Introduction

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

Prototype

public static Escaper xmlContentEscaper() 

Source Link

Document

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

Usage

From source file:org.n52.svalbard.write.XmlStreamWriter.java

/**
 * Write characters to stream.//from  w  w  w .ja  v  a  2  s.com
 *
 * @param chars  Characters to write
 * @param escape if the chars should be XML escaped
 *
 * @throws XMLStreamException If an error occurs when writing to {@link OutputStream}
 */
protected void chars(String chars, boolean escape) throws XMLStreamException {
    this.writer.writeCharacters(escape ? XmlEscapers.xmlContentEscaper().escape(chars) : chars);
}

From source file:org.languagetool.tools.StringTools.java

/**
 * @since 2.9
 */
public static String escapeForXmlContent(String s) {
    return XmlEscapers.xmlContentEscaper().escape(s);
}

From source file:edu.ucsf.rbvi.chemViz2.internal.depict.SvgDrawVisitor.java

private void visit(TextElement elem) {
    appendIdent();//from  ww  w  . j  av a 2s.c  o  m
    double[] points = new double[] { elem.xCoord, elem.yCoord };
    transform(points, 1);
    sb.append("<text ");
    sb.append(" x='").append(toStr(points[0])).append("'");
    sb.append(" y='").append(toStr(points[1])).append("'");
    sb.append(" fill='").append(toStr(elem.color)).append("'");
    sb.append(" text-anchor='middle'");
    // todo need font manager for scaling...
    sb.append(">");
    sb.append(XmlEscapers.xmlContentEscaper().escape(elem.text));
    sb.append("</text>\n");
}

From source file:architecture.ee.util.xml.XmlProperties.java

/**
 * Sets a property to an array of values. Multiple values matching the same
 * property is mapped to an XML file as multiple elements containing each
 * value. For example, using the name "foo.bar.prop", and the value string
 * array containing {"some value", "other value", "last value"} would
 * produce the following XML://  w w w.  ja v a2s. co  m
 * 
 * <pre>
 * &lt;foo&gt;
 *     &lt;bar&gt;
 *         &lt;prop&gt;some value&lt;/prop&gt;
 *         &lt;prop&gt;other value&lt;/prop&gt;
 *         &lt;prop&gt;last value&lt;/prop&gt;
 *     &lt;/bar&gt;
 * &lt;/foo&gt;
 * </pre>
 *
 * @param name
 *            the name of the property.
 * @param values
 *            the values for the property (can be empty but not null).
 */
public void setProperties(String name, List<String> values) {
    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy,
    // stopping one short.
    Element element = document.getRootElement();
    for (int i = 0; i < propName.length - 1; i++) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(propName[i]) == null) {
            element.addElement(propName[i]);
        }
        element = element.element(propName[i]);
    }
    String childName = propName[propName.length - 1];
    // We found matching property, clear all children.
    List<Element> toRemove = new ArrayList<Element>();
    Iterator iter = element.elementIterator(childName);
    while (iter.hasNext()) {
        toRemove.add((Element) iter.next());
    }
    for (iter = toRemove.iterator(); iter.hasNext();) {
        element.remove((Element) iter.next());
    }
    // Add the new children.
    for (String value : values) {
        Element childElement = element.addElement(childName);
        if (value.startsWith("<![CDATA[")) {
            Iterator it = childElement.nodeIterator();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                if (node instanceof CDATA) {
                    childElement.remove(node);
                    break;
                }
            }
            childElement.addCDATA(value.substring(9, value.length() - 3));
        } else {
            childElement.setText(XmlEscapers.xmlContentEscaper().escape(value));// StringEscapeUtils.escapeXml(value));
        }
    }
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", values);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);

}

From source file:architecture.ee.util.xml.XmlProperties.java

/**
 * Sets the value of the specified property. If the property doesn't
 * currently exist, it will be automatically created.
 *
 * @param name//ww  w  .j av  a2  s  . c  o m
 *            the name of the property to set.
 * @param value
 *            the new value for the property.
 */
public synchronized void setProperty(String name, String value) {

    if (!XmlEscapers.xmlContentEscaper().escape(name).equals(name)) {
        throw new IllegalArgumentException();// L10NUtils.getMessage("002155"));
    }
    if (name == null) {
        return;
    }
    if (value == null) {
        value = "";
    }

    // Set cache correctly with prop name and value.
    propertyCache.put(name, value);

    String[] propName = parsePropertyName(name);
    // Search for this property by traversing down the XML heirarchy.
    Element element = document.getRootElement();
    for (String aPropName : propName) {
        // If we don't find this part of the property in the XML heirarchy
        // we add it as a new node
        if (element.element(aPropName) == null) {
            element.addElement(aPropName);
        }
        element = element.element(aPropName);
    }
    // Set the value of the property in this node.
    if (value.startsWith("<![CDATA[")) {
        Iterator it = element.nodeIterator();
        while (it.hasNext()) {
            Node node = (Node) it.next();
            if (node instanceof CDATA) {
                element.remove(node);
                break;
            }
        }
        element.addCDATA(value.substring(9, value.length() - 3));
    } else {
        element.setText(value);
    }
    // Write the XML properties to disk
    saveProperties();

    // Generate event.
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("value", value);

    // PropertyEventDispatcher.dispatchEvent(name,
    // PropertyEventDispatcher.EventType.xml_property_set, params);
}