Exemplo n.º 1
0
  // {{{createCenterPanelAll() method
  public JPanel createCenterPanelAll() {
    long filesSize = 0L;
    JPanel centerPanel = new JPanel(new BorderLayout());

    for (VFSFile selectedFile : selectedFiles) {
      if (selectedFile.getType() == VFSFile.DIRECTORY) {
        File ioFile = new File(selectedFile.getPath());
        filesSize += IOUtilities.fileLength(ioFile);
      } else if (selectedFile.getType() == VFSFile.FILE) filesSize += selectedFile.getLength();
    }

    JPanel propField = new JPanel();
    propField.setLayout(new GridLayout(2, 1));
    String path = local.getPath();
    if (OperatingSystem.isWindows()
        || OperatingSystem.isWindows9x()
        || OperatingSystem.isWindowsNT()) {
      path = path.substring(0, path.lastIndexOf(92)); // 92 = '\'
    } else {
      path = path.substring(0, path.lastIndexOf('/'));
    }
    propField.add(new JLabel(jEdit.getProperty("fileprop.path") + ": " + path));
    propField.add(
        new JLabel(
            jEdit.getProperty("fileprop.size")
                + ": "
                + StandardUtilities.formatFileSize(filesSize)));
    Border etch = BorderFactory.createEtchedBorder();
    propField.setBorder(
        BorderFactory.createTitledBorder(etch, jEdit.getProperty("fileprop.properties")));
    centerPanel.add(BorderLayout.CENTER, propField);

    return centerPanel;
  } // }}}
Exemplo n.º 2
0
 public static Properties loadProperties(String fileName) {
   diag_println(DIAG_OFF, ".props : ", fileName);
   Properties props = new Properties();
   if (notNullOrEmpty(fileName)) {
     InputStream in = null;
     try {
       JarFile jarFile = null;
       if (isJarUri(fileName)) {
         URL url = new URL(fileName);
         url = new URL(url.getFile());
         String[] parts = url.getFile().split("!/");
         jarFile = new JarFile(new File(parts[0]));
         JarEntry jarEntry = jarFile.getJarEntry(parts[1]);
         in = jarFile.getInputStream(jarEntry);
       } else {
         File file = new File(fileName);
         if (file.isFile()) {
           in = new FileInputStream(file);
         } else {
           in = PluginUtils.class.getResourceAsStream(fileName);
         }
       }
       props.load(in);
       if (jarFile != null) jarFile.close();
     } catch (Exception e) {
       Log.log(Log.ERROR, PluginUtils.class + ".loadProperties", e);
     } finally {
       IOUtilities.closeQuietly((Closeable) in);
     }
   }
   return props;
 }
Exemplo n.º 3
0
  // {{{ createCenterPanel() method
  public JPanel createCenterPanel() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");

    JPanel centerPanel = new JPanel(new BorderLayout());

    JPanel propField = new JPanel();
    propField.setLayout(new GridLayout(4, 1));
    propField.add(new JLabel(jEdit.getProperty("fileprop.name") + ": " + local.getName()));
    propField.add(new JLabel(jEdit.getProperty("fileprop.path") + ": " + local.getPath()));

    // Show last modified property only for LocalFile
    if (local instanceof LocalFile) {
      propField.add(
          new JLabel(
              jEdit.getProperty("fileprop.lastmod")
                  + ": "
                  + sdf.format(new Date(((LocalFile) local).getModified()))));
    }
    if (local.getType() == VFSFile.DIRECTORY) {
      File ioFile = new File(local.getPath());
      propField.add(
          new JLabel(
              jEdit.getProperty("fileprop.size")
                  + ": "
                  + StandardUtilities.formatFileSize(IOUtilities.fileLength(ioFile))));
    } else {
      propField.add(
          new JLabel(
              jEdit.getProperty("fileprop.size")
                  + ": "
                  + StandardUtilities.formatFileSize(local.getLength())));
    }
    Border etch = BorderFactory.createEtchedBorder();
    propField.setBorder(
        BorderFactory.createTitledBorder(etch, jEdit.getProperty("fileprop.properties")));
    centerPanel.add(BorderLayout.CENTER, propField);

    JPanel attributeField = new JPanel();
    attributeField.setLayout(new GridLayout(1, 2));
    readable = new JCheckBox(jEdit.getProperty("fileprop.readable"));
    readable.setSelected(local.isReadable());
    readable.setEnabled(false);
    attributeField.add(readable);

    write = new JCheckBox(jEdit.getProperty("fileprop.writeable"));
    write.setSelected(local.isWriteable());
    write.setEnabled(false);
    attributeField.add(write);
    attributeField.setBorder(
        BorderFactory.createTitledBorder(etch, jEdit.getProperty("fileprop.attribute")));
    centerPanel.add(BorderLayout.SOUTH, attributeField);

    return centerPanel;
  } // }}}
