Exemplo n.º 1
0
  /** Rebuild the Roster index and store it. */
  public void reindex() {
    Roster roster = new Roster();
    for (String fileName : Roster.getAllFileNames()) {
      // Read file
      try {
        Element loco =
            (new LocoFile())
                .rootFromName(LocoFile.getFileLocation() + fileName)
                .getChild("locomotive");
        if (loco != null) {
          RosterEntry re = new RosterEntry(loco);
          re.setFileName(fileName);
          roster.addEntry(re);
        }
      } catch (JDOMException | IOException ex) {
        log.error("Exception while loading loco XML file: {} execption: {}", fileName, ex);
      }
    }

    this.makeBackupFile(this.getRosterIndexPath());
    try {
      roster.writeFile(this.getRosterIndexPath());
    } catch (IOException ex) {
      log.error("Exception while writing the new roster file, may not be complete: {}", ex);
    }
    this.reloadRosterFile();
    log.info("Roster rebuilt, stored in {}", this.getRosterIndexPath());
  }
Exemplo n.º 2
0
  /**
   * Read the contents of a roster XML file into this object.
   *
   * <p>Note that this does not clear any existing entries.
   *
   * @param name filename of roster file
   */
  void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException {
    // roster exists?
    if (!(new File(name)).exists()) {
      log.debug(
          "no roster file found; this is normal if you haven't put decoders in your roster yet");
      return;
    }

    // find root
    Element root = rootFromName(name);
    if (root == null) {
      log.error("Roster file exists, but could not be read; roster not available");
      return;
    }
    // if (log.isDebugEnabled()) XmlFile.dumpElement(root);

    // decode type, invoke proper processing routine if a decoder file
    if (root.getChild("roster") != null) { // NOI18N
      List<Element> l = root.getChild("roster").getChildren("locomotive"); // NOI18N
      if (log.isDebugEnabled()) {
        log.debug("readFile sees " + l.size() + " children");
      }
      l.stream()
          .forEach(
              (e) -> {
                addEntry(new RosterEntry(e));
              });

      // Scan the object to check the Comment and Decoder Comment fields for
      // any <?p?> processor directives and change them to back \n characters
      for (int i = 0; i < numEntries(); i++) {
        // Get a RosterEntry object for this index
        RosterEntry r = _list.get(i);

        // Extract the Comment field and create a new string for output
        String tempComment = r.getComment();
        String xmlComment = "";

        // transfer tempComment to xmlComment one character at a time, except
        // when <?p?> is found.  In that case, insert a \n and skip over those
        // characters in tempComment.
        for (int k = 0; k < tempComment.length(); k++) {
          if (tempComment.startsWith("<?p?>", k)) { // NOI18N
            xmlComment = xmlComment + "\n"; // NOI18N
            k = k + 4;
          } else {
            xmlComment = xmlComment + tempComment.substring(k, k + 1);
          }
        }
        r.setComment(xmlComment);

        // Now do the same thing for the decoderComment field
        String tempDecoderComment = r.getDecoderComment();
        String xmlDecoderComment = "";

        for (int k = 0; k < tempDecoderComment.length(); k++) {
          if (tempDecoderComment.startsWith("<?p?>", k)) { // NOI18N
            xmlDecoderComment = xmlDecoderComment + "\n"; // NOI18N
            k = k + 4;
          } else {
            xmlDecoderComment = xmlDecoderComment + tempDecoderComment.substring(k, k + 1);
          }
        }

        r.setDecoderComment(xmlDecoderComment);
      }
    } else {
      log.error("Unrecognized roster file contents in file: " + name);
    }
    if (root.getChild("rosterGroup") != null) { // NOI18N
      List<Element> groups = root.getChild("rosterGroup").getChildren("group"); // NOI18N
      groups
          .stream()
          .forEach(
              (group) -> {
                addRosterGroup(group.getText());
              });
    }
  }