Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2010-2010 LinkedIn, 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 {
    /**
     * This method descodes a string that was previously encoded for being XML
     * safe. It is the exact opposite of xmlEncode
     *
     * @param s the string to decode
     * @return the decoded string
     * @exception IllegalArgumentException if the string cannot be decoded */
    public static String xmlDecode(String s) throws IllegalArgumentException {
        if (s == null)
            return s;

        int idxS = s.indexOf('&');
        if (idxS < 0)
            return s;

        StringBuilder sb = new StringBuilder(s.length());

        int idxE, idx, size;
        char c, c_1;
        int prev = 0;

        while (idxS != -1) {
            idxE = s.indexOf(';', idxS);
            if (idxE < 0)
                throw new IllegalArgumentException("missing ';' in: " + s.substring(idxS));

            sb.append(s.substring(prev, idxS));

            idx = idxS + 1;
            size = idxE - idxS - 1;
            if (size < 2)
                throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
            c = s.charAt(idx);
            c_1 = s.charAt(idx + 1);
            switch (c) {
            case 'l':
                if (size != 2)
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                if (c_1 == 't')
                    sb.append('<');
                else
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                break;

            case 'g':
                if (size != 2)
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                if (c_1 == 't')
                    sb.append('>');
                else
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                break;

            case 'q':
                if (size != 4)
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                if (c_1 != 'u' || s.charAt(idx + 2) != 'o' || s.charAt(idx + 3) != 't')
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                else
                    sb.append('"');
                break;

            case 'a':
                if (size == 3) {
                    if (c_1 != 'm' || s.charAt(idx + 2) != 'p')
                        throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                    else
                        sb.append('&');
                } else if (size == 4) {
                    if (c_1 != 'p' || s.charAt(idx + 2) != 'o' || s.charAt(idx + 3) != 's')
                        throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                    else
                        sb.append('\'');
                } else
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                break;

            case '#':
                int codePoint;
                try {
                    codePoint = (c_1 == 'x') ? Integer.parseInt(s.substring(idx + 2, idxE), 16)
                            : Integer.parseInt(s.substring(idx + 1, idxE));
                } catch (NumberFormatException ex) {
                    throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
                }

                if (codePoint < 0)
                    throw new IllegalArgumentException(
                            "invalid character codepoint: " + s.substring(idxS, idxE + 1));

                if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT)
                    sb.append((char) codePoint);
                else
                    sb.append(Character.toChars(codePoint));
                break;

            default:
                throw new IllegalArgumentException("invalid escape tag: " + s.substring(idxS, idxE + 1));
            }
            prev = idxE + 1;
            idxS = s.indexOf("&", prev);
        }
        if (prev < s.length())
            sb.append(s.substring(prev));

        return sb.toString();
    }
}