Search in sources :

Example 81 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class EncodingManagerImpl method getFavorites.

@Override
@NotNull
public Collection<Charset> getFavorites() {
    Collection<Charset> result = new THashSet<>();
    Project[] projects = ProjectManager.getInstance().getOpenProjects();
    for (Project project : projects) {
        result.addAll(EncodingProjectManager.getInstance(project).getFavorites());
    }
    result.addAll(EncodingProjectManagerImpl.widelyKnownCharsets());
    return result;
}
Also used : Project(com.intellij.openapi.project.Project) Charset(java.nio.charset.Charset) THashSet(gnu.trove.THashSet) NotNull(org.jetbrains.annotations.NotNull)

Example 82 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class EncodingProjectManagerImpl method setEncoding.

@Override
public void setEncoding(@Nullable final VirtualFile virtualFileOrDir, @Nullable final Charset charset) {
    Charset oldCharset;
    if (virtualFileOrDir == null) {
        oldCharset = myProjectCharset;
        myProjectCharset = charset;
    } else {
        if (charset == null) {
            oldCharset = myMapping.remove(virtualFileOrDir);
        } else {
            oldCharset = myMapping.put(virtualFileOrDir, charset);
        }
    }
    if (!Comparing.equal(oldCharset, charset)) {
        myModificationTracker.incModificationCount();
        if (virtualFileOrDir != null) {
            virtualFileOrDir.setCharset(virtualFileOrDir.getBOM() == null ? charset : null);
        }
        reloadAllFilesUnder(virtualFileOrDir);
    }
}
Also used : Charset(java.nio.charset.Charset)

Example 83 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class EncodingUtil method checkCanReload.

@NotNull
public static // returns pair (existing charset (null means N/A); failReason: null means enabled, notnull means disabled and contains error message)
Pair<Charset, String> checkCanReload(@NotNull VirtualFile virtualFile) {
    if (virtualFile.isDirectory()) {
        return Pair.create(null, REASON_FILE_IS_A_DIRECTORY);
    }
    FileDocumentManager documentManager = FileDocumentManager.getInstance();
    Document document = documentManager.getDocument(virtualFile);
    if (document == null)
        return Pair.create(null, REASON_BINARY_FILE);
    Charset charsetFromContent = ((EncodingManagerImpl) EncodingManager.getInstance()).computeCharsetFromContent(virtualFile);
    Charset existing = virtualFile.getCharset();
    String autoDetectedFrom = LoadTextUtil.wasCharsetDetectedFromBytes(virtualFile);
    String failReason;
    if (autoDetectedFrom != null) {
        // no point changing encoding if it was auto-detected
        failReason = "the encoding was " + autoDetectedFrom;
    } else if (charsetFromContent != null) {
        failReason = REASON_HARDCODED_IN_TEXT;
        existing = charsetFromContent;
    } else {
        failReason = fileTypeDescriptionError(virtualFile);
    }
    return Pair.create(existing, failReason);
}
Also used : Charset(java.nio.charset.Charset) FileDocumentManager(com.intellij.openapi.fileEditor.FileDocumentManager) Document(com.intellij.openapi.editor.Document) NotNull(org.jetbrains.annotations.NotNull)

Example 84 with Charset

use of java.nio.charset.Charset in project intellij-community by JetBrains.

the class IncompatibleEncodingDialog method createActions.

