Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

public class Main {
    protected static int testEscape(char[] chars, char[] encodings) {
        int index = 0;
        int length = chars.length;
        while (index < length) {
            char c1 = chars[index++];
            if (Character.isHighSurrogate(c1)) {
                if (index < length) {
                    char c2 = chars[index++];
                    if (Character.isLowSurrogate(c2)) {
                        int cp = Character.toCodePoint(c1, c2);
                        if (isValidCodePoint(cp)) {
                            continue;
                        }
                    }
                    return index - 2;
                }
                return index - 1;
            } else {
                if (isValidCodePoint(c1)) {
                    if (encodings != null) {
                        for (char ch : encodings) {
                            if (c1 == ch) {
                                return index - 1;
                            }
                        }
                    }
                    continue;
                }
                return index - 1;
            }
        }
        return length;
    }

    /**
     * In <a href="http://www.w3.org/TR/REC-xml/">XML specification 1.0 </a>
     * section 2.2 Character, the valid characters in XML should be limited to:
     * 
     * Any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
     * 
     * Char ::= #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] |
     * [#x10000-#x10FFFF]
     * 
     */
    protected static boolean isValidCodePoint(int ch) {
        if (ch == 0x09 || ch == 0x0A || ch == 0x0D || (ch >= 0x20 && ch <= 0xD7FF) || (ch >= 0xE000 && ch <= 0xFFFD)
                || (ch >= 0x10000 && ch <= 0x10FFFF)) {
            return true;
        }
        return false;
    }
}