Ejemplo n.º 1
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;
 }
Ejemplo n.º 2
0
  /**
   * Moves the source file to the destination.
   *
   * <p>If the destination cannot be created or is a read-only file, the method returns <code>false
   * </code>. Otherwise, the contents of the source are copied to the destination, the source is
   * deleted, and <code>true</code> is returned.
   *
   * @param source The source file to move.
   * @param dest The destination where to move the file.
   * @return true on success, false otherwise.
   * @since jEdit 4.3pre9
   */
  public static boolean moveFile(File source, File dest) {
    boolean ok = false;

    if ((dest.exists() && dest.canWrite()) || (!dest.exists() && dest.getParentFile().canWrite())) {
      OutputStream fos = null;
      InputStream fis = null;
      try {
        fos = new FileOutputStream(dest);
        fis = new FileInputStream(source);
        ok = copyStream(32768, null, fis, fos, false);
      } catch (IOException ioe) {
        Log.log(
            Log.WARNING, IOUtilities.class, "Error moving file: " + ioe + " : " + ioe.getMessage());
      } finally {
        closeQuietly(fos);
        closeQuietly(fis);
      }

      if (ok) source.delete();
    }
    return ok;
  } // }}}
Ejemplo n.º 3
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);
    }
  }
Ejemplo n.º 4
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);
    }
  } // }}}
Ejemplo n.º 5
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;
  }
Ejemplo n.º 6
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);
    }
  } // }}}
Ejemplo n.º 7
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);
      }
    }
  } // }}}