Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

public class Main {
    static String entitystr = "><&\'\"\r\n\t";

    public static String xmlEncoding(String input) {
        return xmlEncoding(input, entitystr);
    }

    public static String xmlEncoding(String input, String delimiter) {
        if (input == null || input.equals(""))
            return input;

        delimiter += '&';

        StringTokenizer tmpst = new StringTokenizer(input, delimiter, true);
        StringBuffer tmpsb = new StringBuffer(input.length() + 100);
        String tmps = null;
        while (tmpst.hasMoreTokens()) {
            tmps = tmpst.nextToken();
            if (tmps.length() == 1 && delimiter.indexOf(tmps) >= 0) {
                switch (tmps.charAt(0)) {
                case '<':
                    tmpsb.append("&lt;");
                    break;
                case '>':
                    tmpsb.append("&gt;");
                    break;
                case '&':
                    tmpsb.append("&amp;");
                    break;
                case '\'':
                    tmpsb.append("&apos;");
                    break;
                case '\"':
                    tmpsb.append("&quot;");
                    break;
                case '\n':
                    tmpsb.append("&#10;");
                    break;
                case '\r':
                    tmpsb.append("&#13;");
                    break;
                case '\t':
                    tmpsb.append("&#9;");
                    break;
                }
            } else {
                tmpsb.append(tmps);
            }
        }

        return tmpsb.toString();
    }
}