Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

import java.io.*;

public class Main {
    /**
     * Escape passed string as XML element content (<code>&lt;</code>, 
     * <code>&amp;</code> and <code>><code> in <code>]]></code> sequences).
     *
     * @param val a string to be escaped
     *
     * @return escaped value
     * @throws CharConversionException if val contains an improper XML character
     *
     * @since 1.40
     */
    public static String toElementContent(String val) throws CharConversionException {
        if (val == null)
            throw new CharConversionException("null"); // NOI18N

        if (checkContentCharacters(val))
            return val;

        StringBuffer buf = new StringBuffer();

        for (int i = 0; i < val.length(); i++) {
            char ch = val.charAt(i);
            if ('<' == ch) {
                buf.append("&lt;");
                continue;
            } else if ('&' == ch) {
                buf.append("&amp;");
                continue;
            } else if ('>' == ch && i > 1 && val.charAt(i - 2) == ']' && val.charAt(i - 1) == ']') {
                buf.append("&gt;");
                continue;
            }
            buf.append(ch);
        }
        return buf.toString();

    }

    /**
     * Check if all passed characters match XML expression [2].
     * @return true if no escaping necessary
     * @throws CharConversionException if contains invalid chars
     */
    private static boolean checkContentCharacters(String chars) throws CharConversionException {
        boolean escape = false;
        for (int i = 0; i < chars.length(); i++) {
            char ch = chars.charAt(i);
            if (((int) ch) <= 93) { // we are UNICODE ']'
                switch (ch) {
                case 0x9:
                case 0xA:
                case 0xD:
                    continue;
                case '>': // only ]]> is dangerous
                    if (escape)
                        continue;
                    escape = i > 0 && (chars.charAt(i - 1) == ']');
                    continue;
                case '<':
                case '&':
                    escape = true;
                    continue;
                default:
                    if (((int) ch) < 0x20) {
                        throw new CharConversionException("Invalid XML character &#" + ((int) ch) + ";.");
                    }
                }
            }
        }
        return escape == false;
    }
}