Example usage for javax.swing.text ChangedCharSetException getCharSetSpec

List of usage examples for javax.swing.text ChangedCharSetException getCharSetSpec

Introduction

In this page you can find the example usage for javax.swing.text ChangedCharSetException getCharSetSpec.

Prototype

public String getCharSetSpec() 

Source Link

Document

Returns the char set specification.

Usage

From source file:EditorPaneExample16.java

protected String getNewCharSet(ChangedCharSetException e) {
    String spec = e.getCharSetSpec();
    if (e.keyEqualsCharSet()) {
        // The event contains the new CharSet
        return spec;
    }/*from w w  w.  jav a  2 s.c  om*/

    // The event contains the content type
    // plus ";" plus qualifiers which may
    // contain a "charset" directive. First
    // remove the content type.
    int index = spec.indexOf(";");
    if (index != -1) {
        spec = spec.substring(index + 1);
    }

    // Force the string to lower case
    spec = spec.toLowerCase();

    StringTokenizer st = new StringTokenizer(spec, " \t=", true);
    boolean foundCharSet = false;
    boolean foundEquals = false;
    while (st.hasMoreTokens()) {
        String token = st.nextToken();
        if (token.equals(" ") || token.equals("\t")) {
            continue;
        }
        if (foundCharSet == false && foundEquals == false && token.equals("charset")) {
            foundCharSet = true;
            continue;
        } else if (foundEquals == false && token.equals("=")) {
            foundEquals = true;
            continue;
        } else if (foundEquals == true && foundCharSet == true) {
            return token;
        }

        // Not recognized
        foundCharSet = false;
        foundEquals = false;
    }

    // No charset found - return a guess
    return "8859_1";
}

From source file:com.hexidec.ekit.EkitCore.java

private void openDocument(File whatFile, HTMLEditorKit.ParserCallback cb)
        throws IOException, BadLocationException {
    if (whatFile == null) {
        whatFile = getFileFromChooser(".", JFileChooser.OPEN_DIALOG, extsHTML,
                Translatrix.getTranslationString("FiletypeHTML"));
    }//from  w w  w  . java  2  s . c o  m
    if (whatFile != null) {
        try {
            loadDocument(whatFile, null, cb);
        } catch (ChangedCharSetException ccse) {
            String charsetType = ccse.getCharSetSpec().toLowerCase();
            int pos = charsetType.indexOf("charset");
            if (pos == -1) {
                throw ccse;
            }
            while (pos < charsetType.length() && charsetType.charAt(pos) != '=') {
                pos++;
            }
            pos++; // Places file cursor past the equals sign (=)
            String whatEncoding = charsetType.substring(pos).trim();
            loadDocument(whatFile, whatEncoding, cb);
        }
    }
    refreshOnUpdate();
}