@NotNull
@Override
protected Action[] createActions() {
    DialogWrapperAction reloadAction = new DialogWrapperAction("Reload") {

        @Override
        protected void doAction(ActionEvent e) {
            if (safeToReload == EncodingUtil.Magic8.NO_WAY) {
                Pair<Charset, String> detected = EncodingUtil.checkCanReload(virtualFile);
                String failReason = detected.second;
                Charset autoDetected = detected.first;
                int res;
                byte[] bom = virtualFile.getBOM();
                if (bom != null) {
                    Messages.showErrorDialog(XmlStringUtil.wrapInHtml("File '" + virtualFile.getName() + "' can't be reloaded in the '" + charset.displayName() + "' encoding.<br><br>" + (failReason == null ? "" : "Why: " + failReason + "<br>") + (autoDetected == null ? "" : "Detected encoding: '" + autoDetected.displayName() + "'")), "Incompatible Encoding: " + charset.displayName());
                    res = -1;
                } else {
                    res = Messages.showDialog(XmlStringUtil.wrapInHtml("File '" + virtualFile.getName() + "' most likely isn't stored in the '" + charset.displayName() + "' encoding." + "<br><br>" + (failReason == null ? "" : "Why: " + failReason + "<br>") + (autoDetected == null ? "" : "Detected encoding: '" + autoDetected.displayName() + "'")), "Incompatible Encoding: " + charset.displayName(), new String[] { "Reload anyway", "Cancel" }, 1, AllIcons.General.WarningDialog);
                }
                if (res != 0) {
                    doCancelAction();
                    return;
                }
            }
            close(RELOAD_EXIT_CODE);
        }
    };
    if (!SystemInfo.isMac && safeToReload == EncodingUtil.Magic8.NO_WAY) {
        reloadAction.putValue(Action.SMALL_ICON, AllIcons.General.Warning);
    }
    reloadAction.putValue(Action.MNEMONIC_KEY, (int) 'R');
    DialogWrapperAction convertAction = new DialogWrapperAction("Convert") {

        @Override
        protected void doAction(ActionEvent e) {
            if (safeToConvert == EncodingUtil.Magic8.NO_WAY) {
                String error = EncodingUtil.checkCanConvert(virtualFile);
                int res = Messages.showDialog(XmlStringUtil.wrapInHtml("Please do not convert to '" + charset.displayName() + "'.<br><br>" + (error == null ? "Encoding '" + charset.displayName() + "' does not support some characters from the text." : "Because " + error)), "Incompatible Encoding: " + charset.displayName(), new String[] { "Convert anyway", "Cancel" }, 1, AllIcons.General.WarningDialog);
                if (res != 0) {
                    doCancelAction();
                    return;
                }
            }
            close(CONVERT_EXIT_CODE);
        }

        @Override
        public boolean isEnabled() {
            return !FileUtilRt.isTooLarge(virtualFile.getLength());
        }
    };
    if (!SystemInfo.isMac && safeToConvert == EncodingUtil.Magic8.NO_WAY) {
        convertAction.putValue(Action.SMALL_ICON, AllIcons.General.Warning);
    }
    convertAction.putValue(Action.MNEMONIC_KEY, (int) 'C');
    Action cancelAction = getCancelAction();
    cancelAction.putValue(DEFAULT_ACTION, Boolean.TRUE);
    return new Action[] { reloadAction, convertAction, cancelAction };
}
Also used : ActionEvent(java.awt.event.ActionEvent) Charset(java.nio.charset.Charset) NotNull(org.jetbrains.annotations.NotNull)

Example 85 with Charset

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

the class EncodingValidationStrategy method validate.

@Override
void validate(@NotNull Module module, @NotNull AndroidModuleModel androidModel) {
    GradleVersion modelVersion = (androidModel.getModelVersion());
    if (modelVersion != null) {
        boolean isOneDotTwoOrNewer = modelVersion.compareIgnoringQualifiers(myOneDotTwoPluginVersion) >= 0;
        // Verify that the encoding in the model is the same as the encoding in the IDE's project settings.
        Charset modelEncoding = null;
        if (isOneDotTwoOrNewer) {
            try {
                AndroidProject androidProject = androidModel.getAndroidProject();
                modelEncoding = Charset.forName(androidProject.getJavaCompileOptions().getEncoding());
            } catch (UnsupportedCharsetException ignore) {
            // It's not going to happen.
            }
        }
        if (myMismatchingEncoding == null && modelEncoding != null && myProjectEncoding.compareTo(modelEncoding) != 0) {
            myMismatchingEncoding = modelEncoding.displayName();
        }
    }
}
Also used : UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) Charset(java.nio.charset.Charset) AndroidProject(com.android.builder.model.AndroidProject) GradleVersion(com.android.ide.common.repository.GradleVersion)

Aggregations

Charset (java.nio.charset.Charset)1427 IOException (java.io.IOException)268 Test (org.junit.Test)186 InputStream (java.io.InputStream)115 ByteBuffer (java.nio.ByteBuffer)111 File (java.io.File)106 ArrayList (java.util.ArrayList)106 InputStreamReader (java.io.InputStreamReader)102 HashMap (java.util.HashMap)76 CharBuffer (java.nio.CharBuffer)66 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)58 CharsetDecoder (java.nio.charset.CharsetDecoder)57 List (java.util.List)57 Map (java.util.Map)57 OutputStreamWriter (java.io.OutputStreamWriter)56 ByteArrayInputStream (java.io.ByteArrayInputStream)54 CharsetEncoder (java.nio.charset.CharsetEncoder)50 Path (java.nio.file.Path)50 FileInputStream (java.io.FileInputStream)49 BufferedReader (java.io.BufferedReader)48