download JipeUtilities.java
Language: Java
License: GPL
Copyright: (C) 1999-2000 Romain Guy
LOC: 380
Project Info
Jipe
Server: SourceForge
Type: cvs
...\j\jipe\jipe\jipe\src\jipe\
   BoxComment.java
   BrowserLauncher.java
   ClassBrowser.java
   ClassFilter.java
   DeleteLine.java
   Editor.java
   ExtensionFilter.java
   Find.java
   FindInFiles.java
   HelpConsole.java
   HtmlFilter.java
   IDEOptions.java
   JarFileManager.java
   JavaFilter.java
   JavaOptions.java
   Jipe.java
   JipeCodeCompleter.java
   JipeCompiler.java
   JipeConsole.java
   JipeDecompiler.java
   JipeMethodFinder.java
   JipeProjectManager.java
   JipeUtilities.java
   JSBeautifier.java
   JSFormatter.java
   JSLineBreaker.java
   JspFilter.java
   LeftIndent.java
   ListAllFiles.java
   MasterFilter.java
   Print.java
   PrintManager.java
   ProjectFilter.java
   PropertiesManager.java
   RemoveSpaces.java
   RightIndent.java
   SimpleComment.java
   SpecialFileFilters.java
   StatusBar.java
   StringParser.java
   TextFilter.java
   ToLowerCase.java
   ToUpperCase.java
   WingComment.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/*
 * 11:32:58 12/03/00
 *
 * Utilities.java - Some utilities for Jext and its classes
 * Copyright (C) 1999-2000 Romain Guy
 * guy.romain@bigfoot.com
 * www.chez.com/powerteam/jext
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package jipe;

import java.io.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
import java.lang.reflect.*;

/**
 * This class contains a bunch of methods, useful for the programmer.
 * @author Romain Guy, Slava Pestov, James Gosling
 * @version 2.2.5
 * @see Jext
 */

public class JipeUtilities
{
  /** This constant defines an open dialog box. */
  public static final int OPEN = 0;
  /** This constant defines a save dialog box. */
  public static final int SAVE = 1;
  /** This constant defines an open dialog box. */
  public static final int SCRIPT = 2;



  /**
   * A very nice trick is to center dialog with their parent.
   * @param parent The parent <code>Component</code>
   * @param child The <code>Component</code> to center
   */

  public static void centerComponentChild(Component parent, Component child)
  {
    Rectangle par = parent.getBounds();
    Rectangle chi = child.getBounds();
    child.setLocation(new Point(par.x + (par.width - chi.width) / 2,
                                par.y + (par.height - chi.height) / 2));
  }

  /**
   * Converts a clas name to a file name. All periods are replaced
   * with slashes and the '.class' extension is added.
   * @param name The class name
   */

  public static String classToFile(String name)
  {
    return name.replace('.', '/').concat(".class");
  }

  /**
   * Converts a file name to a class name. All slash characters are
   * replaced with periods and the trailing '.class' is removed.
   * @param name The file name
   */

  public static String fileToClass(String name)
  {
    char[] clsName = name.toCharArray();
    for (int i = clsName.length - 6; i >= 0; i--)
      if (clsName[i] == '/') clsName[i] = '.';
    return new String(clsName, 0, clsName.length - 6);
  }


  /**
   * Create a blank String made of spaces.
   * @param len Amount of spaces contained in the String
   * @return A blank <code>String</code>
   */

  public static String createWhiteSpace(int len)
  {
    return createWhiteSpace(len, 0);
  }

  /**
   * Create a blank String made of tabs.
   * @param len Amount of spaces contained in the String
   * @param tabSize Tabulation size
   * @return A blank <code>String</code>
   */

  public static String createWhiteSpace(int len, int tabSize)
  {
    StringBuffer buf = new StringBuffer();
    if (tabSize == 0)
    {
      while(len-- > 0)
        buf.append(' ');
    } else {
      int count = len / tabSize;
      while(count-- > 0)
        buf.append('\t');
      count = len % tabSize;
      while(count-- > 0)
        buf.append(' ');
    }
    return buf.toString();
  }

