1 20 package com.puppycrawl.tools.checkstyle.api; 21 22 import java.io.FileInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStreamReader ; 25 import java.io.LineNumberReader ; 26 import java.io.File ; 27 import java.io.UnsupportedEncodingException ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 import java.util.regex.Pattern ; 32 import java.util.regex.PatternSyntaxException ; 33 34 import org.apache.commons.beanutils.ConversionException; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 45 public final class Utils 46 { 47 48 private static final Map CREATED_RES = new HashMap (); 49 50 private static final Log EXCEPTION_LOG = 51 LogFactory.getLog("com.puppycrawl.tools.checkstyle.ExceptionLog"); 52 53 55 private Utils() 56 { 57 } 58 60 66 public static Log getExceptionLogger() 67 { 68 return EXCEPTION_LOG; 69 } 70 71 79 public static boolean whitespaceBefore(int aIndex, String aLine) 80 { 81 for (int i = 0; i < aIndex; i++) { 82 if (!Character.isWhitespace(aLine.charAt(i))) { 83 return false; 84 } 85 } 86 return true; 87 } 88 89 96 public static int lengthMinusTrailingWhitespace(String aLine) 97 { 98 int len = aLine.length(); 99 for (int i = len - 1; i >= 0; i--) { 100 if (!Character.isWhitespace(aLine.charAt(i))) { 101 break; 102 } 103 len--; 104 } 105 return len; 106 } 107 108 117 public static int lengthExpandedTabs(String aString, 118 int aToIdx, 119 int aTabWidth) 120 { 121 int len = 0; 122 final char[] chars = aString.toCharArray(); 123 for (int idx = 0; idx < aToIdx; idx++) { 124 if (chars[idx] == '\t') { 125 len = (len / aTabWidth + 1) * aTabWidth; 126 } 127 else { 128 len++; 129 } 130 } 131 return len; 132 } 133 134 142 public static Pattern getPattern(String aPattern) 143 throws PatternSyntaxException 144 { 145 return getPattern(aPattern, 0); 146 } 147 148 158 public static Pattern getPattern(String aPattern, int aCompileFlags) 159 throws PatternSyntaxException 160 { 161 final String key = aPattern + ":flags-" + aCompileFlags; 162 Pattern retVal = (Pattern ) CREATED_RES.get(key); 163 if (retVal == null) { 164 retVal = Pattern.compile(aPattern, aCompileFlags); 165 CREATED_RES.put(key, retVal); 166 } 167 return retVal; 168 } 169 175 public static String [] getLines(String aFileName) 176 throws IOException 177 { 178 return getLines( 179 aFileName, 180 System.getProperty("file.encoding", "UTF-8")); 181 } 182 183 191 public static String [] getLines(String aFileName, String aCharsetName) 192 throws IOException 193 { 194 final ArrayList lines = new ArrayList (); 195 final FileInputStream fr = new FileInputStream (aFileName); 196 LineNumberReader lnr = null; 197 try { 198 lnr = new LineNumberReader (new InputStreamReader (fr, aCharsetName)); 199 } 200 catch (final UnsupportedEncodingException ex) { 201 final String message = "unsupported charset: " + ex.getMessage(); 202 throw new UnsupportedEncodingException (message); 203 } 204 try { 205 while (true) { 206 final String l = lnr.readLine(); 207 if (l == null) { 208 break; 209 } 210 lines.add(l); 211 } 212 } 213 finally { 214 try { 215 lnr.close(); 216 } 217 catch (final IOException e) { 218 ; } 220 } 221 222 return (String []) lines.toArray(new String [0]); 223 } 224 225 231 public static Pattern createPattern(String aPattern) 232 throws ConversionException 233 { 234 Pattern retVal = null; 235 try { 236 retVal = getPattern(aPattern); 237 } 238 catch (final PatternSyntaxException e) { 239 throw new ConversionException( 240 "Failed to initialise regexp expression " + aPattern, e); 241 } 242 return retVal; 243 } 244 245 249 public static String baseClassname(String aType) 250 { 251 final int i = aType.lastIndexOf("."); 252 return (i == -1) ? aType : aType.substring(i + 1); 253 } 254 255 261 public static String getStrippedFileName( 262 final String aBasedir, final String aFileName) 263 { 264 final String stripped; 265 if ((aBasedir == null) || !aFileName.startsWith(aBasedir)) { 266 stripped = aFileName; 267 } 268 else { 269 final int skipSep = aBasedir.endsWith(File.separator) ? 0 : 1; 271 stripped = aFileName.substring(aBasedir.length() + skipSep); 272 } 273 return stripped; 274 } 275 276 } 277 | Popular Tags |