org.openide.util.BaseUtilities Java Examples
The following examples show how to use
org.openide.util.BaseUtilities.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestUtilities.java From netbeans with Apache License 2.0 | 6 votes |
@CheckForNull public static Path getJRTFS() throws IOException { final File java9 = getJava9Home(); if (java9 == null) { return null; } final File jrtFsProvider = new File(java9,"jrt-fs.jar"); //NOI18N if (jrtFsProvider.exists() && jrtFsProvider.isFile() && jrtFsProvider.canRead()) { final ClassLoader cl = new URLClassLoader(new URL[]{ BaseUtilities.toURI(jrtFsProvider).toURL() }); final ServiceLoader<FileSystemProvider> sl = ServiceLoader.load(FileSystemProvider.class, cl); FileSystemProvider jrtp = null; for (FileSystemProvider fsp : sl) { if ("jrt".equals(fsp.getScheme())) { //NOI18N jrtp = fsp; break; } } if (jrtp != null) { return jrtp.getPath(URI.create("jrt:/")); //NOI18N } } return null; }
Example #2
Source File: FileUtilTestHidden.java From netbeans with Apache License 2.0 | 6 votes |
public void testNormalizeFile2() throws Exception { if (!BaseUtilities.isWindows()) { return; } File rootFile = FileUtil.toFile(root); if (rootFile == null) { return; } assertTrue(rootFile.exists()); rootFile = FileUtil.normalizeFile(rootFile); File testFile = new File(rootFile, "abc.txt"); assertTrue(testFile.createNewFile()); assertTrue(testFile.exists()); File testFile2 = new File(rootFile, "ABC.TXT"); assertTrue(testFile2.exists()); assertSame(testFile, FileUtil.normalizeFile(testFile)); assertNotSame(testFile2, FileUtil.normalizeFile(testFile2)); }
Example #3
Source File: ExternalProcessBuilder.java From netbeans with Apache License 2.0 | 6 votes |
public static String getPathName(Map<String, String> systemEnv) { // Find PATH environment variable - on Windows it can be some other // case and we should use whatever it has. String pathName = "PATH"; // NOI18N if (BaseUtilities.isWindows()) { pathName = "Path"; // NOI18N for (String keySystem : systemEnv.keySet()) { if ("PATH".equals(keySystem.toUpperCase(Locale.ENGLISH))) { // NOI18N pathName = keySystem; break; } } } return pathName; }
Example #4
Source File: LuceneIndex.java From netbeans with Apache License 2.0 | 6 votes |
private CleanReference(final RAMDirectory[] dir) { super (dir, BaseUtilities.activeReferenceQueue()); final IndexCacheFactory.RAMController c = IndexCacheFactory.getDefault().getRAMController(); final boolean doHardRef = !c.isFull(); if (doHardRef) { this.hardRef = dir; long _size = dir[0].sizeInBytes(); size.set(_size); c.acquire(_size); } LOGGER.log(Level.FINEST, "Caching index: {0} cache policy: {1}", //NOI18N new Object[]{ folder.getAbsolutePath(), cachePolicy.getSystemName() }); }
Example #5
Source File: SimpleAntArtifact.java From netbeans with Apache License 2.0 | 6 votes |
private URI getArtifactLocation0() { String locationResolved = eval.getProperty(locationProperty); if (locationResolved == null) { return URI.create("file:/UNDEFINED"); // NOI18N } File locF = new File(locationResolved); if (locF.isAbsolute()) { return BaseUtilities.toURI(locF); } else { // Project-relative path. try { return new URI(null, null, locationResolved.replace(File.separatorChar, '/'), null); } catch (URISyntaxException e) { Logger.getLogger(this.getClass().getName()).log(Level.INFO, null, e); return URI.create("file:/BROKEN"); // NOI18N } } }
Example #6
Source File: FileUtilTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testToFileObjectSlash() throws Exception { // #98388 if (!BaseUtilities.isUnix()) { return; } File root = new File("/"); assertTrue(root.isDirectory()); LocalFileSystem lfs = new LocalFileSystem(); lfs.setRootDirectory(root); final FileObject LFS_ROOT = lfs.getRoot(); MockLookup.setInstances(new URLMapper() { public URL getURL(FileObject fo, int type) { return null; } public FileObject[] getFileObjects(URL url) { if (url.toExternalForm().equals("file:/")) { return new FileObject[] {LFS_ROOT}; } else { return null; } } }); URLMapper.reset(); assertEquals(LFS_ROOT, FileUtil.toFileObject(root)); }
Example #7
Source File: BaseFileObj.java From netbeans with Apache License 2.0 | 6 votes |
@Override public final String getPath() { FileNaming fileNaming = getFileName(); Deque<String> stack = new ArrayDeque<String>(); while (fileNaming != null) { stack.addFirst(fileNaming.getName()); fileNaming = fileNaming.getParent(); } String rootName = stack.removeFirst(); if (BaseUtilities.isWindows()) { rootName = rootName.replace(File.separatorChar, '/'); if(rootName.startsWith("//")) { //NOI18N // UNC root like //computer/sharedFolder rootName += "/"; //NOI18N } } StringBuilder path = new StringBuilder(); path.append(rootName); while (!stack.isEmpty()) { path.append(stack.removeFirst()); if (!stack.isEmpty()) { path.append('/'); //NOI18N } } return path.toString(); }
Example #8
Source File: JarClassLoader.java From netbeans with Apache License 2.0 | 6 votes |
private static String toURI(final File file) { class VFile extends File { public VFile() { super(file.getPath()); } @Override public boolean isDirectory() { return false; } @Override public File getAbsoluteFile() { return this; } } return "jar:" + BaseUtilities.toURI(new VFile()) + "!/"; // NOI18N }
Example #9
Source File: URLMapperTest.java From netbeans with Apache License 2.0 | 6 votes |
public void testPlusInName() throws IOException, PropertyVetoException { clearWorkDir(); File plus = new File(getWorkDir(), "plus+plus"); plus.createNewFile(); LocalFileSystem lfs = new LocalFileSystem(); lfs.setRootDirectory(getWorkDir()); Repository.getDefault().addFileSystem(lfs); URL uPlus = BaseUtilities.toURI(plus).toURL(); FileObject fo = URLMapper.findFileObject(uPlus); assertNotNull("File object found", fo); assertEquals("plus+plus", fo.getNameExt()); URL back = URLMapper.findURL(fo, URLMapper.EXTERNAL); assertTrue("plus+plus is there", back.getPath().endsWith("plus+plus")); }
Example #10
Source File: JavaAntLogger.java From netbeans with Apache License 2.0 | 6 votes |
@NonNull private static Collection<? extends File> collectModules(@NonNull final File root) { if (root.isDirectory()) { if (new File(root,MODULE_INFO_CLZ).isFile()) { return Collections.singleton(root); } else { final File[] children = root.listFiles((e) -> { try { return FileUtil.isArchiveFile(BaseUtilities.toURI(e).toURL()); } catch (MalformedURLException mue) { return false; } }); return children == null ? Collections.emptyList() : Arrays.asList(children); } } else { return Collections.singleton(root); } }
Example #11
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
private void doPrimitiveTypeTest(String value, Class<?> expClass) throws Exception { File f = writeFile("layer.xml", "<filesystem>\n" + "<folder name='TestModule'>\n" + "<file name='sample.txt' >" + " <attr name='instanceCreate' " + value + "/>" + "</file>\n" + "</folder>\n" + "</filesystem>\n" ); xfs = FileSystemFactoryHid.createXMLSystem(getName(), this, BaseUtilities.toURI(f).toURL()); FileObject fo = xfs.findResource ("TestModule/sample.txt"); assertNotNull(fo); Object clazz = fo.getAttribute("class:instanceCreate"); assertEquals("Only Runnable guessed as that is the return type of the method", expClass, clazz); Object instance = fo.getAttribute("instanceCreate"); assertNotNull("Returned", instance); assertEquals("Right class", expClass, instance.getClass()); }
Example #12
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
public void testNoInstanceCreatedWithNewValue() throws Exception { Count.cnt = 0; File f = writeFile("layer.xml", "<filesystem>\n" + "<folder name='TestModule'>\n" + "<file name='sample.txt' >" + " <attr name='instanceCreate' newvalue='org.openide.filesystems.Count'/>" + "</file>\n" + "</folder>\n" + "</filesystem>\n" ); xfs = FileSystemFactoryHid.createXMLSystem(getName(), this, BaseUtilities.toURI(f).toURL()); FileObject fo = xfs.findResource ("TestModule/sample.txt"); assertNotNull(fo); Object clazz = fo.getAttribute("class:instanceCreate"); assertEquals("No instance of Count created", 0, Count.cnt); assertEquals("Yet right class guessed", Count.class, clazz); Object instance = fo.getAttribute("instanceCreate"); assertEquals("One instance of Count created", 1, Count.cnt); assertNotNull("Returned", instance); assertEquals("Right class", Count.class, instance.getClass()); }
Example #13
Source File: Util.java From netbeans with Apache License 2.0 | 6 votes |
/** * Resolves URL within a file root. * @param root the root to resolve the path in. * @param relativePath the relative path under the root. * @param isDirectory true if the relativePath is known to point to directory, * false if the relativePath is known to point to file, null if nothing is known * about the target. * @return * @throws MalformedURLException * @throws IllegalStateException when file ends with '/' or {@link File#separatorChar} */ public static URL resolveFile( @NonNull final File file, @NonNull final String relativePath, @NullAllowed final Boolean isDirectory) throws MalformedURLException { if (isDirectory == Boolean.FALSE && (relativePath.isEmpty() || relativePath.charAt(relativePath.length()-1) == '/' || //NOI18N relativePath.charAt(relativePath.length()-1) == File.separatorChar)) { throw new IllegalStateException( MessageFormat.format("relativePath: {0}", relativePath)); //NOI18N } // Performance optimization for File.toURI() which calls this method // and the original implementation calls into native method return BaseUtilities.toURI(new FastFile( file, relativePath, isDirectory)).toURL(); }
Example #14
Source File: RootsListener.java From netbeans with Apache License 2.0 | 6 votes |
@org.netbeans.api.annotations.common.SuppressWarnings( value="DMI_COLLECTION_OF_URLS", justification="URLs have never host part") synchronized boolean addSource( @NonNull final URL root, @NullAllowed ClassPath.Entry entry) { if (sourcesListener != null) { if (!sourceRoots.containsKey(root) && root.getProtocol().equals("file")) { //NOI18N try { final File f = BaseUtilities.toFile(root.toURI()); safeAddRecursiveListener(sourcesListener, f, entry); sourceRoots.put(root, f); } catch (URISyntaxException use) { LOG.log(Level.INFO, null, use); } } } return listens; }
Example #15
Source File: SourcePathProviderImpl.java From netbeans with Apache License 2.0 | 6 votes |
private void storeDisabledSourceRoots(Set<String> disabledSourceRoots) { Properties sourcesProperties = Properties.getDefault ().getProperties ("debugger").getProperties ("sources"); if (baseDir != null) { String projectRoot; try { projectRoot = BaseUtilities.toURI(baseDir).toURL().toExternalForm(); } catch (MalformedURLException ex) { Exceptions.printStackTrace(ex); return ; } Map map = sourcesProperties.getProperties("source_roots"). getMap("project_disabled", new HashMap()); map.put(projectRoot, disabledSourceRoots); sourcesProperties.getProperties("source_roots"). setMap("project_disabled", map); } else { sourcesProperties.getProperties("source_roots"). setCollection("remote_disabled", disabledSourceRoots); } }
Example #16
Source File: LibrariesNode.java From netbeans with Apache License 2.0 | 6 votes |
@NonNull @Override public Collection<File> apply(@NonNull final File file) { Collection<File> entries = new ArrayList<>(); if (file.isDirectory()) { if (new File(file,"module-info.class").exists()) { //NOI18N entries.add(file); } else { Optional.ofNullable(file.listFiles((f) -> { try { return FileUtil.isArchiveFile(BaseUtilities.toURI(f).toURL()); } catch (MalformedURLException e) { Exceptions.printStackTrace(e); return false; } })) .ifPresent((fs) -> Collections.addAll(entries, fs)); } } else { entries.add(file); } return entries; }
Example #17
Source File: FileUtil.java From netbeans with Apache License 2.0 | 6 votes |
/** * 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) { final String u = entry.toString(); if (isArchiveArtifact(entry)) { entry = getArchiveFile(entry); try { return u.endsWith("!/") && entry != null && "file".equals(entry.getProtocol()) ? //NOI18N BaseUtilities.toFile(entry.toURI()): null; } catch (URISyntaxException e) { LOG.log( Level.WARNING, "Invalid URI: {0} ({1})", //NOI18N new Object[]{ entry, e.getMessage() }); return null; } } else if (u.startsWith("file:")) { // NOI18N return BaseUtilities.toFile(URI.create(u)); } else { return null; } }
Example #18
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
public void testNoInstanceCreatedWithMethodValue1() throws Exception { Count.cnt = 0; File f = writeFile("layer.xml", "<filesystem>\n" + "<folder name='TestModule'>\n" + "<file name='sample.txt' >" + " <attr name='instanceCreate' methodvalue='org.openide.filesystems.Count.create'/>" + "</file>\n" + "</folder>\n" + "</filesystem>\n" ); xfs = FileSystemFactoryHid.createXMLSystem(getName(), this, BaseUtilities.toURI(f).toURL()); FileObject fo = xfs.findResource ("TestModule/sample.txt"); assertNotNull(fo); Object clazz = fo.getAttribute("class:instanceCreate"); assertEquals("No instance of Count created", 0, Count.cnt); assertEquals("Yet right class guessed", Count.class, clazz); Object instance = fo.getAttribute("instanceCreate"); assertEquals("One instance of Count created", 1, Count.cnt); assertNotNull("Returned", instance); assertEquals("Right class", Count.class, instance.getClass()); }
Example #19
Source File: MultiModuleClassPathProvider.java From netbeans with Apache License 2.0 | 6 votes |
@CheckForNull private URL getURL(@NonNull final String propname) { URL u = urlCache.get(propname); if (u == null) { final String prop = eval.getProperty(propname); if (prop != null) { final File file = helper.resolveFile(prop); if (file != null) { try { u = BaseUtilities.toURI(file).toURL(); urlCache.put (propname, u); } catch (MalformedURLException e) { LOG.log( Level.WARNING, "Cannot convert to URL: {0}", //NOI18N file.getAbsolutePath()); } } } } return u; }
Example #20
Source File: TreeLoaderOutputFileManager.java From netbeans with Apache License 2.0 | 6 votes |
@Override public Location getLocationForModule(Location location, String moduleName) throws IOException { if (!hasLocation(location)) { throw new IllegalArgumentException(String.valueOf(location)); } final URL cacheRoot = BaseUtilities.toURI(new File(outputRoot)).toURL(); final URL origRoot = JavaIndex.getSourceRootForClassFolder(cacheRoot); if (origRoot == null) { return null; } final String expectedModuleName = SourceUtils.getModuleName(origRoot); return moduleName.equals(expectedModuleName) ? ModuleLocation.create( StandardLocation.CLASS_OUTPUT, Collections.singleton(cacheRoot), moduleName): null; }
Example #21
Source File: MultiModuleClassPathProvider.java From netbeans with Apache License 2.0 | 6 votes |
@NonNull private Collection<? extends URL> getRoots() { synchronized (this) { if (includeIn != null) { return includeIn; } } URL url = null; final String val = eval.getProperty(ownerProp); if (val != null) { try { File f = helper.resolveFile(val); url = BaseUtilities.toURI(f).toURL(); } catch (MalformedURLException e) { LOG.warning(e.toString()); } } synchronized (this) { if (includeIn == null) { includeIn = url == null ? Collections.emptySet() : Collections.singleton(url); } return includeIn; } }
Example #22
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
public void testToStringOnMapDoesNotComputeValues() throws Exception { File layer = new File(getWorkDir(), "layer2.xml"); FileWriter w = new FileWriter(layer); w.write( "<filesystem>" + " <folder name='f'>" + " <file name='empty.xml'>" + " <attr name='displayName' methodvalue='org.openide.filesystems.XMLFileSystemTestHid.computeToString'/>" + " </file>" + " </folder>" + "</filesystem>"); w.close(); xfs = FileSystemFactoryHid.createXMLSystem(getName(), this, BaseUtilities.toURI(layer).toURL()); FileObject fo = xfs.findResource("f/empty.xml"); assertNotNull("File found", fo); String displayName = (String) fo.getAttribute("displayName"); assertNotNull("Attribute provided", displayName); if (!displayName.contains(fo.getPath())) { fail("The file Object name should be in there: " + displayName); } }
Example #23
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
public void testReset () throws Exception { FileObject a = xfs.findResource("a"); assertNotNull(a); FileChangeAdapter fcl = new FileChangeAdapter(); a.addFileChangeListener(fcl); resources = new String[] {"a/b/c","a/b1/c"}; if (!FileSystemFactoryHid.switchXMLSystem(xfs, this, BaseUtilities.toURI(createXMLLayer()).toURL())) { // OK, unsupported return; } FileObject b1 = xfs.findResource("a/b1"); assertNotNull(b1); assertTrue(b1.isFolder()); }
Example #24
Source File: XMLFileSystemTestHid.java From netbeans with Apache License 2.0 | 6 votes |
public void testMultiFileSystemWithOverridenAttributes() throws Exception { File f = writeFile("layer.xml", "<filesystem>\n" + " <folder name =\"org-sepix\">\n" + " <folder name =\"Panes\">\n" + " <file name=\"Demo.txt\">\n" + " <attr name=\"position\" intvalue=\"100\"/>\n" + " </file>\n" + " </folder>\n" + " </folder>\n" + "</filesystem>"); xfs = FileSystemFactoryHid.createXMLSystem(getName(), this, BaseUtilities.toURI(f).toURL()); final LocalFileSystem lfs = new LocalFileSystem(); lfs.setRootDirectory(getWorkDir()); MultiFileSystem mfs = new MultiFileSystem(lfs, xfs); FileObject folder = mfs.findResource("org-sepix/Panes/"); for (FileObject fileObject : folder.getChildren()) { assertEquals("should be 100", 100, fileObject.getAttribute("position")); fileObject.setAttribute("position", 200); assertEquals("should be 200", 200, fileObject.getAttribute("position")); assertEquals("should be 200 still", 200, fileObject.getAttribute("position")); } }
Example #25
Source File: CollocationQuery2Test.java From netbeans with Apache License 2.0 | 6 votes |
@Override public boolean areCollocated(URI uri1, URI uri2) { if (uri1.equals(uri2)) { return true; } File file1 = BaseUtilities.toFile(uri1); File file2 = BaseUtilities.toFile(uri2); String f1 = file1.getPath(); if ((file1.isDirectory() || !file1.exists()) && !f1.endsWith(File.separator)) { f1 += File.separatorChar; } String f2 = file2.getAbsolutePath(); if ((file2.isDirectory() || !file2.exists()) && !f2.endsWith(File.separator)) { f2 += File.separatorChar; } return f1.startsWith(f2) || f2.startsWith(f1); }
Example #26
Source File: LibrariesNode.java From netbeans with Apache License 2.0 | 5 votes |
@CheckForNull private static URL resolveAntArtifact( @NonNull final AntArtifact art, @NonNull final URI loc) { final FileObject prj = art.getProject().getProjectDirectory(); final File f = BaseUtilities.toFile(prj.toURI().resolve(loc)); return FileUtil.urlForArchiveOrDir(f); }
Example #27
Source File: FileObj.java From netbeans with Apache License 2.0 | 5 votes |
@Override public boolean canWrite() { final File f = getFileName().getFile(); if (!BaseUtilities.isWindows() && !f.isFile()) { return false; } return super.canWrite(); }
Example #28
Source File: ExternalExecutable.java From netbeans with Apache License 2.0 | 5 votes |
static Pair<String, List<String>> parseCommand(String command) { if (command == null) { // avoid NPE command = ""; // NOI18N } // try to find program (search for " -" or " /" after space) String[] tokens = command.split(" * (?=\\-|/)", 2); // NOI18N if (tokens.length == 1) { LOGGER.log(Level.FINE, "Only program given (no parameters): {0}", command); return Pair.of(tokens[0].trim(), Collections.<String>emptyList()); } Pair<String, List<String>> parsedCommand = Pair.of(tokens[0].trim(), Arrays.asList(BaseUtilities.parseParameters(tokens[1].trim()))); LOGGER.log(Level.FINE, "Parameters parsed: {0} {1}", new Object[] {parsedCommand.first(), parsedCommand.second()}); return parsedCommand; }
Example #29
Source File: ParentChildCollocationQueryTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testAreCollocated() throws Exception { clearWorkDir(); File base = getWorkDir(); File proj1 = new File(base, "proj1"); proj1.mkdirs(); File proj3 = new File(proj1, "proj3"); proj3.mkdirs(); File proj2 = new File(base, "proj2"); proj2.mkdirs(); ParentChildCollocationQuery query = new ParentChildCollocationQuery(); assertTrue("Must be collocated", query.areCollocated(BaseUtilities.toURI(proj1), BaseUtilities.toURI(proj3))); assertTrue("Must be collocated", query.areCollocated(BaseUtilities.toURI(proj3), BaseUtilities.toURI(proj1))); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(proj1), BaseUtilities.toURI(proj2))); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(proj2), BaseUtilities.toURI(proj1))); // folder does not exist: File proj4 = new File(base, "proj"); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(proj1), BaseUtilities.toURI(proj4))); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(proj4), BaseUtilities.toURI(proj1))); proj4.mkdirs(); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(proj1), BaseUtilities.toURI(proj4))); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(proj4), BaseUtilities.toURI(proj1))); // files do not exist: File file1 = new File(base, "file1.txt"); File file2 = new File(base, "file1"); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(file1), BaseUtilities.toURI(file2))); assertFalse("Cannot be collocated", query.areCollocated(BaseUtilities.toURI(file2), BaseUtilities.toURI(file1))); // passing the same parameter assertTrue("A file must be collocated with itself", query.areCollocated(BaseUtilities.toURI(proj1), BaseUtilities.toURI(proj1))); }
Example #30
Source File: FileUtilTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testLowerAndCapitalNormalization() throws IOException { if (!BaseUtilities.isWindows()) { return; } clearWorkDir(); File a = new File(getWorkDir(), "a"); assertTrue("Lower case file created", a.createNewFile()); File A = new File(getWorkDir(), "A"); assertEquals("Normalizes to lower case", a.getAbsolutePath(), FileUtil.normalizeFile(A).getAbsolutePath()); assertTrue("Can delete the file", a.delete()); assertTrue("Can create capital file", A.createNewFile()); assertEquals("Normalizes to capital case", A.getAbsolutePath(), FileUtil.normalizeFile(A).getAbsolutePath()); }