JAL-1807 still testing
[jalviewjs.git] / unused / com / stevesoft / pat / RegexWriter.java
1 //\r
2 // This software is now distributed according to\r
3 // the Lesser Gnu Public License.  Please see\r
4 // http://www.gnu.org/copyleft/lesser.txt for\r
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 \r
10 import java.io.IOException;\r
11 import java.io.Writer;\r
12 \r
13 import com.stevesoft.pat.wrap.WriterWrap;\r
14 \r
15 /**\r
16  * A basic extension of FilterWriter that uses Transformer to make replacements\r
17  * in data as it is written out. It attempts to transform a string whenever the\r
18  * End-of-Line (EOL) character is written (which is, by default, the carriage\r
19  * return '\n'). Only the transformed portion of the line is written out,\r
20  * allowing the RegexWriter to wait until a complete pattern is present before\r
21  * attempting to write out info. Until a pattern completes, data is stored in a\r
22  * javajs.util.SB -- which can be accessed through the length() and charAt()\r
23  * methods of this class.\r
24  * <p>\r
25  * Note a subtlety here -- while a Transformer normally matches at higher\r
26  * priority against the pattern added to it first, this will not necessarily be\r
27  * true when a multi-line match is in progress because one of the complete\r
28  * multi-line patterns may not be completely loaded in RegexWriter's buffer. For\r
29  * this reason, the Transformer class is equipped with a way to add a pattern\r
30  * and replacement rule in three pieces -- a beginning (once this matches,\r
31  * nothing else in the Transformer can match until the whole pattern matches),\r
32  * an ending (the whole pattern is a String formed by adding the beginning and\r
33  * ending), and a ReplaceRule.\r
34  * <p>\r
35  * An illustration of this is given in the this <a\r
36  * href="../test/trans.java">example.</a>\r
37  */\r
38 public class RegexWriter extends Writer\r
39 {\r
40   Replacer repr;\r
41 \r
42   Writer w;\r
43 \r
44   WriterWrap ww;\r
45 \r
46   javajs.util.SB sb = new javajs.util.SB();\r
47 \r
48   PartialBuffer wrap = new PartialBuffer(sb);\r
49 \r
50   int pos, epos;\r
51 \r
52   int interval = 128;\r
53 \r
54   int bufferSize = 2 * 1024;\r
55 \r
56   public RegexWriter(Transformer t, Writer w)\r
57   {\r
58     this.w = w;\r
59     ww = new WriterWrap(w);\r
60     repr = t.getReplacer();\r
61     repr.setBuffer(new StringBufferLike(ww));\r
62     repr.setSource(wrap);\r
63   }\r
64 \r
65   public RegexWriter(Regex r, Writer w)\r
66   {\r
67     this.w = w;\r
68     ww = new WriterWrap(w);\r
69     repr = r.getReplacer();\r
70     repr.setBuffer(new StringBufferLike(ww));\r
71     repr.setSource(wrap);\r
72   }\r
73 \r
74   char EOLchar = '\n';\r
75 \r
76   /**\r
77    * This method no longer serves any purpose.\r
78    * \r
79    * @deprecated\r
80    */\r
81   @Deprecated\r
82   public char getEOLchar()\r
83   {\r
84     return EOLchar;\r
85   }\r
86 \r
87   /**\r
88    * This method no longer serves any purpose.\r
89    * \r
90    * @deprecated\r
91    */\r
92   @Deprecated\r
93   public void setEOLchar(char c)\r
94   {\r
95     EOLchar = c;\r
96   }\r
97 \r
98   int max_lines = 2;\r
99 \r
100   /**\r
101    * This method no longer serves any purpose.\r
102    * \r
103    * @deprecated\r
104    */\r
105   @Deprecated\r
106   public int getMaxLines()\r
107   {\r
108     return max_lines;\r
109   }\r
110 \r
111   /**\r
112    * This method no longer serves any purpose.\r
113    * \r
114    * @deprecated\r
115    */\r
116   @Deprecated\r
117   public void setMaxLines(int ml)\r
118   {\r
119     max_lines = ml;\r
120   }\r
121 \r
122   void write() throws IOException\r
123   {\r
124     Regex rex = repr.getRegex();\r
125     int eposOld = epos;\r
126     if (rex.matchAt(wrap, epos) && !wrap.overRun)\r
127     {\r
128       while (pos < epos)\r
129       {\r
130         w.write(sb.charAt(pos++));\r
131       }\r
132       int to = rex.matchedTo();\r
133       repr.setPos(to);\r
134       repr.apply(rex, rex.getReplaceRule());\r
135       epos = pos = to;\r
136       if (epos == eposOld && epos < sb.length())\r
137       {\r
138         epos++;\r
139       }\r
140     }\r
141     else if (!wrap.overRun && epos < sb.length())\r
142     {\r
143       epos++;\r
144     }\r
145     while (pos < epos)\r
146     {\r
147       w.write(sb.charAt(pos++));\r
148     }\r
149     if (epos == sb.length())\r
150     {\r
151       sb.setLength(1);\r
152       pos = epos = 1;\r
153     }\r
154     else if (pos > bufferSize)\r
155     {\r
156         \r
157         String s = sb.substring(bufferSize);\r
158         sb.setLength(0);\r
159         sb.append(s);\r
160 //      for (int i = bufferSize; i < sb.length(); i++)\r
161 //      {\r
162 //        sb.setCharAt(i - bufferSize, sb.charAt(i));\r
163 //      }\r
164       pos -= bufferSize;\r
165       epos -= bufferSize;\r
166       sb.setLength(sb.length() - bufferSize);\r
167     }\r
168   }\r
169 \r
170   public void write(char[] ca, int b, int n) throws IOException\r
171   {\r
172     int m = b + n;\r
173     for (int i = b; i < m; i++)\r
174     {\r
175       sb.appendC(ca[i]);\r
176       if (sb.length() % interval == interval - 1)\r
177       {\r
178         wrap.overRun = false;\r
179         while (epos + interval < sb.length() && !wrap.overRun)\r
180         {\r
181           write();\r
182         }\r
183       }\r
184     }\r
185   }\r
186 \r
187   public void flush() throws IOException\r
188   {\r
189   }\r
190 \r
191   public void close() throws IOException\r
192   {\r
193     wrap.allowOverRun = false;\r
194     wrap.overRun = false;\r
195     while (epos < sb.length())\r
196     {\r
197       write();\r
198     }\r
199     write();\r
200     w.close();\r
201   }\r
202 \r
203   /** The current size of the javajs.util.SB in use by RegexWriter. */\r
204   public int length()\r
205   {\r
206     return sb.length();\r
207   }\r
208 \r
209   /** The character at location i in the StringBuffer. */\r
210   public char charAt(int i)\r
211   {\r
212     return sb.charAt(i);\r
213   }\r
214 \r
215   /** Set the interval at which regex patterns are checked. */\r
216   public void setInterval(int i)\r
217   {\r
218     interval = i;\r
219   }\r
220 \r
221   /** Get the interval at which regex matches are checked. */\r
222   public int getInterval()\r
223   {\r
224     return interval;\r
225   }\r
226 \r
227   /** Get the buffer size. */\r
228   public int getBufferSize()\r
229   {\r
230     return bufferSize;\r
231   }\r
232 \r
233   /** Set the buffer size. */\r
234   public void setBufferSize(int i)\r
235   {\r
236     bufferSize = i;\r
237   }\r
238 }\r