1 package com.jetbrains.edu.learning;
3 import com.google.gson.*;
4 import com.google.gson.reflect.TypeToken;
5 import com.intellij.openapi.editor.Document;
6 import com.intellij.openapi.editor.EditorFactory;
7 import com.intellij.openapi.fileEditor.FileDocumentManager;
8 import com.intellij.openapi.project.Project;
9 import com.intellij.openapi.util.io.FileUtil;
10 import com.intellij.openapi.vfs.LocalFileSystem;
11 import com.intellij.openapi.vfs.VirtualFile;
12 import com.intellij.util.containers.hash.HashMap;
13 import com.jetbrains.edu.learning.core.EduNames;
14 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
15 import com.jetbrains.edu.learning.courseFormat.Course;
16 import com.jetbrains.edu.learning.courseFormat.StudyStatus;
17 import com.jetbrains.edu.learning.courseFormat.TaskFile;
18 import org.jdom.Attribute;
19 import org.jdom.Element;
20 import org.jdom.output.XMLOutputter;
23 import java.lang.reflect.Type;
24 import java.util.Collections;
25 import java.util.List;
28 public class StudySerializationUtils {
30 public static final String PLACEHOLDERS = "placeholders";
31 public static final String LINE = "line";
32 public static final String START = "start";
33 public static final String LENGTH = "length";
34 public static final String POSSIBLE_ANSWER = "possible_answer";
35 public static final String HINT = "hint";
36 public static final String ADDITIONAL_HINTS = "additional_hints";
37 public static final String OFFSET = "offset";
38 public static final String TEXT = "text";
39 public static final String LESSONS = "lessons";
40 public static final String COURSE = "course";
41 public static final String COURSE_TITLED = "Course";
42 public static final String STATUS = "status";
43 public static final String AUTHOR = "author";
44 public static final String AUTHORS = "authors";
45 public static final String MY_INITIAL_START = "myInitialStart";
47 private StudySerializationUtils() {
50 public static class StudyUnrecognizedFormatException extends Exception {
53 public static class Xml {
54 public final static String COURSE_ELEMENT = "courseElement";
55 public final static String MAIN_ELEMENT = "StudyTaskManager";
56 public static final String MAP = "map";
57 public static final String KEY = "key";
58 public static final String VALUE = "value";
59 public static final String NAME = "name";
60 public static final String LIST = "list";
61 public static final String OPTION = "option";
62 public static final String INDEX = "index";
63 public static final String STUDY_STATUS_MAP = "myStudyStatusMap";
64 public static final String TASK_STATUS_MAP = "myTaskStatusMap";
65 public static final String LENGTH = "length";
66 public static final String ANSWER_PLACEHOLDERS = "answerPlaceholders";
67 public static final String TASK_LIST = "taskList";
68 public static final String TASK_FILES = "taskFiles";
69 public static final String INITIAL_STATE = "initialState";
70 public static final String MY_INITIAL_STATE = "MyInitialState";
71 public static final String MY_LINE = "myLine";
72 public static final String MY_START = "myStart";
73 public static final String MY_LENGTH = "myLength";
74 public static final String HINTS = "hints";
75 public static final String HINT = "hint";
76 public static final String AUTHOR_TITLED = "Author";
77 public static final String FIRST_NAME = "first_name";
78 public static final String SECOND_NAME = "second_name";
79 public static final String MY_INITIAL_LINE = "myInitialLine";
80 public static final String MY_INITIAL_LENGTH = "myInitialLength";
81 public static final String ANSWER_PLACEHOLDER = "AnswerPlaceholder";
82 public static final String TASK_WINDOWS = "taskWindows";
83 public static final String RESOURCE_PATH = "resourcePath";
84 public static final String COURSE_DIRECTORY = "courseDirectory";
89 public static int getVersion(Element element) throws StudyUnrecognizedFormatException {
90 if (element.getChild(COURSE_ELEMENT) != null) {
94 final Element taskManager = element.getChild(MAIN_ELEMENT);
96 Element versionElement = getChildWithName(taskManager, "VERSION");
97 if (versionElement == null) {
101 return Integer.valueOf(versionElement.getAttributeValue(VALUE));
104 public static Element convertToSecondVersion(Element element) throws StudyUnrecognizedFormatException {
105 final Element oldCourseElement = element.getChild(COURSE_ELEMENT);
106 Element state = new Element(MAIN_ELEMENT);
108 Element course = addChildWithName(state, COURSE, oldCourseElement.clone());
109 course.setName(COURSE_TITLED);
111 Element author = getChildWithName(course, AUTHOR);
112 String authorString = author.getAttributeValue(VALUE);
113 course.removeContent(author);
115 String[] names = authorString.split(" ", 2);
116 Element authorElement = new Element(AUTHOR_TITLED);
117 addChildWithName(authorElement, FIRST_NAME, names[0]);
118 addChildWithName(authorElement, SECOND_NAME, names.length == 1 ? "" : names[1]);
120 addChildList(course, AUTHORS, Collections.singletonList(authorElement));
122 Element courseDirectoryElement = getChildWithName(course, RESOURCE_PATH);
123 renameElement(courseDirectoryElement, COURSE_DIRECTORY);
125 for (Element lesson : getChildList(course, LESSONS)) {
126 incrementIndex(lesson);
127 for (Element task : getChildList(lesson, TASK_LIST)) {
128 incrementIndex(task);
129 Map<String, Element> taskFiles = getChildMap(task, TASK_FILES);
130 for (Element taskFile : taskFiles.values()) {
131 renameElement(getChildWithName(taskFile, TASK_WINDOWS), ANSWER_PLACEHOLDERS);
132 for (Element placeholder : getChildList(taskFile, ANSWER_PLACEHOLDERS)) {
133 placeholder.setName(ANSWER_PLACEHOLDER);
135 Element initialState = new Element(MY_INITIAL_STATE);
136 addChildWithName(placeholder, INITIAL_STATE, initialState);
137 addChildWithName(initialState, MY_LINE, getChildWithName(placeholder, MY_INITIAL_LINE).getAttributeValue(VALUE));
138 addChildWithName(initialState, MY_START, getChildWithName(placeholder, MY_INITIAL_START).getAttributeValue(VALUE));
139 addChildWithName(initialState, MY_LENGTH, getChildWithName(placeholder, MY_INITIAL_LENGTH).getAttributeValue(VALUE));
144 element.removeContent();
145 element.addContent(state);
149 public static Map<String, String> fillStatusMap(Element taskManagerElement, String mapName, XMLOutputter outputter)
150 throws StudyUnrecognizedFormatException {
151 Map<Element, String> sourceMap = getChildMap(taskManagerElement, mapName);
152 Map<String, String> destMap = new HashMap<>();
153 for (Map.Entry<Element, String> entry : sourceMap.entrySet()) {
154 String status = entry.getValue();
155 if (status.equals(StudyStatus.Unchecked.toString())) {
158 destMap.put(outputter.outputString(entry.getKey()), status);
163 public static Element convertToThirdVersion(Element state, Project project) throws StudyUnrecognizedFormatException {
164 Element taskManagerElement = state.getChild(MAIN_ELEMENT);
165 XMLOutputter outputter = new XMLOutputter();
167 Map<String, String> placeholderTextToStatus = fillStatusMap(taskManagerElement, STUDY_STATUS_MAP, outputter);
168 Map<String, String> taskFileToStatusMap = fillStatusMap(taskManagerElement, TASK_STATUS_MAP, outputter);
170 Element courseElement = getChildWithName(taskManagerElement, COURSE).getChild(COURSE_TITLED);
171 for (Element lesson : getChildList(courseElement, LESSONS)) {
172 int lessonIndex = getAsInt(lesson, INDEX);
173 for (Element task : getChildList(lesson, TASK_LIST)) {
174 String taskStatus = null;
175 int taskIndex = getAsInt(task, INDEX);
176 Map<String, Element> taskFiles = getChildMap(task, TASK_FILES);
177 for (Map.Entry<String, Element> entry : taskFiles.entrySet()) {
178 Element taskFileElement = entry.getValue();
179 String taskFileText = outputter.outputString(taskFileElement);
180 String taskFileStatus = taskFileToStatusMap.get(taskFileText);
181 if (taskFileStatus != null && (taskStatus == null || taskFileStatus.equals(StudyStatus.Failed.toString()))) {
182 taskStatus = taskFileStatus;
184 Document document = StudyUtils.getDocument(project.getBasePath(), lessonIndex, taskIndex, entry.getKey());
185 if (document == null) {
188 for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS)) {
189 taskStatus = addStatus(outputter, placeholderTextToStatus, taskStatus, placeholder);
190 addOffset(document, placeholder);
191 addInitialState(document, placeholder);
194 if (taskStatus != null) {
195 addChildWithName(task, STATUS, taskStatus);
202 public static String addStatus(XMLOutputter outputter,
203 Map<String, String> placeholderTextToStatus,
205 Element placeholder) {
206 String placeholderText = outputter.outputString(placeholder);
207 String status = placeholderTextToStatus.get(placeholderText);
208 if (status != null) {
209 addChildWithName(placeholder, STATUS, status);
210 if (taskStatus == null || status.equals(StudyStatus.Failed.toString())) {
217 public static void addInitialState(Document document, Element placeholder) throws StudyUnrecognizedFormatException {
218 Element initialState = getChildWithName(placeholder, INITIAL_STATE).getChild(MY_INITIAL_STATE);
219 int initialLine = getAsInt(initialState, MY_LINE);
220 int initialStart = getAsInt(initialState, MY_START);
221 int initialOffset = document.getLineStartOffset(initialLine) + initialStart;
222 addChildWithName(initialState, OFFSET, initialOffset);
223 renameElement(getChildWithName(initialState, MY_LENGTH), LENGTH);
226 public static void addOffset(Document document, Element placeholder) throws StudyUnrecognizedFormatException {
227 int line = getAsInt(placeholder, LINE);
228 int start = getAsInt(placeholder, START);
229 int offset = document.getLineStartOffset(line) + start;
230 addChildWithName(placeholder, OFFSET, offset);
233 public static int getAsInt(Element element, String name) throws StudyUnrecognizedFormatException {
234 return Integer.valueOf(getChildWithName(element, name).getAttributeValue(VALUE));
237 public static void incrementIndex(Element element) throws StudyUnrecognizedFormatException {
238 Element index = getChildWithName(element, INDEX);
239 int indexValue = Integer.parseInt(index.getAttributeValue(VALUE));
240 changeValue(index, indexValue + 1);
243 public static void renameElement(Element element, String newName) {
244 element.setAttribute(NAME, newName);
247 public static void changeValue(Element element, Object newValue) {
248 element.setAttribute(VALUE, newValue.toString());
251 public static Element addChildWithName(Element parent, String name, Element value) {
252 Element child = new Element(OPTION);
253 child.setAttribute(NAME, name);
254 child.addContent(value);
255 parent.addContent(child);
259 public static Element addChildWithName(Element parent, String name, Object value) {
260 Element child = new Element(OPTION);
261 child.setAttribute(NAME, name);
262 child.setAttribute(VALUE, value.toString());
263 parent.addContent(child);
267 public static Element addChildList(Element parent, String name, List<Element> elements) {
268 Element listElement = new Element(LIST);
269 for (Element element : elements) {
270 listElement.addContent(element);
272 return addChildWithName(parent, name, listElement);
275 public static List<Element> getChildList(Element parent, String name) throws StudyUnrecognizedFormatException {
276 return getChildList(parent, name, false);
279 public static List<Element> getChildList(Element parent, String name, boolean optional) throws StudyUnrecognizedFormatException {
280 Element listParent = getChildWithName(parent, name, optional);
281 if (listParent != null) {
282 Element list = listParent.getChild(LIST);
284 return list.getChildren();
287 return Collections.emptyList();
290 public static Element getChildWithName(Element parent, String name) throws StudyUnrecognizedFormatException {
291 return getChildWithName(parent, name, false);
294 public static Element getChildWithName(Element parent, String name, boolean optional) throws StudyUnrecognizedFormatException {
295 for (Element child : parent.getChildren()) {
296 Attribute attribute = child.getAttribute(NAME);
297 if (attribute == null) {
300 if (name.equals(attribute.getValue())) {
307 throw new StudyUnrecognizedFormatException();
310 public static <K, V> Map<K, V> getChildMap(Element element, String name) throws StudyUnrecognizedFormatException {
311 return getChildMap(element, name, false);
314 public static <K, V> Map<K, V> getChildMap(Element element, String name, boolean optional) throws StudyUnrecognizedFormatException {
315 Element mapParent = getChildWithName(element, name, optional);
316 if (mapParent != null) {
317 Element map = mapParent.getChild(MAP);
319 HashMap result = new HashMap();
320 for (Element entry : map.getChildren()) {
321 Object key = entry.getAttribute(KEY) == null ? entry.getChild(KEY).getChildren().get(0) : entry.getAttributeValue(KEY);
322 Object value = entry.getAttribute(VALUE) == null ? entry.getChild(VALUE).getChildren().get(0) : entry.getAttributeValue(VALUE);
323 result.put(key, value);
328 return Collections.emptyMap();
332 public static class Json {
334 public static final String TASK_LIST = "task_list";
335 public static final String TASK_FILES = "task_files";
340 public static class CourseTypeAdapter implements JsonDeserializer<Course> {
342 private final File myCourseFile;
344 public CourseTypeAdapter(File courseFile) {
345 myCourseFile = courseFile;
349 public Course deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
350 JsonObject courseObject = json.getAsJsonObject();
351 JsonArray lessons = courseObject.getAsJsonArray(LESSONS);
352 for (int lessonIndex = 1; lessonIndex <= lessons.size(); lessonIndex++) {
353 JsonObject lessonObject = lessons.get(lessonIndex - 1).getAsJsonObject();
354 JsonArray tasks = lessonObject.getAsJsonArray(TASK_LIST);
355 for (int taskIndex = 1; taskIndex <= tasks.size(); taskIndex++) {
356 JsonObject taskObject = tasks.get(taskIndex - 1).getAsJsonObject();
357 for (Map.Entry<String, JsonElement> taskFile : taskObject.getAsJsonObject(TASK_FILES).entrySet()) {
358 String name = taskFile.getKey();
359 String filePath = FileUtil.join(myCourseFile.getParent(), EduNames.LESSON + lessonIndex, EduNames.TASK + taskIndex, name);
360 VirtualFile resourceFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(filePath));
361 if (resourceFile == null) {
364 Document document = FileDocumentManager.getInstance().getDocument(resourceFile);
365 if (document == null) {
368 JsonObject taskFileObject = taskFile.getValue().getAsJsonObject();
369 JsonArray placeholders = taskFileObject.getAsJsonArray(PLACEHOLDERS);
370 for (JsonElement placeholder : placeholders) {
371 JsonObject placeholderObject = placeholder.getAsJsonObject();
372 if (placeholderObject.getAsJsonPrimitive(OFFSET) != null) {
375 int line = placeholderObject.getAsJsonPrimitive(LINE).getAsInt();
376 int start = placeholderObject.getAsJsonPrimitive(START).getAsInt();
377 int offset = document.getLineStartOffset(line) + start;
378 placeholderObject.addProperty(OFFSET, offset);
383 return new GsonBuilder().create().fromJson(json, Course.class);
387 public static class StepicTaskFileAdapter implements JsonDeserializer<TaskFile> {
390 public TaskFile deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
391 final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
392 JsonObject taskFileObject = json.getAsJsonObject();
393 JsonArray placeholders = taskFileObject.getAsJsonArray(PLACEHOLDERS);
394 for (JsonElement placeholder : placeholders) {
395 JsonObject placeholderObject = placeholder.getAsJsonObject();
396 int line = placeholderObject.getAsJsonPrimitive(LINE).getAsInt();
397 int start = placeholderObject.getAsJsonPrimitive(START).getAsInt();
399 placeholderObject.addProperty(OFFSET, start);
402 Document document = EditorFactory.getInstance().createDocument(taskFileObject.getAsJsonPrimitive(TEXT).getAsString());
403 placeholderObject.addProperty(OFFSET, document.getLineStartOffset(line) + start);
405 final String hintString = placeholderObject.getAsJsonPrimitive(HINT).getAsString();
406 final JsonArray hintsArray = new JsonArray();
409 final Type listType = new TypeToken<List<String>>() {
411 final List<String> hints = gson.fromJson(hintString, listType);
412 if (!hints.isEmpty()) {
413 for (int i = 0; i < hints.size(); i++) {
415 placeholderObject.addProperty(HINT, hints.get(0));
418 hintsArray.add(hints.get(i));
420 placeholderObject.add(ADDITIONAL_HINTS, hintsArray);
423 placeholderObject.addProperty(HINT, "");
426 catch (JsonParseException e) {
427 hintsArray.add(hintString);
431 return gson.fromJson(json, TaskFile.class);
435 public static class StepicAnswerPlaceholderAdapter implements JsonSerializer<AnswerPlaceholder> {
437 public JsonElement serialize(AnswerPlaceholder src, Type typeOfSrc, JsonSerializationContext context) {
438 final List<String> hints = src.getHints();
440 final int length = src.getLength();
441 final int start = src.getOffset();
442 final String possibleAnswer = src.getPossibleAnswer();
445 final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
446 final JsonObject answerPlaceholder = new JsonObject();
447 answerPlaceholder.addProperty(LINE, line);
448 answerPlaceholder.addProperty(START, start);
449 answerPlaceholder.addProperty(LENGTH, length);
450 answerPlaceholder.addProperty(POSSIBLE_ANSWER, possibleAnswer);
452 final String jsonHints = gson.toJson(hints);
453 answerPlaceholder.addProperty(HINT, jsonHints);
455 return answerPlaceholder;