55import java .io .FileInputStream ;
66import java .io .FileOutputStream ;
77import java .io .IOException ;
8+ import java .util .ArrayList ;
89import java .util .HashMap ;
10+ import java .util .Iterator ;
11+ import java .util .List ;
912import java .util .Map ;
13+ import java .util .regex .Matcher ;
14+ import java .util .regex .Pattern ;
1015
1116public class SourceUtils {
1217
18+ public static void insertLineComment (StringBuffer source , String indent , int index , boolean blankLine ) {
19+ source .append (indent );
20+ source .append ("//+$" );
21+ source .append (index );
22+ source .append ("+\r \n " );
23+ source .append (indent );
24+ source .append ("//-$" );
25+ source .append (index );
26+ source .append ("-\r \n " );
27+ if (blankLine ) {
28+ source .append ("\r \n " );
29+ }
30+ }
31+
32+ public static void wrapALineWithLineComment (StringBuffer source , String indent , int index , String line , boolean blankLine ) {
33+ source .append (indent );
34+ source .append ("//+$" );
35+ source .append (index );
36+ source .append ("+\r \n " );
37+ source .append (indent );
38+ source .append (line );
39+ source .append ("\r \n " );
40+ source .append (indent );
41+ source .append ("//-$" );
42+ source .append (index );
43+ source .append ("-\r \n " );
44+ if (blankLine ) {
45+ source .append ("\r \n " );
46+ }
47+ }
48+
49+ public static void openLineComment (StringBuffer source , String indent , int index ) {
50+ source .append (indent );
51+ source .append ("//+$" );
52+ source .append (index );
53+ source .append ("+\r \n " );
54+ }
55+
56+ public static void closeLineComment (StringBuffer source , String indent , int index , boolean blankLine ) {
57+ source .append (indent );
58+ source .append ("//-$" );
59+ source .append (index );
60+ source .append ("-\r \n " );
61+ if (blankLine ) {
62+ source .append ("\r \n " );
63+ }
64+ }
65+
66+ public static void insertBlockComment (StringBuffer source , int index ) {
67+ source .append (" /*+$" );
68+ source .append (index );
69+ source .append ("+*/ /*-$" );
70+ source .append (index );
71+ source .append ("-*/ " );
72+ }
73+
1374 public static void updateSourceContent (File file , String source ) {
1475 FileInputStream fis = null ;
1576 byte [] buffer = new byte [8096 ];
@@ -32,68 +93,90 @@ public static void updateSourceContent(File file, String source) {
3293 }
3394 }
3495 String oldSource = baos .toString ();
35- Map <Integer , String > map = new HashMap <Integer , String >();
36- for (int i = 0 ; i < 100 ; i ++) {
37- boolean got = false ;
38- String key1 = "//+$" + i + "+" ;
39- int idx1 = oldSource .indexOf (key1 );
40- if (idx1 != -1 ) {
41- String key2 = "//-$" + i + "-" ;
42- int idx2 = oldSource .indexOf (key2 );
43- if (idx2 != -1 ) {
44- String s = oldSource .substring (idx1 + key1 .length (), idx2 );
45- map .put (i , s );
46- got = true ;
47- }
96+ if (oldSource .length () > 0 ) {
97+ Pattern regExp = Pattern .compile ("\\ /[\\ /|\\ *]\\ +\\ $(\\ d+)\\ +" );
98+ Matcher matcher = regExp .matcher (source );
99+ List <String > allIndexes = new ArrayList <String >();
100+ while (matcher .find ()) {
101+ String indexStr = matcher .group (1 );
102+ allIndexes .add (indexStr );
48103 }
49- if (!got ) {
50- key1 = "/*+$" + i + "+*/" ;
51- idx1 = oldSource .indexOf (key1 );
104+ int position = 0 ;
105+ Map <String , String > map = new HashMap <String , String >();
106+ for (Iterator <String > itr = allIndexes .iterator (); itr .hasNext ();) {
107+ String i = (String ) itr .next ();
108+ boolean got = false ;
109+ String key1 = "//+$" + i + "+" ;
110+ int idx1 = oldSource .indexOf (key1 , position );
52111 if (idx1 != -1 ) {
53- String key2 = "/* -$" + i + "-*/ " ;
54- int idx2 = oldSource .indexOf (key2 );
112+ String key2 = "// -$" + i + "-" ;
113+ int idx2 = oldSource .indexOf (key2 , position + key1 . length () );
55114 if (idx2 != -1 ) {
56115 String s = oldSource .substring (idx1 + key1 .length (), idx2 );
57116 map .put (i , s );
58117 got = true ;
118+ position = idx2 + key2 .length ();
59119 }
60120 }
61- }
62- }
63- for (int i = 0 ; i < 100 ; i ++) {
64- boolean changed = false ;
65- String key1 = "//+$" + i + "+" ;
66- int idx1 = source .indexOf (key1 );
67- if (idx1 != -1 ) {
68- String key2 = "//-$" + i + "-" ;
69- int idx2 = source .indexOf (key2 );
70- if (idx2 != -1 ) {
71- String ss = source .substring (idx1 + key1 .length (), idx2 );
72- String s = map .get (i );
73- if (s != null && !s .equals (ss )) {
74- source = source .substring (0 , idx1 + key1 .length ()) + s + source .substring (idx2 );
75- changed = true ;
121+ if (!got ) {
122+ key1 = "/*+$" + i + "+*/" ;
123+ idx1 = oldSource .indexOf (key1 , position );
124+ if (idx1 != -1 ) {
125+ String key2 = "/*-$" + i + "-*/" ;
126+ int idx2 = oldSource .indexOf (key2 , position + key1 .length ());
127+ if (idx2 != -1 ) {
128+ String s = oldSource .substring (idx1 + key1 .length (), idx2 );
129+ map .put (i , s );
130+ got = true ;
131+ position = idx2 + key2 .length ();
132+ }
76133 }
77134 }
78135 }
79- if (!changed ) {
80- key1 = "/*+$" + i + "+*/" ;
81- idx1 = source .indexOf (key1 );
136+ position = 0 ;
137+ for (Iterator <String > itr = allIndexes .iterator (); itr .hasNext ();) {
138+ String i = (String ) itr .next ();
139+ boolean changed = false ;
140+ String key1 = "//+$" + i + "+" ;
141+ int idx1 = source .indexOf (key1 , position );
82142 if (idx1 != -1 ) {
83- String key2 = "/* -$" + i + "-*/ " ;
84- int idx2 = source .indexOf (key2 );
143+ String key2 = "// -$" + i + "-" ;
144+ int idx2 = source .indexOf (key2 , position + key1 . length () );
85145 if (idx2 != -1 ) {
86146 String ss = source .substring (idx1 + key1 .length (), idx2 );
87147 String s = map .get (i );
88- if (s != null && !s .equals (ss )) {
148+ if (s != null && !s .equals (ss )
149+ && !(s .trim ().length () == 0 && ss .trim ().length () == 0 )) {
89150 source = source .substring (0 , idx1 + key1 .length ()) + s + source .substring (idx2 );
90151 changed = true ;
91152 }
153+ position = idx2 + key2 .length ();
92154 }
93155 }
156+ if (!changed ) {
157+ key1 = "/*+$" + i + "+*/" ;
158+ idx1 = source .indexOf (key1 , position );
159+ if (idx1 != -1 ) {
160+ String key2 = "/*-$" + i + "-*/" ;
161+ int idx2 = source .indexOf (key2 , position + key1 .length ());
162+ if (idx2 != -1 ) {
163+ String ss = source .substring (idx1 + key1 .length (), idx2 );
164+ String s = map .get (i );
165+ if (s != null && !s .equals (ss )
166+ && !(s .trim ().length () == 0 && ss .trim ().length () == 0 )) {
167+ source = source .substring (0 , idx1 + key1 .length ()) + s + source .substring (idx2 );
168+ changed = true ;
169+ }
170+ }
171+ position = idx2 + key2 .length ();
172+ }
173+ }
174+ }
175+
176+ if (source .equals (oldSource )) {
177+ return ;
94178 }
95179 }
96-
97180 FileOutputStream fos = null ;
98181 try {
99182 fos = new FileOutputStream (file );
0 commit comments