Java Code Examples for org.openide.util.BaseUtilities#isWindows()

The following examples show how to use org.openide.util.BaseUtilities#isWindows() . 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: FileUtilTestHidden.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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 2
Source File: FileUtilTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testNormalizePathChangeCase() throws Exception {
    if (!BaseUtilities.isWindows()) {
        return;
    }
    clearWorkDir();
    File path = new File(getWorkDir(), "dir");
    path.mkdirs();
    File FILE = new File(path, "FILE");
    FILE.createNewFile();

    File file = new File(path, "file");
    File n2 = FileUtil.normalizeFile(file);
    assertNormalized(n2);
    
    FILE.renameTo(file);
    new FileRenameEvent(FileUtil.getConfigRoot(), "x", "y"); // flushes the caches
    File n1 = FileUtil.normalizeFile(FILE);
    assertNormalized(n1);
    
    assertEquals("now it has to normalize to lowercase", "file", n1.getName());
}
 
Example 3
Source File: BaseFileObj.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@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 4
Source File: ExternalProcessBuilder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
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 5
Source File: EmbedderFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Maven assumes the env vars are included in execution properties with the "env." prefix.
 * @param properties
 * @return 
 * @see EnvironmentUtils#addEnvVars
 */
public static Properties fillEnvVars(Properties properties) {
    for (Map.Entry<String,String> entry : System.getenv().entrySet()) {
        String key = entry.getKey();
        if (BaseUtilities.isWindows()) {
            key = key.toUpperCase(Locale.ENGLISH);
        }
        properties.setProperty("env." + key, entry.getValue());
    }
    return properties;
}
 
Example 6
Source File: FileUtilTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testNormalizeNonExistingButNotAccessibleRootOnWindows() throws IOException {
    if (!BaseUtilities.isWindows()) {
        return;
    }
    for (File r : File.listRoots()) {
        // hopefully one of them is a CD drive
        File g = new File(r + "\\my\\.");
        File gn = FileUtil.normalizeFile(g);
        File gnn = FileUtil.normalizeFile(gn);
        assertEquals("Normalized: " + g, gn, gnn);
    }
    
}
 
Example 7
Source File: FileUtilTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 8
Source File: ExternalProcessBuilder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
List<String> buildArguments() {
    if (!BaseUtilities.isWindows()) {
        return new ArrayList<String>(arguments);
    }
    List<String> result = new ArrayList<String>(arguments.size());
    for (String arg : arguments) {
        if (arg != null && !ESCAPED_PATTERN.matcher(arg).matches()) {
            result.add(escapeString(arg));
        } else {
            result.add(arg);
        }
    }
    return result;
}
 
Example 9
Source File: FileElement.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** #26521, 114976 - ignore not readable and windows' locked files. */
private static void handleIOException(FileObject fo, IOException ioe) throws IOException {
    if (fo.canRead()) {
        if (!BaseUtilities.isWindows() || !(ioe instanceof FileNotFoundException) || !fo.isValid() || !fo.getName().toLowerCase().contains("ntuser")) {//NOI18N
            throw ioe;
        }
    }
}
 
Example 10
Source File: FileBasedFileSystem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public FileBasedFileSystem() {
    if (BaseUtilities.isWindows()) {
        RootObjWindows realRoot = new RootObjWindows();
        root = new RootObj<RootObjWindows>(realRoot);
    } else {
        FileObjectFactory factory = FileObjectFactory.getInstance(new File("/"));//NOI18N
        root = new RootObj<BaseFileObj>(factory.getRoot());
    }
}
 
Example 11
Source File: FileObj.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canWrite() {
    final File f = getFileName().getFile();        
    if (!BaseUtilities.isWindows() && !f.isFile()) {
        return false;
    }                
    return super.canWrite();
}
 
