Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   IBM - Initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * XML encode a text string.
     * 
     * @param text - a String.
     * @return an XML encoded text string.
     */
    public static String xmlRemoveEscapedString(String text) {
        if (text == null)
            return text;
        else {
            StringBuffer sb = new StringBuffer(text);

            int i = sb.indexOf("
");
            while (i != -1) {
                sb.replace(i, i + 5, "\r");
                i = sb.indexOf("
");
            }

            i = sb.indexOf("<");
            while (i != -1) {
                sb.replace(i, i + 4, "<");
                i = sb.indexOf("&lt;");
            }

            i = sb.indexOf("&gt;");
            while (i != -1) {
                sb.replace(i, i + 4, ">");
                i = sb.indexOf("&gt;");
            }

            i = sb.indexOf("&quot;");
            while (i != -1) {
                sb.replace(i, i + 6, "\"");
                i = sb.indexOf("&quot;");
            }

            i = sb.indexOf("&apos;");
            while (i != -1) {
                sb.replace(i, i + 6, "\'");
                i = sb.indexOf("&apos;");
            }

            i = sb.indexOf("&amp;");
            while (i != -1) {
                sb.replace(i, i + 5, "&");
                i = sb.indexOf("&amp;");
            }
            return sb.toString();
        }
    }
}