Java String Substritute substituteSelectedCharacters(String text, boolean skip)

Here you can find the source of substituteSelectedCharacters(String text, boolean skip)

Description

Substitutes the selected characters with entities.

License

Open Source License

Declaration

public static String substituteSelectedCharacters(String text, boolean skip) 

Method Source Code

//package com.java2s;
/*//from  w ww .j ava2 s.c o m
 * Id: 
 *
 * Copyright (C) 2004, Cladonia Ltd. All rights reserved.
 *
 * This software is the proprietary information of Cladonia Ltd.  
 * Use is subject to license terms.
 */

public class Main {
    /**
     * Substitutes the selected characters with entities.
     */
    public static String substituteSelectedCharacters(String text, boolean skip) {

        if (text != null) {

            StringBuffer newText = new StringBuffer();

            for (int i = 0; i < text.length(); i++) {
                char character = text.charAt(i);

                if (character == '<') {
                    if (skip)
                        newText.append("&lt;");
                    else
                        newText.append((char) character);

                } else if (character == '>') {
                    if (skip)
                        newText.append("&gt;");
                    else
                        newText.append((char) character);
                } else if (character == '&') {
                    if (skip) {
                        newText.append("&amp;");
                    } else {
                        newText.append((char) character);
                    }

                } else if (character == '\'') {
                    if (skip)
                        newText.append("&apos;");
                    else
                        newText.append((char) character);

                } else if (character == '\"') {
                    if (skip)
                        newText.append("&quot;");
                    else
                        newText.append((char) character);

                    /*} else if (  character > 127) {
                    if(!skip) {
                    String name = CommonEntities.getEntityName( character);
                        
                    try {
                    if ( !name.equals( "lt") && !name.equals( "gt") && !name.equals( "amp") && !name.equals( "apos") && !name.equals( "quot")) {
                        newText.append( "&");
                        newText.append( name);
                        newText.append( ";");
                    }
                    }
                    catch (NullPointerException e) {
                    //problem with converting char to entity
                    //just use that char
                    newText.append((char)character);
                    }
                    }
                    else {
                    newText.append( (char)character);
                    }*/
                } else {
                    newText.append((char) character);
                }
            }

            return (newText.toString());
        } else {
            return (text);
        }

    }
}

Related

  1. substituteInvalidChars(String str)
  2. substituteJobID(String path)
  3. substituteMarkers(String sql, String marker, Object substitution)
  4. substituteNull(String value, String substitution)
  5. substitutePropertyWithIn(String propertyName, String replacementString, String evaluatedString)
  6. substituteSpaces(String originalPath)
  7. substituteSubString(String input, String find, String replace)
  8. substituteSymbol(String text, String symbol, String value)
  9. substituteSymbol(String text, String symbol, String value)