KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > i18n > util > ResourceCheck


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.i18n.util;
9
10 /**
11 @author <a HREF="mailto:neeme@one.lv">Neeme Praks</a>
12 @version $Id: ResourceCheck.java,v 1.4 2001/12/11 09:53:35 jefft Exp $
13 */

14
15 import java.util.*;
16 import java.io.*;
17
18 // DOM
19
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 JavaDoc[] args) throws Exception JavaDoc
36     {
37         if (args.length >= 3)
38             main( args[0], args[1], args[2]);
39         else
40         {
41             StringBuffer JavaDoc usage = new StringBuffer JavaDoc(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 JavaDoc resPath, String JavaDoc baseLang, String JavaDoc compareLang) throws Exception JavaDoc
53     {
54         String JavaDoc[] 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 JavaDoc resourceName = resourceNames[i];
60             if (logger.isInfoEnabled()) logger.info("Bundle: " + resourceName);
61             String JavaDoc baseName = getResourceFilename(resPath, resourceName, baseLang);
62             String JavaDoc 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 JavaDoc[] getResourceFilenames(String JavaDoc rootPath, String JavaDoc language)
83     {
84         LinkedList listResult = getResourceFilenames(rootPath, language, "", ".xml");
85         String JavaDoc[] result = new String JavaDoc[listResult.size()];
86         listResult.toArray(result);
87         return result;
88     }
89
90     private static LinkedList getResourceFilenames(String JavaDoc rootPath, String JavaDoc language, String JavaDoc relativePath, String JavaDoc 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 JavaDoc 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 JavaDoc getResourceFilename(String JavaDoc rootPath, String JavaDoc resourceName, String JavaDoc language)
119     {
120         return rootPath + File.separator + language + File.separator + resourceName + ".xml";
121     }
122
123     private String JavaDoc bundleName = "";
124     public ResourceCheck(String JavaDoc bundleName)
125     {
126         this.bundleName = bundleName;
127     }
128
129     private void readBaseElement(String JavaDoc parentPath, Element e, boolean addAttributes)
130     {
131         String JavaDoc path = getPath(parentPath, e, addAttributes);
132         List list = e.getChildren();
133         if (list.size() == 0)
134         {
135             defaultPaths.put( path, new Boolean JavaDoc(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 JavaDoc parentPath, Element e, boolean addAttributes)
146     {
147         if (e == null)
148         {
149             logger.error(this.bundleName + ", bundle not found!");
150             return;
151         }
152         String JavaDoc path = getPath(parentPath, e, addAttributes);
153         List list = e.getChildren();
154         if (list.size() == 0)
155         {
156             Boolean JavaDoc value = (Boolean JavaDoc) defaultPaths.get(path);
157             comparePathOrder.add(path);
158             if (value != null)
159             {
160                 defaultPaths.put( path, new Boolean JavaDoc(true) );
161                 comparePaths.put( path, new Boolean JavaDoc(true) );
162             }
163             else
164             {
165                 comparePaths.put( path, new Boolean JavaDoc(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 JavaDoc getPath(String JavaDoc parentPath, Element e, boolean addAttributes)
176     {
177         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(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 JavaDoc keyName, key;
204         Boolean JavaDoc value;
205
206         while (keys.hasMoreElements())
207         {
208             keyName = (String JavaDoc) keys.nextElement();
209             value = (Boolean JavaDoc) 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 JavaDoc keyName, key;
218         Boolean JavaDoc value;
219
220         while (keys.hasMoreElements())
221         {
222             keyName = (String JavaDoc) keys.nextElement();
223             value = (Boolean JavaDoc) comparePaths.get( keyName );
224             if (!value.booleanValue()) logger.warn(this.bundleName + ", del: " + keyName);
225         }
226     }
227 }
228
Popular Tags