|
1 /* |
|
2 * Copyright 1997 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /**/ |
|
25 |
|
26 |
|
27 public class LineGenerator { |
|
28 |
|
29 IntGenerator ig; |
|
30 StringGenerator sg; |
|
31 int limit; |
|
32 |
|
33 public LineGenerator(IntGenerator ig, StringGenerator sg, int limit) { |
|
34 this.ig = ig; |
|
35 this.sg = sg; |
|
36 this.limit = limit; |
|
37 } |
|
38 |
|
39 public LineGenerator(IntGenerator ig) { |
|
40 this.ig = ig; |
|
41 this.sg = new StringGenerator(ig); |
|
42 this.limit = -1; |
|
43 } |
|
44 |
|
45 private char prevTerminator = 0; |
|
46 private int count = 0; |
|
47 public String lineTerminator; |
|
48 |
|
49 public String next() { |
|
50 if ((count >= limit) && (limit >= 0)) |
|
51 return null; |
|
52 |
|
53 String l = sg.next(); |
|
54 |
|
55 /* Avoid "\r\n" sequences |
|
56 in which the '\n' terminates a blank line */ |
|
57 int len = l.length(); |
|
58 int t; |
|
59 do |
|
60 t = ig.next(2); |
|
61 while ((prevTerminator == '\r') && (len == 0) && (t == 0)); |
|
62 |
|
63 String ts; |
|
64 switch (t) { |
|
65 case 0: |
|
66 ts = "\n"; |
|
67 prevTerminator = '\n'; |
|
68 break; |
|
69 case 1: |
|
70 ts = "\r"; |
|
71 prevTerminator = '\r'; |
|
72 break; |
|
73 case 2: |
|
74 default: |
|
75 ts = "\r\n"; |
|
76 prevTerminator = '\n'; |
|
77 break; |
|
78 } |
|
79 |
|
80 count++; |
|
81 lineTerminator = ts; |
|
82 return l; |
|
83 } |
|
84 } |