KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > utils > Utils


1 package org.antlr.works.utils;
2
3 import javax.swing.*;
4 import java.awt.*;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 /*
8
9 [The "BSD licence"]
10 Copyright (c) 2005 Jean Bovet
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions
15 are met:
16
17 1. Redistributions of source code must retain the above copyright
18 notice, this list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22 3. The name of the author may not be used to endorse or promote products
23 derived from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
26 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
29 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
36 */

37
38 public class Utils {
39
40     public static String JavaDoc stringFromFile(String JavaDoc file) throws IOException JavaDoc {
41         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
42         try {
43             int x = fis.available();
44             if(x > 0) {
45                 byte b[]= new byte[x];
46                 int c = 0;
47                 while(c < x) {
48                     c += fis.read(b, c, x-c);
49                 }
50                 return new String JavaDoc(b);
51             } else
52                 return null;
53         } finally {
54             fis.close();
55         }
56     }
57
58     public static String JavaDoc toString(String JavaDoc[] object) {
59         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
60         for(int i=0; i<object.length; i++) {
61             sb.append(object[i]);
62             if(i < object.length - 1)
63                 sb.append(" ");
64         }
65         return sb.toString();
66     }
67
68     public static String JavaDoc trimString(String JavaDoc s) {
69         int a = 0;
70         while(a < s.length() && s.charAt(a) == ' ' || s.charAt(a) == '\n' || s.charAt(a) == '\t') {
71             a++;
72         }
73
74         int b = s.length()-1;
75         while(b >0 && s.charAt(b) == ' ' || s.charAt(b) == '\n' || s.charAt(b) == '\t') {
76             b--;
77         }
78
79         if(a == s.length() || b == 0)
80             return "";
81         else
82             return s.substring(a, b+1);
83     }
84
85     public static boolean isComponentChildOf(Component child, Component parent) {
86         if(child == null)
87             return false;
88         else if(child == parent)
89             return true;
90         else
91             return isComponentChildOf(child.getParent(), parent);
92     }
93
94     /** Quote a path if needed (i.e. white space detected)
95      *
96      * @param path The path to quote
97      * @return The quoted path if needed
98      */

99     public static String JavaDoc quotePath(String JavaDoc path) {
100         if(path == null || path.length() == 0)
101             return path;
102
103         path = unquotePath(path);
104
105         if(path.indexOf(' ') != -1) {
106             path = "\""+path+"\"";
107         }
108
109         return path;
110     }
111
112     /** Unquote a path if it has quote (") at the beginning or at the end
113      * of it.
114      * @param path The path to unquote
115      * @return The unquoted path
116      */

117     public static String JavaDoc unquotePath(String JavaDoc path) {
118         if(path == null || path.length() == 0)
119             return path;
120
121         if(path.charAt(0) == '"') {
122             path = path.substring(1);
123         }
124         if(path.charAt(path.length()-1) == '"') {
125             path = path.substring(0, path.length()-1);
126         }
127
128         return path;
129     }
130
131     public static void fillComboWithEOL(JComboBox combo) {
132         combo.removeAllItems();
133         combo.addItem("Unix (LF)");
134         combo.addItem("Mac (CR)");
135         combo.addItem("Windows (CRLF)");
136     }
137
138     public static String JavaDoc convertRawTextWithEOL(String JavaDoc rawText, JComboBox eolCombo) {
139         return rawText.replaceAll("\n", getEOL(eolCombo));
140     }
141     
142     private static String JavaDoc getEOL(JComboBox eolCombo) {
143         switch(eolCombo.getSelectedIndex()) {
144             case 0: return "\n";
145             case 1: return "\r";
146             case 2: return "\r\n";
147         }
148         return "\n";
149     }
150
151     public static String JavaDoc[] concat(String JavaDoc[] a, String JavaDoc[] b) {
152         String JavaDoc[] n = new String JavaDoc[a.length+b.length];
153         System.arraycopy(a, 0, n, 0, a.length);
154         System.arraycopy(b, 0, n, a.length, b.length);
155         return n;
156     }
157 }
158
Popular Tags