Exemplo n.º 4
0
  /**
   * Copy a file to another using VFS.
   *
   * @param progress the progress observer. It could be null if you don't want to monitor progress.
   *     If not null you should probably launch this command in a WorkThread
   * @param sourceVFS the source VFS
   * @param sourceSession the VFS session
   * @param sourcePath the source path
   * @param targetVFS the target VFS
   * @param targetSession the target session
   * @param targetPath the target path
   * @param comp comp The component that will parent error dialog boxes
   * @param canStop could this copy be stopped ?
   * @return true if the copy was successful
   * @throws IOException IOException If an I/O error occurs
   * @since jEdit 4.3pre3
   */
  public static boolean copy(
      ProgressObserver progress,
      VFS sourceVFS,
      Object sourceSession,
      String sourcePath,
      VFS targetVFS,
      Object targetSession,
      String targetPath,
      Component comp,
      boolean canStop)
      throws IOException {
    if (progress != null) progress.setStatus("Initializing");

    InputStream in = null;
    OutputStream out = null;
    try {
      VFSFile sourceVFSFile = sourceVFS._getFile(sourceSession, sourcePath, comp);
      if (sourceVFSFile == null) throw new FileNotFoundException(sourcePath);
      if (progress != null) {
        progress.setMaximum(sourceVFSFile.getLength());
      }
      VFSFile targetVFSFile = targetVFS._getFile(targetSession, targetPath, comp);
      if (targetVFSFile.getType() == VFSFile.DIRECTORY) {
        if (targetVFSFile.getPath().equals(sourceVFSFile.getPath())) return false;
        targetPath = MiscUtilities.constructPath(targetPath, sourceVFSFile.getName());
      }
      in =
          new BufferedInputStream(
              sourceVFS._createInputStream(sourceSession, sourcePath, false, comp));
      out =
          new BufferedOutputStream(targetVFS._createOutputStream(targetSession, targetPath, comp));
      boolean copyResult = IOUtilities.copyStream(IOBUFSIZE, progress, in, out, canStop);
      VFSManager.sendVFSUpdate(targetVFS, targetPath, true);
      return copyResult;
    } finally {
      IOUtilities.closeQuietly(in);
      IOUtilities.closeQuietly(out);
    }
  }
Exemplo n.º 5
0
  // {{{ loadMode() method
  public void loadMode(Mode mode, XModeHandler xmh) {
    String fileName = (String) mode.getProperty("file");

    Log.log(Log.NOTICE, this, "Loading edit mode " + fileName);

    XMLReader parser;
    try {
      parser = XMLReaderFactory.createXMLReader();
    } catch (SAXException saxe) {
      Log.log(Log.ERROR, this, saxe);
      return;
    }
    mode.setTokenMarker(xmh.getTokenMarker());

    InputStream resource = ClassLoader.getSystemClassLoader().getResourceAsStream(fileName);
    if (resource == null) {
      // don't just print an error to console. This is a problem.
      throw new RuntimeException("File not found " + fileName);
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(resource));

    try {
      InputSource isrc = new InputSource(br);
      // isrc.setSystemId("jedit.jar");
      parser.setContentHandler(xmh);
      parser.setDTDHandler(xmh);
      parser.setEntityResolver(xmh);
      parser.setErrorHandler(xmh);
      parser.parse(isrc);

      mode.setProperties(xmh.getModeProperties());
    } catch (Throwable e) {
      error(fileName, e);
    } finally {
      IOUtilities.closeQuietly(resource);
    }
  } // }}}
