Java tutorial
//package com.java2s; /* This file is part of the OWL API. * The contents of this file are subject to the LGPL License, Version 3.0. * Copyright 2014, The University of Manchester * * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/. * * Alternatively, the contents of this file may be used under the terms of the Apache License, Version 2.0 in which case, the provisions of the Apache License Version 2.0 are applicable instead of those above. * 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. */ public class Main { /** &lt; shortcut. */ public static final String LT = "<"; /** &gt; shortcut. */ public static final String GT = ">"; /** &quot; shortcut. */ public static final String QUOT = """; /** &amp; shortcut. */ public static final String AMP = "&"; /** &apos; shortcut. */ public static final String APOS = "'"; /** * Escapes a character sequence so that it is valid XML. * * @param s * The character sequence. * @return The escaped version of the character sequence. */ public static String escapeXML(CharSequence s) { // double quote -- quot // ampersand -- amp // less than -- lt // greater than -- gt // apostrophe -- apos StringBuilder sb = new StringBuilder(s.length() * 2); for (int i = 0; i < s.length();) { int codePoint = Character.codePointAt(s, i); if (codePoint == '<') { sb.append(LT); } else if (codePoint == '>') { sb.append(GT); } else if (codePoint == '\"') { sb.append(QUOT); } else if (codePoint == '&') { sb.append(AMP); } else if (codePoint == '\'') { sb.append(APOS); } else { sb.appendCodePoint(codePoint); } i += Character.charCount(codePoint); } return sb.toString(); } /** * Escapes a subset of a char sequence so that it is valid XML. Escaped or * unchanged characters are added to destination. * * @param chars * chars to check * @param start * start index (inclusive) * @param count * number of characters * @param destination * destination for escaped chars * @return The modified destination. */ public static StringBuilder escapeXML(char[] chars, int start, int count, StringBuilder destination) { // double quote -- quot // ampersand -- amp // less than -- lt // greater than -- gt // apostrophe -- apos for (int i = 0; i < count; i++) { char codePoint = chars[start + i]; if (codePoint == '<') { destination.append(LT); } else if (codePoint == '>') { destination.append(GT); } else if (codePoint == '\"') { destination.append(QUOT); } else if (codePoint == '&') { destination.append(AMP); } else if (codePoint == '\'') { destination.append(APOS); } else { destination.append(codePoint); } } return destination; } /** * Escapes a string builder so that it is valid XML. * * @param sb * The string builder to escape. */ public static void escapeXML(StringBuilder sb) { // double quote -- quot // ampersand -- amp // less than -- lt // greater than -- gt // apostrophe -- apos for (int i = 0; i < sb.length();) { int codePoint = Character.codePointAt(sb, i); int length = Character.charCount(codePoint); if (codePoint == '<') { sb.replace(i, i + length, LT); i += LT.length(); } else if (codePoint == '>') { sb.replace(i, i + length, GT); i += GT.length(); } else if (codePoint == '\"') { sb.replace(i, i + length, QUOT); i += QUOT.length(); } else if (codePoint == '&') { sb.replace(i, i + length, AMP); i += AMP.length(); } else if (codePoint == '\'') { sb.replace(i, i + length, APOS); i += APOS.length(); } else { i += length; } } } }