Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd16c3c0

Browse files
author
zhourenjian
committed
Add Simple Store APIs for storing data locally.
1 parent0a09280 commitd16c3c0

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+
packagenet.sf.j2s.store;
2+
3+
classCookieStoreimplementsIStore {
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+
publicStringgetProperty(Stringname) {
18+
returnnull;
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+
publicvoidsetProperty(Stringname,Stringvalue) {
30+
31+
}
32+
33+
publicbooleanisReady() {
34+
returntrue;
35+
}
36+
37+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
packagenet.sf.j2s.store;
2+
3+
importjava.io.File;
4+
importjava.io.FileInputStream;
5+
importjava.io.FileNotFoundException;
6+
importjava.io.FileOutputStream;
7+
importjava.io.IOException;
8+
importjava.util.Date;
9+
importjava.util.Properties;
10+
11+
classINIFileStoreimplementsIStore {
12+
13+
privateFilefile;
14+
15+
privatePropertiesproperties;
16+
17+
privatelonglastUpdated;
18+
19+
publicINIFileStore(Stringpath) {
20+
this.file =newFile(path);
21+
load();
22+
}
23+
24+
privatevoidload() {
25+
properties =newProperties();
26+
FileInputStreamfis =null;
27+
try {
28+
fis =newFileInputStream(this.file);
29+
properties.load(fis);
30+
}catch (FileNotFoundExceptione) {
31+
//e.printStackTrace();
32+
}catch (IOExceptione) {
33+
//e.printStackTrace();
34+
}finally {
35+
if (fis !=null) {
36+
try {
37+
fis.close();
38+
}catch (IOExceptione) {
39+
//e.printStackTrace();
40+
}
41+
}
42+
}
43+
lastUpdated =newDate().getTime();
44+
}
45+
46+
publicStringgetProperty(Stringname) {
47+
longlastModified =file.lastModified();
48+
if (lastModified >lastUpdated) {
49+
load();
50+
}
51+
returnproperties.getProperty(name);
52+
}
53+
54+
publicvoidsetProperty(Stringname,Stringvalue) {
55+
longlastModified =file.lastModified();
56+
if (lastModified >lastUpdated) {
57+
load();
58+
}
59+
properties.setProperty(name,value);
60+
save();
61+
}
62+
63+
privatevoidsave() {
64+
FileOutputStreamfos =null;
65+
try {
66+
fos =newFileOutputStream(this.file);
67+
properties.store(fos,"Java2Script Simple Store");
68+
}catch (FileNotFoundExceptione) {
69+
//e.printStackTrace();
70+
}catch (IOExceptione) {
71+
//e.printStackTrace();
72+
}finally {
73+
if (fos !=null) {
74+
try {
75+
fos.close();
76+
}catch (IOExceptione) {
77+
//e.printStackTrace();
78+
}
79+
}
80+
}
81+
lastUpdated =newDate().getTime();
82+
}
83+
84+
publicbooleanisReady() {
85+
returntrue;
86+
}
87+
88+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
packagenet.sf.j2s.store;
2+
3+
interfaceIStore {
4+
5+
publicvoidsetProperty(Stringname,Stringvalue);
6+
7+
publicStringgetProperty(Stringname);
8+
9+
publicbooleanisReady();
10+
11+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
packagenet.sf.j2s.store;
2+
3+
importjava.io.File;
4+
5+
importnet.sf.j2s.annotation.J2SRequireImport;
6+
7+
@J2SRequireImport({CookieStore.class,XSSCookieStore.class})
8+
publicclassSimpleStoreimplementsIStore {
9+
10+
privatestaticSimpleStoresingleton;
11+
12+
privateIStorestore;
13+
14+
privateSimpleStore() {
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+
FilestoreFile =newFile(System.getProperty("user.home"),".java2script.store");
30+
this.store =newINIFileStore(storeFile.getAbsolutePath());
31+
}
32+
}
33+
34+
publicstaticSimpleStoregetDefault() {
35+
if (singleton ==null) {
36+
singleton =newSimpleStore();
37+
}
38+
returnsingleton;
39+
}
40+
41+
publicStringgetProperty(Stringname) {
42+
returnstore.getProperty(name);
43+
}
44+
45+
publicvoidsetProperty(Stringname,Stringvalue) {
46+
store.setProperty(name,value);
47+
}
48+
49+
publicbooleanisReady() {
50+
returnstore.isReady();
51+
}
52+
53+
publicvoidexecute(Runnablerunnable) {
54+
if (storeinstanceofXSSCookieStore && !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+
packagenet.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+
classXSSCookieStoreimplementsIStore {
21+
22+
privateStringurl;
23+
24+
privatestaticbooleaninitialized =false;
25+
26+
publicXSSCookieStore(Stringurl) {
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+
privatestaticvoidinitialize(Stringurl) {
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+
publicStringgetProperty(Stringname) {
74+
if (initialized &&url !=null) {
75+
returnnull;
76+
}
77+
returnnull;
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+
publicvoidsetProperty(Stringname,Stringvalue) {
94+
95+
}
96+
97+
publicbooleanisReady() {
98+
returninitialized;
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+
<htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en">
4+
<head>
5+
<metahttp-equiv="content-type"content="text/html; charset=utf-8"/>
6+
<title>Java2Script Cross Site Cookie</title>
7+
</head>
8+
<body>
9+
<scripttype="text/javascript">
10+
functioncreateCookie(name,value,days){
11+
varexpires="";
12+
if(days!=null){
13+
vartoExpireDate=newDate();
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+
functionreadCookie(name){
21+
varprefix=name+"=";
22+
varallCookies=document.cookie.split(';');
23+
for(vari=0;i<allCookies.length;i++){
24+
varitem=allCookies[i].replace(/^\s*/,"");
25+
if(item.indexOf(prefix)==0){
26+
returnitem.substring(prefix.length,item.length);
27+
}
28+
}
29+
returnnull;
30+
}
31+
32+
varoriginalDomain=document.domain;
33+
varidx=originalDomain.indexOf(".");
34+
if(idx!=-1){
35+
varparentDomain=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)

[8]ページ先頭

©2009-2025 Movatter.jp