Exemplo n.º 6
0
  /**
   * Recursively encrypts or decrypts all files given to the method and all files in directories
   * given to the method.
   *
   * @param relativePath The relative from the initial directory up to the current one, for
   *     encryption or decryption into another directory to be able to rebuild the directory
   *     structure
   * @param files The files and directories to encrypt or decrypt
   * @return Whether all encryption or decryption was fine
   */
  @CheckReturnValue(explanation = "If false is returned, something went wrong")
  private boolean cryptFiles(@NonNull String relativePath, @NonNull VFSFile... files) {
    for (VFSFile file : files) {
      VFS vfs = file.getVFS();
      VFS newVfs = null;
      String path = file.getPath();
      Object session = vfs.createVFSSession(path, this);
      Object sessionNew = null;
      Object sessionNewParent = null;
      InputStream in = null;
      ByteArrayInputStream bais = null;
      OutputStream out = null;
      ByteArrayOutputStream baos = null;
      try {
        if (FILE == file.getType()) {
          in = vfs._createInputStream(session, path, false, this);
          baos = new ByteArrayOutputStream();
          if (!IOUtilities.copyStream(null, in, baos, false)) {
            GUIUtilities.error(
                this,
                encrypt
                    ? "cipher.error.error-while-encrypting-file"
                    : "cipher.error.error-while-decrypting-file",
                new Object[] {path});
            continue;
          }
          baos.flush();
          byte[] cryptResult;
          synchronized (cipher) {
            if (encrypt) {
              cipher.setRawData(baos.toByteArray());
            } else {
              cipher.setEncryptedData(baos.toByteArray());
            }
            cipher.setEntropy(password);
            cipher.setAdditionalInformation(additionalInformation);
            if (encrypt) {
              cryptResult = cipher.encryptToByteArray();
            } else {
              cryptResult = cipher.decryptToByteArray();
            }
          }
          if (null == cryptResult) {
            GUIUtilities.error(
                this,
                encrypt
                    ? "cipher.error.error-while-encrypting-file"
                    : "cipher.error.error-while-decrypting-file",
                new Object[] {path});
            continue;
          }
          bais = new ByteArrayInputStream(cryptResult);
          String newPath;
          switch (newFileHandling) {
            case OVERWRITE:
              newPath = path;
              newVfs = vfs;
              break;

            case OTHER_DIRECTORY:
              if (0 < relativePath.length()) {
                newPath = MiscUtilities.constructPath(directoryTextField.getText(), relativePath);
              } else {
                newPath = directoryTextField.getText();
              }
              newPath = MiscUtilities.constructPath(newPath, file.getName());
              newVfs = VFSManager.getVFSForPath(newPath);
              break;

            case SUFFIX:
              newPath = path + suffixTextField.getText();
              newVfs = vfs;
              break;

            default:
              throw new InternalError(
                  "missing case branch for NewFileHandling: " + newFileHandling);
          }
          String newPathParent = MiscUtilities.getParentOfPath(newPath);
          sessionNewParent = newVfs.createVFSSession(newPathParent, this);
          newVfs._mkdir(sessionNewParent, newPathParent, this);
          sessionNew = newVfs.createVFSSession(newPath, this);
          out = newVfs._createOutputStream(sessionNew, newPath, this);
          if (!IOUtilities.copyStream(null, bais, out, false)) {
            GUIUtilities.error(
                this,
                encrypt
                    ? "cipher.error.error-while-encrypting-file"
                    : "cipher.error.error-while-decrypting-file",
                new Object[] {path});
            continue;
          }
          VFSManager.sendVFSUpdate(newVfs, newPath, true);
        } else {
          String newRelativePath;
          if (0 < relativePath.length()) {
            newRelativePath = MiscUtilities.concatPath(relativePath, file.getName());
          } else {
            newRelativePath = file.getName();
          }
          if (!cryptFiles(newRelativePath, vfs._listFiles(session, path, this))) {
            return false;
          }
        }
      } catch (IOException ioe) {
        Log.log(ERROR, this, ioe);
        new TextAreaDialog(
            this,
            encrypt
                ? "cipher.error.error-while-encrypting-files"
                : "cipher.error.error-while-decrypting-files",
            ioe);
        return false;
      } finally {
        try {
          vfs._endVFSSession(session, this);
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        try {
          if (null != newVfs) {
            newVfs._endVFSSession(sessionNew, this);
          }
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        try {
          if (null != newVfs) {
            newVfs._endVFSSession(sessionNewParent, this);
          }
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        try {
          if (null != out) {
            out.flush();
          }
        } catch (IOException ioe) {
          // just ignore it, we are not interested
        }
        IOUtilities.closeQuietly(in);
        IOUtilities.closeQuietly(bais);
        IOUtilities.closeQuietly(out);
        IOUtilities.closeQuietly(baos);
      }
    }
    return true;
  }
Exemplo n.º 7
0
  // {{{ save() method
  public static void save() {
    if (recentXML == null) return;

    if (recentXML.hasChangedOnDisk()) {
      Log.log(
          Log.WARNING,
          BufferHistory.class,
          recentXML + " changed on disk; will not save recent" + " files");
      return;
    }

    Log.log(Log.MESSAGE, BufferHistory.class, "Saving " + recentXML);

    String lineSep = System.getProperty("line.separator");

    SettingsXML.Saver out = null;

    try {
      out = recentXML.openSaver();
      out.writeXMLDeclaration();

      out.write("<!DOCTYPE RECENT SYSTEM \"recent.dtd\">");
      out.write(lineSep);
      out.write("<RECENT>");
      out.write(lineSep);

      // Make a snapshot to avoid long locking period
      // which may be required by file I/O.
      List<Entry> snapshot = getHistory();

      for (Entry entry : snapshot) {
        out.write("<ENTRY>");
        out.write(lineSep);

        out.write("<PATH>");
        out.write(XMLUtilities.charsToEntities(entry.path, false));
        out.write("</PATH>");
        out.write(lineSep);

        out.write("<CARET>");
        out.write(String.valueOf(entry.caret));
        out.write("</CARET>");
        out.write(lineSep);

        if (entry.selection != null && entry.selection.length() > 0) {
          out.write("<SELECTION>");
          out.write(entry.selection);
          out.write("</SELECTION>");
          out.write(lineSep);
        }

        if (entry.encoding != null) {
          out.write("<ENCODING>");
          out.write(entry.encoding);
          out.write("</ENCODING>");
          out.write(lineSep);
        }

        if (entry.mode != null) {
          out.write("<MODE>");
          out.write(entry.mode);
          out.write("</MODE>");
          out.write(lineSep);
        }

        out.write("</ENTRY>");
        out.write(lineSep);
      }

      out.write("</RECENT>");
      out.write(lineSep);

      out.finish();
    } catch (Exception e) {
      Log.log(Log.ERROR, BufferHistory.class, e);
    } finally {
      IOUtilities.closeQuietly(out);
    }
  } // }}}
Exemplo n.º 8
0
  // {{{ run() method
  public void run() {
    /* if the VFS supports renaming files, we first
     * save to #<filename>#save#, then rename that
     * to <filename>, so that if the save fails,
     * data will not be lost.
     *
     * as of 4.1pre7 we now call vfs.getTwoStageSaveName()
     * instead of constructing the path directly
     * since some VFS's might not allow # in filenames.
     */

    boolean vfsRenameCap = (vfs.getCapabilities() & VFS.RENAME_CAP) != 0;

    boolean wantTwoStage = wantTwoStageSave(buffer);
    boolean twoStageSave = vfsRenameCap && wantTwoStage;

    try {
      String[] args = {vfs.getFileName(path)};
      setStatus(jEdit.getProperty("vfs.status.save", args));

      // the entire save operation can be aborted...
      setAbortable(true);

      path = vfs._canonPath(session, path, view);
      if (!MiscUtilities.isURL(path)) path = MiscUtilities.resolveSymlinks(path);

      String savePath;
      if (twoStageSave) {
        savePath = vfs.getTwoStageSaveName(path);
        if (savePath == null) {
          throw new IOException("Can't get a temporary path for two-stage save: " + path);
        }
      } else {
        makeBackup();
        savePath = path;
      }

      OutputStream out = vfs._createOutputStream(session, savePath, view);
      if (out == null) {
        buffer.setBooleanProperty(ERROR_OCCURRED, true);
        return;
      }
      try {
        // this must be after the stream is created or
        // we deadlock with SSHTools.
        buffer.readLock();
        try {
          // Can't use buffer.getName() here because
          // it is not changed until the save is
          // complete
          if (path.endsWith(".gz")) buffer.setBooleanProperty(Buffer.GZIPPED, true);
          else if (buffer.getName().endsWith(".gz")) {
            // The path do not ends with gz.
            // The buffer name was .gz.
            // So it means it's blabla.txt.gz -> blabla.txt, I remove
            // the gz property
            buffer.setBooleanProperty(Buffer.GZIPPED, false);
          }

          if (buffer.getBooleanProperty(Buffer.GZIPPED)) out = new GZIPOutputStream(out);

          write(buffer, out);
        } finally {
          buffer.readUnlock();
        }
      } finally {
        IOUtilities.closeQuietly(out);
      }

      if (twoStageSave) {
        makeBackup();
        if (!vfs._rename(session, savePath, path, view))
          throw new IOException("Rename failed: " + savePath);
      }

      if (!twoStageSave) VFSManager.sendVFSUpdate(vfs, path, true);
    } catch (FileNotFoundException e) {
      Log.log(Log.ERROR, this, "Unable to save buffer " + e);
      String[] pp = {e.getMessage()};
      VFSManager.error(view, path, "ioerror.write-error", pp);

      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } catch (UnsupportedCharsetException e) {
      Log.log(Log.ERROR, this, e, e);
      String[] pp = {e.getCharsetName()};
      VFSManager.error(view, path, "ioerror.unsupported-encoding-error", pp);

      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } catch (Exception e) {
      Log.log(Log.ERROR, this, e);
      String[] pp = {e.toString()};
      VFSManager.error(view, path, "ioerror.write-error", pp);

      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } catch (WorkThread.Abort a) {
      buffer.setBooleanProperty(ERROR_OCCURRED, true);
    } finally {
      try {
        vfs._saveComplete(session, buffer, path, view);
        if (twoStageSave) {
          vfs._finishTwoStageSave(session, buffer, path, view);
        }
        // clean up left-over markers file
        if (!jEdit.getBooleanProperty("persistentMarkers"))
          vfs._delete(session, Buffer.getMarkersPath(vfs, path), view);
        vfs._endVFSSession(session, view);
      } catch (Exception e) {
        Log.log(Log.ERROR, this, e);
        String[] pp = {e.toString()};
        VFSManager.error(view, path, "ioerror.write-error", pp);

        buffer.setBooleanProperty(ERROR_OCCURRED, true);
      } catch (WorkThread.Abort a) {
        buffer.setBooleanProperty(ERROR_OCCURRED, true);
      }
    }
  } // }}}