Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String toAttribute(String value) {
        return "\'" + encodeXML(value) + "\'";
    }

    public static String encodeXML(String value) {
        if (value == null)
            return "";
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < value.length(); ++i) {
            char c = value.charAt(i);
            if (c >= 32 && c < 128)
                switch (c) {
                case '\'':
                    result.append("&apos;");
                    break;
                case '\"':
                    result.append("&quot;");
                    break;
                case '<':
                    result.append("&lt;");
                    break;
                case '>':
                    result.append("&gt;");
                    break;
                case '&':
                    result.append("&amp;");
                    break;
                default:
                    result.append(c);
                }
            else if (c != 0)
                result.append("&#").append(Integer.toString(c)).append(';');
        }
        return result.toString();
    }
}