예제 #1
0
파일: XmlDAO.java 프로젝트: 7Silvan/ncl
  @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;
  }