public static Document getField(String fieldName) {
    final VFSPathToken token = new VFSPathToken(fieldName + ".xml");
    if (SIS.get().getVFS().exists(FIELDS_DIR.child(token))) {
      try {
        return getInputStreamFile(SIS.get().getVFS().getInputStream(FIELDS_DIR.child(token)));
      } catch (IOException e) {
        Debug.println("Field {0} reported existence, but could not be loaded:\n{1}", fieldName, e);
      }
    }

    return getInputStreamFile(DocumentLoader.class.getResourceAsStream(fieldName + ".xml"));
  }
  public static Document getView() {
    final VFSPathToken token = new VFSPathToken("views.xml");
    if (SIS.get().getVFS().exists(FIELDS_DIR.child(token))) {
      try {
        return SIS.get().getVFS().getMutableDocument(FIELDS_DIR.child(token));
      } catch (IOException e) {
        Debug.println("View reported existence, but could not be loaded:\n{0}", e);
      }
    }

    return BaseDocumentUtils.impl.getInputStreamFile(
        DocumentLoader.class.getResourceAsStream("views.xml"));
  }
  public static synchronized String getNextDraftID(VFS vfs, String speciesID) {
    int nextID = 0;
    VFSPath rootURI;

    try {
      rootURI = VFSUtils.parseVFSPath(ServerPaths.getDraftAssessmentRootURL(speciesID));
    } catch (VFSPathParseException e) {
      e.printStackTrace();
      return null;
    }

    if (vfs.exists(rootURI)) { // If there's already regionals for this guy,
      // get the next
      VFSPathToken[] tokens;
      try {
        tokens = vfs.list(rootURI);
        for (VFSPathToken curToken : tokens) {
          String filename = curToken.toString();
          filename = filename.replaceAll(".xml", "");
          if (!SISContainerApp.amIOnline()) filename = filename.replaceAll("offline", "");

          System.out.println("Crawling file " + curToken + " for new regional ID.");

          try {
            int value = Integer.valueOf(filename.split("_")[1]);
            nextID = Math.max(value, nextID);
          } catch (NumberFormatException e) {
            SysDebugger.getNamedInstance(SISContainerApp.SEVERE_LOG)
                .println(
                    "Annoying file in path "
                        + curToken
                        + " non-conformant "
                        + "to standard region assessment file name pattern.");
          }
        }
      } catch (NotFoundException e) {
        SysDebugger.getNamedInstance(SISContainerApp.SEVERE_LOG)
            .println("Big WTF. " + "List failed on existing path " + rootURI.toString());
        return null;
      }

      nextID++; // Increment it one past the highest found.
    }

    String assessmentID = speciesID + "_" + nextID;
    return assessmentID;
  }
  public Document doImport(final ZipInputStream zis, final ImportMode mode)
      throws ResourceException {
    final Document document = BaseDocumentUtils.impl.newDocument();
    final Element root = document.createElement("results");
    root.setAttribute("mode", mode.toString());

    ZipEntry ze;
    try {
      ze = zis.getNextEntry();
    } catch (IOException e) {
      throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
    }
    byte[] buf = new byte[1024];
    while (ze != null) {
      final VFSPath path;
      try {
        path = VFSUtils.parseVFSPath(ze.getName());
      } catch (VFSUtils.VFSPathParseException e) {
        e.printStackTrace();
        try {
          ze = zis.getNextEntry();
          continue;
        } catch (IOException f) {
          throw new ResourceException(Status.SERVER_ERROR_INTERNAL, f);
        }
      }

      if (isRestricted(path)) {
        if (restrictionListener != null && !ze.isDirectory()) {
          try {
            final VFSPath restrictedUri = VFSUtils.parseVFSPath(ze.getName());
            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] in_bytes = buf;
            int n;
            while ((n = zis.read(in_bytes, 0, 1024)) > -1) baos.write(in_bytes, 0, n);
            ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());

            restrictionListener.handle(restrictedUri, bais);
          } catch (Exception e) {
            e.printStackTrace();
            TrivialExceptionHandler.ignore(this, e);
          }
        }
        try {
          ze = zis.getNextEntry();
          continue;
        } catch (IOException f) {
          throw new ResourceException(Status.SERVER_ERROR_INTERNAL, f);
        }
      }

      final Element el;

      if (ze.isDirectory()) {
        el = document.createElement("folder");
        el.setAttribute("path", path.toString());

        if (!vfs.exists(path)) {
          try {
            vfs.makeCollections(path);
          } catch (IOException e) {
            // Let it fail below for proper iteration behavior
            TrivialExceptionHandler.ignore(this, e);
          }
        } else {
          try {
            el.setAttribute("status", "skipped");
            root.appendChild(el);
            ze = zis.getNextEntry();
            continue;
          } catch (IOException e) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
          }
        }
      } else {
        el = document.createElement("file");
        el.setAttribute("path", path.toString());

        if (ImportMode.FRESHEN.equals(mode) && vfs.exists(path)) {
          try {
            long localTime = vfs.getLastModified(path);
            long remoteTime = ze.getTime();

            if (localTime >= remoteTime) {
              el.setAttribute("status", "skipped");
              root.appendChild(el);
              ze = zis.getNextEntry();
              continue;
            }
          } catch (Exception e) {
            TrivialExceptionHandler.ignore(this, e);
          }
        }

        // Create parent dir if it does not already exist
        if (!vfs.exists(path.getCollection())) {
          try {
            vfs.makeCollections(path.getCollection());
          } catch (IOException e) {
            // Let it fail below for proper iteration behavior
            TrivialExceptionHandler.ignore(this, e);
          }
        }

        final OutputStream os;
        try {
          os = vfs.getOutputStream(path);
        } catch (IOException e) {
          try {
            ze = zis.getNextEntry();
            el.setAttribute("status", "error");
            root.appendChild(el);
            continue;
          } catch (IOException f) {
            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, f);
          }
        }

        int n;
        try {
          while ((n = zis.read(buf, 0, 1024)) > -1) os.write(buf, 0, n);
        } catch (IOException e) {
          el.setAttribute("status", "error");
          root.appendChild(el);
          continue;
        } finally {
          try {
            os.close();
          } catch (IOException e) {
            TrivialExceptionHandler.ignore(this, e);
          }
        }
      }

      el.setAttribute("status", "success");
      root.appendChild(el);

      try {
        ze = zis.getNextEntry();
      } catch (IOException e) {
        throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
      }
    }

    try {
      zis.close();
    } catch (IOException e) {
      TrivialExceptionHandler.ignore(this, e);
    }

    document.appendChild(root);

    return document;
  }
 public void restrict(VFSPath restrictedPath) {
   restrictedPaths.add(restrictedPath.toString());
 }
 private boolean isRestricted(final VFSPath path) {
   for (String folder : restrictedPaths) if (path.toString().startsWith(folder)) return true;
   return false;
 }