1 5 package org.h2.util; 6 7 import java.io.File ; 8 import java.io.FileInputStream ; 9 import java.io.FileWriter ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.PrintWriter ; 13 import java.util.HashMap ; 14 15 public class Resources { 16 17 private static final HashMap FILES = new HashMap (); 18 19 static { 20 ResourceData.load(); 21 } 22 23 public static void main(String [] args) throws Exception { 24 String inDir = args.length > 0 ? args[0] : null; 25 String outDir = args.length > 1 ? args[1] : null; 26 new Resources().run(inDir, outDir); 27 } 28 29 void run(String outDir, String inDir) throws Exception { 30 if(outDir == null) { 31 outDir = "bin"; 32 } 33 if(inDir == null) { 34 inDir = "src/main"; 35 } 36 if(new File (outDir + "/org/h2/util").exists()) { 37 String file = outDir + "/org/h2/util/ResourceData.java"; 38 PrintWriter out = new PrintWriter (new FileWriter (file)); 39 out.println("package org.h2.util;"); 40 out.println("// Do not change this code manually"); 41 out.println("// This code is generated by " + getClass().getName()); 42 out.println("class ResourceData {"); 43 out.println(" public static void load() {"); 44 generate(out, inDir+"/org/h2/res", "org.h2"); 45 generate(out, inDir+"/org/h2/server/web/res", "org.h2.server.web"); 46 out.println(" }"); 47 out.println("}"); 48 out.close(); 49 } 50 } 51 52 void generate(PrintWriter out, String inDir, String packageName) throws Exception { 53 File dir = new File (inDir); 54 String [] list = dir.list(); 55 for(int i=0; list != null && i<list.length; i++) { 56 File f = new File (dir, list[i]); 57 if(!f.isFile()) { 58 continue; 59 } 60 if(list[i].endsWith(".java")) { 61 continue; 62 } 63 String name = "/" + packageName.replace('.', '/') + "/res/" + f.getName(); 64 FileInputStream in = new FileInputStream (f); 66 byte[] buffer = IOUtils.readBytesAndClose(in, 0); 67 String s = ByteUtils.convertToBinString(buffer); 68 out.print(" Resources.add(" + StringUtils.quoteJavaString(name) + ", "); 69 out.print("new String[]{"); 70 do { 71 String s2; 72 if(s.length() < 65000) { 73 s2 = s; 74 s = null; 75 } else { 76 s2 = s.substring(0, 65000); 77 s = s.substring(65000); 78 } 79 out.print(StringUtils.quoteJavaString(s2)); 80 out.println(", "); 81 } while(s != null); 82 out.println("});"); 83 } 84 } 85 86 static void add(String name, String [] data) { 87 StringBuffer buff = new StringBuffer (); 88 for(int i=0; i<data.length; i++) { 89 buff.append(data[i]); 90 } 91 FILES.put(name, ByteUtils.convertBinStringToBytes(buff.toString())); 92 } 93 94 public static byte[] get(String name) throws IOException { 95 byte[] data; 96 if(FILES.size() == 0) { 97 InputStream in = Resources.class.getResourceAsStream(name); 99 if(in == null) { 100 data = null; 101 } else { 102 data = IOUtils.readBytesAndClose(in, 0); 103 } 104 } else { 105 data = (byte[]) FILES.get(name); 106 } 107 return data == null ? new byte[0] : data; 108 } 109 } 110 | Popular Tags |