001/**
002 * Copyright 2005-2018 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kew.api.util;
017
018import org.apache.commons.lang.StringUtils;
019
020public class UserTextFilterForXml {
021
022    /**
023     * Removes invalid XML 1.0 Unicode characters
024     * @param text The String to clean
025     * @return The resulting String
026     */
027    public static String cleanInvalidXmlChars(String text) {
028        if (StringUtils.isBlank(text)) {
029            return text;
030        } else {
031            StringBuilder sb = new StringBuilder();
032            boolean strippedCharacter = false;
033            for (int i = 0; i < text.length(); i++) {
034                char c = text.charAt(i);
035                if (XMLChar.isValid(c)) {
036                    sb.append(c);
037                } else {
038                    strippedCharacter = true;
039                }
040            }
041
042            if (strippedCharacter) {
043                return sb.toString();
044            } else {
045                return text;
046            }
047        }
048    }
049
050}