All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.prowidesoftware.swift.model.mx.DefaultEscapeHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2006-2023 Prowide
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.prowidesoftware.swift.model.mx;

/**
 * Escapes &, <, >, quotes (in attribute) and everything above the US-ASCII code range.
 * Similar to com.sun.xml.bind.marshaller.DumbEscapeHandler or apache.commons.text.StringEscapeUtils#escapeXml
 *
 * @since 9.1.7
 */
public class DefaultEscapeHandler implements EscapeHandler {

    @Override
    public String escape(char[] arr, boolean isAttribute) {
        final StringBuilder sb = new StringBuilder(arr.length);
        for (int i = 0; i < arr.length; i++) {
            switch (arr[i]) {
                case '&':
                    sb.append("&");
                    break;
                case '<':
                    sb.append("<");
                    break;
                case '>':
                    sb.append(">");
                    break;
                case '\"':
                    if (isAttribute) {
                        sb.append(""");
                    } else {
                        sb.append('\"');
                    }
                    break;
                default:
                    if (arr[i] > '\u007f') {
                        sb.append("&#");
                        sb.append(Integer.toString(arr[i]));
                        sb.append(';');
                    } else {
                        sb.append(arr[i]);
                    }
            }
        }
        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy