new version with WorkingCopy Management
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / Util.java
index 93c2697..825cd1a 100644 (file)
  *******************************************************************************/
 package net.sourceforge.phpdt.internal.core;
 
+import java.io.BufferedInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Locale;
 import java.util.MissingResourceException;
 import java.util.ResourceBundle;
 import java.util.StringTokenizer;
 
+import net.sourceforge.phpdt.core.IJavaElement;
+import net.sourceforge.phpdt.core.IJavaModelStatusConstants;
+import net.sourceforge.phpdt.core.IPackageFragment;
 import net.sourceforge.phpdt.core.JavaModelException;
+import net.sourceforge.phpdt.core.Signature;
 import net.sourceforge.phpdt.core.compiler.CharOperation;
 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
 import net.sourceforge.phpdt.internal.corext.Assert;
+import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
 import net.sourceforge.phpeclipse.PHPCore;
 
+import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
@@ -65,16 +75,16 @@ public class Util {
 
        /* Bundle containing messages */
        protected static ResourceBundle bundle;
-       private final static String bundleName = "org.eclipse.jdt.internal.core.messages"; //$NON-NLS-1$
+       private final static String bundleName = "net.sourceforge.phpdt.internal.core.messages"; //$NON-NLS-1$
 
-       public final static char[] SUFFIX_class = ".class".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_CLASS = ".CLASS".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_java = ".java".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_JAVA = ".JAVA".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_jar = ".jar".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_JAR = ".JAR".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_zip = ".zip".toCharArray(); //$NON-NLS-1$
-       public final static char[] SUFFIX_ZIP = ".ZIP".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_class = ".class".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_CLASS = ".CLASS".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_java = ".java".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_JAVA = ".JAVA".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_jar = ".jar".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_JAR = ".JAR".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_zip = ".zip".toCharArray(); //$NON-NLS-1$
+//     public final static char[] SUFFIX_ZIP = ".ZIP".toCharArray(); //$NON-NLS-1$
 
        static {
                String ver = System.getProperty("java.version"); //$NON-NLS-1$
@@ -583,104 +593,106 @@ public class Util {
                return count;
        }
        
-//     /**
-//      * Returns the given file's contents as a byte array.
-//      */
-//     public static byte[] getResourceContentsAsByteArray(IFile file) throws JavaModelException {
-//             InputStream stream= null;
-//             try {
-//                     stream = new BufferedInputStream(file.getContents(true));
-//             } catch (CoreException e) {
-//                     throw new JavaModelException(e);
-//             }
-//             try {
-//                     return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, -1);
-//             } catch (IOException e) {
-//                     throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
-//             } finally {
-//                     try {
-//                             stream.close();
-//                     } catch (IOException e) {
-//                     }
-//             }
-//     }
-//     
-//     /**
-//      * Returns the given file's contents as a character array.
-//      */
-//     public static char[] getResourceContentsAsCharArray(IFile file) throws JavaModelException {
-//             String encoding = JavaCore.create(file.getProject()).getOption(JavaCore.CORE_ENCODING, true);
-//             return getResourceContentsAsCharArray(file, encoding);
-//     }
-//
-//     public static char[] getResourceContentsAsCharArray(IFile file, String encoding) throws JavaModelException {
-//             InputStream stream= null;
-//             try {
-//                     stream = new BufferedInputStream(file.getContents(true));
-//             } catch (CoreException e) {
-//                     throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
-//             }
-//             try {
-//                     return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, -1, encoding);
-//             } catch (IOException e) {
-//                     throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
-//             } finally {
-//                     try {
-//                             stream.close();
-//                     } catch (IOException e) {
-//                     }
-//             }
-//     }
-//     
-//     /**
-//      * Returns a trimmed version the simples names returned by Signature.
-//      */
-//     public static String[] getTrimmedSimpleNames(String name) {
-//             String[] result = Signature.getSimpleNames(name);
-//             if (result == null) return null;
-//             for (int i = 0, length = result.length; i < length; i++) {
-//                     result[i] = result[i].trim();
-//             }
-//             return result;
-//     }
+       /**
+        * Returns the given file's contents as a byte array.
+        */
+       public static byte[] getResourceContentsAsByteArray(IFile file) throws JavaModelException {
+               InputStream stream= null;
+               try {
+                       stream = new BufferedInputStream(file.getContents(true));
+               } catch (CoreException e) {
+                       throw new JavaModelException(e);
+               }
+               try {
+                       return net.sourceforge.phpdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, -1);
+               } catch (IOException e) {
+                       throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
+               } finally {
+                       try {
+                               stream.close();
+                       } catch (IOException e) {
+                       }
+               }
+       }
        
        /**
-        * Returns true iff str.toLowerCase().endsWith(".class")
-        * implementation is not creating extra strings.
+        * Returns the given file's contents as a character array.
         */
-       public final static boolean isClassFileName(String name) {
-               int nameLength = name == null ? 0 : name.length();
-               int suffixLength = SUFFIX_CLASS.length;
-               if (nameLength < suffixLength) return false;
+       public static char[] getResourceContentsAsCharArray(IFile file) throws JavaModelException {
+               String encoding = PHPCore.create(file.getProject()).getOption(PHPCore.CORE_ENCODING, true);
+               return getResourceContentsAsCharArray(file, encoding);
+       }
 
-               for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
-                       char c = name.charAt(offset + i);
-                       if (c != SUFFIX_class[i] && c != SUFFIX_CLASS[i]) return false;
+       public static char[] getResourceContentsAsCharArray(IFile file, String encoding) throws JavaModelException {
+               InputStream stream= null;
+               try {
+                       stream = new BufferedInputStream(file.getContents(true));
+               } catch (CoreException e) {
+                       throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
+               }
+               try {
+                       return net.sourceforge.phpdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, -1, encoding);
+               } catch (IOException e) {
+                       throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
+               } finally {
+                       try {
+                               stream.close();
+                       } catch (IOException e) {
+                       }
                }
-               return true;            
        }
        
+       /**
+        * Returns a trimmed version the simples names returned by Signature.
+        */
+       public static String[] getTrimmedSimpleNames(String name) {
+               String[] result = Signature.getSimpleNames(name);
+               if (result == null) return null;
+               for (int i = 0, length = result.length; i < length; i++) {
+                       result[i] = result[i].trim();
+               }
+               return result;
+       }
+       
+       /**
+        * Returns true iff str.toLowerCase().endsWith(".class")
+        * implementation is not creating extra strings.
+        */
+//     public final static boolean isClassFileName(String name) {
+//             int nameLength = name == null ? 0 : name.length();
+//             int suffixLength = SUFFIX_CLASS.length;
+//             if (nameLength < suffixLength) return false;
+//
+//             for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
+//                     char c = name.charAt(offset + i);
+//                     if (c != SUFFIX_class[i] && c != SUFFIX_CLASS[i]) return false;
+//             }
+//             return true;            
+//     }
+       
        /*
         * Returns whether the given java element is exluded from its root's classpath.
         */
-//     public static final boolean isExcluded(IJavaElement element) {
-//             int elementType = element.getElementType();
-//             switch (elementType) {
+       public static final boolean isExcluded(IJavaElement element) {
+               int elementType = element.getElementType();
+       PackageFragmentRoot root = null;
+       IResource resource = null;
+               switch (elementType) {
 //                     case IJavaElement.PACKAGE_FRAGMENT:
 //                             PackageFragmentRoot root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
 //                             IResource resource = element.getResource();
 //                             return resource != null && Util.isExcluded(resource, root.fullExclusionPatternChars());
-//                     case IJavaElement.COMPILATION_UNIT:
-//                             root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
-//                             resource = element.getResource();
-//                             if (resource != null && Util.isExcluded(resource, root.fullExclusionPatternChars()))
-//                                     return true;
-//                             return isExcluded(element.getParent());
-//                     default:
-//                             IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
-//                             return cu != null && isExcluded(cu);
-//             }
-//     }
+                       case IJavaElement.COMPILATION_UNIT:
+                               root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
+                               resource = element.getResource();
+                               if (resource != null && Util.isExcluded(resource, root.fullExclusionPatternChars()))
+                                       return true;
+                               return isExcluded(element.getParent());
+                       default:
+                               IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
+                               return cu != null && isExcluded(cu);
+               }
+       }
        /*
         * Returns whether the given resource path matches one of the exclusion
         * patterns.
@@ -713,23 +725,23 @@ public class Util {
         * Returns true iff str.toLowerCase().endsWith(".jar" or ".zip")
         * implementation is not creating extra strings.
         */
-       public final static boolean isArchiveFileName(String name) {
-               int nameLength = name == null ? 0 : name.length();
-               int suffixLength = SUFFIX_JAR.length;
-               if (nameLength < suffixLength) return false;
-
-               int i, offset;
-               for ( i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
-                       char c = name.charAt(offset + i);
-                       if (c != SUFFIX_jar[i] && c != SUFFIX_JAR[i]) break;
-               }
-               if (i == suffixLength) return true;             
-               for ( i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
-                       char c = name.charAt(offset + i);
-                       if (c != SUFFIX_zip[i] && c != SUFFIX_ZIP[i]) return false;
-               }
-               return true;
-       }
+//     public final static boolean isArchiveFileName(String name) {
+//             int nameLength = name == null ? 0 : name.length();
+//             int suffixLength = SUFFIX_JAR.length;
+//             if (nameLength < suffixLength) return false;
+//
+//             int i, offset;
+//             for ( i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
+//                     char c = name.charAt(offset + i);
+//                     if (c != SUFFIX_jar[i] && c != SUFFIX_JAR[i]) break;
+//             }
+//             if (i == suffixLength) return true;             
+//             for ( i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
+//                     char c = name.charAt(offset + i);
+//                     if (c != SUFFIX_zip[i] && c != SUFFIX_ZIP[i]) return false;
+//             }
+//             return true;
+//     }
        
        /**
         * Validate the given compilation unit name.
@@ -745,9 +757,10 @@ public class Util {
         *              the given name is valid as a compilation unit name, otherwise a status 
         *              object indicating what is wrong with the name
         */
-//     public static boolean isValidCompilationUnitName(String name) {
+       public static boolean isValidCompilationUnitName(String name) {
+               return PHPFileUtil.isPHPFileName(name);
 //             return JavaConventions.validateCompilationUnitName(name).getSeverity() != IStatus.ERROR;
-//     }
+       }
 
        /**
         * Validate the given .class file name.
@@ -772,15 +785,16 @@ public class Util {
         * implementation is not creating extra strings.
         */
        public final static boolean isJavaFileName(String name) {
-               int nameLength = name == null ? 0 : name.length();
-               int suffixLength = SUFFIX_JAVA.length;
-               if (nameLength < suffixLength) return false;
-
-               for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
-                       char c = name.charAt(offset + i);
-                       if (c != SUFFIX_java[i] && c != SUFFIX_JAVA[i]) return false;
-               }
-               return true;            
+               return PHPFileUtil.isPHPFileName(name);
+//             int nameLength = name == null ? 0 : name.length();
+//             int suffixLength = SUFFIX_JAVA.length;
+//             if (nameLength < suffixLength) return false;
+//
+//             for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
+//                     char c = name.charAt(offset + i);
+//                     if (c != SUFFIX_java[i] && c != SUFFIX_JAVA[i]) return false;
+//             }
+//             return true;            
        }
 
        /**
@@ -818,9 +832,10 @@ public class Util {
         * Returns true if the given folder name is valid for a package,
         * false if it is not.
         */
