1 package org.antlr.works.utils; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.io.FileInputStream ; 6 import java.io.IOException ; 7 37 38 public class Utils { 39 40 public static String stringFromFile(String file) throws IOException { 41 FileInputStream fis = new FileInputStream (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 (b); 51 } else 52 return null; 53 } finally { 54 fis.close(); 55 } 56 } 57 58 public static String toString(String [] object) { 59 StringBuffer sb = new StringBuffer (); 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 trimString(String 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 99 public static String quotePath(String 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 117 public static String unquotePath(String 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 convertRawTextWithEOL(String rawText, JComboBox eolCombo) { 139 return rawText.replaceAll("\n", getEOL(eolCombo)); 140 } 141 142 private static String 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 [] concat(String [] a, String [] b) { 152 String [] n = new String [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 |