1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.commons.pkg;
21
22 import java.io.BufferedWriter;
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.OutputStreamWriter;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28 import java.util.Properties;
29
30 /***
31 * Starts the main application.
32 */
33 public class PackageBuilder {
34
35
36
37 public static final String CONTROL_FILENAME = "metainfo/control";
38
39
40
41 private static PackageManager packman = new PackageManager();
42
43 private static String[] CONTROL_PROPERTIES = new String[] {
44 "Description",
45 "Section",
46 "Package",
47 "Version",
48 };
49
50 private static String[] CONTROL_PROPERTIES_OPTS = new String[] {
51 "Authors",
52 "Depends",
53 };
54
55 private static String[] LIBRARY_PROPERTIES = new String[] {
56 "Description",
57 "Section",
58 "Package",
59 "Version",
60 };
61
62 private static String[] LIBRARY_PROPERTIES_OPTS = new String[] {
63 "Authors",
64 "Class-Path",
65 "License",
66 "Long-Description",
67 };
68
69 private static String[] PLUGIN_PROPERTIES = new String[] {
70 "Description",
71 "Section",
72 "Package",
73 "Version",
74 };
75
76 private static String[] PLUGIN_PROPERTIES_OPTS = new String[] {
77 "Authors",
78 "Class-Name",
79 "Class-Path",
80 "License",
81 "Long-Description",
82 };
83
84
85
86 public static void main(final String[] argv) throws IOException
87 {
88 packman.read(new File(CONTROL_FILENAME));
89
90 if (argv.length == 2 && argv[0].equals("-p")) {
91 printProperties(argv[1], PLUGIN_PROPERTIES,
92 PLUGIN_PROPERTIES_OPTS);
93 }
94 else if (argv.length == 2 && argv[0].equals("-l")) {
95 printProperties(argv[1], LIBRARY_PROPERTIES,
96 LIBRARY_PROPERTIES_OPTS);
97 }
98 else if (argv.length == 3 && argv[0].equals("-c")) {
99 printControl(argv[1], argv[2]);
100 }
101 else {
102 exit("usage: java xnap.pkg.PackageBuilder (-p package |-c package zip)");
103 }
104
105 System.exit(0);
106 }
107
108 public static void printProperties(String packageName, String[] keys,
109 String[] optKeys)
110 throws IOException
111 {
112 PackageInfo info = packman.getPackage(packageName);
113 if (info == null) {
114 exit("no such package: " + packageName);
115 }
116 Properties p = put(info.getProperties(), keys, optKeys);
117 p.put("Installed", "install ok installed");
118 p.put("Release-Nr", getReleaseNr());
119 print(p);
120 }
121
122 public static void printControl(String packageName, String zipFilename)
123 throws IOException
124 {
125 File zip = new File(zipFilename);
126
127 PackageInfo info = packman.getPackage(packageName);
128 if (info == null) {
129 exit("no such package: " + packageName);
130 }
131 Properties p = put(info.getProperties(), CONTROL_PROPERTIES,
132 CONTROL_PROPERTIES_OPTS);
133 p.put("Size", zip.length() + "");
134 p.put("Filename", zip.getName());
135 p.put("Release-Nr", getReleaseNr());
136
137 print(p);
138 }
139
140 public static String getReleaseNr()
141 {
142 SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd01");
143 return df.format(new Date());
144 }
145
146 public static Properties put(Properties p, String[] keys, String[] optKeys)
147 throws IOException
148 {
149 Properties props = new Properties();
150 for (int i = 0; i < keys.length; i++) {
151 if (p.getProperty(keys[i]) == null) {
152 exit("missing key: " + keys[i]);
153 }
154 props.put(keys[i], p.getProperty(keys[i]));
155 }
156 for (int i = 0; i < optKeys.length; i++) {
157 if (p.getProperty(optKeys[i]) != null) {
158 props.put(optKeys[i], p.getProperty(optKeys[i]));
159 }
160 }
161 return props;
162 }
163
164 public static void print(Properties p)
165 throws IOException
166 {
167 PackageInfoWriter.write
168 (new BufferedWriter(new OutputStreamWriter(System.out)), p);
169 }
170
171 public static void exit(String error)
172 {
173 System.err.println(error);
174 System.exit(1);
175 }
176
177 }