org.openide.util.Utilities.toFile()方法的使用及代码示例

x33g5p2x  于2022-01-31 转载在 其他  
字(13.4k)|赞(0)|评价(0)|浏览(134)

本文整理了Java中org.openide.util.Utilities.toFile()方法的一些代码示例,展示了Utilities.toFile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utilities.toFile()方法的具体详情如下:
包路径:org.openide.util.Utilities
类名称:Utilities
方法名:toFile

Utilities.toFile介绍

[英]Converts a URI to a file while being safe for UNC paths. Unlike File#File(URI) UNC URIs with a host field are accepted.
[中]将URI转换为文件,同时确保UNC路径的安全。与文件#文件(URI)不同,可以接受带有主机字段的UNC URI。

代码示例

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

private static File normalizeFileOnUnixAlike(File file) {
  // On Unix, do not want to traverse symlinks.
  // URI.normalize removes ../ and ./ sequences nicely.
  file = Utilities.toFile(Utilities.toURI(file).normalize()).getAbsoluteFile();
  while (file.getAbsolutePath().startsWith("/../")) { // NOI18N
    file = new File(file.getAbsolutePath().substring(3));
  }
  if (file.getAbsolutePath().equals("/..")) { // NOI18N
    // Special treatment.
    file = new File("/"); // NOI18N
  }
  return file;
}

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

/**
 * Convert a classpath-type URL to a corresponding file.
 * If it is a <code>jar</code> URL representing the root folder of a local disk archive,
 * that archive file will be returned.
 * If it is a <code>file</code> URL representing a local disk folder,
 * that folder will be returned.
 * @param entry a classpath entry or similar URL
 * @return a corresponding file, or null for e.g. a network URL or non-root JAR folder entry
 * @since org.openide.filesystems 7.8
 */
public static File archiveOrDirForURL(URL entry) {
  String u = entry.toString();
  if (u.startsWith("jar:file:") && u.endsWith("!/")) { // NOI18N
    return Utilities.toFile(URI.create(u.substring(4, u.length() - 2)));
  } else if (u.startsWith("file:")) { // NOI18N
    return Utilities.toFile(URI.create(u));
  } else {
    return null;
  }
}

代码示例来源:origin: org.netbeans.api/org-openide-util

/**
 * Convert a <code>file:</code> URL to a matching file.
 * <p>You may not use a URL generated from a file on a different
 * platform, as file name conventions may make the result meaningless
 * or even unparsable.
 * @param u a URL with the <code>file</code> protocol
 * @return an absolute file it points to, or <code>null</code> if the URL
 *         does not seem to point to a file at all
 * @see #toURL
 * @see <a href="http://www.netbeans.org/issues/show_bug.cgi?id=29711">Issue #29711</a>
 * @since 3.26
 * @deprecated Use {@link URL#toURI} and {@link #toFile(URI)} instead under JDK 1.4.
 *             (There was no proper equivalent under JDK 1.3.)
 */
@Deprecated
public static File toFile(URL u) {
  if (u == null) {
    throw new NullPointerException();
  }
  try {
    URI uri = u.toURI();
    return toFile(uri);
  } catch (URISyntaxException use) {
    // malformed URL
    return null;
  } catch (IllegalArgumentException iae) {
    // not a file: URL
    return null;
  }
}

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

private static File toFile(URL u) {
  if (u == null) {
    throw new NullPointerException();
  }
  try {
    URI uri = new URI(u.toExternalForm());
    return FileUtil.normalizeFile(Utilities.toFile(uri));
  } catch (URISyntaxException use) {
    // malformed URL
    return null;
  } catch (IllegalArgumentException iae) {
    // not a file: URL
    return null;
  }
}

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

private static File normalizeFileOnMac(final File file) {
  File retVal = file;
  try {
    // URI.normalize removes ../ and ./ sequences nicely.            
    File absoluteFile = Utilities.toFile(Utilities.toURI(file).normalize());
    File canonicalFile = file.getCanonicalFile();
    String absolutePath = absoluteFile.getAbsolutePath();
    if (absolutePath.equals("/..")) { // NOI18N
      // Special treatment.
      absoluteFile = new File(absolutePath = "/"); // NOI18N
    }
    boolean isSymLink = !canonicalFile.getAbsolutePath().equalsIgnoreCase(absolutePath);
    if (isSymLink) {
      retVal = normalizeSymLinkOnMac(absoluteFile);
    } else {
      retVal = canonicalFile;
    }
  } catch (IOException ioe) {
    LOG.log(Level.FINE, "Normalization failed on file " + file, ioe);
    // OK, so at least try to absolutize the path
    retVal = file.getAbsoluteFile();
  }
  return retVal;
}

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

File f = Utilities.toFile(URI.create(url.toExternalForm()));
if (!f.equals(lastFile)) {
  lastFile = f;

代码示例来源:origin: org.netbeans.api/org-openide-filesystems

/** Finds appropriate java.io.File to FileObject if possible.
 * If not possible then null is returned.
 * This is the inverse operation of {@link #toFileObject}.
 * @param fo FileObject whose corresponding File will be looked for
 * @return java.io.File or null if no corresponding File exists.
 * @since 1.29
 */
public static File toFile(FileObject fo) {
  File retVal = (File) fo.getAttribute("java.io.File"); // NOI18N;        
  if (retVal == null) {
    URL fileURL = URLMapper.findURL(fo, URLMapper.INTERNAL);
    if (fileURL == null || !"file".equals(fileURL.getProtocol())) {  //NOI18N
      fileURL = URLMapper.findURL(fo, URLMapper.EXTERNAL);
    }
    if ((fileURL != null) && "file".equals(fileURL.getProtocol())) {
      retVal = Utilities.toFile(URI.create(fileURL.toExternalForm()));
    }
  }
  if (retVal != null) {
    retVal = normalizeFile(retVal);
  }
  assert assertNormalized(retVal);
  return retVal;
}

代码示例来源:origin: org.netbeans.api/org-openide-util-ui

/**
 * Convert a <code>file:</code> URL to a matching file.
 * <p>You may not use a URL generated from a file on a different
 * platform, as file name conventions may make the result meaningless
 * or even unparsable.
 * @param u a URL with the <code>file</code> protocol
 * @return an absolute file it points to, or <code>null</code> if the URL
 *         does not seem to point to a file at all
 * @see #toURL
 * @see <a href="http://www.netbeans.org/issues/show_bug.cgi?id=29711">Issue #29711</a>
 * @since 3.26
 * @deprecated Use {@link URL#toURI} and {@link #toFile(URI)} instead under JDK 1.4.
 *             (There was no proper equivalent under JDK 1.3.)
 */
@Deprecated
public static File toFile(URL u) {
  if (u == null) {
    throw new NullPointerException();
  }
  try {
    URI uri = u.toURI();
    return toFile(uri);
  } catch (URISyntaxException use) {
    // malformed URL
    return null;
  } catch (IllegalArgumentException iae) {
    // not a file: URL
    return null;
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-cnd-utils

public static File createLocalFile(URI uri) {
  File file = Utilities.toFile(uri);
  CndUtils.assertAbsoluteFileInConsole(file); //NOI18N
  return file;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-j2ee-ejbjarproject

@Override
public List<String> keys() {
  List<String> result = new ArrayList<String>();
  result.add(LIBRARIES);
  URL[] testRoots = testSources.getRootURLs();
  boolean addTestSources = false;
  for (int i = 0; i < testRoots.length; i++) {
    File f = Utilities.toFile(URI.create(testRoots[i].toExternalForm()));
    if (f.exists()) {
      addTestSources = true;
      break;
    }
  }
  if (addTestSources) {
    result.add(TEST_LIBRARIES);
  }
  return result;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-project

public List<String> keys() {
  List<String> result = new ArrayList<String>();
  result.add(LIBRARIES);
  URL[] testRoots = testSources.getRootURLs();
  boolean addTestSources = false;
  for (int i = 0; i < testRoots.length; i++) {
    File f = Utilities.toFile(URI.create(testRoots[i].toExternalForm()));
    if (f.exists()) {
      addTestSources = true;
      break;
    }
  }
  if (addTestSources) {
    result.add(TEST_LIBRARIES);
  }
  return result;
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-bootstrap

final void addURL(URL location) throws IOException, URISyntaxException {
  File f = Utilities.toFile(location.toURI());
  assert f.exists() : "URL must be existing local file: " + location;
  List<Source> arr = new ArrayList<Source>(Arrays.asList(sources));
  arr.add(new JarSource(f));
  synchronized (sources) {
    sources = arr.toArray(new Source[arr.size()]);
  }
  // overlaps with old packages doesn't matter,PCL uses sets.
  addCoveredPackages(getCoveredPackages(module, sources));
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-project

@Override
public boolean includes(URL root, String resource) {
  if (matcher == null) {
    File rootFile = Utilities.toFile(URI.create(root.toExternalForm()));
    matcher = new PathMatcher(
        null,
        computeExcludes(rootFile),
        rootFile);
  }
  return matcher.matches(resource, true);
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-web-jsf

public static String getPackageName(URL url) {
  File file = null;
  try {
    file = FileUtil.normalizeFile(Utilities.toFile(url.toURI()));
  } catch (URISyntaxException uRISyntaxException) {
    throw new IllegalArgumentException("Cannot create package name for url " + url);  //NOI18N
  }
  String suffix = "";
  
  do {
    FileObject fileObject = FileUtil.toFileObject(file);
    if (fileObject != null) {
      if ("".equals(suffix))
        return getPackageName(fileObject);
      String prefix = getPackageName(fileObject);
      return prefix + ("".equals(prefix)?"":".") + suffix;
    }
    if (!"".equals(suffix)) {
      suffix = "." + suffix;
    }
    suffix = URLDecoder.decode(file.getPath().substring(file.getPath().lastIndexOf(File.separatorChar)+1)) + suffix;
    file = file.getParentFile();
  } while (file!=null);
  throw new IllegalArgumentException("Cannot create package name for url " + url);  //NOI18N
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-modules-php-editor

@Override
public Set<FileObject> getLocationsForIdentifiers(String identifierName) {
  final Set<FileObject> result = new HashSet<>();
  Collection<? extends IndexResult> idIndexResult = search(
      PHPIndexer.FIELD_IDENTIFIER,
      identifierName.toLowerCase(),
      QuerySupport.Kind.PREFIX,
      PHPIndexer.FIELD_BASE,
      PHPIndexer.FIELD_IDENTIFIER);
  for (IndexResult indexResult : idIndexResult) {
    String[] values = indexResult.getValues(PHPIndexer.FIELD_IDENTIFIER);
    for (String val : values) {
      Signature sig = Signature.get(val);
      if (identifierName.equalsIgnoreCase(sig.string(0))) {
        URL url = indexResult.getUrl();
        FileObject fo = null;
        try {
          fo = "file".equals(url.getProtocol()) ? //NOI18N
              FileUtil.toFileObject(Utilities.toFile(url.toURI())) : URLMapper.findFileObject(url);
        } catch (URISyntaxException ex) {
          Exceptions.printStackTrace(ex);
        }
        if (fo != null) {
          result.add(fo);
        }
      }
    }
  }
  return result;
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-queries

/**
   * Find a root of a logical tree containing this file, if any.
   * @param file a file on disk
   * @return an ancestor directory which is the root of a logical tree,
   *         if any (else null)
   * @since 1.27
   */
  public static URI findRoot(URI file) {
    if (!file.equals(file.normalize())) {
      throw new IllegalArgumentException("Parameter file was not "+  // NOI18N
        "normalized. Was "+file+" instead of "+file.normalize());  // NOI18N
    }
    for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) {
      URI root = cqi.findRoot(file);
      if (root != null) {
        return root;
      }
    }
    if ("file".equals(file.getScheme())) { // NOI18N
      File f = FileUtil.normalizeFile(Utilities.toFile(file));
      for (org.netbeans.spi.queries.CollocationQueryImplementation cqi : implementations.allInstances()) {
        File root = cqi.findRoot(f);
        if (root != null) {
          return Utilities.toURI(root);
        }
      }
    }
    return null;
  }
}

代码示例来源:origin: org.netbeans.modules/org-netbeans-libs-freemarker

private FileObject getFile(String name) {
  FileObject tmp = (getFolder() == null) ? null : getFolder().getFileObject(name);
  if (tmp == null) {
    try {
      tmp = FileUtil.toFileObject(FileUtil.normalizeFile(Utilities.toFile(new URI(name))));
    } catch (URISyntaxException ex) {
    } catch (IllegalArgumentException iae) {
    }
  }
  return tmp;
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-queries

/**
 * Find a root of a logical tree containing this file, if any.
 * @param file a file on disk
 * @return an ancestor directory which is the root of a logical tree,
 *         if any (else null)
 * @deprecated Use {@link #findRoot(java.net.URI)} instead.
 */
@Deprecated public static File findRoot(File file) {
  if (!file.equals(FileUtil.normalizeFile(file))) {
    throw new IllegalArgumentException("Parameter file was not "+  // NOI18N
      "normalized. Was "+file+" instead of "+FileUtil.normalizeFile(file));  // NOI18N
  }
  URI uri = Utilities.toURI(file);
  for (CollocationQueryImplementation2 cqi : implementations2.allInstances()) {
    URI root = cqi.findRoot(uri);
    if (root != null) {
      return Utilities.toFile(root);
    }
  }
  for (org.netbeans.spi.queries.CollocationQueryImplementation cqi : implementations.allInstances()) {
    File root = cqi.findRoot(file);
    if (root != null) {
      return root;
    }
  }
  return null;
}

代码示例来源:origin: org.netbeans.api/org-netbeans-modules-project-ant-ui

/**
 * Create chooser for given AntProjectHelper. Standard file chooser is shown
 * if project is not sharable.
 * 
 * @param helper ant project helper; cannot be null
 * @param copyAllowed is file copying allowed
 */
public FileChooser(AntProjectHelper helper, boolean copyAllowed) {
  super();
  FileObject projectFolder = helper.getProjectDirectory();
  Project p = projectFolder != null ? FileOwnerQuery.getOwner(projectFolder): null;
  LibraryManager lm = p != null ? ReferenceHelper.getProjectLibraryManager(p) : null;
  if (lm != null) {
    URL u = lm.getLocation();
    if (u != null) {
      File libBase = Utilities.toFile(URI.create(u.toExternalForm())).getParentFile();
      accessory = new FileChooserAccessory(this, FileUtil.toFile(helper.getProjectDirectory()), 
        libBase, copyAllowed);
      setAccessory(accessory);
    }
  }
}

代码示例来源:origin: hmvictor/radar-netbeans

protected void configureSourcesAndBinariesProperties(Version sonarQubeVersion, Properties properties) {
  SourceGroup mainSourceGroup = getMainSourceGroup();
  if (mainSourceGroup != null) {
    String sourcePath = mainSourceGroup.getRootFolder().getPath();
    if (SonarMvnProject.isMvnProject(projectContext.getProject()) && sonarQubeVersion.compareTo(4, 5) >= 0) {
      sourcePath = "pom.xml," + sourcePath;
    }
    ClassPath classPath = ClassPath.getClassPath(projectContext.getProject().getProjectDirectory(), ClassPath.COMPILE);
    if (classPath != null) {
      properties.setProperty(getPropertyName("sonar.java.libraries"), getLibrariesPath(classPath));
    }
    properties.setProperty(getPropertyName("sonar.sources"), sourcePath);
    URL[] roots = BinaryForSourceQuery.findBinaryRoots(mainSourceGroup.getRootFolder().toURL()).getRoots();
    if (roots.length > 0) {
      properties.setProperty(getPropertyName("sonar.java.binaries"), Utilities.toFile(roots[0]).getPath());
    }
    URL[] testSources = UnitTestForSourceQuery.findUnitTests(mainSourceGroup.getRootFolder());
    if (testSources != null && testSources.length != 0) {
      File testsDir = FileUtil.archiveOrDirForURL(testSources[0]);
      if (testsDir.exists()) {
        properties.setProperty(getPropertyName("sonar.tests"), testsDir.getPath());
      }
    }
  }
}

相关文章