Skip to content

Commit ad33f2f

Browse files
committed
refactored JSUtil
1 parent f21952c commit ad33f2f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1442
-1072
lines changed
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
package net.sf.j2s.core.adapters;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
public class ExtendedAdapter extends AbstractPluginAdapter {
7+
8+
public static String buildXSource(String fullClassName, String tagName, String firstLine, String sources) {
9+
if (firstLine != null && sources.length() > 0) {
10+
Set<String> xTags = null;
11+
StringBuilder builder = new StringBuilder();
12+
if ("@j2sXHTML".equals(tagName)) {
13+
boolean shouldMergeFirstLine = false;
14+
if (firstLine.startsWith("{$") || firstLine.contains("<") || firstLine.contains(">")) {
15+
shouldMergeFirstLine = true;
16+
}
17+
if (shouldMergeFirstLine) {
18+
sources = firstLine + "\r\n" + sources;
19+
firstLine = "";
20+
}
21+
xTags = parseXTag(sources);
22+
sources = "\"" + sources.replaceAll("\t", "\\\\t").replaceAll("\"", "\\\\\"").replaceAll("\r\n", "\\\\r\\\\n\" +\r\n\"") + "\"";
23+
String[] parts = firstLine.split("(,| |\t)+");
24+
if (firstLine.length() == 0) {
25+
builder.append("Clazz.parseHTML(");
26+
} else if (parts == null || parts.length == 1) {
27+
if ("return".equals(firstLine)) {
28+
builder.append("return Clazz.parseHTML(");
29+
} else {
30+
builder.append("Clazz.parseHTML(").append(firstLine).append(", ");
31+
}
32+
} else {
33+
String firstVar = parts[0];
34+
String leftStr = firstLine.substring(firstVar.length() + 1).replaceAll("^(,| |\t)+", "");
35+
if (leftStr.endsWith(",")) {
36+
leftStr = leftStr.substring(0, leftStr.length() - 1);
37+
}
38+
if ("return".equals(firstVar)) {
39+
builder.append("return Clazz.parseHTML(").append(leftStr).append(", ");
40+
} else {
41+
builder.append(firstVar).append(" = Clazz.parseHTML(").append(leftStr).append(", ");
42+
}
43+
}
44+
} else { // @j2sXCSS
45+
boolean shouldMergeFirstLine = false;
46+
if (firstLine.startsWith(".") || firstLine.contains("#") || firstLine.contains(">") || firstLine.contains("{")) {
47+
shouldMergeFirstLine = true;
48+
} else if (sources.startsWith("{")) {
49+
shouldMergeFirstLine = true;
50+
}
51+
if (shouldMergeFirstLine) {
52+
sources = firstLine + "\r\n" + sources;
53+
xTags = parseXTag(sources);
54+
builder.append("Clazz.parseCSS(");
55+
} else {
56+
xTags = parseXTag(sources);
57+
if (firstLine.endsWith(",")) {
58+
firstLine = firstLine.substring(0, firstLine.length() - 1);
59+
}
60+
builder.append("Clazz.parseCSS(").append(firstLine).append(", ");
61+
}
62+
sources = "\"" + sources.replaceAll("\t", "\\\\t").replaceAll("\"", "\\\\\"").replaceAll("\r\n", "\\\\r\\\\n\" +\r\n\"") + "\"";
63+
}
64+
boolean containsThis = containsBeginning(xTags, "$:") || containsBeginning(xTags, "$.");
65+
boolean containsClass = containsBeginning(xTags, "$/");
66+
if (containsThis) {
67+
builder.append("this, ");
68+
} else if (containsClass) {
69+
builder.append(fullClassName).append(", ");
70+
}
71+
boolean localStarted = false;
72+
for (String s : xTags) {
73+
if (s.startsWith("$~")) {
74+
if (!localStarted) {
75+
builder.append("{");
76+
localStarted = true;
77+
} else {
78+
builder.append(", ");
79+
}
80+
String varName = s.substring(2);
81+
builder.append(varName).append(": ").append(varName);
82+
}
83+
}
84+
if (localStarted) {
85+
builder.append("}, ");
86+
}
87+
builder.append(sources).append(");\r\n");
88+
return builder.toString();
89+
}
90+
return sources;
91+
}
92+
93+
private static Set<String> parseXTag(String sources) {
94+
Set<String> vars = new HashSet<String>();
95+
String key = "{$";
96+
int index = sources.indexOf(key, 0);
97+
while (index != -1) {
98+
int idx = sources.indexOf("}", index + key.length());
99+
if (idx == -1) {
100+
break;
101+
}
102+
String var = sources.substring(index + key.length() - 1, idx); // with prefix $
103+
if (var.indexOf(' ') == -1) {
104+
vars.add(var);
105+
}
106+
index = sources.indexOf(key, idx + 1);
107+
}
108+
key = "<!--";
109+
index = sources.indexOf(key, 0);
110+
while (index != -1) {
111+
int idx = sources.indexOf("-->", index + key.length());
112+
if (idx == -1) {
113+
break;
114+
}
115+
String comment = sources.substring(index + key.length(), idx).trim();
116+
if (comment.startsWith("$") && comment.indexOf(' ') == -1) {
117+
vars.add(comment);
118+
}
119+
index = sources.indexOf(key, idx + 3); // 3: "-->".length()
120+
}
121+
key = "id";
122+
index = sources.indexOf(key, 0);
123+
while (index > 0) {
124+
char last = sources.charAt(index - 1);
125+
if (!(last == ' ' || last == '\t' || last == '\n' || last == '\r')) {
126+
index = sources.indexOf(key, index + key.length());
127+
continue;
128+
}
129+
int idxEqual = index + key.length();
130+
do {
131+
char c = sources.charAt(idxEqual);
132+
if (c == '=') {
133+
break;
134+
} else if (c == ' ' || c == '\t') {
135+
idxEqual++;
136+
if (idxEqual == sources.length() - 1) {
137+
idxEqual = -1;
138+
break;
139+
}
140+
} else {
141+
idxEqual = -1;
142+
break;
143+
}
144+
} while (true);
145+
if (idxEqual == -1 || idxEqual == sources.length() - 1) {
146+
break;
147+
}
148+
char quote = 0;
149+
int idxQuoteStart = idxEqual + 1;
150+
do {
151+
char c = sources.charAt(idxQuoteStart);
152+
if (c == '\'' || c == '\"') {
153+
quote = c;
154+
break;
155+
} else if (c == ' ' || c == '\t') {
156+
idxQuoteStart++;
157+
if (idxQuoteStart == sources.length() - 1) {
158+
idxQuoteStart = -1;
159+
break;
160+
}
161+
} else {
162+
idxQuoteStart = -1;
163+
break;
164+
}
165+
} while (true);
166+
if (idxQuoteStart == -1 || idxQuoteStart == sources.length() - 1) {
167+
break;
168+
}
169+
int idxQuoteEnd = sources.indexOf(quote, idxQuoteStart + 1);
170+
if (idxQuoteEnd == -1 || idxQuoteEnd == sources.length() - 1) {
171+
break;
172+
}
173+
String idStr = sources.substring(idxQuoteStart + 1, idxQuoteEnd).trim();
174+
if (idStr.startsWith("$") && idStr.indexOf(' ') == -1) {
175+
vars.add(idStr);
176+
}
177+
index = sources.indexOf(key, idxQuoteEnd + 1);
178+
}
179+
return vars;
180+
}
181+
182+
private static boolean containsBeginning(Set<String> set, String beginning) {
183+
for (String s : set) {
184+
if (s.startsWith(beginning)) {
185+
return true;
186+
}
187+
}
188+
return false;
189+
}
190+
191+
// /*
192+
// * Read HTML/CSS sources from @j2sXHTML, @J2SXCSS or others
193+
// */
194+
// boolean readXSources(BodyDeclaration node, String tagName, String prefix, String suffix) {
195+
// boolean existed = false;
196+
// Javadoc javadoc = node.getJavadoc();
197+
// if (javadoc != null) {
198+
// List<?> tags = javadoc.tags();
199+
// if (tags.size() != 0) {
200+
// for (Iterator<?> iter = tags.iterator(); iter.hasNext();) {
201+
// TagElement tagEl = (TagElement) iter.next();
202+
// if (tagName.equals(tagEl.getTagName())) {
203+
// List<?> fragments = tagEl.fragments();
204+
// StringBuffer buf = new StringBuffer();
205+
// boolean isFirstLine = true;
206+
// String firstLine = null;
207+
// for (Iterator<?> iterator = fragments.iterator(); iterator.hasNext();) {
208+
// TextElement commentEl = (TextElement) iterator.next();
209+
// String text = commentEl.getText().trim();
210+
// if (isFirstLine) {
211+
// if (text.length() == 0) {
212+
// continue;
213+
// }
214+
// firstLine = text.trim();
215+
// isFirstLine = false;
216+
// continue;
217+
// }
218+
// buf.append(text);
219+
// buf.append("\r\n");
220+
// }
221+
// String sources = buf.toString().trim();
222+
// sources = buildXSource(tagName, firstLine, sources);
223+
// buffer.append(prefix + sources + suffix);
224+
// existed = true;
225+
// }
226+
// }
227+
// }
228+
// }
229+
// return existed;
230+
// }
231+
232+
233+
}

