Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Handles XML encoding of text, e.g. & to &
     * 
     * @param sText Text to XML encode
     * @return XML Encoded text
     */
    public static String encodeXMLText(String sText) {
        StringBuffer sBuff2 = new StringBuffer(sText);
        StringBuffer sNewBuff = new StringBuffer();

        for (int i = 0; i < sBuff2.length(); i++) {
            char currChar = sBuff2.charAt(i);
            Character currCharObj = new Character(sBuff2.charAt(i));
            if (currChar == '&') {
                if ((sBuff2.length() - 1 - i) >= 4 && sBuff2.charAt(i + 1) == 'a' && sBuff2.charAt(i + 2) == 'm'
                        && sBuff2.charAt(i + 3) == 'p' && sBuff2.charAt(i + 4) == ';') {
                    i = i + 4;
                    sNewBuff.append("&amp;");
                } else {
                    sNewBuff.append("&amp;");
                }
            } else if (currChar == '>') {
                sNewBuff.append("&gt;");
            } else if (currChar == '<') {
                sNewBuff.append("&lt;");
            } else {
                sNewBuff.append(currChar);
            }
        }

        return sNewBuff.toString();

    }
}