⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcefilegenerator.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org         |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/

package org.rapla.components.xmlbundle.impl;
import java.util.*;
import java.io.*;




class ResourceFileGenerator {
    PrintWriter w;
    public static final String encoding = "ASCII";

    public void transform(RaplaDictionary dict
                          ,String packageName
                          ,String classPrefix
                          ,String defaultLang
                          ,File destDir
                          )
                          throws IOException 
    {
        String[] lang = dict.getAvailableLanguages();
        for (int  i = 0; i< lang.length; i ++) {
            String className = classPrefix;
	    if (!lang[i].equals(dict.getDefaultLang())) 
		className +=  "_" + lang[i];

            File file = createFile(destDir, className);
            w = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),encoding)));
            generateHeader(packageName);
            generateContent(dict, className, lang[i], defaultLang);
            w.flush();
            w.close();
        }
    }


    public void transformSingleLanguage(RaplaDictionary dict
                                        ,String packageName
                                        ,String classPrefix
                                        ,String defaultLang
                                        ,String lang
                                        ) {
        String className = classPrefix + "_" + lang;
        w =  new PrintWriter(System.out);  
        generateHeader(packageName);
        generateContent(dict,className, lang, defaultLang);
        w.flush();
    }

    public static String toPackageName(String pathName) {
	StringBuffer buf = new StringBuffer();
	char[] c =pathName.toCharArray();
	for (int i=0;i<c.length;i++) {
	    if (c[i] == File.separatorChar ) {
		if (i>0 && i<c.length-1)
		    buf.append('.');
	    } else {
		buf.append(c[i]);
	    }
	}
	return buf.toString();
    }

    public static File createFile(File dir,String className) {
        return new File(dir,className + ".java");
    }
    

    private void generateHeader(String packageName) {
        w.println("/*******************************************");
        w.println(" * Autogenerated file. Please do not edit. *");
        w.println(" * Edit the *Resources.xml file.           *");
        w.println(" *******************************************/");
        w.println();
        w.println("package " + packageName + ";");
    }

    private void generateContent(RaplaDictionary dict
                                 ,String className
                                 ,String lang
                                 ,String defaultLang) 
    {
        w.println("import java.util.ListResourceBundle;");
        w.println("import java.util.ResourceBundle;");
        w.println();
        w.println("public class " + className + " extends ListResourceBundle {");
        w.println("  public Object[][] getContents() { return contents; }");
	// We make the setParent method public, so that we can use it in I18nImpl
        w.println("  public void setParent(ResourceBundle parent) { super.setParent(parent); }");
        w.println("  static final Object[][] contents = { {\"\",\"\"} ");

        Iterator it = dict.getEntries().iterator();
        while ( it.hasNext()) {
            DictionaryEntry entry =  (DictionaryEntry) it.next();
	    String value =  entry.get(lang);
	    if (value != null) {
		String content = convertToJava(value);
		w.println("   , { \"" + entry.getKey() + "\",\"" + content + "\"}");
	    }
        }

        w.println("  };");
        w.println("}");
    }

    static public String byteToHex(byte b) {
        // Returns hex String representation of byte b
        char hexDigit[] = {  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'        };
        char[] array = { hexDigit[(b >> 4) & 0x0f], hexDigit[b & 0x0f] };
        return new String(array);
    }
    
    static public String charToHex(char c) {
        // Returns hex String representation of char c
        byte hi = (byte) (c >>> 8);
        byte lo = (byte) (c & 0xff);
        return byteToHex(hi) + byteToHex(lo);
    }

    private String convertToJava(String text) {
        StringBuffer result = new StringBuffer();
        for ( int i = 0;i< text.length();i++) {
            char c = text.charAt(i);
            
            switch ( c) {
            case '\n': // LineBreaks
                result.append("\" \n      +  \"");               
                break;
            case '\\': // \
                result.append("\\\\");               
                break;
            case '\"': // "
                result.append("\\\"");               
                break;
            default:
                if ( (int)c > 127) {
                    result.append("\\u" + charToHex(c));                                    
                } else {
                    result.append(c);
                } // end of else
                break;
            } // end of switch ()
        } // end of for ()
        return result.toString();
    }

}   
 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -