Java String Quote quoteSpecialCharacters(final String oldString)

Here you can find the source of quoteSpecialCharacters(final String oldString)

Description

Quotes the special XML characters present in oldString so the return string can be embedded within an XML document without problems.

License

Open Source License

Parameter

Parameter Description
oldString A string that needs its XML special characters quoted.

Return

A string safe for inclusing within XML.

Declaration


public static String quoteSpecialCharacters(final String oldString) 

Method Source Code

//package com.java2s;
/*/*from   w ww .j  a v  a 2s.com*/
 * Created on 16/07/2004
 * YAWLEditor v1.01 
 *
 * @author Lindsay Bradford
 * 
 * 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

public class Main {
    /**
     * Quotes the special XML characters present in oldString so the
     * return string can be embedded within an XML document without 
     * problems.
     * @param oldString A string that needs its XML special characters quoted.
     * @return A string safe for inclusing within XML.
     */

    public static String quoteSpecialCharacters(final String oldString) {
        // small mod of method from http://www.javapractices.com/Topic96.cjp

        // TODO: Fix string quoting between editor and engine.

        // needed now only by initial value special character quoting.

        final StringBuffer quotedString = new StringBuffer();

        if (oldString == null) {
            return null;
        }

        for (int i = 0; i < oldString.length(); i++) {
            if (oldString.charAt(i) == '<') {
                quotedString.append("&lt;");
            } else if (oldString.charAt(i) == '>') {
                quotedString.append("&gt;");
            } else if (oldString.charAt(i) == '\"') {
                quotedString.append("&quot;");
            } else if (oldString.charAt(i) == '\'') {
                quotedString.append("&apos;");
            } else if (oldString.charAt(i) == '\\') {
                quotedString.append("&#092;");
            } else if (oldString.charAt(i) == '&') {
                quotedString.append("&amp;");
            } else {
                //the char is not a special one
                //add it to the result as is
                quotedString.append(oldString.charAt(i));
            }
        }
        return quotedString.toString();
    }
}

Related

  1. quotes(CharSequence stringToQuote, boolean escapeQuote)
  2. quotes(String str)
  3. quotesEscape(String text)
  4. QuoteSingle(String S)
  5. quoteSpecial(String orig)
  6. quoteSQL(String string)
  7. quoteSqlIdentifier(String identifier)
  8. quoteSqlIdentifier(String name)
  9. quoteSQLString(String s)