001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.fao.geonet.apps;
025:
026: import java.io.*;
027: import java.util.*;
028:
029: import jeeves.utils.Xml;
030: import org.jdom.Element;
031: import org.jdom.Namespace;
032:
033: public class CheckLocalizedFiles {
034: public static void main(String args[]) {
035: if (args.length != 2)
036: error("usage: CheckLocalizedFiles languageDir mainLanguageCode");
037:
038: final String languageDirPath = args[0];
039: final String mainLanguageCode = args[1];
040:
041: File languagesDir = new File(languageDirPath);
042: if (!languagesDir.isDirectory())
043: error("directory " + languageDirPath + " does not exist");
044:
045: File mainLanguageDir = new File(languagesDir, mainLanguageCode);
046: if (!mainLanguageDir.isDirectory())
047: error("main language directory "
048: + mainLanguageDir.getPath() + " does not exist");
049:
050: println("LOCALIZED FILES REPORT");
051: println();
052: println("main language: " + mainLanguageCode);
053: println();
054:
055: // find additional languages
056: String languages[] = languagesDir
057: .list(new RealFilenameFilter() {
058: public boolean accept(File dir, String name) {
059: return super .accept(dir, name)
060: && !name.equals(mainLanguageCode);
061: }
062: });
063: println("additional languages");
064: for (int i = 0; i < languages.length; i++)
065: println("- " + languages[i]);
066: println();
067:
068: // scan main language directory
069: println("main language files");
070: Hashtable mlFiles = new Hashtable();
071: scan(mainLanguageDir, mlFiles);
072: println();
073:
074: // check additional languages
075: for (int i = 0; i < languages.length; i++) {
076: String language = languages[i];
077: File languageDir = new File(languagesDir, language);
078: Hashtable lFiles = new Hashtable();
079:
080: println("scanning files for language '" + language + "'");
081: scan(languageDir, lFiles);
082:
083: // for each file in main language directory
084: for (Enumeration keys = mlFiles.keys(); keys
085: .hasMoreElements();) {
086: String mlPath = (String) keys.nextElement();
087:
088: // check if file does not exists in localized directory
089: File lFile = (File) lFiles.get(mlPath);
090: if (lFile == null) {
091: println("**** file " + mlPath
092: + " is missing for language '" + language
093: + "'");
094: continue;
095: }
096: }
097: // for each file in localized directory
098: for (Enumeration keys = lFiles.keys(); keys
099: .hasMoreElements();) {
100: String lPath = (String) keys.nextElement();
101:
102: println("- " + lPath);
103:
104: File lFile = (File) lFiles.get(lPath);
105:
106: // check if file does not exists in main language directory
107: File mlFile = (File) mlFiles.get(lPath);
108: if (mlFile == null) {
109: println("**** extra file " + lPath);
110: continue;
111: }
112: // if file is an XML file compare with main language one
113: if (lPath.endsWith(".xml"))
114: compareXML(lFile, mlFile);
115: }
116: println();
117: }
118: }
119:
120: private static void compareXML(File lFile, File mlFile) {
121: try {
122: Element mlElem = Xml.loadFile(mlFile);
123: Element lElem = Xml.loadFile(lFile);
124:
125: // for each root child in mlElem
126: for (Iterator i = mlElem.getChildren().iterator(); i
127: .hasNext();) {
128: Element mlChild = (Element) i.next();
129: String name = mlChild.getName();
130: Namespace ns = mlChild.getNamespace();
131:
132: // check if child not exists in localized document
133: Element lChild = lElem.getChild(name, ns);
134: if (lChild == null) {
135: println("**** element <" + name + "> is missing");
136: println("\t" + Xml.getString(mlChild));
137: continue;
138: }
139: }
140: // for each root child in lElem
141: for (Iterator i = lElem.getChildren().iterator(); i
142: .hasNext();) {
143: Element lChild = (Element) i.next();
144: String name = lChild.getName();
145: Namespace ns = lChild.getNamespace();
146:
147: // check if child not exists in main language document
148: Element mlChild = mlElem.getChild(name, ns);
149: if (mlChild == null) {
150: println("**** extra element <" + name + ">");
151: continue;
152: }
153: }
154: } catch (Exception e) {
155: println("**** exception: " + e.getMessage());
156: }
157: }
158:
159: private static void scan(File file, Hashtable paths) {
160: scan(file, file.getPath().length() + 1, paths);
161: }
162:
163: private static void scan(File file, int basePathOffset,
164: Hashtable paths) {
165: if (file.isDirectory()) {
166: File entries[] = file.listFiles(new RealFilenameFilter());
167: for (int i = 0; i < entries.length; i++)
168: scan(entries[i], basePathOffset, paths);
169: } else {
170: String relPath = file.getPath().substring(basePathOffset);
171: paths.put(relPath, file);
172:
173: // println("- " + relPath); // DEBUG
174: }
175: }
176:
177: private static void error(String message) {
178: System.err.println(message);
179: System.exit(1);
180: }
181:
182: private static void println(String message) {
183: System.out.println(message);
184: }
185:
186: private static void println() {
187: System.out.println();
188: }
189:
190: private static class RealFilenameFilter implements FilenameFilter {
191: public boolean accept(File dir, String name) {
192: return !(name.startsWith(".") || name.equals("CVS"));
193: }
194: }
195: }
|