I wrote a simple java program that you can use to do this.
Dexternalize.java
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;
Basically, all you have to do is call this java program with the location of the properties file and the name of the java file you want to create that will contain the properties.
For example, this properties file:
test.properties
TEST_1=test test test TEST_2=test 2456 TEST_3=123456
will become:
java_test.java
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!
EDIT:
I understand what you asked now. You can use my code to do what you want if you cover a bit of regular expression magic. Suppose you have a java_test file on top. Copy the built-in properties into the file that you want to replace with the code myResourceBundle.
For instance,
TestFile.java
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")); } }
Ok, now if you use eclipse (any modern IDE should do this), go to the Edit menu β Find / Replace. In the window you will see the checkbox "Regular Expressions", check this. Now enter the following text into the search field:
myResourceBundle\.getString\(\"(.+)\"\)
And backward link
\1
in replacement.
Now click "Replace All" and voila! The code should be built into your needs.
Now TestFile.java will become:
TestFile.java
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); } }