Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.StringTokenizer;

public class Main {
    /**
     * gets the character encoding from the String representation of an XML file.
     * @param sXml
     * @return the encoding specified in the String, or "UTF-8" (default encoding for XML files) if none is specified.
     * @postcondition result != null
     */
    public static String getXMLEncoding(String sXml) {
        final int iEncodingStart = sXml.indexOf("encoding=");
        String result = null;

        if (iEncodingStart > 0) {
            final String encbuf = sXml.substring(iEncodingStart);
            final StringTokenizer tokenizer = new StringTokenizer(encbuf, "\"'", true);
            boolean encfound = false;
            while (tokenizer.hasMoreTokens()) {
                sXml = tokenizer.nextToken();
                if (sXml.equals("'") || sXml.equals("\"")) {
                    encfound = true;
                } else {
                    if (encfound) {
                        result = sXml;
                        break;
                    }
                }
            }
        }
        if (result == null) {
            result = "UTF-8";
        }
        assert result != null;
        return result;
    }
}