Skip to content

Commit d16c3c0

Browse files
author
zhourenjian
committed
Add Simple Store APIs for storing data locally.
1 parent 0a09280 commit d16c3c0

File tree

6 files changed

+354
-0
lines changed

6 files changed

+354
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.sf.j2s.store;
2+
3+
class CookieStore implements IStore {
4+
5+
/**
6+
* @j2sNative
7+
var prefix = name + "=";
8+
var allCookies = document.cookie.split (';');
9+
for(var i = 0; i < allCookies.length; i++) {
10+
var item = allCookies[i].replace (/^\s*-/, "");
11+
if (item.indexOf (prefix) == 0) {
12+
return item.substring (prefix.length, item.length);
13+
}
14+
}
15+
return null;
16+
*/
17+
public String getProperty(String name) {
18+
return null;
19+
}
20+
21+
/**
22+
* @j2sNative
23+
var toExpire = new Date();
24+
toExpire.setTime (new Date().getTime () + (365 * 24 * 3600 * 1000));
25+
document.cookie = name + "=" + value
26+
+ "; expires=" + toExpire.toGMTString ()
27+
+ "; path=/";
28+
*/
29+
public void setProperty(String name, String value) {
30+
31+
}
32+
33+
public boolean isReady() {
34+
return true;
35+
}
36+
37+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package net.sf.j2s.store;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.util.Date;
9+
import java.util.Properties;
10+
11+
class INIFileStore implements IStore {
12+
13+
private File file;
14+
15+
private Properties properties;
16+
17+
private long lastUpdated;
18+
19+
public INIFileStore(String path) {
20+
this.file = new File(path);
21+
load();
22+
}
23+
24+
private void load() {
25+
properties = new Properties();
26+
FileInputStream fis = null;
27+
try {
28+
fis = new FileInputStream(this.file);
29+
properties.load(fis);
30+
} catch (FileNotFoundException e) {
31+
//e.printStackTrace();
32+
} catch (IOException e) {
33+
//e.printStackTrace();
34+
} finally {
35+
if (fis != null) {
36+
try {
37+
fis.close();
38+
} catch (IOException e) {
39+
//e.printStackTrace();
40+
}
41+
}
42+
}
43+
lastUpdated = new Date().getTime();
44+
}
45+
46+
public String getProperty(String name) {
47+
long lastModified = file.lastModified();
48+
if (lastModified > lastUpdated) {
49+
load();
50+
}
51+
return properties.getProperty(name);
52+
}
53+
54+
public void setProperty(String name, String value) {
55+
long lastModified = file.lastModified();
56+
if (lastModified > lastUpdated) {
57+
load();
58+
}
59+
properties.setProperty(name, value);
60+
save();
61+
}
62+
63+
private void save() {
64+
FileOutputStream fos = null;
65+
try {
66+
fos = new FileOutputStream(this.file);
67+
properties.store(fos, "Java2Script Simple Store");
68+
} catch (FileNotFoundException e) {
69+
//e.printStackTrace();
70+
} catch (IOException e) {
71+
//e.printStackTrace();
72+
} finally {
73+
if (fos != null) {
74+
try {
75+
fos.close();
76+
} catch (IOException e) {
77+
//e.printStackTrace();
78+
}
79+
}
80+
}
81+
lastUpdated = new Date().getTime();
82+
}
83+
84+
public boolean isReady() {
85+
return true;
86+
}
87+
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package net.sf.j2s.store;
2+
3+
interface IStore {
4+
5+
public void setProperty(String name, String value);
6+
7+
public String getProperty(String name);
8+
9+
public boolean isReady();
10+
11+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package net.sf.j2s.store;
2+
3+
import java.io.File;
4+
5+
import net.sf.j2s.annotation.J2SRequireImport;
6+
7+
@J2SRequireImport({CookieStore.class, XSSCookieStore.class})
8+
public class SimpleStore implements IStore {
9+
10+
private static SimpleStore singleton;
11+
12+
private IStore store;
13+
14+
private SimpleStore() {
15+
/**
16+
* @j2sNative
17+
* var ua = navigator.userAgent.toLowerCase ();
18+
* var isIE = (ua.indexOf ("msie") != -1);
19+
* var isIE6OrEarlier = isIE && ((ua.indexOf ("msie 6.0") != -1)
20+
* || (ua.indexOf ("msie 5.5") != -1) || (ua.indexOf ("msie 5.0") != -1));
21+
* var cookieURL = window["j2s.xss.cookie.url"];
22+
* var isLocal = window.location.protocol == "file:";
23+
* if (!isLocal && cookieURL != null && !isIE6OrEarlier) {
24+
* this.store = new net.sf.j2s.store.XSSCookieStore(cookieURL);
25+
* } else {
26+
* this.store = new net.sf.j2s.store.CookieStore();
27+
* }
28+
*/ {
29+
File storeFile = new File(System.getProperty("user.home"), ".java2script.store");
30+
this.store = new INIFileStore(storeFile.getAbsolutePath());
31+
}
32+
}
33+
34+
public static SimpleStore getDefault() {
35+
if (singleton == null) {
36+
singleton = new SimpleStore();
37+
}
38+
return singleton;
39+
}
40+
41+
public String getProperty(String name) {
42+
return store.getProperty(name);
43+
}
44+
45+
public void setProperty(String name, String value) {
46+
store.setProperty(name, value);
47+
}
48+
49+
public boolean isReady() {
50+
return store.isReady();
51+
}
52+
53+
public void execute(Runnable runnable) {
54+
if (store instanceof XSSCookieStore && !store.isReady()) {
55+
/**
56+
* @j2sNative
57+
window.xssCookieReadyCallback = (function (r) {
58+
return function () {
59+
net.sf.j2s.store.XSSCookieStore.initialized = true;
60+
r.run ();
61+
};
62+
}) (runnable);
63+
*/ {}
64+
return;
65+
}
66+
runnable.run();
67+
}
68+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package net.sf.j2s.store;
2+
3+
/**
4+
*
5+
* @author Zhou Renjian (http://zhourenjian.com)
6+
*
7+
* Mar 28, 2009
8+
*
9+
* @j2sSuffix
10+
* var ua = navigator.userAgent.toLowerCase ();
11+
* var isIE = (ua.indexOf ("msie") != -1);
12+
* var isIE6OrEarlier = isIE && ((ua.indexOf ("msie 6.0") != -1)
13+
* || (ua.indexOf ("msie 5.5") != -1) || (ua.indexOf ("msie 5.0") != -1));
14+
* var xssCookieURL = window["j2s.xss.cookie.url"];
15+
* var isLocal = window.location.protocol == "file:";
16+
* if (!isLocal && xssCookieURL != null && !isIE6OrEarlier) {
17+
* net.sf.j2s.store.XSSCookieStore.initialize(xssCookieURL);
18+
* }
19+
*/
20+
class XSSCookieStore implements IStore {
21+
22+
private String url;
23+
24+
private static boolean initialized = false;
25+
26+
public XSSCookieStore(String url) {
27+
if (url == null) {
28+
url = "http://cookie.java2script.org/xss-cookie.html";
29+
}
30+
this.url = url;
31+
initialize(url);
32+
}
33+
34+
/**
35+
* @j2sNative
36+
var ua = navigator.userAgent.toLowerCase ();
37+
var isIE = (ua.indexOf ("msie") != -1);
38+
if (!isIE) {
39+
document.domain = document.domain;
40+
}
41+
var xssIfr = document.getElementById ("xss-cookie");
42+
if (xssIfr != null) {
43+
return;
44+
}
45+
window.xssCookieReadyCallback = function () {
46+
net.sf.j2s.store.XSSCookieStore.initialized = true;
47+
};
48+
var xssIfr = document.createElement ("IFRAME");
49+
xssIfr.id = "xss-cookie";
50+
xssIfr.src = url;
51+
xssIfr.style.display = "none";
52+
document.body.appendChild (xssIfr);
53+
*/
54+
private static void initialize(String url) {
55+
56+
}
57+
58+
/**
59+
* @j2sNative
60+
if (!net.sf.j2s.store.XSSCookieStore.initialized) {
61+
return null;
62+
}
63+
var xssIfr = document.getElementById ("xss-cookie");
64+
if (xssIfr == null) {
65+
return null;
66+
}
67+
try {
68+
return xssIfr.contentWindow.readCookie (name);
69+
} catch (e) {
70+
return null;
71+
}
72+
*/
73+
public String getProperty(String name) {
74+
if (initialized && url != null) {
75+
return null;
76+
}
77+
return null;
78+
}
79+
80+
/**
81+
* @j2sNative
82+
if (!net.sf.j2s.store.XSSCookieStore.initialized) {
83+
return;
84+
}
85+
var xssIfr = document.getElementById ("xss-cookie");
86+
if (xssIfr == null) {
87+
return;
88+
}
89+
try {
90+
xssIfr.contentWindow.createCookie (name, value, 365);
91+
} catch (e) { }
92+
*/
93+
public void setProperty(String name, String value) {
94+
95+
}
96+
97+
public boolean isReady() {
98+
return initialized;
99+
}
100+
101+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4+
<head>
5+
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
6+
<title>Java2Script Cross Site Cookie</title>
7+
</head>
8+
<body>
9+
<script type="text/javascript">
10+
function createCookie (name, value, days) {
11+
var expires = "";
12+
if (days != null) {
13+
var toExpireDate = new Date();
14+
toExpireDate.setTime (toExpireDate.getTime () + (days * 24 * 3600 * 1000));
15+
expires = "; expires=" + toExpireDate.toGMTString ();
16+
}
17+
document.cookie = name + "=" + value + expires + "; path=/";
18+
}
19+
20+
function readCookie (name) {
21+
var prefix = name + "=";
22+
var allCookies = document.cookie.split (';');
23+
for(var i = 0; i < allCookies.length; i++) {
24+
var item = allCookies[i].replace (/^\s*/, "");
25+
if (item.indexOf (prefix) == 0) {
26+
return item.substring (prefix.length, item.length);
27+
}
28+
}
29+
return null;
30+
}
31+
32+
var originalDomain = document.domain;
33+
var idx = originalDomain.indexOf (".");
34+
if (idx != -1) {
35+
var parentDomain = originalDomain.substring (idx + 1);
36+
document.domain = parentDomain;
37+
}
38+
try {
39+
with (window.parent) {
40+
if (xssCookieReadyCallback != null) {
41+
try {
42+
xssCookieReadyCallback ();
43+
} catch (e) {};
44+
}
45+
}
46+
} catch (e) {};
47+
</script>
48+
</body>
49+
</html>

0 commit comments

Comments
 (0)