1 8 package org.apache.avalon.excalibur.i18n.util; 9 10 14 15 import java.util.*; 16 import java.io.*; 17 18 import org.jdom.*; 20 import org.jdom.input.*; 21 22 import org.apache.log.Hierarchy; 23 import org.apache.log.Logger; 24 25 public class ResourceCheck 26 { 27 protected static Logger logger = Hierarchy.getDefaultHierarchy() 28 .getLoggerFor("XMLResourceBundle"); 29 30 private HashMap defaultPaths = new HashMap(); 31 private HashMap comparePaths = new HashMap(); 32 private Vector defaultPathOrder = new Vector(); 33 private Vector comparePathOrder = new Vector(); 34 35 public static void main(String [] args) throws Exception 36 { 37 if (args.length >= 3) 38 main( args[0], args[1], args[2]); 39 else 40 { 41 StringBuffer usage = new StringBuffer (20); 42 usage.append("XMLResourceBundle resource checker.\n"); 43 usage.append("Usage example:\n"); 44 usage.append("\t arguments \"\\resources en lv\" will take "); 45 usage.append("\"\\resources\" path \n\t as the root path for all resource "); 46 usage.append("files and will compare \n\t resources.xml in \\resources\\en "); 47 usage.append("to resources.xml \n\t in \\resources\\lv.\n"); 48 System.out.print(usage.toString()); 49 } 50 } 51 52 public static void main(String resPath, String baseLang, String compareLang) throws Exception 53 { 54 String [] resourceNames = getResourceFilenames(resPath, baseLang); 55 SAXBuilder builder = new SAXBuilder(false); 56 if (logger.isInfoEnabled()) logger.info("Comparing " + baseLang + " to " + compareLang + ":"); 57 for (int i = 0; i < resourceNames.length; i++) 58 { 59 String resourceName = resourceNames[i]; 60 if (logger.isInfoEnabled()) logger.info("Bundle: " + resourceName); 61 String baseName = getResourceFilename(resPath, resourceName, baseLang); 62 String compareName = getResourceFilename(resPath, resourceName, compareLang); 63 64 ResourceCheck rc = new ResourceCheck(resourceName); 65 Document doc = builder.build( new File(baseName) ); 66 rc.readBaseElement("", doc.getRootElement(), false); 67 68 File compareFile = new File(compareName); 69 Element rootElement = null; 70 if (compareFile.exists()) 71 { 72 doc = builder.build( compareFile ); 73 rootElement = doc.getRootElement(); 74 } 75 rc.readCompareElement("", rootElement, false); 76 77 rc.printMissingKeys(); 78 rc.printExtraKeys(); 79 } 80 } 81 82 private static String [] getResourceFilenames(String rootPath, String language) 83 { 84 LinkedList listResult = getResourceFilenames(rootPath, language, "", ".xml"); 85 String [] result = new String [listResult.size()]; 86 listResult.toArray(result); 87 return result; 88 } 89 90 private static LinkedList getResourceFilenames(String rootPath, String language, String relativePath, String extension) 91 { 92 LinkedList result; 93 File file = new File(rootPath + File.separatorChar + language + File.separatorChar + relativePath); 94 if (file.isDirectory()) 95 { 96 File[] files = file.listFiles(); 97 result = new LinkedList(); 98 for (int i = 0; i < files.length; i++) 99 { 100 result.addAll( getResourceFilenames(rootPath, language, relativePath + File.separatorChar + files[i].getName(), extension) ); 101 } 102 } 103 else 104 { 105 result = new LinkedList(); 106 String fileName = relativePath; 107 if (fileName.endsWith(extension)) 108 { 109 fileName = fileName.substring(0, fileName.length() - extension.length()); 110 if (logger.isDebugEnabled()) logger.debug("Found bundle: " + fileName); 111 result.add(fileName); 112 } 113 } 114 115 return result; 116 } 117 118 private static String getResourceFilename(String rootPath, String resourceName, String language) 119 { 120 return rootPath + File.separator + language + File.separator + resourceName + ".xml"; 121 } 122 123 private String bundleName = ""; 124 public ResourceCheck(String bundleName) 125 { 126 this.bundleName = bundleName; 127 } 128 129 private void readBaseElement(String parentPath, Element e, boolean addAttributes) 130 { 131 String path = getPath(parentPath, e, addAttributes); 132 List list = e.getChildren(); 133 if (list.size() == 0) 134 { 135 defaultPaths.put( path, new Boolean (false) ); 136 defaultPathOrder.add(path); 137 } 138 else 139 { 140 for (int i = 0; i < list.size(); i++) 141 this.readBaseElement(path, (Element) list.get(i), true); 142 } 143 } 144 145 private void readCompareElement(String parentPath, Element e, boolean addAttributes) 146 { 147 if (e == null) 148 { 149 logger.error(this.bundleName + ", bundle not found!"); 150 return; 151 } 152 String path = getPath(parentPath, e, addAttributes); 153 List list = e.getChildren(); 154 if (list.size() == 0) 155 { 156 Boolean value = (Boolean ) defaultPaths.get(path); 157 comparePathOrder.add(path); 158 if (value != null) 159 { 160 defaultPaths.put( path, new Boolean (true) ); 161 comparePaths.put( path, new Boolean (true) ); 162 } 163 else 164 { 165 comparePaths.put( path, new Boolean (false) ); 166 } 167 } 168 else 169 { 170 for (int i = 0; i < list.size(); i++) 171 readCompareElement(path, (Element) list.get(i), true); 172 } 173 } 174 175 private static String getPath(String parentPath, Element e, boolean addAttributes) 176 { 177 StringBuffer sb = new StringBuffer (parentPath); 178 List list; 179 sb.append('/'); 180 sb.append( e.getName() ); 181 182 list = e.getAttributes(); 183 if (list.size() > 0 && addAttributes) 184 { 185 sb.append("["); 186 for (int i = 0; i < list.size(); i++) 187 { 188 Attribute a = (Attribute) list.get(i); 189 sb.append("@").append(a.getName()).append("='").append(a.getValue()).append("'"); 190 if (i < list.size() - 1) 191 sb.append(" and "); 192 } 193 sb.append("]"); 194 } 195 196 return sb.toString(); 197 } 198 199 private void printMissingKeys() 200 { 201 Enumeration keys = defaultPathOrder.elements(); 202 203 String keyName, key; 204 Boolean value; 205 206 while (keys.hasMoreElements()) 207 { 208 keyName = (String ) keys.nextElement(); 209 value = (Boolean ) defaultPaths.get( keyName ); 210 if (!value.booleanValue()) logger.error(this.bundleName + ", add: " + keyName); 211 } 212 } 213 214 private void printExtraKeys() 215 { 216 Enumeration keys = comparePathOrder.elements(); 217 String keyName, key; 218 Boolean value; 219 220 while (keys.hasMoreElements()) 221 { 222 keyName = (String ) keys.nextElement(); 223 value = (Boolean ) comparePaths.get( keyName ); 224 if (!value.booleanValue()) logger.warn(this.bundleName + ", del: " + keyName); 225 } 226 } 227 } 228 | Popular Tags |