Allow a file to be loaded if it references a missing component property,

but show a warning.
This commit is contained in:
Branislav Stojkovic
2019-02-15 23:23:12 +01:00
parent 20645fed13
commit f53ad8c206
2 changed files with 76 additions and 36 deletions
@@ -21,36 +21,38 @@
*/ */
package org.diylc.presenter; package org.diylc.presenter;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.HashSet;
import java.util.Set; import java.util.List;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.Utils; import org.apache.log4j.Logger;
import org.diylc.appframework.simplemq.MessageDispatcher; import org.diylc.appframework.miscutils.Utils;
import org.diylc.common.EventType; import org.diylc.appframework.simplemq.MessageDispatcher;
import org.diylc.core.Project; import org.diylc.common.EventType;
import org.diylc.parsing.IOldFileParser; import org.diylc.core.Project;
import org.w3c.dom.Document; import org.diylc.parsing.IOldFileParser;
import org.xml.sax.InputSource; import org.w3c.dom.Document;
import org.xml.sax.SAXException; import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver; import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.MapperWrapper;
public class ProjectFileManager { public class ProjectFileManager {
@@ -66,11 +68,29 @@ public class ProjectFileManager {
private MessageDispatcher<EventType> messageDispatcher; private MessageDispatcher<EventType> messageDispatcher;
private List<IOldFileParser> parsers; private List<IOldFileParser> parsers;
private Set<String> missingFields = new HashSet<String>();
public ProjectFileManager(MessageDispatcher<EventType> messageDispatcher) { public ProjectFileManager(MessageDispatcher<EventType> messageDispatcher) {
super(); super();
this.xStream = new XStream(new DomDriver("UTF-8")); this.xStream = new XStream(new DomDriver("UTF-8")) {
@Override
protected MapperWrapper wrapMapper(MapperWrapper next) {
return new MapperWrapper(next) {
@SuppressWarnings("rawtypes")
@Override
public boolean shouldSerializeMember(Class definedIn, String fieldName) {
if (definedIn == Object.class) {
missingFields.add(definedIn.getName() + "." + fieldName);
return false;
}
return super.shouldSerializeMember(definedIn, fieldName);
}
};
}
};
xStream.autodetectAnnotations(true); xStream.autodetectAnnotations(true);
xStream.registerConverter(new PointConverter()); xStream.registerConverter(new PointConverter());
this.xStreamOld = new XStream(new DomDriver()); this.xStreamOld = new XStream(new DomDriver());
@@ -131,7 +151,7 @@ public class ProjectFileManager {
Document doc = db.parse(new InputSource(new InputStreamReader(new FileInputStream(file)))); Document doc = db.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));
doc.getDocumentElement().normalize(); doc.getDocumentElement().normalize();
if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(Project.class.getName())) { if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(Project.class.getName())) {
project = parseV3File(fileName); project = parseV3File(fileName, warnings);
} else { } else {
if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase("layout")) { if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase("layout")) {
throw new IllegalArgumentException("Could not open DIY file. Root node is not named 'Layout'."); throw new IllegalArgumentException("Could not open DIY file. Root node is not named 'Layout'.");
@@ -171,9 +191,10 @@ public class ProjectFileManager {
messageDispatcher.dispatchMessage(EventType.FILE_STATUS_CHANGED, getCurrentFileName(), isModified()); messageDispatcher.dispatchMessage(EventType.FILE_STATUS_CHANGED, getCurrentFileName(), isModified());
} }
private Project parseV3File(String fileName) throws IOException { private Project parseV3File(String fileName, List<String> warnings) throws IOException {
Project project; Project project;
FileInputStream fis = new FileInputStream(fileName); FileInputStream fis = new FileInputStream(fileName);
missingFields.clear();
try { try {
Reader reader = new InputStreamReader(fis, "UTF-8"); Reader reader = new InputStreamReader(fis, "UTF-8");
project = (Project) xStream.fromXML(reader); project = (Project) xStream.fromXML(reader);
@@ -182,6 +203,9 @@ public class ProjectFileManager {
fis.close(); fis.close();
fis = new FileInputStream(fileName); fis = new FileInputStream(fileName);
project = (Project) xStreamOld.fromXML(fis); project = (Project) xStreamOld.fromXML(fis);
}
if (!missingFields.isEmpty()) {
warnings.add("The project references unknown component properties, most likely because it was created with a newer version of DIYLC.");
} }
fis.close(); fis.close();
return project; return project;
+17 -1
View File
@@ -2460,6 +2460,22 @@
</org.diylc.appframework.update.Change> </org.diylc.appframework.update.Change>
</changes> </changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url> <url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version> </org.diylc.appframework.update.Version>
<org.diylc.appframework.update.Version>
<versionNumber>
<major>3</major>
<minor>54</minor>
<build>0</build>
</versionNumber>
<releaseDate>2019-02-03 00:00:00.000 CET</releaseDate>
<name></name>
<changes>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Allow a file to be loaded if it references a missing component property, but show a warning.</description>
</org.diylc.appframework.update.Change>
</changes>
<url>https://github.com/bancika/diy-layout-creator/releases</url>
</org.diylc.appframework.update.Version>
</a> </a>
</java.util.Arrays_-ArrayList> </java.util.Arrays_-ArrayList>