Skip to content

Commit 6ab22c8

Browse files
committed
JSUtil for File delete
1 parent 593c519 commit 6ab22c8

File tree

1 file changed

+92
-39
lines changed

1 file changed

+92
-39
lines changed

sources/net.sf.j2s.java.core/src/swingjs/JSUtil.java

Lines changed: 92 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828

2929
public class JSUtil {
3030

31-
public JSUtil() {
32-
System.out.println("swingjs.JSUtil initialized");
33-
}
34-
3531
static {
3632
boolean j2sdebug = false;
3733
J2SInterface j2sself = null;
@@ -46,6 +42,7 @@ public JSUtil() {
4642
}
4743
debugging = j2sdebug;
4844
J2S = j2sself;
45+
System.out.println("swingjs.JSUtil initialized;debug=" + j2sdebug);
4946
}
5047

5148
/**
@@ -67,23 +64,29 @@ private static Map<String, Object> getFileCache() {
6764

6865
public static Object getCachedFileData(String path) {
6966
return (useCache && fileCache != null ?
70-
fileCache.get(path) : null);
67+
fileCache.get(fixCachePath(path)) : null);
7168
}
7269

70+
/**
71+
* Set the cache to Boolean.FALSE to indicate that we have checked this
72+
* @param path
73+
* @return
74+
*/
7375
public static Object removeCachedFileData(String path) {
7476
return (useCache && fileCache != null ?
75-
fileCache.remove(path) : null);
77+
fileCache.put(fixCachePath(path), Boolean.FALSE) : null);
7678
}
7779

7880

7981
/**
8082
* This could be a simple String, a javajs.util.SB, or unsigned or signed bytes
8183
* depending upon the browser and the file type.
8284
*
83-
* It will not be cached, but it might come from a cache;
85+
* It will not be cached, but it might come from a cache;
8486
*
85-
* @param uriOrJSFile
86-
* @return
87+
* @param uriOrJSFile File or URL or URI or String
88+
*
89+
* @return may be byte[], String, or javajs.util.SB if found; Boolean FALSE if not found
8790
*/
8891
@SuppressWarnings("unused")
8992
private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
@@ -103,9 +106,11 @@ private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
103106
if (!uri.startsWith("/"))
104107
uri = "/" + uri;
105108
uri = "http://." + uri;
109+
// Note that SwingJS will convert this to https if necessary.
106110
}
107-
if (data == null)
111+
if (data == null) {
108112
data = getCachedFileData(uri);
113+
}
109114
if (data == null && !uri.startsWith("./")) {
110115
// Java applications may use "./" here
111116
try {
@@ -114,11 +119,33 @@ private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
114119
} catch (Exception e) {
115120
// bypasses AjaxURLConnection
116121
data = J2S.getFileData(uri, null, false, asBytes);
122+
if (data == null)
123+
removeCachedFileData(uri);
117124
}
118125
}
119126
return data;
120127
}
121128

129+
private static String fixCachePath(String uri) {
130+
int pt;
131+
if (uri.startsWith("./"))
132+
uri = "/" + uri;
133+
if (uri.startsWith("https:/"))
134+
uri = uri.substring(7);
135+
if (uri.startsWith("http:/"))
136+
uri = uri.substring(6);
137+
uri = uri.replace("//", "/");
138+
while ((pt = uri.indexOf("/././")) >= 0) {
139+
// https://././xxx --> /./xxx
140+
uri = uri.substring(0, pt) + uri.substring(pt + 2);
141+
}
142+
if (uri.startsWith("/"))
143+
uri = uri.substring(1);
144+
if (uri.startsWith("./"))
145+
uri = uri.substring(2);
146+
return uri;
147+
}
148+
122149
/**
123150
* Regardless of how returned by Jmol._getFileContents(),
124151
* this method ensures that we get a String.
@@ -128,11 +155,37 @@ private static Object getFileContents(Object uriOrJSFile, boolean asBytes) {
128155
*/
129156
public static String getFileAsString(String filename) {
130157
Object data = getFileContents(filename, false);
131-
return ensureString(data);
158+
return ensureString(data);
159+
}
160+
161+
/**
162+
* Transform byte[], SB, or InputStream data to String
163+
* @param data
164+
* @return String data or null
165+
*/
166+
static String ensureString(Object data) {
167+
if (data == null)
168+
return null;
169+
if (data instanceof byte[])
170+
return Rdr.bytesToUTF8String((byte[]) data);
171+
if (data instanceof String || data instanceof SB)
172+
return data.toString();
173+
if (data instanceof InputStream)
174+
return Rdr.streamToUTF8String(new BufferedInputStream((InputStream) data));
175+
return null;
132176
}
133177

