Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Method to decode a string xml text.
     * @param text string xml to decode.
     * @return the string xml decoded.
     * @throws Exception throw if any  error is occurred.
     */
    public static String xmlDecode(String text) throws Exception {
        String origText = text;
        String newText = "";
        while (text.contains("&")) {
            int pos = text.indexOf("&");
            newText += text.substring(0, pos);
            text = text.substring(pos + 1);
            pos = text.indexOf(";");
            if (pos <= 0) {
                throw new Exception("Improperly escaped character: " + origText);
            }
            String charref = text.substring(0, pos);
            text = text.substring(pos + 1);
            if (charref.equals("lt")) {
                newText += "<";
            } else if (charref.equals("gt")) {
                newText += ">";
            } else if (charref.equals("amp")) {
                newText += "&";
            } else if (charref.equals("quot")) {
                newText += "\"";
            } else if (charref.equals("apos")) {
                newText += "'";
            } else if (charref.startsWith("#")) {
                String number = charref.substring(1);
                int radix = 10;
                if (charref.startsWith("#x") || charref.startsWith("#X")) {
                    number = charref.substring(2);
                    radix = 16;
                }
                if ("".equals(number)) {
                    throw new Exception("Improperly escaped character: " + charref);
                }
                char ch;
                try {
                    ch = (char) Integer.parseInt(number, radix);
                } catch (NumberFormatException nfe) {
                    throw new Exception("Improperly escaped character: " + charref);
                }
                newText += ch;
            } else {
                throw new Exception("Improperly escaped character: " + charref);
            }
        } //while
        return newText + text;
    }
}