1 /*******************************************************************************
2 * Copyright (c) 2000, 2003 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import java.util.Locale;
14 import java.util.MissingResourceException;
15 import java.util.ResourceBundle;
16 import java.util.StringTokenizer;
18 import net.sourceforge.phpdt.core.JavaModelException;
19 import net.sourceforge.phpdt.core.compiler.CharOperation;
20 import net.sourceforge.phpdt.internal.core.util.CharArrayBuffer;
21 import net.sourceforge.phpdt.internal.corext.Assert;
22 import net.sourceforge.phpeclipse.PHPCore;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
30 * Provides convenient utility methods to other types in this package.
34 private final static char[] DOUBLE_QUOTES = "''".toCharArray(); //$NON-NLS-1$
35 private final static char[] SINGLE_QUOTE = "'".toCharArray(); //$NON-NLS-1$
36 private static final String ARGUMENTS_DELIMITER = "#"; //$NON-NLS-1$
37 private static final String EMPTY_ARGUMENT = " "; //$NON-NLS-1$
39 public interface Comparable {
41 * Returns 0 if this and c are equal, >0 if this is greater than c,
42 * or <0 if this is less than c.
44 int compareTo(Comparable c);
47 public interface Comparer {
49 * Returns 0 if a and b are equal, >0 if a is greater than b,
50 * or <0 if a is less than b.
52 int compare(Object a, Object b);
55 public interface Displayable {
56 String displayString(Object o);
59 public static final String[] fgEmptyStringArray = new String[0];
62 * Are we running JDK 1.1?
64 private static boolean JDK1_1 = false;
66 /* Bundle containing messages */
67 protected static ResourceBundle bundle;
68 private final static String bundleName = "org.eclipse.jdt.internal.core.messages"; //$NON-NLS-1$
70 public final static char[] SUFFIX_class = ".class".toCharArray(); //$NON-NLS-1$
71 public final static char[] SUFFIX_CLASS = ".CLASS".toCharArray(); //$NON-NLS-1$
72 public final static char[] SUFFIX_java = ".java".toCharArray(); //$NON-NLS-1$
73 public final static char[] SUFFIX_JAVA = ".JAVA".toCharArray(); //$NON-NLS-1$
74 public final static char[] SUFFIX_jar = ".jar".toCharArray(); //$NON-NLS-1$
75 public final static char[] SUFFIX_JAR = ".JAR".toCharArray(); //$NON-NLS-1$
76 public final static char[] SUFFIX_zip = ".zip".toCharArray(); //$NON-NLS-1$
77 public final static char[] SUFFIX_ZIP = ".ZIP".toCharArray(); //$NON-NLS-1$
80 String ver = System.getProperty("java.version"); //$NON-NLS-1$
81 JDK1_1 = ((ver != null) && ver.startsWith("1.1")); //$NON-NLS-1$
86 * Lookup the message with the given ID in this catalog
88 public static String bind(String id) {
89 return bind(id, (String[])null);
93 * Lookup the message with the given ID in this catalog and bind its
94 * substitution locations with the given string values.
96 public static String bind(String id, String[] bindings) {
98 return "No message available"; //$NON-NLS-1$
99 String message = null;
101 message = bundle.getString(id);
102 } catch (MissingResourceException e) {
103 // If we got an exception looking for the message, fail gracefully by just returning
104 // the id we were looking for. In most cases this is semi-informative so is not too bad.
105 return "Missing message: " + id + " in: " + bundleName; //$NON-NLS-2$ //$NON-NLS-1$
107 // for compatibility with MessageFormat which eliminates double quotes in original message
108 char[] messageWithNoDoubleQuotes =
109 CharOperation.replace(message.toCharArray(), DOUBLE_QUOTES, SINGLE_QUOTE);
110 message = new String(messageWithNoDoubleQuotes);
112 if (bindings == null)
115 int length = message.length();
118 StringBuffer output = new StringBuffer(80);
120 if ((end = message.indexOf('{', start)) > -1) {
121 output.append(message.substring(start + 1, end));
122 if ((start = message.indexOf('}', end)) > -1) {
125 index = Integer.parseInt(message.substring(end + 1, start));
126 output.append(bindings[index]);
127 } catch (NumberFormatException nfe) {
128 output.append(message.substring(end + 1, start + 1));
129 } catch (ArrayIndexOutOfBoundsException e) {
130 output.append("{missing " + Integer.toString(index) + "}"); //$NON-NLS-2$ //$NON-NLS-1$
133 output.append(message.substring(end, length));
137 output.append(message.substring(start + 1, length));
141 return output.toString();
145 * Lookup the message with the given ID in this catalog and bind its
146 * substitution locations with the given string.
148 public static String bind(String id, String binding) {
149 return bind(id, new String[] {binding});
153 * Lookup the message with the given ID in this catalog and bind its
154 * substitution locations with the given strings.
156 public static String bind(String id, String binding1, String binding2) {
157 return bind(id, new String[] {binding1, binding2});
161 * Checks the type signature in String sig,
162 * starting at start and ending before end (end is not included).
163 * Returns the index of the character immediately after the signature if valid,
164 * or -1 if not valid.
166 private static int checkTypeSignature(String sig, int start, int end, boolean allowVoid) {
167 if (start >= end) return -1;
169 char c = sig.charAt(i++);
170 int nestingDepth = 0;
173 if (i >= end) return -1;
187 if (!allowVoid) return -1;
188 // array of void is not allowed
189 if (nestingDepth != 0) return -1;
192 int semicolon = sig.indexOf(';', i);
193 // Must have at least one character between L and ;
194 if (semicolon <= i || semicolon >= end) return -1;
204 * Combines two hash codes to make a new one.
206 public static int combineHashCodes(int hashCode1, int hashCode2) {
207 return hashCode1 * 17 + hashCode2;
211 * Compares two byte arrays.
212 * Returns <0 if a byte in a is less than the corresponding byte in b, or if a is shorter, or if a is null.
213 * Returns >0 if a byte in a is greater than the corresponding byte in b, or if a is longer, or if b is null.
214 * Returns 0 if they are equal or both null.
216 public static int compare(byte[] a, byte[] b) {
223 int len = Math.min(a.length, b.length);
224 for (int i = 0; i < len; ++i) {
225 int diff = a[i] - b[i];
237 * Compares two char arrays lexicographically.
238 * The comparison is based on the Unicode value of each character in
240 * @return the value <code>0</code> if a is equal to
241 * b; a value less than <code>0</code> if a
242 * is lexicographically less than b; and a
243 * value greater than <code>0</code> if a is
244 * lexicographically greater than b.
246 public static int compare(char[] v1, char[] v2) {
247 int len1 = v1.length;
248 int len2 = v2.length;
249 int n = Math.min(len1, len2);
252 if (v1[i] != v2[i]) {
253 return v1[i] - v2[i];
261 * Concatenate two strings with a char in between.
262 * @see #concat(String, String)
264 public static String concat(String s1, char c, String s2) {
265 if (s1 == null) s1 = "null"; //$NON-NLS-1$
266 if (s2 == null) s2 = "null"; //$NON-NLS-1$
267 int l1 = s1.length();
268 int l2 = s2.length();
269 char[] buf = new char[l1 + 1 + l2];
270 s1.getChars(0, l1, buf, 0);
272 s2.getChars(0, l2, buf, l1 + 1);
273 return new String(buf);
277 * Concatenate two strings.
278 * Much faster than using +, which:
279 * - creates a StringBuffer,
280 * - which is synchronized,
281 * - of default size, so the resulting char array is
282 * often larger than needed.
283 * This implementation creates an extra char array, since the
284 * String constructor copies its argument, but there's no way around this.
286 public static String concat(String s1, String s2) {
287 if (s1 == null) s1 = "null"; //$NON-NLS-1$
288 if (s2 == null) s2 = "null"; //$NON-NLS-1$
289 int l1 = s1.length();
290 int l2 = s2.length();
291 char[] buf = new char[l1 + l2];
292 s1.getChars(0, l1, buf, 0);
293 s2.getChars(0, l2, buf, l1);
294 return new String(buf);
298 * Concatenate three strings.
299 * @see #concat(String, String)
301 public static String concat(String s1, String s2, String s3) {
302 if (s1 == null) s1 = "null"; //$NON-NLS-1$
303 if (s2 == null) s2 = "null"; //$NON-NLS-1$
304 if (s3 == null) s3 = "null"; //$NON-NLS-1$
305 int l1 = s1.length();
306 int l2 = s2.length();
307 int l3 = s3.length();
308 char[] buf = new char[l1 + l2 + l3];
309 s1.getChars(0, l1, buf, 0);
310 s2.getChars(0, l2, buf, l1);
311 s3.getChars(0, l3, buf, l1 + l2);
312 return new String(buf);
316 * Converts a type signature from the IBinaryType representation to the DC representation.
318 public static String convertTypeSignature(char[] sig) {
319 return new String(sig).replace('/', '.');
323 * Returns true iff str.toLowerCase().endsWith(end.toLowerCase())
324 * implementation is not creating extra strings.
326 public final static boolean endsWithIgnoreCase(String str, String end) {
328 int strLength = str == null ? 0 : str.length();
329 int endLength = end == null ? 0 : end.length();
331 // return false if the string is smaller than the end.
332 if(endLength > strLength)
335 // return false if any character of the end are
336 // not the same in lower case.
337 for(int i = 1 ; i <= endLength; i++){
338 if(Character.toLowerCase(end.charAt(endLength - i)) != Character.toLowerCase(str.charAt(strLength - i)))
346 * Compares two arrays using equals() on the elements.
347 * Either or both arrays may be null.
348 * Returns true if both are null.
349 * Returns false if only one is null.
350 * If both are arrays, returns true iff they have the same length and
351 * all elements are equal.
353 public static boolean equalArraysOrNull(int[] a, int[] b) {
356 if (a == null || b == null)
361 for (int i = 0; i < len; ++i) {
369 * Compares two arrays using equals() on the elements.
370 * Either or both arrays may be null.
371 * Returns true if both are null.
372 * Returns false if only one is null.
373 * If both are arrays, returns true iff they have the same length and
374 * all elements compare true with equals.
376 public static boolean equalArraysOrNull(Object[] a, Object[] b) {
377 if (a == b) return true;
378 if (a == null || b == null) return false;
381 if (len != b.length) return false;
382 for (int i = 0; i < len; ++i) {
384 if (b[i] != null) return false;
386 if (!a[i].equals(b[i])) return false;
393 * Compares two String arrays using equals() on the elements.
394 * The arrays are first sorted.
395 * Either or both arrays may be null.
396 * Returns true if both are null.
397 * Returns false if only one is null.
398 * If both are arrays, returns true iff they have the same length and
399 * iff, after sorting both arrays, all elements compare true with equals.
400 * The original arrays are left untouched.
402 public static boolean equalArraysOrNullSortFirst(String[] a, String[] b) {
403 if (a == b) return true;
404 if (a == null || b == null) return false;
406 if (len != b.length) return false;
407 if (len >= 2) { // only need to sort if more than two items
411 for (int i = 0; i < len; ++i) {
412 if (!a[i].equals(b[i])) return false;
418 * Compares two arrays using equals() on the elements.
419 * The arrays are first sorted.
420 * Either or both arrays may be null.
421 * Returns true if both are null.
422 * Returns false if only one is null.
423 * If both are arrays, returns true iff they have the same length and
424 * iff, after sorting both arrays, all elements compare true with equals.
425 * The original arrays are left untouched.
427 public static boolean equalArraysOrNullSortFirst(Comparable[] a, Comparable[] b) {
428 if (a == b) return true;
429 if (a == null || b == null) return false;
431 if (len != b.length) return false;
432 if (len >= 2) { // only need to sort if more than two items
436 for (int i = 0; i < len; ++i) {
437 if (!a[i].equals(b[i])) return false;
443 * Compares two objects using equals().
444 * Either or both array may be null.
445 * Returns true if both are null.
446 * Returns false if only one is null.
447 * Otherwise, return the result of comparing with equals().
449 public static boolean equalOrNull(Object a, Object b) {
453 if (a == null || b == null) {
460 * Given a qualified name, extract the last component.
461 * If the input is not qualified, the same string is answered.
463 public static String extractLastName(String qualifiedName) {
464 int i = qualifiedName.lastIndexOf('.');
465 if (i == -1) return qualifiedName;
466 return qualifiedName.substring(i+1);
470 * Extracts the parameter types from a method signature.
472 public static String[] extractParameterTypes(char[] sig) {
473 int count = getParameterCount(sig);
474 String[] result = new String[count];
477 int i = CharOperation.indexOf('(', sig) + 1;
479 int len = sig.length;
491 i = CharOperation.indexOf(';', sig, i + 1) + 1;
492 Assert.isTrue(i != 0);
493 result[count++] = convertTypeSignature(CharOperation.subarray(sig, start, i));
497 result[count++] = convertTypeSignature(CharOperation.subarray(sig, start, i));
505 * Extracts the return type from a method signature.
507 public static String extractReturnType(String sig) {
508 int i = sig.lastIndexOf(')');
509 Assert.isTrue(i != -1);
510 return sig.substring(i+1);
514 * Finds the first line separator used by the given text.
516 * @return </code>"\n"</code> or </code>"\r"</code> or </code>"\r\n"</code>,
517 * or <code>null</code> if none found
519 public static String findLineSeparator(char[] text) {
520 // find the first line separator
521 int length = text.length;
523 char nextChar = text[0];
524 for (int i = 0; i < length; i++) {
525 char currentChar = nextChar;
526 nextChar = i < length-1 ? text[i+1] : ' ';
527 switch (currentChar) {
528 case '\n': return "\n"; //$NON-NLS-1$
529 case '\r': return nextChar == '\n' ? "\r\n" : "\r"; //$NON-NLS-1$ //$NON-NLS-2$
538 * Returns the line separator used by the given buffer.
539 * Uses the given text if none found.
541 * @return </code>"\n"</code> or </code>"\r"</code> or </code>"\r\n"</code>
543 private static String getLineSeparator(char[] text, char[] buffer) {
544 // search in this buffer's contents first
545 String lineSeparator = findLineSeparator(buffer);
546 if (lineSeparator == null) {
547 // search in the given text
548 lineSeparator = findLineSeparator(text);
549 if (lineSeparator == null) {
550 // default to system line separator
551 return System.getProperty("line.separator");
554 return lineSeparator;
558 * Returns the number of parameter types in a method signature.
560 public static int getParameterCount(char[] sig) {
561 int i = CharOperation.indexOf('(', sig) + 1;
562 Assert.isTrue(i != 0);
564 int len = sig.length;
576 i = CharOperation.indexOf(';', sig, i + 1) + 1;
577 Assert.isTrue(i != 0);
587 // * Returns the given file's contents as a byte array.
589 // public static byte[] getResourceContentsAsByteArray(IFile file) throws JavaModelException {
590 // InputStream stream= null;
592 // stream = new BufferedInputStream(file.getContents(true));
593 // } catch (CoreException e) {
594 // throw new JavaModelException(e);
597 // return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsByteArray(stream, -1);
598 // } catch (IOException e) {
599 // throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
603 // } catch (IOException e) {
609 // * Returns the given file's contents as a character array.
611 // public static char[] getResourceContentsAsCharArray(IFile file) throws JavaModelException {
612 // String encoding = JavaCore.create(file.getProject()).getOption(JavaCore.CORE_ENCODING, true);
613 // return getResourceContentsAsCharArray(file, encoding);
616 // public static char[] getResourceContentsAsCharArray(IFile file, String encoding) throws JavaModelException {
617 // InputStream stream= null;
619 // stream = new BufferedInputStream(file.getContents(true));
620 // } catch (CoreException e) {
621 // throw new JavaModelException(e, IJavaModelStatusConstants.ELEMENT_DOES_NOT_EXIST);
624 // return org.eclipse.jdt.internal.compiler.util.Util.getInputStreamAsCharArray(stream, -1, encoding);
625 // } catch (IOException e) {
626 // throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
630 // } catch (IOException e) {
636 // * Returns a trimmed version the simples names returned by Signature.
638 // public static String[] getTrimmedSimpleNames(String name) {
639 // String[] result = Signature.getSimpleNames(name);
640 // if (result == null) return null;
641 // for (int i = 0, length = result.length; i < length; i++) {
642 // result[i] = result[i].trim();
648 * Returns true iff str.toLowerCase().endsWith(".class")
649 * implementation is not creating extra strings.
651 public final static boolean isClassFileName(String name) {
652 int nameLength = name == null ? 0 : name.length();
653 int suffixLength = SUFFIX_CLASS.length;
654 if (nameLength < suffixLength) return false;
656 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
657 char c = name.charAt(offset + i);
658 if (c != SUFFIX_class[i] && c != SUFFIX_CLASS[i]) return false;
664 * Returns whether the given java element is exluded from its root's classpath.
666 // public static final boolean isExcluded(IJavaElement element) {
667 // int elementType = element.getElementType();
668 // switch (elementType) {
669 // case IJavaElement.PACKAGE_FRAGMENT:
670 // PackageFragmentRoot root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
671 // IResource resource = element.getResource();
672 // return resource != null && Util.isExcluded(resource, root.fullExclusionPatternChars());
673 // case IJavaElement.COMPILATION_UNIT:
674 // root = (PackageFragmentRoot)element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
675 // resource = element.getResource();
676 // if (resource != null && Util.isExcluded(resource, root.fullExclusionPatternChars()))
678 // return isExcluded(element.getParent());
680 // IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
681 // return cu != null && isExcluded(cu);
685 * Returns whether the given resource path matches one of the exclusion
688 * @see IClasspathEntry#getExclusionPatterns
690 public final static boolean isExcluded(IPath resourcePath, char[][] exclusionPatterns) {
691 if (exclusionPatterns == null) return false;
692 char[] path = resourcePath.toString().toCharArray();
693 for (int i = 0, length = exclusionPatterns.length; i < length; i++)
694 if (CharOperation.pathMatch(exclusionPatterns[i], path, true, '/'))
700 * Returns whether the given resource matches one of the exclusion patterns.
702 * @see IClasspathEntry#getExclusionPatterns
704 public final static boolean isExcluded(IResource resource, char[][] exclusionPatterns) {
705 IPath path = resource.getFullPath();
706 // ensure that folders are only excluded if all of their children are excluded
707 if (resource.getType() == IResource.FOLDER)
708 path = path.append("*"); //$NON-NLS-1$
709 return isExcluded(path, exclusionPatterns);
713 * Returns true iff str.toLowerCase().endsWith(".jar" or ".zip")
714 * implementation is not creating extra strings.
716 public final static boolean isArchiveFileName(String name) {
717 int nameLength = name == null ? 0 : name.length();
718 int suffixLength = SUFFIX_JAR.length;
719 if (nameLength < suffixLength) return false;
722 for ( i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
723 char c = name.charAt(offset + i);
724 if (c != SUFFIX_jar[i] && c != SUFFIX_JAR[i]) break;
726 if (i == suffixLength) return true;
727 for ( i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
728 char c = name.charAt(offset + i);
729 if (c != SUFFIX_zip[i] && c != SUFFIX_ZIP[i]) return false;
735 * Validate the given compilation unit name.
736 * A compilation unit name must obey the following rules:
738 * <li> it must not be null
739 * <li> it must include the <code>".java"</code> suffix
740 * <li> its prefix must be a valid identifier
743 * @param name the name of a compilation unit
744 * @return a status object with code <code>IStatus.OK</code> if
745 * the given name is valid as a compilation unit name, otherwise a status
746 * object indicating what is wrong with the name
748 // public static boolean isValidCompilationUnitName(String name) {
749 // return JavaConventions.validateCompilationUnitName(name).getSeverity() != IStatus.ERROR;
753 * Validate the given .class file name.
754 * A .class file name must obey the following rules:
756 * <li> it must not be null
757 * <li> it must include the <code>".class"</code> suffix
758 * <li> its prefix must be a valid identifier
761 * @param name the name of a .class file
762 * @return a status object with code <code>IStatus.OK</code> if
763 * the given name is valid as a .class file name, otherwise a status
764 * object indicating what is wrong with the name
766 // public static boolean isValidClassFileName(String name) {
767 // return JavaConventions.validateClassFileName(name).getSeverity() != IStatus.ERROR;
771 * Returns true iff str.toLowerCase().endsWith(".java")
772 * implementation is not creating extra strings.
774 public final static boolean isJavaFileName(String name) {
775 int nameLength = name == null ? 0 : name.length();
776 int suffixLength = SUFFIX_JAVA.length;
777 if (nameLength < suffixLength) return false;
779 for (int i = 0, offset = nameLength - suffixLength; i < suffixLength; i++) {
780 char c = name.charAt(offset + i);
781 if (c != SUFFIX_java[i] && c != SUFFIX_JAVA[i]) return false;
787 * Returns true if the given method signature is valid,
788 * false if it is not.
790 public static boolean isValidMethodSignature(String sig) {
791 int len = sig.length();
792 if (len == 0) return false;
794 char c = sig.charAt(i++);
795 if (c != '(') return false;
796 if (i >= len) return false;
797 while (sig.charAt(i) != ')') {
798 // Void is not allowed as a parameter type.
799 i = checkTypeSignature(sig, i, len, false);
800 if (i == -1) return false;
801 if (i >= len) return false;
804 i = checkTypeSignature(sig, i, len, true);
809 * Returns true if the given type signature is valid,
810 * false if it is not.
812 public static boolean isValidTypeSignature(String sig, boolean allowVoid) {
813 int len = sig.length();
814 return checkTypeSignature(sig, 0, len, allowVoid) == len;
818 * Returns true if the given folder name is valid for a package,
819 * false if it is not.
821 // public static boolean isValidFolderNameForPackage(String folderName) {
822 // return JavaConventions.validateIdentifier(folderName).getSeverity() != IStatus.ERROR;
828 public static void log(Throwable e, String message) {
829 Throwable nestedException;
830 if (e instanceof JavaModelException
831 && (nestedException = ((JavaModelException)e).getException()) != null) {
834 IStatus status= new Status(
836 PHPCore.getPlugin().getDescriptor().getUniqueIdentifier(),
840 PHPCore.getPlugin().getLog().log(status);
844 * Normalizes the cariage returns in the given text.
845 * They are all changed to use the given buffer's line separator.
847 public static char[] normalizeCRs(char[] text, char[] buffer) {
848 CharArrayBuffer result = new CharArrayBuffer();
850 int length = text.length;
851 if (length == 0) return text;
852 String lineSeparator = getLineSeparator(text, buffer);
853 char nextChar = text[0];
854 for (int i = 0; i < length; i++) {
855 char currentChar = nextChar;
856 nextChar = i < length-1 ? text[i+1] : ' ';
857 switch (currentChar) {
859 int lineLength = i-lineStart;
860 char[] line = new char[lineLength];
861 System.arraycopy(text, lineStart, line, 0, lineLength);
863 result.append(lineSeparator);
867 lineLength = i-lineStart;
868 if (lineLength >= 0) {
869 line = new char[lineLength];
870 System.arraycopy(text, lineStart, line, 0, lineLength);
872 result.append(lineSeparator);
873 if (nextChar == '\n') {
877 // when line separator are mixed in the same file
878 // \r might not be followed by a \n. If not, we should increment
879 // lineStart by one and not by two.
883 // when line separator are mixed in the same file
884 // we need to prevent NegativeArraySizeException
892 int lastLineLength = length-lineStart;
893 if (lastLineLength > 0) {
894 lastLine = new char[lastLineLength];
895 System.arraycopy(text, lineStart, lastLine, 0, lastLineLength);
896 result.append(lastLine);
898 return result.getContents();
905 * Normalizes the cariage returns in the given text.
906 * They are all changed to use given buffer's line sepatator.
908 public static String normalizeCRs(String text, String buffer) {
909 return new String(normalizeCRs(text.toCharArray(), buffer.toCharArray()));
913 * Sort the objects in the given collection using the given sort order.
915 private static void quickSort(Object[] sortedCollection, int left, int right, int[] sortOrder) {
916 int original_left = left;
917 int original_right = right;
918 int mid = sortOrder[ (left + right) / 2];
920 while (sortOrder[left] < mid) {
923 while (mid < sortOrder[right]) {
927 Object tmp = sortedCollection[left];
928 sortedCollection[left] = sortedCollection[right];
929 sortedCollection[right] = tmp;
930 int tmp2 = sortOrder[left];
931 sortOrder[left] = sortOrder[right];
932 sortOrder[right] = tmp2;
936 } while (left <= right);
937 if (original_left < right) {
938 quickSort(sortedCollection, original_left, right, sortOrder);
940 if (left < original_right) {
941 quickSort(sortedCollection, left, original_right, sortOrder);
946 * Sort the objects in the given collection using the given comparer.
948 private static void quickSort(Object[] sortedCollection, int left, int right, Comparer comparer) {
949 int original_left = left;
950 int original_right = right;
951 Object mid = sortedCollection[ (left + right) / 2];
953 while (comparer.compare(sortedCollection[left], mid) < 0) {
956 while (comparer.compare(mid, sortedCollection[right]) < 0) {
960 Object tmp = sortedCollection[left];
961 sortedCollection[left] = sortedCollection[right];
962 sortedCollection[right] = tmp;
966 } while (left <= right);
967 if (original_left < right) {
968 quickSort(sortedCollection, original_left, right, comparer);
970 if (left < original_right) {
971 quickSort(sortedCollection, left, original_right, comparer);
976 * Sort the strings in the given collection.
978 private static void quickSort(String[] sortedCollection, int left, int right) {
979 int original_left = left;
980 int original_right = right;
981 String mid = sortedCollection[ (left + right) / 2];
983 while (sortedCollection[left].compareTo(mid) < 0) {
986 while (mid.compareTo(sortedCollection[right]) < 0) {
990 String tmp = sortedCollection[left];
991 sortedCollection[left] = sortedCollection[right];
992 sortedCollection[right] = tmp;
996 } while (left <= right);
997 if (original_left < right) {
998 quickSort(sortedCollection, original_left, right);
1000 if (left < original_right) {
1001 quickSort(sortedCollection, left, original_right);
1006 * Converts the given relative path into a package name.
1007 * Returns null if the path is not a valid package name.
1009 // public static String packageName(IPath pkgPath) {
1010 // StringBuffer pkgName = new StringBuffer(IPackageFragment.DEFAULT_PACKAGE_NAME);
1011 // for (int j = 0, max = pkgPath.segmentCount(); j < max; j++) {
1012 // String segment = pkgPath.segment(j);
1013 // if (!isValidFolderNameForPackage(segment)) {
1016 // pkgName.append(segment);
1017 // if (j < pkgPath.segmentCount() - 1) {
1018 // pkgName.append("." ); //$NON-NLS-1$
1021 // return pkgName.toString();
1025 * Sort the comparable objects in the given collection.
1027 private static void quickSort(Comparable[] sortedCollection, int left, int right) {
1028 int original_left = left;
1029 int original_right = right;
1030 Comparable mid = sortedCollection[ (left + right) / 2];
1032 while (sortedCollection[left].compareTo(mid) < 0) {
1035 while (mid.compareTo(sortedCollection[right]) < 0) {
1038 if (left <= right) {
1039 Comparable tmp = sortedCollection[left];
1040 sortedCollection[left] = sortedCollection[right];
1041 sortedCollection[right] = tmp;
1045 } while (left <= right);
1046 if (original_left < right) {
1047 quickSort(sortedCollection, original_left, right);
1049 if (left < original_right) {
1050 quickSort(sortedCollection, left, original_right);
1055 * Sort the strings in the given collection in reverse alphabetical order.
1057 private static void quickSortReverse(String[] sortedCollection, int left, int right) {
1058 int original_left = left;
1059 int original_right = right;
1060 String mid = sortedCollection[ (left + right) / 2];
1062 while (sortedCollection[left].compareTo(mid) > 0) {
1065 while (mid.compareTo(sortedCollection[right]) > 0) {
1068 if (left <= right) {
1069 String tmp = sortedCollection[left];
1070 sortedCollection[left] = sortedCollection[right];
1071 sortedCollection[right] = tmp;
1075 } while (left <= right);
1076 if (original_left < right) {
1077 quickSortReverse(sortedCollection, original_left, right);
1079 if (left < original_right) {
1080 quickSortReverse(sortedCollection, left, original_right);
1085 * Sorts an array of objects in place, using the sort order given for each item.
1087 public static void sort(Object[] objects, int[] sortOrder) {
1088 if (objects.length > 1)
1089 quickSort(objects, 0, objects.length - 1, sortOrder);
1093 * Sorts an array of objects in place.
1094 * The given comparer compares pairs of items.
1096 public static void sort(Object[] objects, Comparer comparer) {
1097 if (objects.length > 1)
1098 quickSort(objects, 0, objects.length - 1, comparer);
1102 * Sorts an array of strings in place using quicksort.
1104 public static void sort(String[] strings) {
1105 if (strings.length > 1)
1106 quickSort(strings, 0, strings.length - 1);
1110 * Sorts an array of Comparable objects in place.
1112 public static void sort(Comparable[] objects) {
1113 if (objects.length > 1)
1114 quickSort(objects, 0, objects.length - 1);
1118 * Sorts an array of Strings, returning a new array
1119 * with the sorted items. The original array is left untouched.
1121 public static Object[] sortCopy(Object[] objects, Comparer comparer) {
1122 int len = objects.length;
1123 Object[] copy = new Object[len];
1124 System.arraycopy(objects, 0, copy, 0, len);
1125 sort(copy, comparer);
1130 * Sorts an array of Strings, returning a new array
1131 * with the sorted items. The original array is left untouched.
1133 public static String[] sortCopy(String[] objects) {
1134 int len = objects.length;
1135 String[] copy = new String[len];
1136 System.arraycopy(objects, 0, copy, 0, len);
1142 * Sorts an array of Comparable objects, returning a new array
1143 * with the sorted items. The original array is left untouched.
1145 public static Comparable[] sortCopy(Comparable[] objects) {
1146 int len = objects.length;
1147 Comparable[] copy = new Comparable[len];
1148 System.arraycopy(objects, 0, copy, 0, len);
1154 * Sorts an array of strings in place using quicksort
1155 * in reverse alphabetical order.
1157 public static void sortReverseOrder(String[] strings) {
1158 if (strings.length > 1)
1159 quickSortReverse(strings, 0, strings.length - 1);
1163 * Converts a String[] to char[][].
1165 public static char[][] toCharArrays(String[] a) {
1167 char[][] result = new char[len][];
1168 for (int i = 0; i < len; ++i) {
1169 result[i] = toChars(a[i]);
1175 * Converts a String to char[].
1177 public static char[] toChars(String s) {
1178 int len = s.length();
1179 char[] chars = new char[len];
1180 s.getChars(0, len, chars, 0);
1185 * Converts a String to char[][], where segments are separate by '.'.
1187 public static char[][] toCompoundChars(String s) {
1188 int len = s.length();
1190 return CharOperation.NO_CHAR_CHAR;
1193 for (int off = s.indexOf('.'); off != -1; off = s.indexOf('.', off + 1)) {
1196 char[][] segs = new char[segCount][];
1198 for (int i = 0; i < segCount; ++i) {
1199 int dot = s.indexOf('.', start);
1200 int end = (dot == -1 ? s.length() : dot);
1201 segs[i] = new char[end - start];
1202 s.getChars(start, end, segs[i], 0);
1209 * Converts a char[][] to String, where segments are separated by '.'.
1211 public static String toString(char[][] c) {
1212 StringBuffer sb = new StringBuffer();
1213 for (int i = 0, max = c.length; i < max; ++i) {
1214 if (i != 0) sb.append('.');
1217 return sb.toString();
1221 * Converts a char[][] and a char[] to String, where segments are separated by '.'.
1223 public static String toString(char[][] c, char[] d) {
1224 if (c == null) return new String(d);
1225 StringBuffer sb = new StringBuffer();
1226 for (int i = 0, max = c.length; i < max; ++i) {
1231 return sb.toString();
1235 * Converts a char[] to String.
1237 public static String toString(char[] c) {
1238 return new String(c);
1242 * Converts an array of Objects into String.
1244 public static String toString(Object[] objects) {
1245 return toString(objects,
1247 public String displayString(Object o) {
1248 if (o == null) return "null"; //$NON-NLS-1$
1249 return o.toString();
1255 * Converts an array of Objects into String.
1257 public static String toString(Object[] objects, Displayable renderer) {
1258 if (objects == null) return ""; //$NON-NLS-1$
1259 StringBuffer buffer = new StringBuffer(10);
1260 for (int i = 0; i < objects.length; i++){
1261 if (i > 0) buffer.append(", "); //$NON-NLS-1$
1262 buffer.append(renderer.displayString(objects[i]));
1264 return buffer.toString();
1268 * Asserts that the given method signature is valid.
1270 public static void validateMethodSignature(String sig) {
1271 Assert.isTrue(isValidMethodSignature(sig));
1275 * Asserts that the given type signature is valid.
1277 public static void validateTypeSignature(String sig, boolean allowVoid) {
1278 Assert.isTrue(isValidTypeSignature(sig, allowVoid));
1282 * Creates a NLS catalog for the given locale.
1284 public static void relocalize() {
1286 bundle = ResourceBundle.getBundle(bundleName, Locale.getDefault());
1287 } catch(MissingResourceException e) {
1288 System.out.println("Missing resource : " + bundleName.replace('.', '/') + ".properties for locale " + Locale.getDefault()); //$NON-NLS-1$//$NON-NLS-2$
1294 * Put all the arguments in one String.
1296 public static String getProblemArgumentsForMarker(String[] arguments){
1297 StringBuffer args = new StringBuffer(10);
1299 args.append(arguments.length);
1303 for (int j = 0; j < arguments.length; j++) {
1305 args.append(ARGUMENTS_DELIMITER);
1307 if(arguments[j].length() == 0) {
1308 args.append(EMPTY_ARGUMENT);
1310 args.append(arguments[j]);
1314 return args.toString();
1318 * Separate all the arguments of a String made by getProblemArgumentsForMarker
1320 public static String[] getProblemArgumentsFromMarker(String argumentsString){
1321 if (argumentsString == null) return null;
1322 int index = argumentsString.indexOf(':');
1326 int length = argumentsString.length();
1329 numberOfArg = Integer.parseInt(argumentsString.substring(0 , index));
1330 } catch (NumberFormatException e) {
1333 argumentsString = argumentsString.substring(index + 1, length);
1335 String[] args = new String[length];
1338 StringTokenizer tokenizer = new StringTokenizer(argumentsString, ARGUMENTS_DELIMITER);
1339 while(tokenizer.hasMoreTokens()) {
1340 String argument = tokenizer.nextToken();
1341 if(argument.equals(EMPTY_ARGUMENT))
1342 argument = ""; //$NON-NLS-1$
1343 args[count++] = argument;
1346 if(count != numberOfArg)
1349 System.arraycopy(args, 0, args = new String[count], 0, count);