178+
/**
179+
* Ensure byte[] or null
180+
*
181+
* @param file
182+
* @param checkNotFound
183+
* @return
184+
*/
134185
public static byte[] getFileAsBytes(Object file, boolean checkNotFound) {
135186
byte[] data = getFileAsBytes(file);
187+
if (data == null)
188+
return null;
136189
if (checkNotFound) {
137190
if (data.length == 0)
138191
return null;
@@ -151,14 +204,18 @@ public static byte[] getFileAsBytes(Object file, boolean checkNotFound) {
151204
}
152205

153206
/**
207+
* Standard call for getting file contents.
208+
*
154209
* Regardless of how returned by Jmol.getFileContents(),
155210
* this method ensures that we get signed bytes.
156211
*
157212
* @param filename
158-
* @return
213+
* @return byte[] or null
159214
*/
160215
public static byte[] getFileAsBytes(Object file) {
161216
Object data = getFileContents(file, true);
217+
if (data == null || data == Boolean.FALSE)
218+
return null;
162219
byte[] b = null;
163220
if (data instanceof byte[])
164221
b = (byte[]) data;
@@ -186,28 +243,34 @@ public static boolean haveCachedResource(String resourceName, boolean isJavaPath
186243
*
187244
* @param resourceName
188245
* @param isJavaPath
189-
* @param doProcess
190-
* evaluate JS or load CSS
246+
* @param doProcess evaluate JS or load CSS
191247
* @return the resource as a string
192248
*/
193-
public static String getJavaResource(String resourceName, boolean isJavaPath,
194-
boolean doCache, boolean doProcess) {
249+
public static String getJavaResource(String resourceName, boolean isJavaPath, boolean doCache, boolean doProcess) {
195250
System.out.println("JSUtil getting Java resource " + resourceName);
196251
String path = J2S.getResourcePath(resourceName, isJavaPath);
197252
if (path == null)
198253
return null;
199254
Object data = getCachedFileData(path);
200-
if (data == null
201-
&& (data = J2S.getFileData(path, null, false, false)) != null
202-
&& useCache && doCache)
203-
cacheFileData(path, data);
255+
if (data == Boolean.FALSE)
256+
return null;
257+
if (data == null) {
258+
data = J2S.getFileData(path, null, false, false);
259+
if (data == null) {
260+
if (useCache && doCache) {
261+
removeCachedFileData(path);
262+
}
263+
} else if (useCache && doCache) {
264+
cacheFileData(path, data);
265+
}
266+
}
204267
String sdata = ensureString(data);
205268
boolean ok = (sdata != null && sdata.indexOf("[Exception") != 0);
206-
System.out.println("Processing " + path + " ["
207-
+ (ok ? "" + sdata.length() : sdata) + "]");
208-
return (!ok ? null : !doProcess ? sdata
209-
: path.endsWith(".css") ? processCSS(sdata, path) : path
210-
.endsWith(".js") ? processJS(sdata, resourceName) : sdata);
269+
System.out.println("Processing " + path + " [" + (ok ? "" + sdata.length() : sdata) + "]");
270+
return (!ok ? null
271+
: !doProcess ? sdata
272+
: path.endsWith(".css") ? processCSS(sdata, path)
273+
: path.endsWith(".js") ? processJS(sdata, resourceName) : sdata);
211274
}
212275

213276
public static InputStream getCachedResourceAsStream(String name) {
@@ -241,8 +304,10 @@ public static void cacheFileData(String path, Object data) {
241304
if (data instanceof byte[])
242305
count = ""+ ((byte[]) data).length;
243306
else if (data instanceof String)
244-
count = "" + ((String) data).length();
245-
System.out.println("JSUtil caching " + count + " bytes for " + path);
307+
count = "" + ((String) data).length();
308+
path = fixCachePath(path);
309+
if (!getFileCache().containsKey(path))
310+
System.out.println("JSUtil caching " + count + " bytes for " + path);
246311
getFileCache().put(path, data);
247312
}
248313
}
@@ -324,18 +389,6 @@ static String processJS(String js, String resourceName) {
324389
return js;
325390
}
326391

327-
static String ensureString(Object data) {
328-
if (data == null)
329-
return null;
330-
if (data instanceof byte[])
331-
return Rdr.bytesToUTF8String((byte[]) data);
332-
if (data instanceof String || data instanceof SB)
333-
return data.toString();
334-
if (data instanceof InputStream)
335-
return Rdr.streamToUTF8String(new BufferedInputStream((InputStream) data));
336-
return null;
337-
}
338-
339392
/**
340393
* Sets window.jQuery.$ = window.jQuery, so that we can call jQuery.$
341394
*

0 commit comments

Comments
 (0)