Example 12
Source File: FileObj.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isReadOnly() {
    final File f = getFileName().getFile();
    boolean res;
    if (!BaseUtilities.isWindows() && !f.isFile()) {
        res = true;
    } else {
        res = super.isReadOnly();
    }
    markReadOnly(res);
    return res;
}
 
Example 13
Source File: FileObj.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public OutputStream getOutputStream(final FileLock lock) throws IOException {
    ProvidedExtensions extensions = getProvidedExtensions();
    File file = getFileName().getFile();
    if (!BaseUtilities.isWindows() && !file.isFile()) {
        throw new IOException(file.getAbsolutePath());
    }
    return getOutputStream(lock, extensions, this);
}
 
Example 14
Source File: CoreBridge.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Define {@code org.openide.modules.os.*} tokens according to the current platform.
 * @param provides a collection that may be added to
 */
public static void defineOsTokens(Collection<? super String> provides) {
    if (BaseUtilities.isUnix()) {
        provides.add("org.openide.modules.os.Unix"); // NOI18N
        if (!BaseUtilities.isMac()) {
            provides.add("org.openide.modules.os.PlainUnix"); // NOI18N
        }
    }
    if (BaseUtilities.isWindows()) {
        provides.add("org.openide.modules.os.Windows"); // NOI18N
    }
    if (BaseUtilities.isMac()) {
        provides.add("org.openide.modules.os.MacOSX"); // NOI18N
    }
    if ((BaseUtilities.getOperatingSystem() & BaseUtilities.OS_OS2) != 0) {
        provides.add("org.openide.modules.os.OS2"); // NOI18N
    }
    if ((BaseUtilities.getOperatingSystem() & BaseUtilities.OS_LINUX) != 0) {
        provides.add("org.openide.modules.os.Linux"); // NOI18N
    }
    if ((BaseUtilities.getOperatingSystem() & BaseUtilities.OS_SOLARIS) != 0) {
        provides.add("org.openide.modules.os.Solaris"); // NOI18N
    }
    
    if (isJavaFX(new File(System.getProperty("java.home")))) {
        provides.add("org.openide.modules.jre.JavaFX"); // NOI18N
    }
}
 
