org.apache.xml.utils.XMLChar Java Examples

The following examples show how to use org.apache.xml.utils.XMLChar. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserTextFilterForXml.java    From rice with Educational Community License v2.0 6 votes vote down vote up
/**
 * Removes invalid XML 1.0 Unicode characters
 * @param text The String to clean
 * @return The resulting String
 */
public static String cleanInvalidXmlChars(String text) {
    if (StringUtils.isBlank(text)) {
        return text;
    } else {
        StringBuilder sb = new StringBuilder();
        boolean strippedCharacter = false;
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (XMLChar.isValid(c)) {
                sb.append(c);
            } else {
                strippedCharacter = true;
            }
        }

        if (strippedCharacter) {
            return sb.toString();
        } else {
            return text;
        }
    }
}
 
Example #2
Source File: StringUtil.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks, whether string contains invalid xml chars
 * @param string to check
 * @return true if there're xml invalid chars
 */
public static Boolean containsInvalidXmlChars2(String string) {
    for (int i = 0; i < string.length(); i++) {
        if (!XMLChar.isValid(string.charAt(i))) {
            return Boolean.TRUE;
        }
    }
    return Boolean.FALSE;
}
 
Example #3
Source File: MCRMetaLink.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * This method set the xlink:label
 * 
 * @param label
 *            the xlink:label
 */
public final void setXLinkLabel(String label) {
    if (label != null && !XMLChar.isValidNCName(label)) {
        throw new MCRException("xlink:label is not a valid NCName: " + label);
    }
    this.label = label;
}
 
Example #4
Source File: MCRMetaLink.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Validates this MCRMetaLink. This method throws an exception if:
 * <ul>
 * <li>the subtag is not null or empty</li>
 * <li>the xlink:type not "locator" or "arc"</li>
 * <li>the from or to are not valid</li>
 * </ul>
 * 
 * @throws MCRException the MCRMetaLink is invalid
 */
public void validate() throws MCRException {
    super.validate();
    if (label != null && label.length() > 0) {
        if (!XMLChar.isValidNCName(label)) {
            throw new MCRException(getSubTag() + ": label is no valid NCName:" + label);
        }
    }
    if (linktype == null) {
        throw new MCRException(getSubTag() + ": linktype is null");
    }
    if (!linktype.equals("locator") && !linktype.equals("arc")) {
        throw new MCRException(getSubTag() + ": linktype is unsupported: " + linktype);
    }
    if (linktype.equals("arc")) {
        if (from == null || from.length() == 0) {
            throw new MCRException(getSubTag() + ": from is null or empty");
        } else if (!XMLChar.isValidNCName(from)) {
            throw new MCRException(getSubTag() + ": from is no valid NCName:" + from);
        }

        if (to == null || to.length() == 0) {
            throw new MCRException(getSubTag() + ": to is null or empty");
        } else if (!XMLChar.isValidNCName(to)) {
            throw new MCRException(getSubTag() + ": to is no valid NCName:" + to);
        }
    }
    if (linktype.equals("locator")) {
        if (href == null || href.length() == 0) {
            throw new MCRException(getSubTag() + ": href is null or empty");
        }
    }
}
 
Example #5
Source File: MCRXMLFunctions.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Converts a string to valid NCName.
 *
 * @see <a href="https://www.w3.org/TR/1999/WD-xmlschema-2-19990924/#NCName">w3.org</a>
 *
 * @param name the string to convert
 * @return a string which is a valid NCName
 * @throws IllegalArgumentException if there is no way to convert the string to an NCName
 */
public static String toNCName(String name) {
    while (name.length() > 0 && !XMLChar.isNameStart(name.charAt(0))) {
        name = name.substring(1);
    }
    name = toNCNameSecondPart(name);
    if (name.length() == 0) {
        throw new IllegalArgumentException("Unable to convert '" + name + "' to valid NCName.");
    }
    return name;
}
 
Example #6
Source File: AbstractToken.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Checks a string to see if any characters are invalid for inclusion in 
 * XML strings.
 * @param value     The string to check.
 * @return  A parse result of success if the string uses only valid characters.
 */
protected ParseResult checkForInvalidXMLChars(String value)
{
	for (char character : value.toCharArray())
	{
		if (!XMLChar.isValid(character))
		{
			return new ParseResult.Fail(
				"Invalid XML character 0x" + Integer.toString(character, 16) + " in " + value);
		}
	}

	return ParseResult.SUCCESS;
}
 
Example #7
Source File: AbstractToken.java    From pcgen with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Checks a string to see if any characters are invalid for inclusion in 
 * XML strings.
 * @param value     The string to check.
 * @return  A parse result of success if the string uses only valid characters.
 */
protected ParseResult checkForInvalidXMLChars(String value)
{
	for (char character : value.toCharArray())
	{
		if (!XMLChar.isValid(character))
		{
			return new ParseResult.Fail(
				"Invalid XML character 0x" + Integer.toString(character, 16) + " in " + value);
		}
	}

	return ParseResult.SUCCESS;
}
 
Example #8
Source File: StringUtil.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Checks, whether string contains invalid xml chars
 * @param string to check
 * @return true if there're xml invalid chars
 */
public static Boolean containsInvalidXmlChars2(String string) {
    for (int i = 0; i < string.length(); i++) {
        if (!XMLChar.isValid(string.charAt(i))) {
            return Boolean.TRUE;
        }
    }
    return Boolean.FALSE;
}