Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.IOException;

public class Main {
    private static final String XML_LESS_THAN = "<";
    private static final String XML_AMPERSAND = "&";
    private static final String XML_QUOTE = """;
    /** Preferred over ' according to http://www.w3.org/TR/xhtml1/#C_16 */
    private static final String XML_APOSTROPHE = "'";

    /**
     * Used to write out an attribute for an element.  Surrounding whitespace will
     * not be added to the buffer.  The given value will be XML encoded before
     * appending to the buffer.
     * <p>
     * For example, given attrName="foo" and attrValue="val&lt;bar" writes out:
     * <pre>foo="val&amp;lt;bar"</pre>
     *
     * @param attrName the attribute name
     * @param attrValue the attribute value
     * @param buf the {@code StringBuffer} to which to append the attribute
     *
     * @deprecated - Use {@link #xmlAppendAttr(String, String, Appendable)}.
     */
    @Deprecated
    public static void xmlAppendAttrValuePair(String attrName, String attrValue, StringBuffer buf) {
        buf.append(attrName);
        buf.append("=\"");
        XmlEncodeAttrValue(attrValue, buf);
        buf.append('"');
    }

    /**
     * XML encodes an attribute value, encoding some characters as
     * character entities, and dropping invalid control characters.
     *
     * @param attrValue the attribute value
     * @param buf the {@code StringBuffer} to which to append the attribute
     *
     * @deprecated - Use {@link #xmlAppendAttrValue(String, Appendable)}.
     */
    @Deprecated
    public static void XmlEncodeAttrValue(String attrValue, StringBuffer buf) {
        try {
            xmlAppendAttrValue(attrValue, buf);
        } catch (IOException e) {
            // This can't happen with StringBuffer.
            throw new AssertionError(e);
        }
    }

    /**
     * XML encodes an attribute value, escaping some characters as
     * character entities, and dropping invalid control characters.
     * <p>
     * Only four characters need to be encoded, according to the
     * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">
     * W3C XML 1.0 Specification Character definition</a>: {@code < & " '}
     * (less-than, ampersand, double-quote, single-quote).
     * <p>
     * Actually, we could only encode one of the quote characters if
     * we knew that that was the one used to wrap the value, but we'll
     * play it safe and encode both.
     * <p>
     * We drop invalid XML characters, following the
     * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">
     * W3C XML 1.0 Specification Character definition</a>:
     * <pre>
     * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
     * </pre>
     * Java uses UTF-16 internally, so Unicode characters U+10000 to
     * U+10FFFF are encoded using the surrogate characters excluded
     * above, 0xD800 to 0xDFFF. So we allow just 0x09, 0x0A, 0x0D,
     * and the range 0x20 to 0xFFFD.
     *
     * @param attrValue the attribute value
     * @param buf the {@code Appendable} to which to append the attribute value
     * @throws IOException from {@code Appendable} (but {@code StringBuffer} or
     *        {@code StringBuilder} will never actually throw {@code IOException})
     * @since 2.4
     */
    public static void xmlAppendAttrValue(String attrValue, Appendable buf) throws IOException {
        for (int i = 0; i < attrValue.length(); i++) {
            char c = attrValue.charAt(i);
            switch (c) {
            case '<':
                buf.append(XML_LESS_THAN);
                break;
            case '&':
                buf.append(XML_AMPERSAND);
                break;
            case '"':
                buf.append(XML_QUOTE);
                break;
            case '\'':
                buf.append(XML_APOSTROPHE);
                break;
            case '\t':
            case '\n':
            case '\r':
                // TODO: what happens to white-space?
                buf.append(c);
                break;
            default:
                if (c >= 0x20 && c <= 0xFFFD) {
                    buf.append(c);
                }
                break;
            }
        }
    }
}