1 | package org.greenstone.gsdl3.gs3build.util;
|
---|
2 |
|
---|
3 | public class GS2TextFileHandler
|
---|
4 | {
|
---|
5 | String content;
|
---|
6 | String line;
|
---|
7 |
|
---|
8 | public GS2TextFileHandler(String content)
|
---|
9 | { this.content = content;
|
---|
10 | }
|
---|
11 |
|
---|
12 | public boolean hasMore()
|
---|
13 | { return this.line != null;
|
---|
14 | }
|
---|
15 |
|
---|
16 | public boolean hasMoreLines()
|
---|
17 | { return this.content != null;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public String getEntry()
|
---|
21 | { return this.getEntry(false);
|
---|
22 | }
|
---|
23 |
|
---|
24 | public String getEntry(boolean breakSpace)
|
---|
25 | { String reply;
|
---|
26 | int start, tab = 0;
|
---|
27 | boolean quoted = false;
|
---|
28 |
|
---|
29 | if (!this.hasMore()) {
|
---|
30 | return null;
|
---|
31 | }
|
---|
32 |
|
---|
33 | start = 0;
|
---|
34 | while (start < this.line.length() &&
|
---|
35 | this.line.charAt(start) == ' ') {
|
---|
36 | start ++;
|
---|
37 | }
|
---|
38 |
|
---|
39 | if (start == this.line.length()) {
|
---|
40 | this.line = null;
|
---|
41 | return null;
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (this.line.charAt(start) == '"') {
|
---|
45 | quoted = true;
|
---|
46 | breakSpace = false;
|
---|
47 | start ++;
|
---|
48 | }
|
---|
49 | tab = start;
|
---|
50 |
|
---|
51 | while (tab != this.line.length() &&
|
---|
52 | this.line.charAt(tab) != '\t' &&
|
---|
53 | !(quoted && this.line.charAt(tab) == '"') &&
|
---|
54 | !(this.line.charAt(tab) == ' ' && breakSpace))
|
---|
55 | { tab ++;
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (start > 0) {
|
---|
59 | this.line = this.line.substring(start);
|
---|
60 | tab -= start;
|
---|
61 | }
|
---|
62 |
|
---|
63 | if (tab == this.line.length()) {
|
---|
64 | reply = this.line;
|
---|
65 | this.line = null;
|
---|
66 | }
|
---|
67 | else {
|
---|
68 | reply = this.line.substring(0, tab);
|
---|
69 | this.line = this.line.substring(tab+1);
|
---|
70 | }
|
---|
71 |
|
---|
72 | reply.trim();
|
---|
73 |
|
---|
74 | return reply;
|
---|
75 | }
|
---|
76 |
|
---|
77 | public String getLine()
|
---|
78 | { if (this.content == null) {
|
---|
79 | this.line = null;
|
---|
80 | return null;
|
---|
81 | }
|
---|
82 |
|
---|
83 | do {
|
---|
84 | int eol = this.content.indexOf('\n');
|
---|
85 | if (eol < 0) {
|
---|
86 | this.line = this.content;
|
---|
87 | this.content = null;
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | this.line = this.content.substring(0, eol);
|
---|
91 | this.content = this.content.substring(eol+1);
|
---|
92 | while (this.content.length() > 0 &&
|
---|
93 | this.content.charAt(0) < ' ')
|
---|
94 | { this.content = this.content.substring(1);
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (this.line != null) {
|
---|
99 | this.line.trim();
|
---|
100 | }
|
---|
101 | if (this.line.length() > 0 &&
|
---|
102 | this.line.charAt(this.line.length() - 1) == (char) 13) {
|
---|
103 | this.line = this.line.substring(0, this.line.length()-1);
|
---|
104 | this.line.trim();
|
---|
105 | }
|
---|
106 | } while (this.content != null && this.line != null && this.line.length() == 0);
|
---|
107 |
|
---|
108 | return this.line;
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|