sources/net.sf.j2s.core/src/net/sf/j2s/core/astvisitors/ASTTigerVisitor.java

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package test;
2+
3+
class Test_Assert {
4+
5+
@SuppressWarnings("unchecked")
6+
public static void main(String[] args) {
7+
ClassLoader.getSystemClassLoader().setClassAssertionStatus("test.Test_assert2", true);
8+
try {
9+
Class<Test_assert2> cl = (Class<Test_assert2>) ClassLoader.getSystemClassLoader().loadClass("test.Test_assert2");
10+
Test_assert2 ta2 = cl.newInstance();
11+
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
12+
// TODO Auto-generated catch block
13+
e.printStackTrace();
14+
}
15+
}
16+
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package test;
2+
3+
class Test_Clone implements Cloneable {
4+
int i = 30;
5+
int[] ia = new int[3];
6+
7+
public static void main(String[] args) {
8+
Test_Clone tc = new Test_Clone();
9+
try {
10+
tc.i = 50;
11+
Test_Clone tc1 = (Test_Clone) tc.clone();
12+
tc.ia[1] = 33;
13+
System.out.println(tc.i + " " + tc1.i + " " + tc.ia[1] + " " + tc1.ia[1]);
14+
assert (tc.i == tc1.i && tc.ia == tc1.ia);
15+
} catch (CloneNotSupportedException e) {
16+
// TODO Auto-generated catch block
17+
e.printStackTrace();
18+
}
19+
}
20+
21+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package test;
2+
3+
/**
4+
* @j2sXHTML
5+
*
6+
* <a href=testing>testing</a>
7+
*
8+
*/
9+
class Test_Native {
10+
11+
private abstract class IF {
12+
abstract void testing(int a);
13+
}
14+
15+
int test = 3;
16+
17+
public native void testNative();
18+
19+
public native void testNative2();
20+
21+
/**
22+
* @j2sNative
23+
*
24+
* return /-* testing *-/ and <@>here
25+
*
26+
*/
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
public void test() {
39+
}
40+
41+
public void test2() {
42+
/**
43+
* @j2sNative
44+
*
45+
* return /-* testing *-/ and <@>here
46+
*
47+
*/
48+
{
49+
}
50+
}
51+
52+
public static void main(String[] args) {
53+
}
54+
55+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package test;
2+
3+
class Test_assert2 {
4+
5+
public Test_assert2() {
6+
boolean assertOn = false;
7+
assert assertOn = true;
8+
9+
System.out.println("assert is " + (assertOn ? "enabled" : "disabled"));
10+
assert assertOn;
11+
}
12+
13+
14+
}

0 commit comments

Comments
 (0)