Package org.olat.test

Source Code of org.olat.test.CheckTranslationKeys

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.olat.core.util.FileUtils;


/**
*  Description:<br>
*
* @author Felix Jost
*/
public class CheckTranslationKeys {
  private static Map fileToProp = new HashMap();
  private static Map fileToCont = new HashMap();
 
  /**
   * @param args
   * @throws Exception
   */
  public static void main(String[] args) throws Exception {
    String path ="c:/workspace/olat3/webapp/";
    File basedir = new File(path);
    List res = new ArrayList();
    buildList(res, basedir);
    for (Iterator iter = res.iterator(); iter.hasNext();) {
      File file = (File) iter.next();
      String name = file.getName();
      if (name.startsWith("LocalStrings") && name.endsWith(".properties")) {
        //
        Properties p = new Properties();
        p.load(new FileInputStream(file));
        fileToProp.put(file, p);
        System.out.println("read prop "+file.getAbsolutePath());
      }
      else if (name.toLowerCase().endsWith(".html") || name.toLowerCase().endsWith(".java")) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        FileUtils.copy(new FileInputStream(file), bos);
        String cont = bos.toString();
        fileToCont.put(file, cont);
        System.out.println("read java/html "+file.getAbsolutePath());
      }
    }
    // all in RAM now...
    // check
    findInHTMLorJava("sdfsdfsdfaaaaa"+"aaaaaaaaaaaafsdf");
   
    List dispList = new ArrayList();
    for (Iterator iter = fileToProp.keySet().iterator(); iter.hasNext();) {
      File fil = (File) iter.next();
      Properties p = (Properties)fileToProp.get(fil);
      for (Iterator iterator = p.keySet().iterator(); iterator.hasNext(); ) {
        String key = (String) iterator.next();
        boolean ok = findInHTMLorJava(key);
        String value = p.getProperty(key);
        if (!ok) {
          String msg = fil.getAbsolutePath().substring(path.length())+" unused key "+key+ "="+value;
          dispList.add(msg);
        }
      }
    }
    Collections.sort(dispList);
    for (Iterator iter = dispList.iterator(); iter.hasNext();) {
      String out = (String) iter.next();
      System.out.println(out);
    }
   
  }
 
  /**
   * @param key
   * @return True if key found.
   */
  private static boolean findInHTMLorJava(String key) {
    String search = "\""+key+"\"";
    for (Iterator iter = fileToCont.keySet().iterator(); iter.hasNext();) {
      File fil = (File) iter.next();
      String cont = (String) fileToCont.get(fil);
      if (cont.indexOf(search) != -1) {
        return true;
      }
    }
    return false;
  }

  private static void buildList(List fileList, File cur) {
    if (cur.isDirectory()) {
      File[] children = cur.listFiles();
      for (int i = 0; i < children.length; i++) {
        File curChd = children[i];
        buildList(fileList, curChd);
      }
    }
    else { // regular file
      fileList.add(cur);
    }
  }
}
TOP

Related Classes of org.olat.test.CheckTranslationKeys

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.