public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();
    java.util.prefs.Preferences prefs = node();

    OutputStream out;
    if (file == null) {
      out = io.streams.out;
    } else {
      io.println("Exporting preferences to: {}", file); // TODO: i18n
      out = new BufferedOutputStream(new FileOutputStream(file));
    }

    try {
      if (subTree) {
        prefs.exportSubtree(out);
      } else {
        prefs.exportNode(out);
      }

      Flusher.flush(out);
    } finally {
      if (file != null) {
        Closer.close(out);
      }
    }

    prefs.sync();

    return Result.SUCCESS;
  }
Esempio n. 2
0
  public Object execute(final CommandContext context) throws Exception {
    assert context != null;
    IO io = context.getIo();

    FileObject file = resolveFile(context, path);

    io.println("URL: {}", file.getURL());
    io.println("Name: {}", file.getName());
    io.println("BaseName: {}", file.getName().getBaseName());
    io.println("Extension: {}", file.getName().getExtension());
    io.println("Path: {}", file.getName().getPath());
    io.println("Scheme: {}", file.getName().getScheme());
    io.println("URI: {}", file.getName().getURI());
    io.println("Root URI: {}", file.getName().getRootURI());
    io.println("Parent: {}", file.getName().getParent());
    io.println("Type: {}", file.getType());
    io.println("Exists: {}", file.exists());
    io.println("Readable: {}", file.isReadable());
    io.println("Writeable: {}", file.isWriteable());
    io.println("Root path: {}", file.getFileSystem().getRoot().getName().getPath());

    if (file.exists()) {
      FileContent content = file.getContent();
      FileContentInfo contentInfo = content.getContentInfo();
      io.println("Content type: {}", contentInfo.getContentType());
      io.println("Content encoding: {}", contentInfo.getContentEncoding());

      try {
        // noinspection unchecked
        Map<String, Object> attrs = content.getAttributes();
        if (attrs != null && !attrs.isEmpty()) {
          io.println("Attributes:");
          for (Map.Entry<String, Object> entry : attrs.entrySet()) {
            io.println("    {}='{}'", entry.getKey(), entry.getValue());
          }
        }
      } catch (FileSystemException e) {
        io.println("File attributes are NOT supported");
      }

      try {
        Certificate[] certs = content.getCertificates();
        if (certs != null && certs.length != 0) {
          io.println("Certificate:");
          for (Certificate cert : certs) {
            io.println("    {}", cert);
          }
        }
      } catch (FileSystemException e) {
        io.println("File certificates are NOT supported");
      }

      if (file.getType().equals(FileType.FILE)) {
        io.println("Size: {} bytes", content.getSize());
      } else if (file.getType().hasChildren() && file.isReadable()) {
        FileObject[] children = file.getChildren();
        io.println("Directory with {} files", children.length);

        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
          io.println("#{}:{}", iterChildren, children[iterChildren].getName());
          if (iterChildren > 5) {
            break;
          }
        }
      }

      io.println(
          "Last modified: {}",
          DateFormat.getInstance().format(new Date(content.getLastModifiedTime())));
    } else {
      io.println("The file does not exist");
    }

    FileObjects.close(file);

    return Result.SUCCESS;
  }