'Un'-外部化Eclipse或Intellij中的字符串 [英] 'Un'-externalize strings from Eclipse or Intellij

查看:77
本文介绍了'Un'-外部化Eclipse或Intellij中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在属性文件中有一堆字符串,我想对其进行非外部化",即内联到我的代码中.

I have a bunch of strings in a properties file which i want to 'un-externalize', ie inline into my code.

我看到Eclipse和Intellij都对从代码中外部化"字符串提供了强大的支持,但是它们中的任何一个都支持将属性文件中的字符串内联回代码吗?

I see that both Eclipse and Intellij have great support to 'externalize' strings from within code, however do any of them support inlining strings from a properties file back into code?

例如,如果我有类似-

My.java

System.out.println(myResourceBundle.getString("key"));

我的属性

key=a whole bunch of text

我希望将我的Java代码替换为-

I want my java code to be replaced as -

My.java

System.out.println("a whole bunch of text");

推荐答案

我编写了一个简单的Java程序,您可以使用它来完成此任务.

I wrote a simple java program that you can use to do this.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;


public class Deexternalize {

    public static final Logger logger = Logger.getLogger(Deexternalize.class.toString());

    public static void main(String[] args) throws IOException {
        if(args.length != 2) {
            System.out.println("Deexternalize props_file java_file_to_create");
            return;
        }

        Properties defaultProps = new Properties();
        FileInputStream in = new FileInputStream(args[0]);
        defaultProps.load(in);
        in.close();

        File javaFile = new File(args[1]);

        List<String> data = process(defaultProps,javaFile);

        buildFile(javaFile,data);

    }

    public static List<String> process(Properties propsFile, File javaFile) {
        List<String> data = new ArrayList<String>();
        Set<Entry<Object,Object>> setOfProps = propsFile.entrySet();
        int indexOf = javaFile.getName().indexOf(".");
        String javaClassName = javaFile.getName().substring(0,indexOf);

        data.add("public class " + javaClassName + " {\n");
        StringBuilder sb = null;

        // for some reason it's adding them in reverse order so putting htem on a stack
        Stack<String> aStack = new Stack<String>();

        for(Entry<Object,Object> anEntry : setOfProps) {
            sb = new StringBuilder("\tpublic static final String ");
            sb.append(anEntry.getKey().toString());
            sb.append(" = \"");
            sb.append(anEntry.getValue().toString());
            sb.append("\";\n");
            aStack.push(sb.toString());
        }

        while(!aStack.empty()) {
            data.add(aStack.pop());
        }

        if(sb != null) {
            data.add("}");
        }

        return data;
    }

    public static final void buildFile(File fileToBuild, List<String> lines) {
        BufferedWriter theWriter = null;

        try {
            // Check to make sure if the file exists already.
            if(!fileToBuild.exists()) {
                fileToBuild.createNewFile();
            }

            theWriter = new BufferedWriter(new FileWriter(fileToBuild));

            // Write the lines to the file.
            for(String theLine : lines) {
                // DO NOT ADD windows carriage return.
                if(theLine.endsWith("\r\n")){
                    theWriter.write(theLine.substring(0, theLine.length()-2));
                    theWriter.write("\n");
                } else if(theLine.endsWith("\n")) {
                    // This case is UNIX format already since we checked for
                    // the carriage return already.
                    theWriter.write(theLine);
                } else {
                    theWriter.write(theLine);
                    theWriter.write("\n");
                }
            }

        } catch(IOException ex) {
            logger.log(Level.SEVERE, null, ex);
        } finally {
            try {
                theWriter.close();
            } catch(IOException ex) {
                logger.log(Level.SEVERE, null, ex);
            }
        }
    }
}

基本上,您所需要做的就是使用属性文件的位置以及要创建的包含属性的Java文件的名称来调用此Java程序.

Basically, all you need to do is call this java program with the location of the property file and the name of the java file you want to create that will contain the properties.

例如,此属性文件:

TEST_1=test test test
TEST_2=test 2456
TEST_3=123456

将变为:

public class java_test {
    public static final String TEST_1 = "test test test";
    public static final String TEST_2 = "test 2456";
    public static final String TEST_3 = "123456";
}

希望这就是您所需要的!

Hope this is what you need!

我了解您现在的要求.如果您撒了一些正则表达式魔术,则可以使用我的代码执行所需的操作.假设您有上面的java_test文件.将内联属性复制到您要替换myResourceBundle代码的文件中.

I understand what you requested now. You can use my code to do what you want if you sprinkle a bit of regex magic. Lets say you have the java_test file from above. Copy the inlined properties into the file you want to replace the myResourceBundle code with.

例如,

public class TestFile {

    public static final String TEST_1 = "test test test";
    public static final String TEST_2 = "test 2456";
    public static final String TEST_3 = "123456";

    public static void regexTest() {

        System.out.println(myResourceBundle.getString("TEST_1"));
        System.out.println(myResourceBundle.getString("TEST_1"));
        System.out.println(myResourceBundle.getString("TEST_3"));

    }
}

好吧,现在,如果您使用的是eclipse(任何现代IDE都应该能够做到这一点),请转到编辑菜单"->查找/替换".在窗口中,您应该看到一个正则表达式"复选框,选中该复选框.现在,在查找文本"区域中输入以下文本:

Ok, now if you are using eclipse (any modern IDE should be able to do this) go to the Edit Menu -> Find/Replace. In the window, you should see a "Regular Expressions" checkbox, check that. Now input the following text into the Find text area:

myResourceBundle\.getString\(\"(.+)\"\)

后面的参考

\1

替换.

现在单击全部替换",瞧!该代码应该已经内嵌到您的需求中.

Now click "Replace all" and voila! The code should have been inlined to your needs.

现在TestFile.java将变为:

Now TestFile.java will become:

public class TestFile {

    public static final String TEST_1 = "test test test";
    public static final String TEST_2 = "test 2456";
    public static final String TEST_3 = "123456";

    public static void regexTest() {

        System.out.println(TEST_1);
        System.out.println(TEST_1);
        System.out.println(TEST_3);

    }
}

这篇关于'Un'-外部化Eclipse或Intellij中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