Java Encode encodeAttribute(String value)

Here you can find the source of encodeAttribute(String value)

Description

encode Attribute

License

Apache License

Declaration

public static String encodeAttribute(String value) 

Method Source Code


//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);//from ww  w  .j a  v  a 2s . c o  m
                break;

            }

        }

        return writer.toString();

    }
}

Related

  1. encode_u8(String what)
  2. encodeAddresses(String string, String charset)
  3. encodeArguments(final String arg)
  4. encodeArray(String[] sourceArray, String sysCharset, String charset)
  5. encodeAsModifiedUTF8(String str)
  6. encodeBackslashAhead(char c, char next, Appendable buffer)
  7. encodeCodePoolData(InputStream input)
  8. encodedInputStreamReader(InputStream stream, String encoding)
  9. encodedStringToProperties(String theEncodedPropertyString)