KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > Resources


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.io.File JavaDoc;
8 import java.io.FileInputStream JavaDoc;
9 import java.io.FileWriter JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.InputStream JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.util.HashMap JavaDoc;
14
15 public class Resources {
16
17     private static final HashMap JavaDoc FILES = new HashMap JavaDoc();
18
19     static {
20         ResourceData.load();
21     }
22
23     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
24         String JavaDoc inDir = args.length > 0 ? args[0] : null;
25         String JavaDoc outDir = args.length > 1 ? args[1] : null;
26         new Resources().run(inDir, outDir);
27     }
28
29     void run(String JavaDoc outDir, String JavaDoc inDir) throws Exception JavaDoc {
30         if(outDir == null) {
31             outDir = "bin";
32         }
33         if(inDir == null) {
34             inDir = "src/main";
35         }
36         if(new File JavaDoc(outDir + "/org/h2/util").exists()) {
37             String JavaDoc file = outDir + "/org/h2/util/ResourceData.java";
38             PrintWriter JavaDoc out = new PrintWriter JavaDoc(new FileWriter JavaDoc(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 JavaDoc out, String JavaDoc inDir, String JavaDoc packageName) throws Exception JavaDoc {
53         File JavaDoc dir = new File JavaDoc(inDir);
54         String JavaDoc[] list = dir.list();
55         for(int i=0; list != null && i<list.length; i++) {
56             File JavaDoc f = new File JavaDoc(dir, list[i]);
57             if(!f.isFile()) {
58                 continue;
59             }
60             if(list[i].endsWith(".java")) {
61                 continue;
62             }
63             String JavaDoc name = "/" + packageName.replace('.', '/') + "/res/" + f.getName();
64             // System.out.println(name+": "+f.length());
65
FileInputStream JavaDoc in = new FileInputStream JavaDoc(f);
66             byte[] buffer = IOUtils.readBytesAndClose(in, 0);
67             String JavaDoc s = ByteUtils.convertToBinString(buffer);
68             out.print(" Resources.add(" + StringUtils.quoteJavaString(name) + ", ");
69             out.print("new String[]{");
70             do {
71                 String JavaDoc 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 JavaDoc name, String JavaDoc[] data) {
87         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
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 JavaDoc name) throws IOException JavaDoc {
95         byte[] data;
96         if(FILES.size() == 0) {
97             // TODO web: security (check what happens with files like 'lpt1.txt' on windows)
98
InputStream JavaDoc 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