Detect when a file is created with a newer version of DIYLC and show a

warning

Force correct prefix for transistors in spice
This commit is contained in:
Branislav Stojkovic
2019-03-10 09:03:22 +01:00
parent a83cda9ecd
commit b279510e5c
9 changed files with 74 additions and 6 deletions
@@ -300,8 +300,9 @@ public class Presenter implements IPlugInPort {
@Override
public void loadProjectFromFile(String fileName) {
LOG.info(String.format("loadProjectFromFile(%s)", fileName));
List<String> warnings = null;
try {
List<String> warnings = new ArrayList<String>();
warnings = new ArrayList<String>();
Project project = (Project) projectFileManager.deserializeProjectFromFile(fileName, warnings);
loadProject(project, true);
projectFileManager.fireFileStatusChanged();
@@ -317,7 +318,15 @@ public class Presenter implements IPlugInPort {
addToRecentFiles(fileName);
} catch (Exception ex) {
LOG.error("Could not load file", ex);
view.showMessage("Could not open file " + fileName + ". Check the log for details.", "Error", IView.ERROR_MESSAGE);
String errorMessage = "Could not open file " + fileName + ". Check the log for details.";
if (warnings != null && !warnings.isEmpty()) {
errorMessage += " Possible reasons are:\n\n";
for (String warn : warnings) {
errorMessage += warn;
errorMessage += "\n";
}
}
view.showMessage(errorMessage, "Error", IView.ERROR_MESSAGE);
}
}
@@ -44,10 +44,13 @@ import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.diylc.appframework.miscutils.Utils;
import org.diylc.appframework.simplemq.MessageDispatcher;
import org.diylc.appframework.update.VersionNumber;
import org.diylc.common.EventType;
import org.diylc.core.Project;
import org.diylc.parsing.IOldFileParser;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -209,7 +212,15 @@ public class ProjectFileManager {
}
private Project parseV3File(String fileName, List<String> warnings) throws IOException {
Project project;
Project project;
VersionNumber fileVersion;
try {
fileVersion = readV3Version(fileName);
if (fileVersion.compareTo(Presenter.CURRENT_VERSION) > 0)
warnings.add("The file is created with a newer version of DIYLC and may contain features that are not supported by your version of DIYLC. Please update.");
} catch (Exception e) {
warnings.add("Could not read file version number, the file may be corrupted.");
}
FileInputStream fis = new FileInputStream(fileName);
missingFields.clear();
try {
@@ -226,5 +237,32 @@ public class ProjectFileManager {
}
fis.close();
return project;
}
private VersionNumber readV3Version(String fileName) throws Exception {
File fXmlFile = new File(fileName);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("fileVersion");
int items = nList.getLength();
if (items != 1)
throw new Exception("File version information could not be read from the XML.");
Node versionNode = nList.item(0);
Node n = versionNode.getFirstChild().getNextSibling();
int major = Integer.parseInt(n.getFirstChild().getNodeValue());
n = n.getNextSibling().getNextSibling();
int minor = Integer.parseInt(n.getFirstChild().getNodeValue());
n = n.getNextSibling().getNextSibling();
int build = Integer.parseInt(n.getFirstChild().getNodeValue());
return new VersionNumber(major, minor, build);
}
}