-//     public static boolean isValidFolderNameForPackage(String folderName) {
+       public static boolean isValidFolderNameForPackage(String folderName) {
 //             return JavaConventions.validateIdentifier(folderName).getSeverity() != IStatus.ERROR;
-//     }       
+         return true;
+       }       
 
        /*
         * Add a log entry
@@ -1006,20 +1021,20 @@ public class Util {
         * Converts the given relative path into a package name.
         * Returns null if the path is not a valid package name.
         */
-//     public static String packageName(IPath pkgPath) {
-//             StringBuffer pkgName = new StringBuffer(IPackageFragment.DEFAULT_PACKAGE_NAME);
-//             for (int j = 0, max = pkgPath.segmentCount(); j < max; j++) {
-//                     String segment = pkgPath.segment(j);
-//                     if (!isValidFolderNameForPackage(segment)) {
-//                             return null;
-//                     }
-//                     pkgName.append(segment);
-//                     if (j < pkgPath.segmentCount() - 1) {
-//                             pkgName.append("." ); //$NON-NLS-1$
-//                     }
-//             }
-//             return pkgName.toString();
-//     }
+       public static String packageName(IPath pkgPath) {
+               StringBuffer pkgName = new StringBuffer(IPackageFragment.DEFAULT_PACKAGE_NAME);
+               for (int j = 0, max = pkgPath.segmentCount(); j < max; j++) {
+                       String segment = pkgPath.segment(j);
+                       if (!isValidFolderNameForPackage(segment)) {
+                               return null;
+                       }
+                       pkgName.append(segment);
+                       if (j < pkgPath.segmentCount() - 1) {
+                               pkgName.append("." ); //$NON-NLS-1$
+                       }
+               }
+               return pkgName.toString();
+       }
 
        /**
         * Sort the comparable objects in the given collection.