fixed encoding problems
authorLiana Bakradze <liana.bakradze@jetbrains.com>
Tue, 16 Dec 2014 15:03:58 +0000 (18:03 +0300)
committerLiana Bakradze <liana.bakradze@jetbrains.com>
Tue, 16 Dec 2014 15:03:58 +0000 (18:03 +0300)
python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/actions/CCCreateCourseArchive.java
python/edu/course-creator/src/org/jetbrains/plugins/coursecreator/ui/CreateTaskWindowDialog.java
python/edu/learn-python/src/com/jetbrains/python/edu/StudyDirectoryProjectGenerator.java
python/edu/learn-python/src/com/jetbrains/python/edu/StudyUtils.java
python/edu/learn-python/src/com/jetbrains/python/edu/actions/StudyEditInputAction.java
python/edu/learn-python/src/com/jetbrains/python/edu/actions/StudyShowHintAction.java
python/edu/learn-python/src/com/jetbrains/python/edu/course/Task.java

index de76d5d287de33ee3af1113086e92a6c413b483a..a3d4f56e2b349bb5de249bc1d13cc930cd39fea5 100644 (file)
@@ -226,14 +226,10 @@ public class CCCreateCourseArchive extends DumbAwareAction {
     final Gson gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
     final String json = gson.toJson(course);
     final File courseJson = new File(project.getBasePath(), "course.json");
-    FileWriter writer = null;
+    OutputStreamWriter outputStreamWriter = null;
     try {
-      writer = new FileWriter(courseJson);
-      writer.write(json);
-    }
-    catch (IOException e) {
-      Messages.showErrorDialog(e.getMessage(), "Failed to Generate Json");
-      LOG.info(e);
+      outputStreamWriter = new OutputStreamWriter(new FileOutputStream(courseJson), "UTF-8");
+      outputStreamWriter.write(json);
     }
     catch (Exception e) {
       Messages.showErrorDialog(e.getMessage(), "Failed to Generate Json");
@@ -241,8 +237,8 @@ public class CCCreateCourseArchive extends DumbAwareAction {
     }
     finally {
       try {
-        if (writer != null) {
-          writer.close();
+        if (outputStreamWriter != null) {
+          outputStreamWriter.close();
         }
       }
       catch (IOException e1) {
index 3595e2c28a7babe1d8fd173e552f3366aefdbad8..bcde449693b00351d06df9c414dd6b35d729c871 100644 (file)
@@ -57,7 +57,7 @@ public class CreateTaskWindowDialog extends DialogWrapper {
       if (file.exists()) {
         BufferedReader bufferedReader =  null;
         try {
-          bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
+          bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
           String line;
           while ((line = bufferedReader.readLine()) != null) {
             hintText.append(line).append("\n");
@@ -108,18 +108,29 @@ public class CreateTaskWindowDialog extends DialogWrapper {
     VirtualFile hintsDir = myProject.getBaseDir().findChild("hints");
     if (hintsDir != null) {
       File hintFile = new File(hintsDir.getPath(), hintName);
-      PrintWriter printWriter = null;
+      OutputStreamWriter outputStreamWriter = null;
       try {
-        printWriter = new PrintWriter(hintFile);
-        printWriter.print(hintText);
+        outputStreamWriter = new OutputStreamWriter(new FileOutputStream(hintFile), "UTF-8");
+       outputStreamWriter.write(hintText);
       }
       catch (FileNotFoundException e) {
         //TODO:show error in UI
         return;
       }
+      catch (UnsupportedEncodingException e) {
+        LOG.error(e);
+      }
+      catch (IOException e) {
+        LOG.error(e);
+      }
       finally {
-        if (printWriter != null) {
-          printWriter.close();
+        if (outputStreamWriter != null) {
+          try {
+            outputStreamWriter.close();
+          }
+          catch (IOException e) {
+            //close silently
+          }
         }
       }
     }
index f6435e6b2a9adf457c4e0ee47b5c81b68cec51a4..cc2bc803f0ead60dbf3310e7ff6d80996c782b24 100644 (file)
@@ -160,7 +160,7 @@ public class StudyDirectoryProjectGenerator extends PythonProjectGenerator imple
     myProject = project;
     Reader reader = null;
     try {
-      reader = new InputStreamReader(new FileInputStream(mySelectedCourseFile));
+      reader = new InputStreamReader(new FileInputStream(mySelectedCourseFile), "UTF-8");
       Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
       final Course course = gson.fromJson(reader, Course.class);
       course.init(false);
@@ -227,6 +227,9 @@ public class StudyDirectoryProjectGenerator extends PythonProjectGenerator imple
     catch (FileNotFoundException e) {
       LOG.error(e);
     }
+    catch (UnsupportedEncodingException e) {
+      LOG.error(e);
+    }
     finally {
       StudyUtils.closeSilently(reader);
     }
@@ -314,7 +317,7 @@ public class StudyDirectoryProjectGenerator extends PythonProjectGenerator imple
     BufferedReader reader = null;
     try {
       if (courseFile.getName().equals(COURSE_META_FILE)) {
-        reader = new BufferedReader(new InputStreamReader(new FileInputStream(courseFile)));
+        reader = new BufferedReader(new InputStreamReader(new FileInputStream(courseFile), "UTF-8"));
         JsonReader r = new JsonReader(reader);
         JsonParser parser = new JsonParser();
         JsonElement el = parser.parse(r);
index 4add8f4ce41431553809a227b7fbf532591cec4e..324e58aa4b4c5aec480f99db22d36d44dd76f6ff 100644 (file)
@@ -63,14 +63,14 @@ public class StudyUtils {
 
   @SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
   @Nullable
-  public static String getFileText(String parentDir, String fileName, boolean wrapHTML) {
+  public static String getFileText(String parentDir, String fileName, boolean wrapHTML, String encoding) {
 
     File inputFile = parentDir != null ? new File(parentDir, fileName) : new File(fileName);
     if (!inputFile.exists()) return null;
     StringBuilder taskText = new StringBuilder();
     BufferedReader reader = null;
     try {
-      reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile)));
+      reader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), encoding));
       String line;
       while ((line = reader.readLine()) != null) {
         taskText.append(line).append("\n");
index 72660fc9c2c6db782dddb77121e53cce13c25d03..50dcc3a2e69ac27901f204dec845f5d5b803a279 100644 (file)
@@ -79,8 +79,8 @@ public class StudyEditInputAction extends DumbAwareAction {
       List<UserTest> userTests = currentTask.getUserTests();
       int i = 1;
       for (UserTest userTest : userTests) {
-        String inputFileText = StudyUtils.getFileText(null, userTest.getInput(), false);
-        String outputFileText = StudyUtils.getFileText(null, userTest.getOutput(), false);
+        String inputFileText = StudyUtils.getFileText(null, userTest.getInput(), false, "UTF-8");
+        String outputFileText = StudyUtils.getFileText(null, userTest.getOutput(), false, "UTF-8");
         StudyTestContentPanel myContentPanel = new StudyTestContentPanel(userTest);
         myContentPanel.addInputContent(inputFileText);
         myContentPanel.addOutputContent(outputFileText);
index c07888519d153d3cd41003c63926379c187bde62..482a7417bec33aeeeb0013305a1d6f617f3a1fff 100644 (file)
@@ -81,7 +81,7 @@ public class StudyShowHintAction extends DumbAwareAction {
       if (resourceRoot != null && resourceRoot.exists()) {
         File hintsDir = new File(resourceRoot, Course.HINTS_DIR);
         if (hintsDir.exists()) {
-          hintText = StudyUtils.getFileText(hintsDir.getAbsolutePath(), hintFileName, true);
+          hintText = StudyUtils.getFileText(hintsDir.getAbsolutePath(), hintFileName, true, "UTF-8");
         }
       }
     }
index 26def69d14c5f6ee44e879f4a1d1897cc3055c18..0a106ff597ad8db057f00b5455518b61396d3d15 100644 (file)
@@ -195,7 +195,7 @@ public class Task implements Stateful {
   public String getResourceText(@NotNull final Project project, @NotNull final String fileName, boolean wrapHTML) {
     VirtualFile taskDir = getTaskDir(project);
     if (taskDir != null) {
-      return StudyUtils.getFileText(taskDir.getCanonicalPath(), fileName, wrapHTML);
+      return StudyUtils.getFileText(taskDir.getCanonicalPath(), fileName, wrapHTML, "UTF-8");
     }
     return null;
   }