1 package org.catacomb.icon.splash;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import org.catacomb.Root;
11 import org.catacomb.report.E;
12
13
14 public class Splasher {
15
16 static Splasher splasher;
17
18 Splash splash;
19
20
21 public static void showSplash(String confpath) {
22 new Splasher(confpath);
23 }
24
25
26 public static void hideSplash() {
27 if (splasher != null) {
28 splasher.instanceHideSplash();
29 }
30 }
31
32
33
34 public Splasher(String configPath) {
35
36
37 String config = getXMLResource(configPath);
38
39 String att = getAttribute("splashScreen", "src", config);
40
41 if (att != null) {
42 String pp = parentPath(configPath);
43 splash = new Splash(pp, att);
44
45 splash.show();
46 }
47 }
48
49
50 public void instanceHideSplash() {
51 if (splash != null) {
52 splash.hide();
53 }
54 }
55
56
57
58 public static void main(String[] argv) {
59
60
61 showSplash(argv[0]);
62
63 }
64
65
66 public static String parentPath(String pth) {
67 String separator = ".";
68 int ils = pth.lastIndexOf(separator);
69 String ret = null;
70 if (ils > 0) {
71 ret = pth.substring(0, ils);
72 } else {
73 ret = pth;
74 }
75 return ret;
76 }
77
78
79
80
81 public static String getXMLResource(String pathin) {
82 String path = pathin;
83 String sret = null;
84
85 if (path.endsWith(".xml") || path.indexOf(".") < 0) {
86 E.warning("getXMLReousrce should have a dot path, not " + path);
87
88 } else {
89 path = path.replaceAll("\\.", "/") + ".xml";
90 }
91 try {
92 String pref = "org/catacomb/";
93 if (path.startsWith(pref)) {
94 path = path.substring(pref.length(), path.length());
95 InputStream fis = (new Root()).getClass().getResourceAsStream(path);
96 sret = readInputStream(fis);
97
98 } else {
99 InputStream fis = ClassLoader.getSystemResourceAsStream(path);
100 sret = readInputStream(fis);
101 }
102
103 } catch (Exception ex) {
104 E.error("ResourceAccess - cant get " + path + " " + ex);
105 ex.printStackTrace();
106 }
107 return sret;
108 }
109
110
111
112 private static String readInputStream(InputStream fis)
113 throws NullPointerException, IOException {
114 String sret = null;
115
116 InputStreamReader insr = new InputStreamReader(fis);
117 BufferedReader fr = new BufferedReader(insr);
118
119 StringBuffer sb = new StringBuffer();
120 while (fr.ready()) {
121 sb.append(fr.readLine());
122 sb.append("\n");
123 }
124 fr.close();
125 sret = sb.toString();
126
127 return sret;
128 }
129
130
131
132
133
134
135 private static String getAttribute(String enm, String atnm, String src) {
136 String ret = null;
137
138 String etxt = getElementText(enm, src);
139
140 if (etxt != null) {
141 ret = getAttribute(atnm, etxt);
142 }
143
144 return ret;
145 }
146
147
148
149 private static String getAttribute(String atnm, String src) {
150 String ret = null;
151 Pattern pat = Pattern.compile(atnm + "=\"(.*)\"");
152
153 Matcher matcher = pat.matcher(src);
154 if (matcher.find()) {
155 ret = matcher.group(1);
156 }
157 return ret;
158 }
159
160
161
162
163
164 private static String getElementText(String enm, String src) {
165
166 String ret = getVerboseElementText(enm, src);
167
168 if (ret == null) {
169 ret = getCompactElementText(enm, src);
170 }
171 return ret;
172 }
173
174
175
176 private static String getVerboseElementText(String enm, String src) {
177 String ret = null;
178
179
180
181 Pattern pat = Pattern.compile("<" + enm + ">(.*)</" + enm + ">");
182
183 Matcher matcher = pat.matcher(src);
184 if (matcher.find()) {
185 ret = matcher.group(1);
186 }
187 return ret;
188 }
189
190
191
192
193 private static String getCompactElementText(String enm, String src) {
194 String ret = null;
195
196 Pattern pat = Pattern.compile("<" + enm + " (.*)/>");
197
198 Matcher matcher = pat.matcher(src);
199 if (matcher.find()) {
200 ret = matcher.group(1);
201 }
202 return ret;
203 }
204 }