Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 *  Copyright 2009 Welocalize, Inc. 
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  
 *  You may obtain a copy of the License at 
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  
 */

public class Main {
    private static final String NUMBER_ENTITY_START = "&#";
    private static final String NUMBER_ENTITY_END = ";";

    private static String saveNonAsciiAsNumberEntity(String p_src) {
        if (p_src == null || p_src.length() == 0 || p_src.trim().length() == 0) {
            return p_src;
        }

        int length = p_src.length();
        StringBuffer src = new StringBuffer(p_src);
        StringBuffer ret = new StringBuffer(length);
        for (int i = 0; i < length; i++) {
            char c = src.charAt(i);
            int ci = (int) c;
            if (isAscii(ci)) {
                ret.append(c);
            } else if (isEncodeable(src, i)) {
                ret.append(NUMBER_ENTITY_START).append(ci).append(NUMBER_ENTITY_END);
            } else {
                ret.append(c);
            }
        }

        return ret.toString();
    }

    private static boolean isAscii(int ci) {
        return (ci >= 0 && ci <= 127);
    }

    private static boolean isEncodeable(StringBuffer p_src, int index) {
        // white space
        boolean blank = false;
        boolean lt = false;
        String sub = null;

        for (int i = index - 1; i >= 0; i--) {
            char c = p_src.charAt(i);
            String cstr = "" + c;

            if ("".equals(cstr.trim())) {
                blank = true;
            } else if (c == '<') {
                sub = p_src.substring(i, index);
                lt = true;
                break;
            }
            // node value, encode
            else if (c == '>') {
                return true;
            }
        }

        // node name, do not encode
        if (lt && !blank) {
            return false;
        }

        // comments, encode
        if (sub.startsWith("<!--")) {
            return true;
        }

        // cdata, encode
        if (sub.startsWith("<![CDATA[")) {
            return true;
        }

        // dtd or version, do not encode
        if (sub.startsWith("<!") || sub.startsWith("<?")) {
            return false;
        }

        // determine attribute
        int subLen = sub.length();
        boolean eq = false;
        boolean singleQuote = false;
        boolean doubleQuote = false;
        for (int i = 1; i < subLen; i++) {
            char c = sub.charAt(i);

            if (c == '=' && !singleQuote && !doubleQuote) {
                eq = true;
            }

            if (c == '\'' && eq && !doubleQuote) {
                singleQuote = !singleQuote;
                if (!singleQuote) {
                    eq = false;
                }
            }

            if (c == '"' && eq && !singleQuote) {
                doubleQuote = !doubleQuote;
                if (!doubleQuote) {
                    eq = false;
                }
            }
        }

        if (singleQuote || doubleQuote) {
            return true;
        } else {
            return false;
        }
    }
}