Example 15
Source File: ExternalProcessBuilder.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the new {@link Process} based on the properties configured
 * in this builder. Created process will try to kill all its children on
 * call to {@link Process#destroy()}.
 * <p>
 * Process is created by executing the executable with configured arguments.
 * If custom working directory is specified it is used otherwise value
 * of system property <code>user.dir</code> is used as working dir.
 * <p>
 * Environment variables are prepared in following way:
 * <ol>
 *   <li>Get table of system environment variables.
 *   <li>Put all environment variables configured by
 * {@link #addEnvironmentVariable(java.lang.String, java.lang.String)}.
 * This rewrites system variables if conflict occurs.
 *   <li>Get <code>PATH</code> variable and append all paths added
 * by {@link #prependPath(java.io.File)}. The order of paths in <code>PATH</code>
 * variable is reversed to order of addition (the last added is the first
 * one in <code>PATH</code>). Original content of <code>PATH</code> follows
 * the added content.
 *   <li>If neither <code>http_proxy</code> nor <code>HTTP_PROXY</code>
 * environment variable is set then HTTP proxy settings configured in the
 * IDE are stored as <code>http_proxy</code> environment variable
 * (the format of the value is <code>http://username:password@host:port</code>).
 * </ol>
 * @return the new {@link Process} based on the properties configured
 *             in this builder
 * @throws IOException if the process could not be created
 */
@NonNull
@Override
public Process call() throws IOException {
    List<String> commandList = new ArrayList<String>();

    if (BaseUtilities.isWindows() && !ESCAPED_PATTERN.matcher(executable).matches()) {
        commandList.add(escapeString(executable));
    } else {
        commandList.add(executable);
    }

    List<String> args = buildArguments();
    commandList.addAll(args);

    java.lang.ProcessBuilder pb = new java.lang.ProcessBuilder(commandList.toArray(new String[commandList.size()]));
    if (workingDirectory != null) {
        pb.directory(workingDirectory);
    }

    Map<String, String> pbEnv = pb.environment();
    Map<String, String> env = buildEnvironment(pbEnv);
    pbEnv.putAll(env);
    String uuid = UUID.randomUUID().toString();
    pbEnv.put(WrapperProcess.KEY_UUID, uuid);
    adjustProxy(pb);
    pb.redirectErrorStream(redirectErrorStream);
    logProcess(Level.FINE, pb);
    WrapperProcess wp = new WrapperProcess(pb.start(), uuid);
    return wp;
}
 
Example 16
Source File: FileObjects.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected PathBase(
        @NonNull final String pkgName,
        @NonNull final String name,
        @NullAllowed final Charset encoding) {
    super(
        pkgName,
        name,
        encoding,
        !BaseUtilities.isWindows());
}
 
Example 17
Source File: Attributes.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String preparePrefix(File fileSystemRoot) {
    fileSystemRoot = FileUtil.normalizeFile(fileSystemRoot);
    String rootPath = fileSystemRoot.getAbsolutePath().replace('\\', '/');
    return ((BaseUtilities.isWindows () || (BaseUtilities.getOperatingSystem () == BaseUtilities.OS_OS2))) ? rootPath.toLowerCase() : rootPath;
}
 
Example 18
Source File: FileUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** Test if given name is free in given folder.
 * @param fo folder to check in
 * @param name name of the file or folder to check
 * @param ext extension of the file (null for folders)
 * @return true, if such name does not exists
 */
private static boolean checkFreeName(FileObject fo, String name, String ext) {
    if ((BaseUtilities.isWindows() || (BaseUtilities.getOperatingSystem() == BaseUtilities.OS_OS2)) || BaseUtilities.isMac()) {
        // case-insensitive, do some special check
        Enumeration<? extends FileObject> en = fo.getChildren(false);

        while (en.hasMoreElements()) {
            fo = en.nextElement();

            String n = fo.getName();
            String e = fo.getExt();

            // different names => check others
            if (!n.equalsIgnoreCase(name)) {
                continue;
            }

            // same name + without extension => no
            if (((ext == null) || (ext.trim().length() == 0)) && ((e == null) || (e.trim().length() == 0))) {
                return fo.isVirtual();
            }

            // one of there is witout extension => check next
            if ((ext == null) || (e == null)) {
                continue;
            }

            if (ext.equalsIgnoreCase(e)) {
                // same name + same extension => no
                return fo.isVirtual();
            }
        }

        // no of the files has similar name and extension
        return true;
    } else {
        if (ext == null) {
            fo = fo.getFileObject(name);

            if (fo == null) {
                return true;
            }

            return fo.isVirtual();
        } else {
            fo = fo.getFileObject(name, ext);

            if (fo == null) {
                return true;
            }

            return fo.isVirtual();
        }
    }
}
 
Example 19
Source File: WebLogicDeployer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private String getJavaBinary() {
    if (javaBinary != null) {
        return javaBinary.getAbsolutePath();
    }
    return BaseUtilities.isWindows() ? "java.exe" : "java"; // NOI18N
}
 
Example 20
Source File: LocalFileSystem.java    From netbeans with Apache License 2.0 3 votes vote down vote up
/** Compute the system name of this filesystem for a given root directory.
* <P>
* The default implementation simply returns the filename separated by slashes.
* @see FileSystem#setSystemName
* @param rootFile root directory for the filesystem
* @return system name for the filesystem
*/
protected String computeSystemName(File rootFile) {
    String retVal = rootFile.getAbsolutePath().replace(File.separatorChar, '/');

    return ((BaseUtilities.isWindows() || (BaseUtilities.getOperatingSystem() == BaseUtilities.OS_OS2))) ? retVal.toLowerCase()
                                                                                             : retVal;
}