@Override public void saveAllTasks(Collection<Task> tasks) { try { doc = new Document(new Element("tasks"), new DocType("tasks", "XmlDTD.dtd")); Iterator it = tasks.iterator(); while (it.hasNext()) { Task task = (Task) it.next(); doc.getRootElement() .addContent( new Element("task") .setAttribute("id", task.getId()) .addContent(new Element("name").addContent(task.getName())) .addContent(new Element("description").addContent(task.getDescription())) .addContent(new Element("date").addContent(sdf.format(task.getDate())))); } XMLOutputter outPutter = new XMLOutputter(Format.getRawFormat().setIndent(" ").setLineSeparator("\n")); outPutter.output(doc, new FileWriter(config.getFileName())); } catch (IOException ex) { log.error(null, ex); throw new WritingFileException("Didn't write xml", ex.getCause()); } }
@Override public Collection<Task> loadTasks() { Collection<Task> loadedTasks = null; try { String path = config.getFileName(); if (path == null) { log.error("Cannot get path to taskFile. Will halt."); throw new BadConfigException("Cannot get path to taskFile. Will halt."); } if (new File(path).exists()) { Boolean valid = Tools.valXML(path); if (valid) { try { SAXBuilder builder = new SAXBuilder(); doc = builder.build(config.getFileName()); Iterator it = doc.getRootElement().getChildren("task").iterator(); loadedTasks = new TreeSet<Task>(taskComparator); while (it.hasNext()) { Element element = (Element) it.next(); String id = element.getAttributeValue("id"); String name = element.getChildText("name"); String description = element.getChildText("description"); String date = element.getChildText("date"); Task task = new Task(id, name, description, sdf.parse(date)); loadedTasks.add(task); } } catch (JDOMException ex) { log.error("Error in loading tasks from xml ", ex); throw new IOException("Error in loading tasks from xml ", ex); } catch (ParseException ex) { log.error("Error in xml parsing."); throw new IOException("Error in xml parsing.", ex); } } else { log.info("XML File is not valid. Check it."); throw new InvalidFileException("XML File is not valid. Check it."); } } else { saveTask(null); } } catch (Exception ex) { log.error("Tasks wasn't loaded, IO or XML errors occured", ex); throw new InternalControllerException("Tasks wasn't loaded, IO or XML errors occured", ex); } return loadedTasks; }