Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.CharArrayWriter;

public class Main {
    private static final String ENCODED_GREATER_THAN = ">";
    private static final String ENCODED_LESS_THAN = "<";
    private static final String ENCODED_DOUBLE_QUOTE = """;
    private static final String ENCODED_AMPERSAND = "&";

    public static String encodeAttribute(String value) {

        CharArrayWriter writer = new CharArrayWriter();

        int size = value.length();

        for (int i = 0; i < size; i++) {

            char c = value.charAt(i);

            switch (c) {

            case '&':
                writer.append(ENCODED_AMPERSAND);
                break;

            case '"':
                writer.append(ENCODED_DOUBLE_QUOTE);
                break;

            case '<':
                writer.append(ENCODED_LESS_THAN);
                break;

            case '>':
                writer.append(ENCODED_GREATER_THAN);
                break;

            default:
                writer.append(c);
                break;

            }

        }

        return writer.toString();

    }
}