  /**
   * Returns the number of leading white space characters in the
   * specified string.
   * @param str The string
   */

  public static int getLeadingWhiteSpace(String str)
  {
    int whitespace = 0;
    loop: for( ; whitespace < str.length(); )
    {
      switch(str.charAt(whitespace))
      {
        case ' ': case '\t':
          whitespace++;
          break;
        default:
          break loop;
      }
    }
    return whitespace;
  }

  /**
   * Returns the width of the leading white space in the specified
   * string.
   * @param str The string
   * @param tabSize The tab size
   */

  public static int getLeadingWhiteSpaceWidth(String str, int tabSize)
  {
    int whitespace = 0;
    loop: for (int i = 0; i < str.length(); i++)
    {
      switch(str.charAt(i))
      {
        case ' ':
          whitespace++;
          break;
        case '\t':
          whitespace += (tabSize - whitespace % tabSize);
          break;
        default:
          break loop;
      }
    }
    return whitespace;
  }

  /**
   * Some String can be too long to be correctly displayed on the screen.
   * Mainly when it is a path to a file. This method truncate a String.
   * @param longString The <code>String</code> to be truncated
   * @param maxLength The maximum length of the <code>String</code>
   * @return The truncated string
   */

  public static String getShortStringOf(String longString, int maxLength)
  {
    int len = longString.length();
    if (len < maxLength)
      return longString;
    else
      return longString.substring(0, maxLength / 2) + "..." +
             longString.substring(len - (maxLength / 2));
  }

  /**
   * Constructs a new path from current user path. This is an easy way to get a path
   * if the user specified, for example, "..\Java" as new path. This method will return
   * the argument if this one is a path to a root (i.e, if <code>change</code> is equal
   * to C:\Jdk, constructPath will return C:\Jdk).
   * @param change The modification to apply to the path
   */

  public static String constructPath(String change)
  {
    if (beginsWithRoot(change))
      return change;

    StringBuffer newPath = new StringBuffer(getUserDirectory());

    char current;
    char lastChar = '\0';
    boolean toAdd = false;
    change = change.trim();
    StringBuffer buf = new StringBuffer(change.length());

    for (int i = 0; i < change.length(); i++)
    {
      switch ((current = change.charAt(i)))
      {
        case '.':
          if (lastChar == '.')
          {
            String parent = (new File(newPath.toString())).getParent();
            if (parent != null) newPath = new StringBuffer(parent);
          } else if ((lastChar != '\0' && lastChar != '\\' && lastChar != '/') ||
                     (i < change.length() - 1 && change.charAt(i + 1) != '.'))
            buf.append('.');
          lastChar = '.';
          break;
        case '\\': case '/':
          if (lastChar == '\0')
            newPath = new StringBuffer(getRoot(newPath.toString()));
          else {
            char c = newPath.charAt(newPath.length() - 1);
            if (c != '\\' && c != '/')
              newPath.append(File.separator).append(buf.toString());
            else
              newPath.append(buf.toString());
            buf = new StringBuffer();
            toAdd = false;
          }
          lastChar = '\\';
          break;
        case '~':
          if (i < change.length() - 1)
          {
            if (change.charAt(i + 1) == '\\' || change.charAt(i + 1) == '/')
              newPath = new StringBuffer(getHomeDirectory());
            else
              buf.append('~');
          } else if (i == 0)
            newPath = new StringBuffer(getHomeDirectory());
          else
            buf.append('~');
          lastChar = '~';
          break;
        default:
          lastChar = current;
          buf.append(current);
          toAdd = true;
          break;
      }
    }

    if (toAdd)
    {
      char c = newPath.charAt(newPath.length() - 1);
      if (c != '\\' && c != '/')
        newPath.append(File.separator).append(buf.toString());
      else
        newPath.append(buf.toString());
    }

    return newPath.toString();
  }

