Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

public class Main {
    /**
     * Method to encode the xml text.
     * @param rawtext string of the xml text.
     * @return the encode string.
     */
    public static String xmlEncode(String rawtext) {
        // Now turn that UTF-8 string into something "safe"
        String rdfString = "<?xml version='1.0' encoding='ISO-8859-1'?>\n";
        char[] sbuf = rawtext.toCharArray();
        int lastPos = 0;
        int pos = 0;
        while (pos < sbuf.length) {
            char ch = sbuf[pos];
            if (!(ch == '\n' || (ch >= ' ' && ch <= '~'))) {
                if (pos > lastPos) {
                    String range = new String(sbuf, lastPos, pos - lastPos);
                    rdfString += range;
                }
                rdfString += "&#" + (int) ch + ";";
                lastPos = pos + 1;
            }
            pos++;
        }
        if (pos > lastPos) {
            String range = new String(sbuf, lastPos, pos - lastPos);
            rdfString += range;
        }
        return rdfString;
    }
}