import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
+import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
*/
@State(name = "CCProjectService", storages = @Storage("course_service.xml"))
public class CCProjectService implements PersistentStateComponent<Element> {
+ private static final Logger LOG = Logger.getInstance(CCProjectService.class);
private Course myCourse;
@Transient private final Project myProject;
@Override
public Element getState() {
+ if (myCourse == null) {
+ return null;
+ }
return XmlSerializer.serialize(this);
}
@Override
public void loadState(Element state) {
- Element courseElement = getChildWithName(state, COURSE).getChild(COURSE_TITLED);
- for (Element lesson : getChildList(courseElement, LESSONS)) {
- int lessonIndex = getAsInt(lesson, INDEX);
- for (Element task : getChildList(lesson, TASK_LIST)) {
- int taskIndex = getAsInt(task, INDEX);
- Map<String, Element> taskFiles = getChildMap(task, TASK_FILES);
- for (Map.Entry<String, Element> entry : taskFiles.entrySet()) {
- Element taskFileElement = entry.getValue();
- String name = entry.getKey();
- String answerName = FileUtil.getNameWithoutExtension(name) + CCUtils.ANSWER_EXTENSION_DOTTED + FileUtilRt.getExtension(name);
- Document document = StudyUtils.getDocument(myProject.getBasePath(), lessonIndex, taskIndex, answerName);
- if (document == null) {
- continue;
- }
- for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS)) {
- Element lineElement = getChildWithName(placeholder, LINE);
- int line = lineElement != null ? Integer.valueOf(lineElement.getAttributeValue(VALUE)) : 0;
- Element startElement = getChildWithName(placeholder, START);
- int start = startElement != null ? Integer.valueOf(startElement.getAttributeValue(VALUE)) : 0;
- int offset = document.getLineStartOffset(line) + start;
- addChildWithName(placeholder, OFFSET, offset);
- addChildWithName(placeholder, "useLength", "false");
+ try {
+ Element courseElement = getChildWithName(state, COURSE).getChild(COURSE_TITLED);
+ for (Element lesson : getChildList(courseElement, LESSONS)) {
+ int lessonIndex = getAsInt(lesson, INDEX);
+ for (Element task : getChildList(lesson, TASK_LIST)) {
+ int taskIndex = getAsInt(task, INDEX);
+ Map<String, Element> taskFiles = getChildMap(task, TASK_FILES);
+ for (Map.Entry<String, Element> entry : taskFiles.entrySet()) {
+ Element taskFileElement = entry.getValue();
+ String name = entry.getKey();
+ String answerName = FileUtil.getNameWithoutExtension(name) + CCUtils.ANSWER_EXTENSION_DOTTED + FileUtilRt.getExtension(name);
+ Document document = StudyUtils.getDocument(myProject.getBasePath(), lessonIndex, taskIndex, answerName);
+ if (document == null) {
+ continue;
+ }
+ for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS)) {
+ Element lineElement = getChildWithName(placeholder, LINE);
+ int line = lineElement != null ? Integer.valueOf(lineElement.getAttributeValue(VALUE)) : 0;
+ Element startElement = getChildWithName(placeholder, START);
+ int start = startElement != null ? Integer.valueOf(startElement.getAttributeValue(VALUE)) : 0;
+ int offset = document.getLineStartOffset(line) + start;
+ addChildWithName(placeholder, OFFSET, offset);
+ addChildWithName(placeholder, "useLength", "false");
+ }
}
}
}
+ XmlSerializer.deserializeInto(this, state);
+ } catch (StudyUnrecognizedFormatException e) {
+ LOG.error(e);
}
- XmlSerializer.deserializeInto(this, state);
}
public static CCProjectService getInstance(@NotNull Project project) {
import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
-import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.lang.reflect.Type;
-import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
private StudySerializationUtils() {
}
+ public static class StudyUnrecognizedFormatException extends Exception {}
+
public static class Xml {
public final static String COURSE_ELEMENT = "courseElement";
public final static String MAIN_ELEMENT = "StudyTaskManager";
private Xml() {
}
- public static int getVersion(Element element) {
+ public static int getVersion(Element element) throws StudyUnrecognizedFormatException {
if (element.getChild(COURSE_ELEMENT) != null) {
return 1;
}
return -1;
}
- return Integer.valueOf(versionElement.getAttributeValue("value"));
+ return Integer.valueOf(versionElement.getAttributeValue(VALUE));
}
- public static Element convertToSecondVersion(Element element) {
+ public static Element convertToSecondVersion(Element element) throws StudyUnrecognizedFormatException {
final Element oldCourseElement = element.getChild(COURSE_ELEMENT);
Element state = new Element(MAIN_ELEMENT);
addChildWithName(authorElement, FIRST_NAME, names[0]);
addChildWithName(authorElement, SECOND_NAME, names.length == 1 ? "" : names[1]);
- addChildList(course, AUTHORS, Arrays.asList(authorElement));
+ addChildList(course, AUTHORS, Collections.singletonList(authorElement));
Element courseDirectoryElement = getChildWithName(course, RESOURCE_PATH);
renameElement(courseDirectoryElement, COURSE_DIRECTORY);
return element;
}
- public static Map<String, String> fillStatusMap(Element taskManagerElement, String mapName, XMLOutputter outputter) {
+ public static Map<String, String> fillStatusMap(Element taskManagerElement, String mapName, XMLOutputter outputter)
+ throws StudyUnrecognizedFormatException {
Map<Element, String> sourceMap = getChildMap(taskManagerElement, mapName);
Map<String, String> destMap = new HashMap<>();
for (Map.Entry<Element, String> entry : sourceMap.entrySet()) {
return destMap;
}
- public static Element convertToThirdVersion(Element state, Project project) {
+ public static Element convertToThirdVersion(Element state, Project project) throws StudyUnrecognizedFormatException {
Element taskManagerElement = state.getChild(MAIN_ELEMENT);
XMLOutputter outputter = new XMLOutputter();
return taskStatus;
}
- public static void addInitialState(Document document, Element placeholder) {
+ public static void addInitialState(Document document, Element placeholder) throws StudyUnrecognizedFormatException {
Element initialState = getChildWithName(placeholder, INITIAL_STATE).getChild(MY_INITIAL_STATE);
int initialLine = getAsInt(initialState, MY_LINE);
int initialStart = getAsInt(initialState, MY_START);
renameElement(getChildWithName(initialState, MY_LENGTH), LENGTH);
}
- public static void addOffset(Document document, Element placeholder) {
+ public static void addOffset(Document document, Element placeholder) throws StudyUnrecognizedFormatException {
int line = getAsInt(placeholder, LINE);
int start = getAsInt(placeholder, START);
int offset = document.getLineStartOffset(line) + start;
addChildWithName(placeholder, OFFSET, offset);
}
- public static int getAsInt(Element element, String name) {
+ public static int getAsInt(Element element, String name) throws StudyUnrecognizedFormatException {
return Integer.valueOf(getChildWithName(element, name).getAttributeValue(VALUE));
}
- public static void incrementIndex(Element element) {
+ public static void incrementIndex(Element element) throws StudyUnrecognizedFormatException {
Element index = getChildWithName(element, INDEX);
int indexValue = Integer.parseInt(index.getAttributeValue(VALUE));
changeValue(index, indexValue + 1);
return addChildWithName(parent, name, listElement);
}
- public static List<Element> getChildList(Element parent, String name) {
+ public static List<Element> getChildList(Element parent, String name) throws StudyUnrecognizedFormatException {
Element listParent = getChildWithName(parent, name);
if (listParent != null) {
Element list = listParent.getChild(LIST);
return Collections.emptyList();
}
- @Nullable
- public static Element getChildWithName(Element parent, String name) {
+ public static Element getChildWithName(Element parent, String name) throws StudyUnrecognizedFormatException {
for (Element child : parent.getChildren()) {
Attribute attribute = child.getAttribute(NAME);
if (attribute == null) {
return child;
}
}
- return null;
+ throw new StudyUnrecognizedFormatException();
}
- public static <K, V> Map<K, V> getChildMap(Element element, String name) {
+ public static <K, V> Map<K, V> getChildMap(Element element, String name) throws StudyUnrecognizedFormatException {
Element mapParent = getChildWithName(element, name);
if (mapParent != null) {
Element map = mapParent.getChild(MAP);
import com.jetbrains.edu.learning.stepic.StepicUser;
import com.jetbrains.edu.learning.ui.StudyToolWindow;
import org.jdom.Element;
+import org.jdom.output.XMLOutputter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@Nullable
@Override
public Element getState() {
- Element el = new Element("taskManager");
- if (myCourse != null) {
- Element courseElement = new Element(StudySerializationUtils.Xml.MAIN_ELEMENT);
- XmlSerializer.serializeInto(this, courseElement);
- el.addContent(courseElement);
+ if (myCourse == null) {
+ return null;
}
+ Element el = new Element("taskManager");
+ Element courseElement = new Element(StudySerializationUtils.Xml.MAIN_ELEMENT);
+ XmlSerializer.serializeInto(this, courseElement);
+ el.addContent(courseElement);
return el;
}
@Override
public void loadState(Element state) {
- int version = StudySerializationUtils.Xml.getVersion(state);
- if (version == -1) {
- LOG.error("StudyTaskManager doesn't contain any version:\n" + state.getValue());
- return;
- }
- switch (version) {
- case 1:
- state = StudySerializationUtils.Xml.convertToSecondVersion(state);
- case 2:
- state = StudySerializationUtils.Xml.convertToThirdVersion(state, myProject);
- //uncomment for future versions
- //case 3:
- //state = StudySerializationUtils.Xml.convertToForthVersion(state, myProject);
- }
-
- XmlSerializer.deserializeInto(this, state.getChild(StudySerializationUtils.Xml.MAIN_ELEMENT));
- VERSION = CURRENT_VERSION;
- if (myCourse != null) {
- myCourse.initCourse(true);
- if (version != VERSION) {
- String updatedCoursePath = FileUtil.join(PathManager.getConfigPath(), "courses", myCourse.getName());
- if (new File(updatedCoursePath).exists()) {
- myCourse.setCourseDirectory(updatedCoursePath);
+ try {
+ int version = StudySerializationUtils.Xml.getVersion(state);
+ if (version == -1) {
+ LOG.error("StudyTaskManager doesn't contain any version:\n" + state.getValue());
+ return;
+ }
+ switch (version) {
+ case 1:
+ state = StudySerializationUtils.Xml.convertToSecondVersion(state);
+ case 2:
+ state = StudySerializationUtils.Xml.convertToThirdVersion(state, myProject);
+ //uncomment for future versions
+ //case 3:
+ //state = StudySerializationUtils.Xml.convertToForthVersion(state, myProject);
+ }
+ XmlSerializer.deserializeInto(this, state.getChild(StudySerializationUtils.Xml.MAIN_ELEMENT));
+ VERSION = CURRENT_VERSION;
+ if (myCourse != null) {
+ myCourse.initCourse(true);
+ if (version != VERSION) {
+ String updatedCoursePath = FileUtil.join(PathManager.getConfigPath(), "courses", myCourse.getName());
+ if (new File(updatedCoursePath).exists()) {
+ myCourse.setCourseDirectory(updatedCoursePath);
+ }
}
}
}
+ catch (StudySerializationUtils.StudyUnrecognizedFormatException e) {
+ LOG.error("Unexpected course format:\n", new XMLOutputter().outputString(state));
+ }
}
public static StudyTaskManager getInstance(@NotNull final Project project) {
public void setTurnEditingMode(boolean turnEditingMode) {
myTurnEditingMode = turnEditingMode;
}
-
+
public StepicUser getUser() {
return myUser;
}