Here you can find the source of textToXML(String text)
Parameter | Description |
---|---|
text | The string to convert. |
public static final String textToXML(String text)
//package com.java2s; /*//from ww w . ja v a2s .c o m * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. The ASF licenses this file to You * 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. For additional information regarding * copyright in this work, please see the NOTICE file in the top level * directory of this distribution. */ public class Main { /** * Converts a text string to XML entities. * * @param text The string to convert. * @author Erik C. Thauvin * @return The converted string. */ public static final String textToXML(String text) { return textToHTML(text, true); } /** * Converts a text string to HTML or XML entities. * * @author Erik C. Thauvin * @param text The string to convert. * @param xml Convert the string to XML if set to true. * * @return The converted string. */ public static final String textToHTML(String text, boolean xml) { if (text == null) return "null"; final StringBuffer html = new StringBuffer(); // Loop thru each characters of the text for (int i = 0; i < text.length(); i++) { // Convert character to HTML/XML html.append(charToHTML(text.charAt(i), xml)); } // Return HTML/XML string return html.toString(); } /** * Converts a text string to HTML or XML entities. * * @param text The string to convert. * @author Erik C. Thauvin * @return The converted string. */ public static final String textToHTML(String text) { return textToHTML(text, false); } /** * Converts a character to HTML or XML entity. * * @param ch The character to convert. * @param xml Convert the character to XML if set to true. * @author Erik C. Thauvin * * @return The converted string. */ public static final String charToHTML(char ch, boolean xml) { int c; // Convert left bracket if (ch == '<') { return ("<"); } // Convert left bracket else if (ch == '>') { return (">"); } // Convert ampersand else if (ch == '&') { return ("&"); } // Commented out to eliminate redundant numeric character codes (ROL-507) // High-ASCII character //else if (ch >= 128) //{ //c = ch; //return ("&#" + c + ';'); //} // Convert double quote else if (xml && (ch == '"')) { return ("""); } // Convert single quote else if (xml && (ch == '\'')) { return ("'"); } // No conversion else { // Return character as string return (String.valueOf(ch)); } } }