KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > OS


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.util.Arrays JavaDoc;
14 import org.eclipse.core.runtime.Platform;
15
16 /**
17  * Captures platform specific attributes relevant to the core resources plugin. This
18  * class is not intended to be instantiated.
19  */

20 public abstract class OS {
21     private static final String JavaDoc INSTALLED_PLATFORM;
22
23     public static final char[] INVALID_RESOURCE_CHARACTERS;
24     public static final String JavaDoc[] INVALID_RESOURCE_NAMES;
25
26     static {
27         //find out the OS being used
28
//setup the invalid names
29
INSTALLED_PLATFORM = Platform.getOS();
30         if (INSTALLED_PLATFORM.equals(Platform.OS_WIN32)) {
31             //valid names and characters taken from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
32
INVALID_RESOURCE_CHARACTERS = new char[] {'\\', '/', ':', '*', '?', '"', '<', '>', '|'};
33             INVALID_RESOURCE_NAMES = new String JavaDoc[] {"aux", "clock$", "com1", "com2", "com3", "com4", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
34
"com5", "com6", "com7", "com8", "com9", "con", "lpt1", "lpt2", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
35
"lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", "nul", "prn"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$ //$NON-NLS-9$
36
Arrays.sort(INVALID_RESOURCE_NAMES);
37         } else {
38             //only front slash and null char are invalid on UNIXes
39
//taken from http://www.faqs.org/faqs/unix-faq/faq/part2/section-2.html
40
INVALID_RESOURCE_CHARACTERS = new char[] {'/', '\0',};
41             INVALID_RESOURCE_NAMES = new String JavaDoc[] {};
42         }
43     }
44
45     /**
46      * Returns true if the given name is a valid resource name on this operating system,
47      * and false otherwise.
48      */

49     public static boolean isNameValid(String JavaDoc name) {
50         //. and .. have special meaning on all platforms
51
if (name.equals(".") || name.equals("..")) //$NON-NLS-1$ //$NON-NLS-2$
52
return false;
53         if (INSTALLED_PLATFORM.equals(Platform.OS_WIN32)) {
54             //empty names are not valid
55
final int length = name.length();
56             if (length == 0)
57                 return false;
58             final char lastChar = name.charAt(length-1);
59             // filenames ending in dot are not valid
60
if (lastChar == '.')
61                 return false;
62             // file names ending with whitespace are truncated (bug 118997)
63
if (Character.isWhitespace(lastChar))
64                 return false;
65             int dot = name.indexOf('.');
66             //on windows, filename suffixes are not relevant to name validity
67
name = dot == -1 ? name : name.substring(0, dot);
68         }
69         return Arrays.binarySearch(INVALID_RESOURCE_NAMES, name.toLowerCase()) < 0;
70     }
71 }
72
Popular Tags