1414import java .io .File ;
1515import java .io .FileFilter ;
1616import java .io .FileInputStream ;
17+ import java .io .FileNotFoundException ;
1718import java .io .FileOutputStream ;
1819import 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}
0 commit comments