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 

import java.io.Writer;

public class Main {
    /**
     * Escape the given XML string (character text or attribute value; unescaped)
     * according to either XML 1.0 or XML 1.1 specification.
     * 
     * Only element content (excluding markup) and attributes (values) need to be escaped.
     * Markup, comments, CDATA sections, and prosessing instructions need to be left unchanged.
     * 
     *
     * @param writer         The writer where the escaped string is written to.
     * @param str            The given XML string (character text or attribute value; unescaped).
     * @param xmlVersion      The XML version (1.0 or 1.1).
     * @param isAttributeValue   <code>true</code> if the given XML string is attribute value,
     *                      otherwise <code>false</code>.
     */
    private static void escape(Writer writer, String str, String xmlVersion, boolean isAttributeValue)
            throws Exception {
        StringBuffer sb = new StringBuffer("");

        // First, escape all characters except the > character
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);

            switch (c) {
            // less-than:
            // Always escape.
            case '<': {
                sb.append("&lt;");
                break;
            }
            // ampersand:
            // Always escape.
            case '&': {
                sb.append("&amp;");
                break;
            }
            // CR (CARRIAGE RETURN):
            // Always escape.
            case '\r': {
                sb.append("&#xD;");
                break;
            }
            // LF (LINE FEED) or sometimes NL (NEW LINE): 
            // Escape in attribute values.
            case '\n': {
                if (isAttributeValue == true) {
                    sb.append("&#xA;");
                } else {
                    sb.append("\n");
                }
                break;
            }
            // TAB (TABULATOR):
            // Escape in attribute values.
            case '\t': {
                if (isAttributeValue == true) {
                    sb.append("&#x9;");
                } else {
                    sb.append("\t");
                }
                break;
            }
            // single-quote:
            // Escape in attribute values quoted with the same kind of quote.
            case '\'': {
                if (isAttributeValue == true) {
                    sb.append("&apos;");
                } else {
                    sb.append("\'");
                }
                break;
            }
            // double-quote:
            // Escape in attribute values quoted with the same kind of quote.
            case '\"': {
                if (isAttributeValue == true) {
                    sb.append("&quot;");
                } else {
                    sb.append("\"");
                }
                break;
            }
            // else, default print char
            default: {
                if (("1.1".equals(xmlVersion) == true)
                        && (((c >= 0x1) && (c <= 0x1F)) || ((c >= 0x7F) && (c <= 0x9F)) || (c == 0x2028))) {
                    sb.append("&#x");
                    sb.append(Integer.toHexString(c).toUpperCase());
                    sb.append(";");
                } else {
                    sb.append(c);
                }
            }
            }
        }

        // Finally, escape the > character if necessary
        // greater-than:
        // Escape in text if it immediately follows two close-square-brackets, as
        // that sequence is only allowed as the end of a CDATA marked section.
        if (isAttributeValue == false) {
            sb.replace(0, sb.length(), sb.toString().replace("]]>", "]]&gt;"));
        }
        writer.write(sb.toString());
    }
}