Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I wrote a simple java program that you can use to do this.</p> <h2>Dexternalize.java</h2> <pre><code>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&lt;String&gt; data = process(defaultProps,javaFile); buildFile(javaFile,data); } public static List&lt;String&gt; process(Properties propsFile, File javaFile) { List&lt;String&gt; data = new ArrayList&lt;String&gt;(); Set&lt;Entry&lt;Object,Object&gt;&gt; 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&lt;String&gt; aStack = new Stack&lt;String&gt;(); for(Entry&lt;Object,Object&gt; 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&lt;String&gt; 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); } } } } </code></pre> <p>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.</p> <p>For instance this property file:</p> <h2>test.properties</h2> <pre><code>TEST_1=test test test TEST_2=test 2456 TEST_3=123456 </code></pre> <p>will become:</p> <h2>java_test.java</h2> <pre><code>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"; } </code></pre> <p>Hope this is what you need!</p> <p>EDIT:</p> <p>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.</p> <p>For example,</p> <h2>TestFile.java</h2> <pre><code>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")); } } </code></pre> <p>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:</p> <pre><code>myResourceBundle\.getString\(\"(.+)\"\) </code></pre> <p>And the back reference</p> <pre><code>\1 </code></pre> <p>into the replace.</p> <p>Now click "Replace all" and voila! The code should have been inlined to your needs.</p> <p>Now TestFile.java will become:</p> <h2>TestFile.java</h2> <pre><code>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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload