for (VirtualFile answerFile : files) {
ApplicationManager.getApplication().runWriteAction(() -> {
String answerName = answerFile.getName();
- String name = FileUtil.getNameWithoutExtension(FileUtil.getNameWithoutExtension(answerName)) + "." + FileUtilRt.getExtension(answerName);
- VirtualFile file = answerFile.getParent().findChild(name);
- try {
- if (file != null) {
- file.delete(CCProjectComponent.class);
- }
- answerFile.rename(CCProjectComponent.class, name);
- }
- catch (IOException e) {
- LOG.error(e);
- }
+ String nameWithoutExtension = FileUtil.getNameWithoutExtension(answerName);
+ String name = FileUtil.getNameWithoutExtension(nameWithoutExtension) + "." + FileUtilRt.getExtension(answerName);
+ VirtualFile parent = answerFile.getParent();
+ VirtualFile file = parent.findChild(name);
+ try {
+ if (file != null) {
+ file.delete(CCProjectComponent.class);
+ }
+ VirtualFile windowsDescrFile = parent.findChild(FileUtil.getNameWithoutExtension(name) + EduNames.WINDOWS_POSTFIX);
+ if (windowsDescrFile != null) {
+ windowsDescrFile.delete(CCProjectComponent.class);
+ }
+ answerFile.rename(CCProjectComponent.class, name);
+ }
+ catch (IOException e) {
+ LOG.error(e);
+ }
});
}
}
if (taskFile == null) {
taskFile = LocalFileSystem.getInstance().findFileByPath(FileUtil.join(taskPath, EduNames.SRC, answerName));
}
- if (taskFile!= null) {
+ if (taskFile != null) {
result.add(taskFile);
}
}
public void loadState(Element state) {
try {
Element courseElement = getChildWithName(state, COURSE).getChild(COURSE_TITLED);
- for (Element lesson : getChildList(courseElement, LESSONS)) {
+ for (Element lesson : getChildList(courseElement, LESSONS, true)) {
int lessonIndex = getAsInt(lesson, INDEX);
- for (Element task : getChildList(lesson, TASK_LIST)) {
+ for (Element task : getChildList(lesson, TASK_LIST, true)) {
int taskIndex = getAsInt(task, INDEX);
- Map<String, Element> taskFiles = getChildMap(task, TASK_FILES);
+ Map<String, Element> taskFiles = getChildMap(task, TASK_FILES, true);
for (Map.Entry<String, Element> entry : taskFiles.entrySet()) {
Element taskFileElement = entry.getValue();
String name = entry.getKey();
if (document == null) {
continue;
}
- for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS)) {
- Element lineElement = getChildWithName(placeholder, LINE);
+ for (Element placeholder : getChildList(taskFileElement, ANSWER_PLACEHOLDERS, true)) {
+ Element lineElement = getChildWithName(placeholder, LINE, true);
int line = lineElement != null ? Integer.valueOf(lineElement.getAttributeValue(VALUE)) : 0;
- Element startElement = getChildWithName(placeholder, START);
+ Element startElement = getChildWithName(placeholder, START, true);
int start = startElement != null ? Integer.valueOf(startElement.getAttributeValue(VALUE)) : 0;
int offset = document.getLineStartOffset(line) + start;
addChildWithName(placeholder, OFFSET, offset);
private StudySerializationUtils() {
}
- public static class StudyUnrecognizedFormatException extends Exception {}
+ public static class StudyUnrecognizedFormatException extends Exception {
+ }
public static class Xml {
public final static String COURSE_ELEMENT = "courseElement";
addChildWithName(initialState, MY_LENGTH, getChildWithName(placeholder, MY_INITIAL_LENGTH).getAttributeValue(VALUE));
}
}
-
}
}
element.removeContent();
}
public static void addHints(@NotNull Element placeholder) throws StudyUnrecognizedFormatException {
- final String hint = getChildWithName(placeholder, HINT).getAttribute(VALUE).getValue();
+ Element element = getChildWithName(placeholder, HINT, true);
+ if (element == null) {
+ return;
+ }
+ final String hint = element.getAttribute(VALUE).getValue();
Element listElement = new Element(LIST);
final Element hintElement = new Element(OPTION);
hintElement.setAttribute(VALUE, hint);
}
public static List<Element> getChildList(Element parent, String name) throws StudyUnrecognizedFormatException {
- Element listParent = getChildWithName(parent, name);
+ return getChildList(parent, name, false);
+ }
+
+ public static List<Element> getChildList(Element parent, String name, boolean optional) throws StudyUnrecognizedFormatException {
+ Element listParent = getChildWithName(parent, name, optional);
if (listParent != null) {
Element list = listParent.getChild(LIST);
if (list != null) {
}
public static Element getChildWithName(Element parent, String name) throws StudyUnrecognizedFormatException {
+ return getChildWithName(parent, name, false);
+ }
+
+ public static Element getChildWithName(Element parent, String name, boolean optional) throws StudyUnrecognizedFormatException {
for (Element child : parent.getChildren()) {
Attribute attribute = child.getAttribute(NAME);
if (attribute == null) {
return child;
}
}
+ if (optional) {
+ return null;
+ }
throw new StudyUnrecognizedFormatException();
}
public static <K, V> Map<K, V> getChildMap(Element element, String name) throws StudyUnrecognizedFormatException {
- Element mapParent = getChildWithName(element, name);
+ return getChildMap(element, name, false);
+ }
+
+ public static <K, V> Map<K, V> getChildMap(Element element, String name, boolean optional) throws StudyUnrecognizedFormatException {
+ Element mapParent = getChildWithName(element, name, optional);
if (mapParent != null) {
Element map = mapParent.getChild(MAP);
if (map != null) {
int start = placeholderObject.getAsJsonPrimitive(START).getAsInt();
if (line == -1) {
placeholderObject.addProperty(OFFSET, start);
- } else {
+ }
+ else {
Document document = EditorFactory.getInstance().createDocument(taskFileObject.getAsJsonPrimitive(TEXT).getAsString());
placeholderObject.addProperty(OFFSET, document.getLineStartOffset(line) + start);
}
final JsonArray hintsArray = new JsonArray();
try {
- final Type listType = new TypeToken<List<String>>() {}.getType();
+ final Type listType = new TypeToken<List<String>>() {
+ }.getType();
final List<String> hints = gson.fromJson(hintString, listType);
for (String hint : hints) {
hintsArray.add(hint);