Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (c) 2010 Inside Secure, All Rights Reserved.
 *
 * 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 {
    static final char[] mPrintableURIChars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
            'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
            'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', ':', '/', '?', '#', '[', ']', '@', '!', '$', '&', '\'', '(', ')', '*',
            '+', ',', ';', '=', '-', '.', '_', '~', ' ' };
    static final char sHexaChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
            'F' };

    /**
     * Compares two absolute URI TNF records
     *
     * @param[in] pString1 The string corresponding to the first TNF record
     * @param[in] pString2 The string corresponding to the second TNF record
     * @return true if the TNF records are equal
     */
    /* package protected */static boolean compareAbsoluteURI(String pString1, String pString2) {

        if (pString1 == null) {
            if (pString2 == null) {
                return (true);
            }
            return (false);
        }

        String pTempString1 = convertStringToPrintableString(pString1);
        String pTempString2 = convertStringToPrintableString(pString2);

        if (pTempString1 != null && pTempString2 != null) {
            if (pTempString1.length() != pTempString2.length())
                return false;
        } else {
            return false;
        }

        if (pTempString1.length() == 0) {
            return true;
        }

        pTempString1 = normalizeAbsoluteURI(pTempString1);
        pTempString2 = normalizeAbsoluteURI(pTempString2);

        return (pTempString1.compareTo(pTempString2) == 0) ? true : false;
    }

    /**
     * Converts a string into a printable URI UTF-16 string
     *
     * @param[in] pInBuffer  The source buffer
     * return  The printable URI of the string
     */
    /* package protected */static String convertStringToPrintableString(final String pSourceString) {

        if (pSourceString == null) {
            return null;
        }

        char nCar;
        StringBuffer pDestString = new StringBuffer();

        for (int i = 0; i < pSourceString.length(); i++) {
            nCar = pSourceString.charAt(i);
            if (nCar > 255) {
                /* must be encoded using %uXXXX format */
                pDestString.append('%');
                pDestString.append('u');
                pDestString.append(sHexaChars[(nCar >> 12) & 0x0F]);
                pDestString.append(sHexaChars[(nCar >> 8) & 0x0F]);
                pDestString.append(sHexaChars[(nCar >> 4) & 0x0F]);
                pDestString.append(sHexaChars[(nCar) & 0x0F]);
            } else {
                if (isPrintableURI(nCar)) {
                    pDestString.append(nCar);
                } else {
                    /* must be encoded using %XX format */
                    pDestString.append('%');
                    pDestString.append(sHexaChars[(nCar >> 4) & 0x0F]);
                    pDestString.append(sHexaChars[(nCar) & 0x0F]);
                }
            }
        }
        return pDestString.toString();
    }

    /**
     * Normalizes an absolute URI (lower scheme and host part)
     *
     * @param pString The absolute URI to be normalized
     */
    /* package protected */static String normalizeAbsoluteURI(String pString) {

        int index = -1;

        /* lower the scheme part */
        if ((index = pString.indexOf(':')) == -1) {
            return pString;
        }
        String scheme = pString.substring(0, index);
        pString = pString.replaceFirst(scheme, scheme.toLowerCase());

        /* check the presence of "//" */
        if ((index = pString.indexOf("//", index)) == -1) {
            /* no // found, return */
            return pString;
        }

        /* lower the host part ( authority : [user@]host[:port] )*/
        index += 2;
        int indexStart = pString.indexOf("@", index);
        indexStart = (indexStart == -1) ? index : indexStart + 1;
        int indexEnd = pString.indexOf(indexStart, ':');
        if (indexEnd == -1) {
            if ((indexEnd = pString.indexOf("/", indexStart)) == -1) {
                indexEnd = pString.length();
            }
        }
        String host = pString.substring(indexStart, indexEnd);

        return pString.replaceFirst(host, host.toLowerCase());
    }

    static boolean isPrintableURI(char car) {
        for (int i = 0; i < mPrintableURIChars.length; i++) {
            if (mPrintableURIChars[i] == car) {
                return true;
            }
        }
        return false;
    }

    static boolean isPrintableURI(String type) {

        if (type == null)
            return false;

        boolean bInEscapement = false;
        int nEscapementLength = 0;
        char nCar;
        for (int i = 0; i < type.length(); i++) {
            nCar = type.charAt(i);
            if (bInEscapement == false) {
                if (nCar != '%') {
                    if (isPrintableURI(nCar) == false)
                        return false;
                } else {
                    bInEscapement = true;
                    nEscapementLength = (type.charAt(i + 1) == 'u') ? 4 : 2;
                }
            } else {
                if ((nEscapementLength == 4) && (nCar == 'u'))
                    continue;

                /* Checks if the character is an hexadecimal digit */
                if (!(((nCar >= 'A') && (nCar <= 'F')) || ((nCar >= '0') && (nCar <= '9')))) {
                    return false;
                }

                if (--nEscapementLength == 0) {
                    bInEscapement = false;
                }
            }
        }

        return (bInEscapement == false) ? true : false;
    }
}