Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {

    public static String parseXMLCData(String strValue) {
        if (strValue == null)
            return strValue;
        //System.out.println("----" + strValue);
        if (strValue.indexOf('<') > -1 || strValue.indexOf('>') > -1 || strValue.indexOf('\n') > -1
                || strValue.indexOf('&') > -1 || strValue.indexOf('\'') > -1 || strValue.indexOf('"') > -1) {
            StringBuffer sb = new StringBuffer();

            if (strValue.indexOf("]]>") > -1) {
                sb.append(parseEndXMLCData(strValue));
            } else {
                sb.append("<![CDATA[");
                sb.append(strValue);
                sb.append("]]>");
            }
            return sb.toString();
        } else {
            return strValue;
        }
    }

    private static String parseEndXMLCData(String strValue) {
        int pos = strValue.indexOf("]]>");
        StringBuffer sb = new StringBuffer();
        if (pos > -1) {
            sb.append("<![CDATA[");
            sb.append(strValue.substring(0, pos + 1));
            sb.append("]]>");
            sb.append(parseEndXMLCData(strValue.substring(pos + 1)));
        } else {
            sb.append("<![CDATA[");
            sb.append(strValue);
            sb.append("]]>");
        }
        return sb.toString();
    }
}