X Tutup
Skip to content

Commit a8eb7f5

Browse files
author
zhourenjian
committed
1. Add IE special CSS packing support
2. Fix bug of *.js compressing
1 parent 7ea74bf commit a8eb7f5

File tree

3 files changed

+144
-72
lines changed

3 files changed

+144
-72
lines changed

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/PackCSSIntoJS.java

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.io.File;
1515
import java.io.FileFilter;
1616
import java.io.FileInputStream;
17+
import java.io.FileNotFoundException;
1718
import java.io.FileOutputStream;
1819
import java.io.IOException;
1920

@@ -56,7 +57,8 @@ public boolean accept(File pathname) {
5657
}
5758
File[] cssFiles = folder.listFiles(new FileFilter() {
5859
public boolean accept(File pathname) {
59-
if (pathname.isFile() && pathname.getName().endsWith(".css")) {
60+
String fileName = pathname.getName();
61+
if (pathname.isFile() && fileName.endsWith(".css") && !fileName.endsWith(".IE.css")) {
6062
return true;
6163
}
6264
return false;
@@ -68,59 +70,80 @@ public boolean accept(File pathname) {
6870
File jsFile = new File(path.substring(0, path.length() - 4) + ".js");
6971
if (jsFile.exists()) {
7072
String jsContent = RegExCompress.readFileAll(new FileInputStream(jsFile));
71-
String key = "$WTC$$.registerCSS";
72-
int idx1 = jsContent.indexOf(key);
73-
if (idx1 != -1) {
74-
int idx2 = jsContent.indexOf(");", idx1);
75-
if (idx2 != -1) {
76-
int idx3 = jsContent.indexOf(",", idx1);
77-
if (idx3 == -1 || idx3 > idx2) {
78-
// not packed yet
79-
idx3 = idx2;
80-
idx2 += 2;
81-
} else {
82-
// already packed
83-
idx2 = jsContent.indexOf(");\r\n", idx1);
84-
if (idx2 == -1) {
85-
System.err.println("O, no, packed CSS is not packed correctly.");
86-
} else {
87-
idx2 += 4;
88-
}
89-
}
90-
String cssContent = RegExCompress.readFileAll(new FileInputStream(cssFile));
91-
cssContent = cssContent
92-
.replaceAll("\\s*[\\r\\n]+", "\n")
93-
// It's OK to remove unnecessary whitespace and line breaks
94-
.replaceAll("[\\r\\n]+\\s*", "\n")
95-
.replaceAll("\\\\", "\\\\\\\\")
96-
//.replaceAll("\\t", "\\\\t")
97-
//.replaceAll("\\r", "\\\\r")
98-
//.replaceAll("\\n", "\\\\n")
99-
.replaceAll("\\t", "")
100-
.replaceAll("\\r", "\\\\r")
101-
.replaceAll("\\n", "\\\\n")
102-
.replaceAll("\\'", "\\\\'")
103-
.replaceAll("\\\"", "\\\\\"");
104-
String jsContent2 = jsContent.substring(0, idx3) + ", \"" + cssContent + "\");\r\n" + jsContent.substring(idx2);
105-
if (!jsContent.equals(jsContent2)) {
106-
System.out.println("Updating " + jsFile.getName() + " ...");
107-
/*
108-
FileOutputStream fos = new FileOutputStream(jsFile);
109-
fos.write(jsContent2.getBytes());
110-
fos.close();
111-
*/
112-
try {
113-
FileOutputStream fos = new FileOutputStream(jsFile);
114-
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
115-
fos.write(jsContent2.getBytes("UTF-8"));
116-
fos.close();
117-
} catch (IOException e) {
118-
e.printStackTrace();
119-
}
120-
}
73+
int index = 0;
74+
String jsContentAfter = mergeCSS(cssFile, jsContent, index);
75+
if (!jsContent.equals(jsContentAfter)) {
76+
System.out.println("Updating " + jsFile.getName() + " ...");
77+
/*
78+
FileOutputStream fos = new FileOutputStream(jsFile);
79+
fos.write(jsContentAfter.getBytes());
80+
fos.close();
81+
*/
82+
try {
83+
FileOutputStream fos = new FileOutputStream(jsFile);
84+
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
85+
fos.write(jsContentAfter.getBytes("UTF-8"));
86+
fos.close();
87+
} catch (IOException e) {
88+
e.printStackTrace();
12189
}
12290
}
12391
}
12492
}
12593
}
94+
95+
private static String mergeCSS(File cssFile, String jsContent, int index)
96+
throws FileNotFoundException {
97+
String key = "$WTC$$.registerCSS";
98+
int idx1 = jsContent.indexOf(key, index);
99+
if (idx1 != -1) {
100+
int idx2 = jsContent.indexOf(");", idx1);
101+
if (idx2 != -1) {
102+
int idx3 = jsContent.indexOf(",", idx1);
103+
if (idx3 == -1 || idx3 > idx2) {
104+
// not packed yet
105+
idx3 = idx2;
106+
idx2 += 2;
107+
} else {
108+
// already packed
109+
idx2 = jsContent.indexOf(");\r\n", idx1);
110+
if (idx2 == -1) {
111+
System.err.println("O, no, packed CSS is not packed correctly.");
112+
} else {
113+
idx2 += 4;
114+
}
115+
}
116+
String cssContent = readCSSFileContent(cssFile);
117+
String alreadyMerged = jsContent.substring(0, idx3) + ", \"" + cssContent + "\");\r\n";
118+
String jsContentAfter = alreadyMerged + jsContent.substring(idx2);
119+
String ieCSSPath = cssFile.getAbsolutePath().replaceAll("\\.css", ".IE.css");
120+
File ieCSSFile = new File(ieCSSPath);
121+
if (ieCSSFile.exists()) {
122+
return mergeCSS(ieCSSFile, jsContentAfter, alreadyMerged.length());
123+
} else {
124+
return jsContentAfter;
125+
}
126+
}
127+
}
128+
return jsContent;
129+
}
130+
131+
private static String readCSSFileContent(File cssFile)
132+
throws FileNotFoundException {
133+
String cssContent = RegExCompress.readFileAll(new FileInputStream(cssFile));
134+
cssContent = cssContent
135+
.replaceAll("\\s*[\\r\\n]+", "\n")
136+
// It's OK to remove unnecessary whitespace and line breaks
137+
.replaceAll("[\\r\\n]+\\s*", "\n")
138+
.replaceAll("\\\\", "\\\\\\\\")
139+
//.replaceAll("\\t", "\\\\t")
140+
//.replaceAll("\\r", "\\\\r")
141+
//.replaceAll("\\n", "\\\\n")
142+
.replaceAll("\\t", "")
143+
.replaceAll("\\r", "\\\\r")
144+
.replaceAll("\\n", "\\\\n")
145+
.replaceAll("\\'", "\\\\'")
146+
.replaceAll("\\\"", "\\\\\"");
147+
return cssContent;
148+
}
126149
}

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/RegExCompress.java

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public static String mk(String str, String regex, String replacement) {
3636
StringBuffer sb = new StringBuffer();
3737
do {
3838
if (matcher.group(10) != null) {
39-
matcher.appendReplacement(sb, "$10 $11");
39+
matcher.appendReplacement(sb, "$11 $12");
4040
} else if (matcher.group(13) != null) {
41-
matcher.appendReplacement(sb, "$13 $14");
41+
matcher.appendReplacement(sb, "$14 $15");
4242
} else {
4343
matcher.appendReplacement(sb, replacement);
4444
}
@@ -98,34 +98,78 @@ private static void pack(File src, File dest, boolean completelyCompress) throws
9898
}
9999
}
100100
public static String regexCompress(String str) {
101+
str = str.replaceAll("(\r([^\n]))|(([^\r])\n)", "$4\r\n$2"); // fix line terminators
102+
boolean ignoreCSS = false;
103+
String cssCodes = null;
104+
int idx1 = str.indexOf("$WTC$$.registerCSS");
105+
int idx2 = -1;
106+
String specialFunKey = "@324@();\r\n";
107+
if (idx1 != -1) {
108+
idx2 = str.indexOf("\");\r\n", idx1);
109+
if (idx2 != -1) {
110+
ignoreCSS = true;
111+
cssCodes = str.substring(idx1, idx2 + 5);
112+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
113+
}
114+
}
101115
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
102-
"(\"[^\\n\\r]*[^\\\\]\")|" + // 1:2
103-
"(\\/\\/[^\\n\\r]*$)|" + // 1:3
104-
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:4,5
105-
"(\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:6,7
106-
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:8
116+
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 1:3
117+
"(\\/\\/[^\\n\\r]*[\\n\\r])|" + // 1:4
118+
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:5,6
119+
"(\\s+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:7,8
120+
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:9
107121
//str = str.replaceAll(regEx, "$1$2$7$8");
108122
str = mk(str, regEx +
109-
"|((\\b|\\x24)\\s+(\\b|\\x24))|" + // 3:9,10,11
110-
"(([+\\-])\\s+([+\\-]))|" + // 3:12,13,14
123+
"|((\\b|\\x24)\\s+(\\b|\\x24))|" + // 3:10,11,12
124+
"(([+\\-])\\s+([+\\-]))|" + // 3:13,14,15
111125
"(\\s+)",
112-
"$1$2$7$8");
126+
"$1$2$8$9");
127+
if (ignoreCSS) {
128+
int idx = str.indexOf(specialFunKey);
129+
if (idx != -1) {
130+
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
131+
} else {
132+
System.err.println("Error! Fail to ignore CSS codes!");
133+
}
134+
}
113135
return str;
114136
}
115137
public static String regexCompress2(String str) {
138+
str = str.replaceAll("(\r([^\n]))|(([^\r])\n)", "$4\r\n$2"); // fix line terminators
139+
boolean ignoreCSS = false;
140+
String cssCodes = null;
141+
int idx1 = str.indexOf("$WTC$$.registerCSS");
142+
int idx2 = -1;
143+
String specialFunKey = "@324@();\r\n";
144+
if (idx1 != -1) {
145+
idx2 = str.indexOf("\");\r\n", idx1);
146+
if (idx2 != -1) {
147+
ignoreCSS = true;
148+
cssCodes = str.substring(idx1, idx2 + 5);
149+
str = str.substring(0, idx1) + specialFunKey + str.substring(idx2 + 5);
150+
}
151+
}
116152
String whiteSpace = "[ \\f\\t\\v]";
117153
String regEx = "('[^\\n\\r]*[^\\\\]')|" + // 1:1
118-
"(\"[^\\n\\r]*[^\\\\]\")|" + // 1:2
119-
"(\\/\\/[^\\n\\r]*$)|" + // 1:3 // line comments
120-
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:4,5 // block comments
121-
"(" + whiteSpace + "+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:6,7 // regular expression
122-
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:8 // regular expression
154+
"(\"([^\\n\\r\\\"]|\\\\\\\")*[^\\\\]\")|" + // 2:2,3
155+
"(\\/\\/[^\\n\\r]*[\\n\\r])|" + // 1:4 // line comments
156+
"(\\/\\*[^*]*\\*+([^\\/][^*]*\\*+)*\\/)|" + // 2:5,6 // block comments
157+
"(" + whiteSpace + "+(\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*))|" + // 2:7,8 // regular expression
158+
"([^\\w\\x24\\/'\"*)\\?:]\\/[^\\/\\n\\r\\*][^\\/\\n\\r]*\\/[gim]*)"; // 1:9 // regular expression
123159
//str = str.replaceAll(regEx, "$1$2$7$8");
124160
str = mk(str, regEx +
125-
"|((\\b|\\x24)" + whiteSpace + "+(\\b|\\x24))|" + // 3:9,10,11
126-
"(([+\\-])" + whiteSpace + "+([+\\-]))|" + // 3:12,13,14
161+
"|((\\b|\\x24)" + whiteSpace + "+(\\b|\\x24))|" + // 3:10,11,12
162+
"(([+\\-])" + whiteSpace + "+([+\\-]))|" + // 3:13,14,15
127163
"(" + whiteSpace + "+)",
128-
"$1$2$7$8");
164+
"$1$2$8$9");
165+
if (ignoreCSS) {
166+
int idx = str.indexOf(specialFunKey);
167+
if (idx != -1) {
168+
str = str.substring(0, idx) + cssCodes + str.substring(idx + specialFunKey.length());
169+
} else {
170+
System.err.println("Error! Fail to ignore CSS codes!");
171+
}
172+
}
129173
return str;
130174
}
131175
public static String readFileAll(InputStream res) {

sources/net.sf.j2s.lib/src/net/sf/j2s/lib/build/UTF8Concat.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ public static void main(String[] args) {
6767
if (s.startsWith(j2sKeySig)) {
6868
s = s.substring(j2sKeySig.length());
6969
}
70-
/*
71-
if (completelyCompressing) {
70+
//*
71+
if (noCompressing) {
72+
buf.append(s);
73+
} else if (completelyCompressing) {
7274
buf.append(RegExCompress.regexCompress(s));
7375
} else {
7476
buf.append(RegExCompress.regexCompress2(s));
7577
}
76-
*/
78+
//*/
7779
buf.append(s);
7880
} catch (FileNotFoundException e) {
7981
e.printStackTrace();
@@ -83,13 +85,16 @@ public static void main(String[] args) {
8385
try {
8486
FileOutputStream fos = new FileOutputStream(dest);
8587
fos.write(new byte[] {(byte) 0xef, (byte) 0xbb, (byte) 0xbf}); // UTF-8 header!
88+
fos.write(buf.toString().getBytes("UTF-8"));
89+
/*
8690
if (noCompressing) {
8791
fos.write(buf.toString().getBytes("UTF-8"));
8892
} else if (completelyCompressing) {
8993
fos.write(RegExCompress.regexCompress(buf.toString()).getBytes("UTF-8"));
9094
} else {
9195
fos.write(RegExCompress.regexCompress2(buf.toString()).getBytes("UTF-8"));
9296
}
97+
*/
9398
fos.close();
9499
} catch (IOException e) {
95100
e.printStackTrace();

0 commit comments

Comments
 (0)
X Tutup