Esempio n. 1
0
    @Override
    protected void addNotify() {
      super.addNotify();

      Set<Node> children = new TreeSet<Node>();
      FileObject[] kids = sourceRoot.getChildren();
      boolean archive = FileUtil.isArchiveFile(sourceRoot);
      VisibilityQuery vq = VisibilityQuery.getDefault();
      for (int ii = 0; ii < kids.length; ii++) {
        if (archive || vq.isVisible(kids[ii])) {
          if (kids[ii].isFolder()) {
            findVisiblePackages(children, kids[ii], sourceRoot, !archive);
          } else {
            try {
              DataObject data = DataObject.find(kids[ii]);
              // For sorting, wrap a filter around the node.
              Node node = new SortableNode(data.getNodeDelegate());
              children.add(node);
            } catch (DataObjectNotFoundException donfe) {
              // in that case, ignore the file
            }
          }
        }
      }

      // Add the children to our own set (which should be empty).
      Node[] kidsArray = children.toArray(new Node[children.size()]);
      super.add(kidsArray);
    }
  private FileObject getArchivedFile(FileObject fileObject) {
    // ZIP and JAR archives
    if (FileUtil.isArchiveFile(fileObject)) {
      try {
        fileObject = FileUtil.getArchiveRoot(fileObject).getChildren()[0];
      } catch (Exception e) {
        throw new RuntimeException(
            "The archive can't be opened, be sure it has no password and contains a single file, without folders");
      }
    } else { // GZ or BZIP2 archives
      boolean isGz = fileObject.getExt().equalsIgnoreCase("gz");
      boolean isBzip = fileObject.getExt().equalsIgnoreCase("bz2");
      if (isGz || isBzip) {
        try {
          String[] splittedFileName = fileObject.getName().split("\\.");
          if (splittedFileName.length < 2) {
            return fileObject;
          }

          String fileExt1 = splittedFileName[splittedFileName.length - 1];
          String fileExt2 = splittedFileName[splittedFileName.length - 2];

          File tempFile = null;
          if (fileExt1.equalsIgnoreCase("tar")) {
            String fname = fileObject.getName().replaceAll("\\.tar$", "");
            fname = fname.replace(fileExt2, "");
            tempFile = File.createTempFile(fname, "." + fileExt2);
            // Untar & unzip
            if (isGz) {
              tempFile = getGzFile(fileObject, tempFile, true);
            } else {
              tempFile = getBzipFile(fileObject, tempFile, true);
            }
          } else {
            String fname = fileObject.getName();
            fname = fname.replace(fileExt1, "");
            tempFile = File.createTempFile(fname, "." + fileExt1);
            // Unzip
            if (isGz) {
              tempFile = getGzFile(fileObject, tempFile, false);
            } else {
              tempFile = getBzipFile(fileObject, tempFile, false);
            }
          }
          tempFile.deleteOnExit();
          tempFile = FileUtil.normalizeFile(tempFile);
          fileObject = FileUtil.toFileObject(tempFile);
        } catch (IOException ex) {
          Exceptions.printStackTrace(ex);
        }
      }
    }
    return fileObject;
  }
 private String createSpecURL() {
   File file =
       InstalledFileLocator.getDefault()
           .locate(SPEC_ARCHIVE_NAME, "org.netbeans.modules.css.editor", false); // NoI18N
   if (file != null) {
     try {
       URL urll = file.toURI().toURL(); // toURI should escape the illegal characters like spaces
       assert FileUtil.isArchiveFile(urll);
       return FileUtil.getArchiveRoot(urll).toExternalForm();
     } catch (java.net.MalformedURLException e) {
       // should not happen
       LOGGER.log(
           Level.SEVERE,
           String.format("Error obtaining archive root URL for file '%s'", file.getAbsolutePath()),
           e); // NOI18N
     }
   } else {
     LOGGER.warning(
         String.format(
             "Cannot locate the css documentation file '%s'.", SPEC_ARCHIVE_NAME)); // NOI18N
   }
   return null;
 }
    @Override
    public URL[] getRoots() {
      ResolvedDependencies rd = rd();
      if (rd == null) { // corrupt project metadata
        return new URL[0];
      }
      ResolvedDependency d = rd.get(dep.getID());
      if (d == null) {
        // temporary workaround for mis-created project deps
        Logger.getLogger(DependenciesClasspathImpl.class.getName())
            .log(Level.WARNING, "Unresolvable dependency {0} in {1}", new Object[] {dep, get()});
        return new URL[0];
      }
      assert d != null : "Dependency " + dep + " resolves to null";

      List<URL> urls = new ArrayList<URL>();
      File f = d.resolveFile(ArtifactKind.ORIGIN);
      if (f != null) {
        if (d.getKind().isProjectDependency()) {
          FileObject fo = FileUtil.toFileObject(f);
          if (fo != null) {
            Project p = FileOwnerQuery.getOwner(fo);
            if (p != null) {
              try {
                URL url = p.getProjectDirectory().getURL();
                AntArtifactProvider prov = p.getLookup().lookup(AntArtifactProvider.class);
                for (AntArtifact a : prov.getBuildArtifacts()) {
                  if (JavaProjectConstants.ARTIFACT_TYPE_JAR.equals(a.getType())) {
                    URI[] uris = a.getArtifactLocations();
                    for (URI u : uris) {
                      url = new URL(u.toString());
                      if (FileUtil.isArchiveFile(url)) {
                        url = FileUtil.getArchiveRoot(url);
                        urls.add(url);
                      }
                    }
                  }
                }
              } catch (MalformedURLException ex) {
                Exceptions.printStackTrace(ex);
              } catch (FileStateInvalidException ex) {
                Exceptions.printStackTrace(ex);
              }
            }
          }
        } else {
          if (f != null) {
            try {
              URL url = f.toURI().toURL();
              if (FileUtil.isArchiveFile(url)) {
                url = FileUtil.getArchiveRoot(url);
              }
              urls.add(url);
            } catch (MalformedURLException ex) {
              Exceptions.printStackTrace(ex);
            }
          }
        }
      }
      return urls.toArray(new URL[urls.size()]);
    }