  /**
   * It can be necessary to check if a path specified by the user is an absolute
   * path (i.e C:\Gfx\3d\Utils is absolute whereas ..\Jext is relative).
   * @param path The path to check
   * @return <code>true</code> if <code>path</code> begins with a root name
   */

  public static boolean beginsWithRoot(String path)
  {
    if (path.length() == 0)
      return false;

    File file = new File(path);
    Class clazz = file.getClass();
    try
    {
      Class[] signature = new Class[0];
      Method method = clazz.getMethod("listRoots", signature);
      Object[] args = new Object[0];
      File[] roots = (File[]) method.invoke(file, args);
      for (int i = 0; i < roots.length; i++)
        if (path.regionMatches(true, 0, roots[i].getPath(), 0, roots[i].getPath().length()))
          return true;
      return false;
    } catch (Exception e) {
      if (path.charAt(0) == '/')
        return true;
      else if (path.length() >= 3 && path.charAt(1) == ':' && path.charAt(2) == '\\')
        return true;
      else
        return false;
    }
  }

  /**
   * Returns user directory.
   */

  public static String getUserDirectory()
  {
    return System.getProperty("user.dir");
  }

  /**
   * Returns user's home directory.
   */

  public static String getHomeDirectory()
  {
    return System.getProperty("user.home");
  }

  /**
   * It can be necessary to determine which is the root of a path.
   * For example, the root of D:\Projects\Java is D:\.
   * @param path The path used to get a root
   * @return The root which contais the specified path
   */

  public static String getRoot(String path)
  {
    File file = new File(path);
    Class clazz = file.getClass();
    try
    {
      Class[] signature = new Class[0];
      Method method = clazz.getMethod("listRoots", signature);
      Object[] args = new Object[0];
      File[] roots = (File[]) method.invoke(file, args);
      for (int i = 0; i < roots.length; i++)
        if (path.startsWith(roots[i].getPath())) return roots[i].getPath();
      return path;
    } catch (Exception e) {
      if (path.charAt(0) == '/')
        return "/";
      else if (path.length() >= 3 && path.charAt(1) == ':' && path.charAt(2) == '\\')
        return path.substring(0, 3);
      else
        return "/";
    }
  }

  /**
   * When the user has to specify file names, he can use wildcards (*, ?). This methods
   * handles the usage of these wildcards.
   * @param s Wilcards
   * @param sort Set to true will sort file names
   * @return An array of String which contains all files matching <code>s</code>
   * in current directory.
   */

  public static String[] getWildCardMatches(String s, boolean sort)
  {
    return getWildCardMatches(null, s, sort);
  }

   /**
   * When the user has to specify file names, he can use wildcards (*, ?). This methods
   * handles the usage of these wildcards.
   * @param path The path were to search
   * @param s Wilcards
   * @param sort Set to true will sort file names
   * @return An array of String which contains all files matching <code>s</code>
   * in current directory.
   */

  public static String[] getWildCardMatches(String path, String s, boolean sort)
  {
    String args = new String(s.trim());
    String files[];
    Vector filesThatMatchVector = new Vector();
    String filesThatMatch[];

    if (path == null)
      files = (new File(getUserDirectory())).list();
    else
      files = (new File(path)).list();

    for (int i = 0; i < files.length; i++)
    {
      if (match(args, files[i]))
      {
        File temp = new File(getUserDirectory(), files[i]);
        filesThatMatchVector.addElement(new String(temp.getName()));
      }
    }

    filesThatMatch = new String[filesThatMatchVector.size()];
    filesThatMatchVector.copyInto(filesThatMatch);

    if (sort) sortStrings(filesThatMatch);

    return filesThatMatch;
  }

  /**
   * This method can determine if a String matches a pattern of wildcards
   * @param pattern The pattern used for comparison
   * @param string The String to be checked
   * @return true if <code>string</code> matches <code>pattern</code>
   */

