001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.util;
007:
008: import java.io.BufferedWriter;
009: import java.io.File;
010: import java.io.FileInputStream;
011: import java.io.FileWriter;
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.io.PrintWriter;
015: import java.util.HashMap;
016:
017: /**
018: * This class is responsible to read resources and generate the
019: * ResourceData.java file from the resources.
020: */
021: public class Resources {
022:
023: private static final HashMap FILES = new HashMap();
024:
025: static {
026: ResourceData.load();
027: }
028:
029: public static void main(String[] args) throws Exception {
030: String inDir = args.length > 0 ? args[0] : null;
031: String outDir = args.length > 1 ? args[1] : null;
032: new Resources().run(inDir, outDir);
033: }
034:
035: void run(String outDir, String inDir) throws Exception {
036: if (outDir == null) {
037: outDir = "bin";
038: }
039: if (inDir == null) {
040: inDir = "src/main";
041: }
042: if (new File(outDir + "/org/h2/util").exists()) {
043: String file = outDir + "/org/h2/util/ResourceData.java";
044: PrintWriter out = new PrintWriter(new BufferedWriter(
045: new FileWriter(file)));
046: out.println("package org.h2.util;");
047: out.println("// Do not change this code manually");
048: out.println("// This code is generated by "
049: + getClass().getName());
050: out.println("class ResourceData {");
051: out.println(" public static void load() {");
052: generate(out, inDir + "/org/h2/res", "org.h2");
053: generate(out, inDir + "/org/h2/server/web/res",
054: "org.h2.server.web");
055: out.println(" }");
056: out.println("}");
057: out.close();
058: }
059: }
060:
061: void generate(PrintWriter out, String inDir, String packageName)
062: throws Exception {
063: File dir = new File(inDir);
064: String[] list = dir.list();
065: for (int i = 0; list != null && i < list.length; i++) {
066: File f = new File(dir, list[i]);
067: if (!f.isFile()) {
068: continue;
069: }
070: if (list[i].endsWith(".java")) {
071: continue;
072: }
073: String name = "/" + packageName.replace('.', '/') + "/res/"
074: + f.getName();
075: // System.out.println(name+": "+f.length());
076: InputStream in = new FileInputStream(f);
077: byte[] buffer = IOUtils.readBytesAndClose(in, 0);
078: String s = ByteUtils.convertToBinString(buffer);
079: out.print(" Resources.add("
080: + StringUtils.quoteJavaString(name) + ", ");
081: out.print("new String[]{");
082: do {
083: String s2;
084: if (s.length() < 65000) {
085: s2 = s;
086: s = null;
087: } else {
088: s2 = s.substring(0, 65000);
089: s = s.substring(65000);
090: }
091: out.print(StringUtils.quoteJavaString(s2));
092: out.println(", ");
093: } while (s != null);
094: out.println("});");
095: }
096: }
097:
098: static void add(String name, String[] data) {
099: StringBuffer buff = new StringBuffer();
100: for (int i = 0; i < data.length; i++) {
101: buff.append(data[i]);
102: }
103: FILES.put(name, ByteUtils.convertBinStringToBytes(buff
104: .toString()));
105: }
106:
107: public static byte[] get(String name) throws IOException {
108: byte[] data;
109: if (FILES.size() == 0) {
110: // TODO web: security (check what happens with files like 'lpt1.txt' on windows)
111: InputStream in = Resources.class.getResourceAsStream(name);
112: if (in == null) {
113: data = null;
114: } else {
115: data = IOUtils.readBytesAndClose(in, 0);
116: }
117: } else {
118: data = (byte[]) FILES.get(name);
119: }
120: return data == null ? new byte[0] : data;
121: }
122: }
|