1 package com.puppycrawl.tools.checkstyle.checks; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.Properties ; 32 import java.util.Set ; 33 34 import com.puppycrawl.tools.checkstyle.Defn; 35 import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck; 36 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; 37 import com.puppycrawl.tools.checkstyle.api.MessageDispatcher; 38 import com.puppycrawl.tools.checkstyle.api.Utils; 39 40 56 public class TranslationCheck 57 extends AbstractFileSetCheck 58 { 59 62 public TranslationCheck() 63 { 64 setFileExtensions(new String []{"properties"}); 65 } 66 67 75 private static String extractPropertyIdentifier(final File aFile) 76 { 77 final String filePath = aFile.getPath(); 78 final int dirNameEnd = filePath.lastIndexOf(File.separatorChar); 79 final int baseNameStart = dirNameEnd + 1; 80 final int underscoreIdx = filePath.indexOf('_', baseNameStart); 81 final int dotIdx = filePath.indexOf('.', baseNameStart); 82 final int cutoffIdx = (underscoreIdx != -1) ? underscoreIdx : dotIdx; 83 return filePath.substring(0, cutoffIdx); 84 } 85 86 93 private static Map arrangePropertyFiles(File [] aPropFiles) 94 { 95 final Map propFileMap = new HashMap (); 96 97 for (int i = 0; i < aPropFiles.length; i++) { 98 final File f = aPropFiles[i]; 99 final String identifier = extractPropertyIdentifier(f); 100 101 Set fileSet = (Set ) propFileMap.get(identifier); 102 if (fileSet == null) { 103 fileSet = new HashSet (); 104 propFileMap.put(identifier, fileSet); 105 } 106 fileSet.add(f); 107 } 108 return propFileMap; 109 } 110 111 112 117 private Set loadKeys(File aFile) 118 { 119 final Set keys = new HashSet (); 120 InputStream inStream = null; 121 122 try { 123 inStream = new FileInputStream (aFile); 125 final Properties props = new Properties (); 126 props.load(inStream); 127 128 final Enumeration e = props.propertyNames(); 130 while (e.hasMoreElements()) { 131 keys.add(e.nextElement()); 132 } 133 } 134 catch (final IOException e) { 135 logIOException(e, aFile); 136 } 137 finally { 138 try { 139 if (inStream != null) { 140 inStream.close(); 141 } 142 } 143 catch (final IOException e) { 144 logIOException(e, aFile); 145 } 146 } 147 return keys; 148 } 149 150 155 private void logIOException(IOException aEx, File aFile) 156 { 157 String [] args = null; 158 String key = "general.fileNotFound"; 159 if (!(aEx instanceof FileNotFoundException )) { 160 args = new String [] {aEx.getMessage()}; 161 key = "general.exception"; 162 } 163 final LocalizedMessage message = 164 new LocalizedMessage( 165 0, 166 Defn.CHECKSTYLE_BUNDLE, 167 key, 168 args, 169 getId(), 170 this.getClass()); 171 final LocalizedMessage[] messages = new LocalizedMessage[] {message}; 172 getMessageDispatcher().fireErrors(aFile.getPath(), messages); 173 Utils.getExceptionLogger().debug("IOException occured.", aEx); 174 } 175 176 177 183 private void compareKeySets(Set aKeys, Map aFileMap) 184 { 185 final Set fls = aFileMap.entrySet(); 186 187 for (final Iterator iter = fls.iterator(); iter.hasNext();) { 188 final Map.Entry entry = (Map.Entry ) iter.next(); 189 final File currentFile = (File ) entry.getKey(); 190 final MessageDispatcher dispatcher = getMessageDispatcher(); 191 final String path = currentFile.getPath(); 192 dispatcher.fireFileStarted(path); 193 final Set currentKeys = (Set ) entry.getValue(); 194 195 final Set keysClone = new HashSet (aKeys); 197 keysClone.removeAll(currentKeys); 198 199 if (!keysClone.isEmpty()) { 201 for (final Iterator it = keysClone.iterator(); it.hasNext();) { 202 final Object key = it.next(); 203 log(0, "translation.missingKey", key); 204 } 205 } 206 fireErrors(path); 207 dispatcher.fireFileFinished(path); 208 } 209 } 210 211 212 222 private void checkPropertyFileSets(Map aPropFiles) 223 { 224 final Set entrySet = aPropFiles.entrySet(); 225 226 for (final Iterator iterator = entrySet.iterator(); iterator.hasNext();) 227 { 228 final Map.Entry entry = (Map.Entry ) iterator.next(); 229 final Set files = (Set ) entry.getValue(); 230 231 if (files.size() >= 2) { 232 final Set keys = new HashSet (); 234 final Map fileMap = new HashMap (); 235 236 for (final Iterator iter = files.iterator(); iter.hasNext();) { 237 final File file = (File ) iter.next(); 238 final Set fileKeys = loadKeys(file); 239 keys.addAll(fileKeys); 240 fileMap.put(file, fileKeys); 241 } 242 243 compareKeySets(keys, fileMap); 245 } 246 } 247 } 248 249 250 260 public void process(File [] aFiles) 261 { 262 final File [] propertyFiles = filter(aFiles); 263 final Map propFilesMap = arrangePropertyFiles(propertyFiles); 264 checkPropertyFileSets(propFilesMap); 265 } 266 267 268 269 } 270 | Popular Tags |