  public static boolean match(String pattern, String string)
  {
    for (int p = 0; ; p++)
    {
      for (int s = 0; ; p++, s++)
      {
        boolean sEnd = (s >= string.length());
        boolean pEnd = (p >= pattern.length() || pattern.charAt(p) == '|');
        if (sEnd && pEnd)
          return true;
        if (sEnd || pEnd)
          break;
        if (pattern.charAt(p) == '?')
          continue;
        if (pattern.charAt(p) == '*')
        {
          int i;
          p++;
          for (i = string.length(); i >= s; --i)
            if (match(pattern.substring(p), string.substring(i))) return true;
          break;
        }
        if (pattern.charAt(p) != string.charAt(s))
          break;
      }
      p = pattern.indexOf('|', p);
      if (p == -1)
        return false;
    }
  }

  /**
   * Quick sort an array of Strings.
   * @param string Strings to be sorted
   */

  public static void sortStrings(String[] strings)
  {
    sortStrings(strings, 0, strings.length - 1);
  }

  /**
   * Quick sort an array of Strings.
   * @param a Strings to be sorted
   * @param lo0 Lower bound
   * @param hi0 Higher bound
   */

  public static void sortStrings(String a[], int lo0, int hi0)
  {
    int lo = lo0;
    int hi = hi0;
    String mid;

    if (hi0 > lo0)
    {
      mid = a[(lo0 + hi0) / 2];

      while (lo <= hi)
      {
        while (lo < hi0 && a[lo].compareTo(mid) < 0)
          ++lo;

        while (hi > lo0 && a[hi].compareTo(mid) > 0)
          --hi;

        if (lo <= hi)
        {
          swap(a, lo, hi);
          ++lo;
          --hi;
        }
      }

      if (lo0 < hi)
        sortStrings(a, lo0, hi);

      if (lo < hi0)
        sortStrings(a, lo, hi0);
    }
  }

  /**
   * Swaps two Strings.
   * @param a The array to be swapped
   * @param i First String index
   * @param j Second String index
   */

  public static void swap(String a[], int i, int j)
  {
    String T;
    T = a[i];
    a[i] = a[j];
    a[j] = T;
  }

  /**
   * Because a lot of people still use JDK 1.1, we need this method
   * to create an array of Files from an array of String.
   * @param names Names of the files
   * @param construct Set it to true if names does not contain full paths
   * @return An array of Files
   */

  public static File[] listFiles(String[] names, boolean construct)
  {
    File[] files = new File[names.length];

    String path = getUserDirectory();

    if (construct)
    {
      if (!path.endsWith(File.separator))
        path += File.separator;
    }

    for (int i = 0; i < files.length; i++)
    {
      if (construct)
        files[i] = new File(path + names[i]);
      else
        files[i] = new File(names[i]);
    }

    return files;
  }

  /**
   * Turns a Un*x glob filter to regexp one
   * @param glob Globbed filter
   */

  public static String globToRE(String glob)
  {
    char c = '\0';
    boolean escape = false, enclosed = false;
    StringBuffer _buf = new StringBuffer(glob.length());

    for (int i = 0; i < glob.length(); i++)
    {
      c = glob.charAt(i);

      if (escape)
      {
        _buf.append('\\');
        _buf.append(c);
        escape = false;
        continue;
      }

      switch(c)
      {
        case '*':
          _buf.append('.').append('*');
          break;
        case '?':
          _buf.append('.');
          break;
        case '\\':
          escape = true;
          break;
        case '.':
          _buf.append('\\').append('.');
          break;
        case '{':
          _buf.append('(');
          enclosed = true;
          break;
        case '}':
          _buf.append(')');
          enclosed = false;
          break;
        case ',':
          if (enclosed)
            _buf.append('|');
          else
            _buf.append(',');
          break;
        default:
          _buf.append(c);
      }
    }
    return _buf.toString();
  }
}

// End of Utilities.java

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us