protected static void writeHistoryFile() { if ((History.HISTORY_FILE.isFile() && History.HISTORY_FILE.canWrite()) || (!History.HISTORY_FILE.exists())) { try { BufferedWriter out = new BufferedWriter(new FileWriter(History.HISTORY_FILE, false)); out.write("<?xml version=\"1.0\"?>\n"); out.write("<pcsim2 version=\"" + History.PLATFORM_VERSION + "\">\n"); out.write("\t<pcf dir=\"" + history.lastPCFDirectory.getAbsolutePath() + "\"/>\n"); out.write("\t<dcf dir=\"" + history.lastDCFDirectory.getAbsolutePath() + "\"/>\n"); out.write("\t<scripts dir=\"" + history.lastScriptsDirectory.getAbsolutePath() + "\"/>\n"); out.write("\t<batch dir=\"" + history.lastBatchDirectory.getAbsolutePath() + "\"/>\n"); for (int i = 0; i < History.MAX_NUM_HISTORY_FILES; i++) { if (history.histFiles[i] != null) { out.write("\t<history file=\"" + history.histFiles[i].getAbsolutePath() + "\"/>\n"); } } out.write("</pcsim2>"); out.close(); } catch (IOException e) { if (PC2UI.logger != null) { PC2UI.logger.error( PC2LogCategory.UI, PC2UI.subCat, "PCSim2 encountered an error while trying to write the history file information.\n" + e.getMessage()); } } } }
public History parse() throws SAXException, IOException { xr = XMLReaderFactory.createXMLReader(); if (xr != null) { xr.setContentHandler(this); if (History.HISTORY_FILE.exists()) { FileReader reader = new FileReader(History.HISTORY_FILE); xr.parse(new InputSource(reader)); } else { history = History.getInstance(); writeHistoryFile(); } return history; } else throw new SAXException("XMLReader did not get created successfully."); }
/** * This is a method that must be overwritten for the SAXParser. It is called at the start of each * element and is were the HistoryParser begins to separate the various tags into their respective * objects. Validation of each mandatory attribute and children elements begins in this method. * * <p>(non-Javadoc) * * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, * java.lang.String, org.xml.sax.Attributes) */ @Override public void startElement(String uri, String name, String qName, Attributes atts) throws SAXException { logger.trace(PC2LogCategory.UI, subCat, "starting element (" + qName + ")"); logger.trace(PC2LogCategory.UI, subCat, " number of attributes: " + atts.getLength()); curTag = qName; boolean unrecognizedTag = false; int count = atts.getLength(); if (qName.equals(PCSIM2)) { for (int i = 0; i < count; i++) { String attr = atts.getLocalName(i); if (attr.equals(VERSION)) { // Add any versioning operations that might be needed in the future here // for this initial release though, there is no operation as the version // is assigned automatically to the History class. } else { logger.trace( PC2LogCategory.UI, subCat, "The attribute[" + attr + "] is an unexpected attribute for the " + PCSIM2 + " tag. It has been ignored."); } } if (history == null) history = History.getInstance(); return; } else { switch (qName.charAt(0)) { case 'b': if (qName.equals(BATCH)) { for (int i = 0; i < count; i++) { String attr = atts.getLocalName(i); if (attr.equals(DIR)) { File f = new File(atts.getValue(i)); if (f.isDirectory() && f.canRead()) history.lastBatchDirectory = f; } else { logger.trace( PC2LogCategory.UI, subCat, "The attribute[" + attr + "] is an unexpected attribute for the " + BATCH + " tag. It has been ignored."); } } } else unrecognizedTag = true; break; case 'd': if (qName.equals(DCF)) { for (int i = 0; i < count; i++) { String attr = atts.getLocalName(i); if (attr.equals(DIR)) { File f = new File(atts.getValue(i)); if (f.isDirectory() && f.canRead()) history.lastDCFDirectory = f; } else { logger.trace( PC2LogCategory.UI, subCat, "The attribute[" + attr + "] is an unexpected attribute for the " + DCF + " tag. It has been ignored."); } } } else unrecognizedTag = true; break; case 'h': if (qName.equals(HISTORY)) { for (int i = 0; i < count; i++) { String attr = atts.getLocalName(i); if (attr.equals(FILE)) { String value = atts.getValue(i); File f = new File(value); if (f.isFile() && f.canRead()) history.addHistoryFile(f); } else { logger.trace( PC2LogCategory.UI, subCat, "The attribute[" + attr + "] is an unexpected attribute for the " + DCF + " tag. It has been ignored."); } } } else unrecognizedTag = true; break; case 'p': if (qName.equals(PCF)) { for (int i = 0; i < count; i++) { String attr = atts.getLocalName(i); if (attr.equals(DIR)) { File f = new File(atts.getValue(i)); if (f.isDirectory() && f.canRead()) history.lastPCFDirectory = f; } else { logger.trace( PC2LogCategory.UI, subCat, "The attribute[" + attr + "] is an unexpected attribute for the " + PCF + " tag. It has been ignored."); } } } else unrecognizedTag = true; break; case 's': if (qName.equals(SCRIPTS)) { for (int i = 0; i < count; i++) { String attr = atts.getLocalName(i); if (attr.equals(DIR)) { File f = new File(atts.getValue(i)); if (f.isDirectory() && f.canRead()) history.lastScriptsDirectory = f; } else { logger.trace( PC2LogCategory.UI, subCat, "The attribute[" + attr + "] is an unexpected attribute for the " + SCRIPTS + " tag. It has been ignored."); } } } else unrecognizedTag = true; break; default: throw new PC2XMLException( History.HISTORY_FILE.getName(), "Encountered unexpected tag(" + qName + ") element.", l); } } if (unrecognizedTag) throw new PC2XMLException( History.HISTORY_FILE.getName(), "Encountered unexpected tag(" + qName + ") element.", l); }