Example usage for org.jdom2 Verifier isXMLCharacter

List of usage examples for org.jdom2 Verifier isXMLCharacter

Introduction

In this page you can find the example usage for org.jdom2 Verifier isXMLCharacter.

Prototype

public static boolean isXMLCharacter(final int c) 

Source Link

Document

This is a utility function for determining whether a specified character is a character according to production 2 of the XML 1.0 specification.

Usage

From source file:org.mycore.common.xml.MCRXMLHelper.java

License:Open Source License

/**
 * Removes characters that are illegal in XML text nodes or attribute
 * values./*w  w  w.  ja v a  2  s. c o m*/
 * 
 * @param text
 *            the String that should be used in XML elements or attributes
 * @return the String with all illegal characters removed
 */
public static String removeIllegalChars(String text) {
    if (text == null || text.trim().length() == 0) {
        return text;
    }
    if (org.jdom2.Verifier.checkCharacterData(text) == null) {
        return text;
    }

    // It seems we have to filter out invalid XML characters...
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < text.length(); i++) {
        if (Verifier.isXMLCharacter(text.charAt(i))) {
            sb.append(text.charAt(i));
        }
    }
    return sb.toString();
}