1 package com.puppycrawl.tools.checkstyle.api; 20 21 22 import java.io.File ; 23 import java.io.UnsupportedEncodingException ; 24 import java.util.ArrayList ; 25 26 31 public abstract class AbstractFileSetCheck 32 extends AbstractViolationReporter 33 implements FileSetCheck 34 { 35 36 private MessageDispatcher mDispatcher; 37 38 39 private String [] mFileExtensions = {}; 40 41 42 private final LocalizedMessages mMessages = new LocalizedMessages(); 43 44 45 private String mCharset = System.getProperty("file.encoding", "UTF-8"); 46 47 48 public void destroy() 49 { 50 } 51 52 53 public String getCharset() 54 { 55 return mCharset; 56 } 57 58 63 public void setCharset(String aCharset) 64 throws UnsupportedEncodingException 65 { 66 try { 69 new String (new byte[] {}, aCharset); 70 } 71 catch (final UnsupportedEncodingException es) { 72 final String message = "unsupported charset: " + es.getMessage(); 73 throw new UnsupportedEncodingException (message); 74 } 75 mCharset = aCharset; 76 } 77 78 79 public final void setMessageDispatcher(MessageDispatcher aDispatcher) 80 { 81 mDispatcher = aDispatcher; 82 } 83 84 90 protected final MessageDispatcher getMessageDispatcher() 91 { 92 return mDispatcher; 93 } 94 95 109 protected final File [] filter(File [] aFiles) 110 { 111 if ((mFileExtensions == null) || (mFileExtensions.length == 0)) { 112 return aFiles; 113 } 114 115 final ArrayList files = new ArrayList (aFiles.length); 116 for (int i = 0; i < aFiles.length; i++) { 117 final File f = aFiles[i]; 118 final String fileName = f.getName(); 119 for (int j = 0; j < mFileExtensions.length; j++) { 120 final String fileExtension = mFileExtensions[j]; 121 if (fileName.endsWith(fileExtension)) { 122 files.add(f); 123 } 124 } 125 } 126 return (File []) files.toArray(new File [files.size()]); 127 } 128 129 135 public final void setFileExtensions(String [] aExtensions) 136 { 137 if (aExtensions == null) { 138 mFileExtensions = null; 139 return; 140 } 141 142 mFileExtensions = new String [aExtensions.length]; 143 for (int i = 0; i < aExtensions.length; i++) { 144 final String extension = aExtensions[i]; 145 if (extension.startsWith(".")) { 146 mFileExtensions[i] = extension; 147 } 148 else { 149 mFileExtensions[i] = "." + extension; 150 } 151 } 152 } 153 154 161 protected final LocalizedMessages getMessageCollector() 162 { 163 return mMessages; 164 } 165 166 171 protected final void log(int aLine, String aKey, Object aArgs[]) 172 { 173 log(aLine, 0, aKey, aArgs); 174 } 175 176 181 protected final void log(int aLineNo, int aColNo, 182 String aKey, Object [] aArgs) 183 { 184 getMessageCollector().add( 185 new LocalizedMessage(aLineNo, 186 aColNo, 187 getMessageBundle(), 188 aKey, 189 aArgs, 190 getSeverityLevel(), 191 getId(), 192 this.getClass())); 193 } 194 195 201 protected final void fireErrors(String aFileName) 202 { 203 final LocalizedMessage[] errors = getMessageCollector().getMessages(); 204 getMessageCollector().reset(); 205 getMessageDispatcher().fireErrors(aFileName, errors); 206 } 207 } 208 | Popular Tags |