Search in sources :

Example 41 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project android by JetBrains.

the class EclipseProject method initEncoding.

private void initEncoding() throws IOException {
    File propertyFile = new File(myDir, ".settings" + separator + "org.eclipse.core.resources.prefs");
    if (propertyFile.exists()) {
        Properties properties = PropertiesFiles.getProperties(propertyFile);
        if (properties != null) {
            Enumeration<?> enumeration = properties.propertyNames();
            String prefix = "encoding/";
            while (enumeration.hasMoreElements()) {
                Object next = enumeration.nextElement();
                if (next instanceof String) {
                    String key = (String) next;
                    if (key.startsWith(prefix)) {
                        String path = key.substring(prefix.length());
                        String encoding = properties.getProperty(key);
                        if (encoding != null && !encoding.isEmpty()) {
                            try {
                                Charset charset = Charset.forName(encoding);
                                if ("<project>".equals(path)) {
                                    myDefaultCharset = charset;
                                } else {
                                    if (myFileCharsets == null) {
                                        myFileCharsets = Maps.newHashMap();
                                    }
                                    File file = resolveVariableExpression(path);
                                    if (file != null) {
                                        myFileCharsets.put(file, charset);
                                    } else {
                                        myFileCharsets.put(new File(path), charset);
                                    }
                                }
                            } catch (UnsupportedCharsetException uce) {
                                myImporter.reportWarning(this, propertyFile, "Unknown charset " + encoding);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) File(java.io.File)

Example 42 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project android by JetBrains.

the class GradleImportTest method testEncoding.

@SuppressWarnings("ResultOfMethodCallIgnored")
public void testEncoding() throws Exception {
    // Checks that we properly convert source files from other encodings to UTF-8.
    // The following scenarios are tested:
    //  - For files that have a specific encoding associated with that file, use it
    //  - For XML files that specify an encoding in the prologue, use it
    //  - For files that have a specific encoding specified by a BOM, use it
    //  - For files that are in a project where there is a project-specific encoding, use it
    //  - For all other files, use the default encoding specified in the workspace
    File root = Files.createTempDir();
    File app = createLibrary(root, "test.lib2.pkg", false);
    // Write some source files where encoding matters
    // The Project App will have a default project encoding of MacRoman
    // The workspace will have a default encoding of windows1252
    // Some individual files will specify a file encoding of iso-8859-1
    Charset macRoman;
    Charset windows1252;
    Charset utf32;
    Charset iso8859 = Charsets.ISO_8859_1;
    try {
        macRoman = Charset.forName("MacRoman");
        windows1252 = Charset.forName("windows-1252");
        utf32 = Charset.forName("UTF_32");
    } catch (UnsupportedCharsetException uce) {
        System.err.println("This test machine does not have all the charsets we need: " + uce.getCharsetName() + ": skipping test");
        return;
    }
    String java = "" + "package test.pkg;\n" + "\n" + "public class Text {\n" + "\tpublic static final String TEXT_1 = \"This is plain\";\n" + "\tpublic static final String TEXT_2 = \"æØå\";\n" + "\tpublic static final String TEXT_3 = \"10£\";\n" + "}\n";
    String xml = "" + "<?xml version=\"1.0\" encoding=\"" + windows1252.name() + "\"?>\n" + "<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + "    android:layout_width=\"match_parent\"\n" + "    android:layout_height=\"match_parent\"\n" + "    android:orientation=\"vertical\" >\n" + "<!-- £ -->\n" + "</LinearLayout>";
    File appFile = new File(root, "App/src/test/pkg/Text.java".replace('/', separatorChar));
    File lib1File = new File(root, "Lib1/src/test/pkg/Text.java".replace('/', separatorChar));
    File lib2File = new File(root, "Lib2/src/test/pkg/Text.java".replace('/', separatorChar));
    File xmlFile = new File(root, "App/res/layout/foo.xml".replace('/', separatorChar));
    appFile.getParentFile().mkdirs();
    lib1File.getParentFile().mkdirs();
    lib2File.getParentFile().mkdirs();
    xmlFile.getParentFile().mkdirs();
    Files.write(java, appFile, iso8859);
    Files.write(java, lib1File, macRoman);
    Files.write(java, lib2File, windows1252);
    Files.write(xml, xmlFile, windows1252);
    assertEquals(java, Files.toString(appFile, iso8859));
    assertEquals(java, Files.toString(lib1File, macRoman));
    assertEquals(java, Files.toString(lib2File, windows1252));
    assertEquals(xml, Files.toString(xmlFile, windows1252));
    // Make sure that these contents don't happen to be the same regardless of encoding
    assertFalse(java.equals(Files.toString(appFile, UTF_8)));
    assertFalse(java.equals(Files.toString(lib1File, UTF_8)));
    assertFalse(java.equals(Files.toString(lib2File, UTF_8)));
    assertFalse(xml.equals(Files.toString(xmlFile, UTF_8)));
    // Write App project specific encoding, and file specific encoding
    File file = new File(root, "App" + separator + ".settings" + separator + "org.eclipse.core.resources.prefs");
    file.getParentFile().mkdirs();
    Files.write("" + "eclipse.preferences.version=1\n" + "encoding//src/test/pkg/Text.java=" + iso8859.name() + "\n" + "encoding/<project>=" + macRoman.name(), file, Charsets.US_ASCII);
    // Write Lib1 project specific encoding
    file = new File(root, "Lib1" + separator + ".settings" + separator + "org.eclipse.core.resources.prefs");
    file.getParentFile().mkdirs();
    Files.write("" + "eclipse.preferences.version=1\n" + "encoding/<project>=" + macRoman.name(), file, Charsets.US_ASCII);
    // Write workspace default encoding, used for the Lib2 file
    final File workspace = new File(root, "workspace");
    workspace.mkdirs();
    File metadata = new File(workspace, ".metadata");
    metadata.mkdirs();
    new File(metadata, "version.ini").createNewFile();
    assertTrue(isEclipseWorkspaceDir(workspace));
    File resourceFile = new File(workspace, (".metadata/.plugins/org.eclipse.core.runtime/" + ".settings/org.eclipse.core.resources.prefs").replace('/', separatorChar));
    resourceFile.getParentFile().mkdirs();
    Files.write("" + "eclipse.preferences.version=1\n" + "encoding=" + windows1252.name() + "\n" + "version=1", resourceFile, Charsets.US_ASCII);
    File imported = checkProject(app, "" + MSG_HEADER + MSG_MANIFEST + MSG_UNHANDLED + "From App:\n" + "* .gitignore\n" + "From JavaLib:\n" + "* .gitignore\n" + MSG_FOLDER_STRUCTURE + "In JavaLib:\n" + "* src/ => javaLib/src/main/java/\n" + "In Lib1:\n" + "* AndroidManifest.xml => lib1/src/main/AndroidManifest.xml\n" + "* src/ => lib1/src/main/java/\n" + "In Lib2:\n" + "* AndroidManifest.xml => lib2/src/main/AndroidManifest.xml\n" + "* src/ => lib2/src/main/java/\n" + "In App:\n" + "* AndroidManifest.xml => app/src/main/AndroidManifest.xml\n" + "* res/ => app/src/main/res/\n" + "* src/ => app/src/main/java/\n" + MSG_FOOTER, false, /* checkBuild */
    importer -> importer.setEclipseWorkspace(workspace));
    // Read back text files *as UTF-8* and make sure it's correct
    File newAppFile = new File(imported, "app/src/main/java/test/pkg/Text.java".replace('/', separatorChar));
    File newLib1File = new File(imported, "lib1/src/main/java/test/pkg/Text.java".replace('/', separatorChar));
    File newLib2File = new File(imported, "lib2/src/main/java/test/pkg/Text.java".replace('/', separatorChar));
    File newXmlFile = new File(imported, "app/src/main/res/layout/foo.xml".replace('/', separatorChar));
    assertTrue(newAppFile.exists());
    assertTrue(newLib1File.exists());
    assertTrue(newLib2File.exists());
    assertTrue(newXmlFile.exists());
    assertEquals(java, Files.toString(newAppFile, UTF_8));
    assertEquals(java, Files.toString(newLib1File, UTF_8));
    assertEquals(java, Files.toString(newLib2File, UTF_8));
    // references old encoding
    assertFalse(xml.equals(Files.toString(newXmlFile, UTF_8)));
    assertEquals(xml.replace(windows1252.name(), "utf-8"), Files.toString(newXmlFile, UTF_8));
    deleteDir(root);
    deleteDir(imported);
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) File(java.io.File)

Example 43 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project gerrit by GerritCodeReview.

the class Text method charset.

private static Charset charset(byte[] content, String encoding) {
    if (encoding == null) {
        UniversalDetector d = new UniversalDetector(null);
        d.handleData(content, 0, content.length);
        d.dataEnd();
        encoding = d.getDetectedCharset();
    }
    if (encoding == null) {
        return ISO_8859_1;
    }
    try {
        return Charset.forName(encoding);
    } catch (IllegalCharsetNameException err) {
        log.error("Invalid detected charset name '" + encoding + "': " + err);
        return ISO_8859_1;
    } catch (UnsupportedCharsetException err) {
        log.error("Detected charset '" + encoding + "' not supported: " + err);
        return ISO_8859_1;
    }
}
Also used : IllegalCharsetNameException(java.nio.charset.IllegalCharsetNameException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) UniversalDetector(org.mozilla.universalchardet.UniversalDetector)

Example 44 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project jabref by JabRef.

the class BasePanel method saveDatabase.

private boolean saveDatabase(File file, boolean selectedOnly, Charset enc, SavePreferences.DatabaseSaveType saveType) throws SaveException {
    SaveSession session;
    frame.block();
    final String SAVE_DATABASE = Localization.lang("Save library");
    try {
        SavePreferences prefs = SavePreferences.loadForSaveFromPreferences(Globals.prefs).withEncoding(enc).withSaveType(saveType);
        BibtexDatabaseWriter<SaveSession> databaseWriter = new BibtexDatabaseWriter<>(FileSaveSession::new);
        if (selectedOnly) {
            session = databaseWriter.savePartOfDatabase(bibDatabaseContext, mainTable.getSelectedEntries(), prefs);
        } else {
            session = databaseWriter.saveDatabase(bibDatabaseContext, prefs);
        }
        registerUndoableChanges(session);
    }// FIXME: not sure if this is really thrown anywhere
     catch (UnsupportedCharsetException ex) {
        JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + ' ' + Localization.lang("Character encoding '%0' is not supported.", enc.displayName()), SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
        throw new SaveException("rt");
    } catch (SaveException ex) {
        if (ex.specificEntry()) {
            // Error occurred during processing of the entry. Highlight it:
            highlightEntry(ex.getEntry());
            showEntry(ex.getEntry());
        } else {
            LOGGER.warn("Could not save", ex);
        }
        JOptionPane.showMessageDialog(frame, Localization.lang("Could not save file.") + "\n" + ex.getMessage(), SAVE_DATABASE, JOptionPane.ERROR_MESSAGE);
        throw new SaveException("rt");
    } finally {
        frame.unblock();
    }
    boolean commit = true;
    if (!session.getWriter().couldEncodeAll()) {
        FormBuilder builder = FormBuilder.create().layout(new FormLayout("left:pref, 4dlu, fill:pref", "pref, 4dlu, pref"));
        JTextArea ta = new JTextArea(session.getWriter().getProblemCharacters());
        ta.setEditable(false);
        builder.add(Localization.lang("The chosen encoding '%0' could not encode the following characters:", session.getEncoding().displayName())).xy(1, 1);
        builder.add(ta).xy(3, 1);
        builder.add(Localization.lang("What do you want to do?")).xy(1, 3);
        String tryDiff = Localization.lang("Try different encoding");
        int answer = JOptionPane.showOptionDialog(frame, builder.getPanel(), SAVE_DATABASE, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE, null, new String[] { Localization.lang("Save"), tryDiff, Localization.lang("Cancel") }, tryDiff);
        if (answer == JOptionPane.NO_OPTION) {
            // The user wants to use another encoding.
            Object choice = JOptionPane.showInputDialog(frame, Localization.lang("Select encoding"), SAVE_DATABASE, JOptionPane.QUESTION_MESSAGE, null, Encodings.ENCODINGS_DISPLAYNAMES, enc);
            if (choice == null) {
                commit = false;
            } else {
                Charset newEncoding = Charset.forName((String) choice);
                return saveDatabase(file, selectedOnly, newEncoding, saveType);
            }
        } else if (answer == JOptionPane.CANCEL_OPTION) {
            commit = false;
        }
    }
    if (commit) {
        session.commit(file.toPath());
        // Make sure to remember which encoding we used.
        this.bibDatabaseContext.getMetaData().setEncoding(enc);
    } else {
        session.cancel();
    }
    return commit;
}
Also used : FormLayout(com.jgoodies.forms.layout.FormLayout) FormBuilder(com.jgoodies.forms.builder.FormBuilder) SaveException(org.jabref.logic.exporter.SaveException) JTextArea(javax.swing.JTextArea) BibtexDatabaseWriter(org.jabref.logic.exporter.BibtexDatabaseWriter) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) Charset(java.nio.charset.Charset) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) SavePreferences(org.jabref.logic.exporter.SavePreferences) FileSaveSession(org.jabref.logic.exporter.FileSaveSession) SaveSession(org.jabref.logic.exporter.SaveSession)

Example 45 with UnsupportedCharsetException

use of java.nio.charset.UnsupportedCharsetException in project stanbol by apache.

the class JerseyUtils method parseForm.

/**
     * This Method is intended to parse form data from 
     * {@link MediaType#APPLICATION_FORM_URLENCODED} requests. This functionality
     * us usually needed when writing a {@link MessageBodyReader} to get the
     * data from the "{@link InputStream} entityStream" parameter of the 
     * {@link MessageBodyReader#readFrom(Class, Type, java.lang.annotation.Annotation[], MediaType, javax.ws.rs.core.MultivaluedMap, InputStream)}
     * method.
     * @param entityStream the stream with the form data
     * @param charset The charset used for the request (if <code>null</code> or
     * empty UTF-8 is used as default.
     * @return the parsed form data as key value map
     * @throws IOException on any exception while reading the data form the stream
     */
public static Map<String, String> parseForm(InputStream entityStream, String charset) throws IOException {
    /* TODO: Question: 
         * If I get an Post Request with "application/x-www-form-urlencoded" 
         * and a charset (lets assume "iso-2022-kr") do I need to use the 
         * charset to read the String from the Stream, or to URL decode the 
         * String or both?
         * 
         * This code assumes that it needs to be used for both, but this needs
         * validation!
         */
    if (charset == null || charset.isEmpty()) {
        charset = "UTF-8";
    }
    String data;
    try {
        data = IOUtils.toString(entityStream, charset);
    } catch (UnsupportedCharsetException e) {
        throw new IOException(e.getMessage(), e);
    }
    Map<String, String> form = new HashMap<String, String>();
    StringTokenizer tokenizer = new StringTokenizer(data, "&");
    String token;
    try {
        while (tokenizer.hasMoreTokens()) {
            token = tokenizer.nextToken();
            int index = token.indexOf('=');
            if (index < 0) {
                form.put(URLDecoder.decode(token, charset), null);
            } else if (index > 0) {
                form.put(URLDecoder.decode(token.substring(0, index), charset), URLDecoder.decode(token.substring(index + 1), charset));
            }
        }
    } catch (UnsupportedCharsetException e) {
        throw new IOException(e.getMessage(), e);
    }
    return form;
}
Also used : StringTokenizer(java.util.StringTokenizer) HashMap(java.util.HashMap) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) IOException(java.io.IOException) TextConstraint(org.apache.stanbol.entityhub.servicesapi.query.TextConstraint)

Aggregations

UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)46 Charset (java.nio.charset.Charset)18 IllegalCharsetNameException (java.nio.charset.IllegalCharsetNameException)13 IOException (java.io.IOException)10 File (java.io.File)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 FileOutputStream (java.io.FileOutputStream)4 PrintStream (java.io.PrintStream)4 HashMap (java.util.HashMap)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Test (org.junit.Test)3 FormBuilder (com.jgoodies.forms.builder.FormBuilder)2 FormLayout (com.jgoodies.forms.layout.FormLayout)2 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)2 InputStreamReader (java.io.InputStreamReader)2 StringReader (java.io.StringReader)2 ReadableByteChannel (java.nio.channels.ReadableByteChannel)2 WritableByteChannel (java.nio.channels.WritableByteChannel)2