|
@@ -1,220 +1,220 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.aokeyhole.objects.support; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.aokeyhole.objects.*; |
5 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
6 |
|
- |
|
7 |
|
- |
public class ToLinesObjectsParseStream { |
8 |
|
- |
private static final Line NULL = new Line( -1, null ); |
9 |
|
- |
|
10 |
|
- |
private String[] mLines; |
11 |
|
- |
private int mNextIndex = 0; |
12 |
|
- |
private Line mCurrent, mNext; |
13 |
|
- |
|
14 |
|
- |
public ToLinesObjectsParseStream( String... pLines ) { |
15 |
|
- |
mLines = (pLines != null) ? pLines : Strings.EMPTY_ARRAY; |
16 |
|
- |
mCurrent = NULL; |
17 |
|
- |
readNext(); |
18 |
|
- |
} |
19 |
|
- |
|
20 |
|
- |
public boolean readLine() { |
21 |
|
- |
mCurrent = mNext; |
22 |
|
- |
readNext(); |
23 |
|
- |
return mCurrent.isNotIgnorable(); |
24 |
|
- |
} |
25 |
|
- |
|
26 |
|
- |
private boolean isAnotherLine() { |
27 |
|
- |
return (mNextIndex < mLines.length); |
28 |
|
- |
} |
29 |
|
- |
|
30 |
|
- |
private void readNext() { |
31 |
|
- |
while ( isAnotherLine() ) { |
32 |
|
- |
mNext = new Line( mNextIndex, mLines[mNextIndex] ); |
33 |
|
- |
mNextIndex++; |
34 |
|
- |
if ( mNext.isNotIgnorable() ) { |
35 |
|
- |
return; |
36 |
|
- |
} |
37 |
|
- |
} |
38 |
|
- |
mNext = NULL; |
39 |
|
- |
} |
40 |
|
- |
|
41 |
|
- |
public ParseException error( String pError ) { |
42 |
|
- |
return mCurrent.error( pError ); |
43 |
|
- |
} |
44 |
|
- |
|
45 |
|
- |
public boolean isNextDeeper() { |
46 |
|
- |
return (mNext.getDepth() > mCurrent.getDepth()); |
47 |
|
- |
} |
48 |
|
- |
|
49 |
|
- |
public boolean isNextDepth( int pDepth ) { |
50 |
|
- |
return (mNext.getDepth() == pDepth); |
51 |
|
- |
} |
52 |
|
- |
|
53 |
|
- |
public void validateDepth( int pDepth ) { |
54 |
|
- |
mCurrent.validateDepth( pDepth ); |
55 |
|
- |
} |
56 |
|
- |
|
57 |
|
- |
public int getDepth() { |
58 |
|
- |
return mCurrent.getDepth(); |
59 |
|
- |
} |
60 |
|
- |
|
61 |
|
- |
public String getWhat() { |
62 |
|
- |
return mCurrent.getWhat(); |
63 |
|
- |
} |
64 |
|
- |
|
65 |
|
- |
public String getData() { |
66 |
|
- |
return mCurrent.getData(); |
67 |
|
- |
} |
68 |
|
- |
|
69 |
|
- |
public boolean isNextWhat( String pWhat ) { |
70 |
|
- |
return mNext.isWhat( pWhat ); |
71 |
|
- |
} |
72 |
|
- |
|
73 |
|
- |
public String[] parseData( char pSeperator ) { |
74 |
|
- |
String data = mCurrent.getData(); |
75 |
|
- |
int zParts = 1; |
76 |
|
- |
for ( int i = 0; i < data.length(); i++ ) { |
77 |
|
- |
if ( data.charAt( i ) == pSeperator ) { |
78 |
|
- |
zParts++; |
79 |
|
- |
} |
80 |
|
- |
} |
81 |
|
- |
String[] parts = new String[zParts]; |
82 |
|
- |
int part = 0; |
83 |
|
- |
int from = 0; |
84 |
|
- |
for ( int at; -1 != (at = data.indexOf( pSeperator, from )); from = at + 1 ) { |
85 |
|
- |
parts[part++] = data.substring( from, at ).trim(); |
86 |
|
- |
} |
87 |
|
- |
parts[part] = data.substring( from ).trim(); |
88 |
|
- |
return parts; |
89 |
|
- |
} |
90 |
|
- |
|
91 |
|
- |
public String[] parseData( int pExpectedParts, char pSeperator ) { |
92 |
|
- |
if ( pExpectedParts < 1 ) { |
93 |
|
- |
throw new IllegalArgumentException( "? ExpectedParts: " + pExpectedParts ); |
94 |
|
- |
} |
95 |
|
- |
String[] zParts = parseData( pSeperator ); |
96 |
|
- |
if ( zParts.length == pExpectedParts ) { |
97 |
|
- |
return zParts; |
98 |
|
- |
} |
99 |
|
- |
if ( zParts.length > pExpectedParts ) { |
100 |
|
- |
throw error( "Expected (" + pExpectedParts + ") Data Parts (seperated by '" + pSeperator + "'), but was (" + zParts.length + ") Parts" ); |
101 |
|
- |
} |
102 |
|
- |
String[] rv = new String[pExpectedParts]; |
103 |
|
- |
System.arraycopy( zParts, 0, rv, 0, zParts.length ); |
104 |
|
- |
return rv; |
105 |
|
- |
} |
106 |
|
- |
|
107 |
|
- |
// "vs:Simple|Name", |
108 |
|
- |
// "Object:A1|", |
109 |
|
- |
// ".ID:ID|Persisted|Simple", |
110 |
|
- |
// "Object:B1|A1", |
111 |
|
- |
// "..Active:true", |
112 |
|
- |
// ".ID:ID|Persisted|Simple", |
113 |
|
- |
// "...ObjectDisplayValueOrder:1", |
114 |
|
- |
// "...ColumnName:ID", |
115 |
|
- |
|
116 |
|
- |
private static class Line { |
117 |
|
- |
private int mIndex; |
118 |
|
- |
private String mOrigLine; |
119 |
|
- |
private boolean mIgnore = false; |
120 |
|
- |
private boolean mBadFormat = true; |
121 |
|
- |
private int mDepth = -1; |
122 |
|
- |
private String mWhat = ""; |
123 |
|
- |
private String mData = ""; |
124 |
|
- |
|
125 |
|
- |
public Line( int pIndex, String pOrigLine ) { |
126 |
|
- |
mIndex = pIndex; |
127 |
|
- |
mOrigLine = (pOrigLine == null) ? null : pOrigLine.trim(); |
128 |
|
- |
if ( (mOrigLine == null) || (mOrigLine.length() == 0) || mOrigLine.startsWith( "#" ) || mOrigLine.startsWith( "//" ) ) { |
129 |
|
- |
mBadFormat = false; |
130 |
|
- |
mIgnore = true; |
131 |
|
- |
return; |
132 |
|
- |
} |
133 |
|
- |
int colonAt = mOrigLine.indexOf( ':' ); |
134 |
|
- |
if ( colonAt == -1 ) { |
135 |
|
- |
return; |
136 |
|
- |
} |
137 |
|
- |
int depth = 0; |
138 |
|
- |
while ( mOrigLine.charAt( depth ) == '.' ) { |
139 |
|
- |
depth++; |
140 |
|
- |
} |
141 |
|
- |
mWhat = mOrigLine.substring( depth, colonAt ).trim(); |
142 |
|
- |
if ( mWhat.length() == 0 ) { |
143 |
|
- |
return; |
144 |
|
- |
} |
145 |
|
- |
mData = mOrigLine.substring( colonAt + 1 ).trim(); |
146 |
|
- |
mDepth = depth; |
147 |
|
- |
mBadFormat = false; |
148 |
|
- |
} |
149 |
|
- |
|
150 |
|
- |
public int getIndex() { |
151 |
|
- |
return mIndex; |
152 |
|
- |
} |
153 |
|
- |
|
154 |
|
- |
public String getOrigLine() { |
155 |
|
- |
return mOrigLine; |
156 |
|
- |
} |
157 |
|
- |
|
158 |
|
- |
public boolean isNotIgnorable() { |
159 |
|
- |
return !mIgnore; |
160 |
|
- |
} |
161 |
|
- |
|
162 |
|
- |
public int getDepth() { |
163 |
|
- |
return mDepth; |
164 |
|
- |
} |
165 |
|
- |
|
166 |
|
- |
public String getWhat() { |
167 |
|
- |
return mWhat; |
168 |
|
- |
} |
169 |
|
- |
|
170 |
|
- |
public String getData() { |
171 |
|
- |
return mData; |
172 |
|
- |
} |
173 |
|
- |
|
174 |
|
- |
public boolean isWhat( String pWhat ) { |
175 |
|
- |
return mWhat.equals( pWhat ); |
176 |
|
- |
} |
177 |
|
- |
|
178 |
|
- |
public void validateWhat( String pWhat ) { |
179 |
|
- |
if ( !isWhat( pWhat ) ) { |
180 |
|
- |
throw badWhat( pWhat ); |
181 |
|
- |
} |
182 |
|
- |
} |
183 |
|
- |
|
184 |
|
- |
public int validateWhat( String[] pWhats ) { |
185 |
|
- |
for ( int i = 0; i < pWhats.length; i++ ) { |
186 |
|
- |
if ( isWhat( pWhats[i] ) ) { |
187 |
|
- |
return i; |
188 |
|
- |
} |
189 |
|
- |
} |
190 |
|
- |
StringBuilder sb = new StringBuilder(); |
191 |
|
- |
for ( int i = pWhats.length; --i > 0; ) { |
192 |
|
- |
sb.append( pWhats[i] ); |
193 |
|
- |
sb.append( ", " ); |
194 |
|
- |
} |
195 |
|
- |
sb.append( "or " ); |
196 |
|
- |
sb.append( pWhats[0] ); |
197 |
|
- |
throw badWhat( sb.toString() ); |
198 |
|
- |
} |
199 |
|
- |
|
200 |
|
- |
public void validateDepth( int pDepth ) { |
201 |
|
- |
if ( mDepth != pDepth ) { |
202 |
|
- |
throw error( "Expected (" + pDepth + ") Depth, but was (" + mDepth + ")" ); |
203 |
|
- |
} |
204 |
|
- |
} |
205 |
|
- |
|
206 |
|
- |
private ParseException badWhat( String pExpected ) { |
207 |
|
- |
return error( "Expected (" + pExpected + "), but was (" + mWhat + ")" ); |
208 |
|
- |
} |
209 |
|
- |
|
210 |
|
- |
public void rejectBadFormat() { |
211 |
|
- |
if ( mBadFormat ) { |
212 |
|
- |
throw error( "Unacceptable Format" ); |
213 |
|
- |
} |
214 |
|
- |
} |
215 |
|
- |
|
216 |
|
- |
public ParseException error( String pError ) { |
217 |
|
- |
return new ParseException( pError + ", line (" + mIndex + ") was: " + mOrigLine ); |
218 |
|
- |
} |
219 |
|
- |
} |
220 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.aokeyhole.objects.support; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.aokeyhole.objects.*; |
|
5 |
+ |
import org.litesoft.commonfoundation.typeutils.*; |
|
6 |
+ |
|
|
7 |
+ |
public class ToLinesObjectsParseStream { |
|
8 |
+ |
private static final Line NULL = new Line( -1, null ); |
|
9 |
+ |
|
|
10 |
+ |
private String[] mLines; |
|
11 |
+ |
private int mNextIndex = 0; |
|
12 |
+ |
private Line mCurrent, mNext; |
|
13 |
+ |
|
|
14 |
+ |
public ToLinesObjectsParseStream( String... pLines ) { |
|
15 |
+ |
mLines = (pLines != null) ? pLines : Strings.EMPTY_ARRAY; |
|
16 |
+ |
mCurrent = NULL; |
|
17 |
+ |
readNext(); |
|
18 |
+ |
} |
|
19 |
+ |
|
|
20 |
+ |
public boolean readLine() { |
|
21 |
+ |
mCurrent = mNext; |
|
22 |
+ |
readNext(); |
|
23 |
+ |
return mCurrent.isNotIgnorable(); |
|
24 |
+ |
} |
|
25 |
+ |
|
|
26 |
+ |
private boolean isAnotherLine() { |
|
27 |
+ |
return (mNextIndex < mLines.length); |
|
28 |
+ |
} |
|
29 |
+ |
|
|
30 |
+ |
private void readNext() { |
|
31 |
+ |
while ( isAnotherLine() ) { |
|
32 |
+ |
mNext = new Line( mNextIndex, mLines[mNextIndex] ); |
|
33 |
+ |
mNextIndex++; |
|
34 |
+ |
if ( mNext.isNotIgnorable() ) { |
|
35 |
+ |
return; |
|
36 |
+ |
} |
|
37 |
+ |
} |
|
38 |
+ |
mNext = NULL; |
|
39 |
+ |
} |
|
40 |
+ |
|
|
41 |
+ |
public ParseException error( String pError ) { |
|
42 |
+ |
return mCurrent.error( pError ); |
|
43 |
+ |
} |
|
44 |
+ |
|
|
45 |
+ |
public boolean isNextDeeper() { |
|
46 |
+ |
return (mNext.getDepth() > mCurrent.getDepth()); |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
public boolean isNextDepth( int pDepth ) { |
|
50 |
+ |
return (mNext.getDepth() == pDepth); |
|
51 |
+ |
} |
|
52 |
+ |
|
|
53 |
+ |
public void validateDepth( int pDepth ) { |
|
54 |
+ |
mCurrent.validateDepth( pDepth ); |
|
55 |
+ |
} |
|
56 |
+ |
|
|
57 |
+ |
public int getDepth() { |
|
58 |
+ |
return mCurrent.getDepth(); |
|
59 |
+ |
} |
|
60 |
+ |
|
|
61 |
+ |
public String getWhat() { |
|
62 |
+ |
return mCurrent.getWhat(); |
|
63 |
+ |
} |
|
64 |
+ |
|
|
65 |
+ |
public String getData() { |
|
66 |
+ |
return mCurrent.getData(); |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
public boolean isNextWhat( String pWhat ) { |
|
70 |
+ |
return mNext.isWhat( pWhat ); |
|
71 |
+ |
} |
|
72 |
+ |
|
|
73 |
+ |
public String[] parseData( char pSeperator ) { |
|
74 |
+ |
String data = mCurrent.getData(); |
|
75 |
+ |
int zParts = 1; |
|
76 |
+ |
for ( int i = 0; i < data.length(); i++ ) { |
|
77 |
+ |
if ( data.charAt( i ) == pSeperator ) { |
|
78 |
+ |
zParts++; |
|
79 |
+ |
} |
|
80 |
+ |
} |
|
81 |
+ |
String[] parts = new String[zParts]; |
|
82 |
+ |
int part = 0; |
|
83 |
+ |
int from = 0; |
|
84 |
+ |
for ( int at; -1 != (at = data.indexOf( pSeperator, from )); from = at + 1 ) { |
|
85 |
+ |
parts[part++] = data.substring( from, at ).trim(); |
|
86 |
+ |
} |
|
87 |
+ |
parts[part] = data.substring( from ).trim(); |
|
88 |
+ |
return parts; |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
public String[] parseData( int pExpectedParts, char pSeperator ) { |
|
92 |
+ |
if ( pExpectedParts < 1 ) { |
|
93 |
+ |
throw new IllegalArgumentException( "? ExpectedParts: " + pExpectedParts ); |
|
94 |
+ |
} |
|
95 |
+ |
String[] zParts = parseData( pSeperator ); |
|
96 |
+ |
if ( zParts.length == pExpectedParts ) { |
|
97 |
+ |
return zParts; |
|
98 |
+ |
} |
|
99 |
+ |
if ( zParts.length > pExpectedParts ) { |
|
100 |
+ |
throw error( "Expected (" + pExpectedParts + ") Data Parts (seperated by '" + pSeperator + "'), but was (" + zParts.length + ") Parts" ); |
|
101 |
+ |
} |
|
102 |
+ |
String[] rv = new String[pExpectedParts]; |
|
103 |
+ |
System.arraycopy( zParts, 0, rv, 0, zParts.length ); |
|
104 |
+ |
return rv; |
|
105 |
+ |
} |
|
106 |
+ |
|
|
107 |
+ |
// "vs:Simple|Name", |
|
108 |
+ |
// "Object:A1|", |
|
109 |
+ |
// ".ID:ID|Persisted|Simple", |
|
110 |
+ |
// "Object:B1|A1", |
|
111 |
+ |
// "..Active:true", |
|
112 |
+ |
// ".ID:ID|Persisted|Simple", |
|
113 |
+ |
// "...ObjectDisplayValueOrder:1", |
|
114 |
+ |
// "...ColumnName:ID", |
|
115 |
+ |
|
|
116 |
+ |
private static class Line { |
|
117 |
+ |
private int mIndex; |
|
118 |
+ |
private String mOrigLine; |
|
119 |
+ |
private boolean mIgnore = false; |
|
120 |
+ |
private boolean mBadFormat = true; |
|
121 |
+ |
private int mDepth = -1; |
|
122 |
+ |
private String mWhat = ""; |
|
123 |
+ |
private String mData = ""; |
|
124 |
+ |
|
|
125 |
+ |
public Line( int pIndex, String pOrigLine ) { |
|
126 |
+ |
mIndex = pIndex; |
|
127 |
+ |
mOrigLine = (pOrigLine == null) ? null : pOrigLine.trim(); |
|
128 |
+ |
if ( (mOrigLine == null) || (mOrigLine.length() == 0) || mOrigLine.startsWith( "#" ) || mOrigLine.startsWith( "//" ) ) { |
|
129 |
+ |
mBadFormat = false; |
|
130 |
+ |
mIgnore = true; |
|
131 |
+ |
return; |
|
132 |
+ |
} |
|
133 |
+ |
int colonAt = mOrigLine.indexOf( ':' ); |
|
134 |
+ |
if ( colonAt == -1 ) { |
|
135 |
+ |
return; |
|
136 |
+ |
} |
|
137 |
+ |
int depth = 0; |
|
138 |
+ |
while ( mOrigLine.charAt( depth ) == '.' ) { |
|
139 |
+ |
depth++; |
|
140 |
+ |
} |
|
141 |
+ |
mWhat = mOrigLine.substring( depth, colonAt ).trim(); |
|
142 |
+ |
if ( mWhat.length() == 0 ) { |
|
143 |
+ |
return; |
|
144 |
+ |
} |
|
145 |
+ |
mData = mOrigLine.substring( colonAt + 1 ).trim(); |
|
146 |
+ |
mDepth = depth; |
|
147 |
+ |
mBadFormat = false; |
|
148 |
+ |
} |
|
149 |
+ |
|
|
150 |
+ |
public int getIndex() { |
|
151 |
+ |
return mIndex; |
|
152 |
+ |
} |
|
153 |
+ |
|
|
154 |
+ |
public String getOrigLine() { |
|
155 |
+ |
return mOrigLine; |
|
156 |
+ |
} |
|
157 |
+ |
|
|
158 |
+ |
public boolean isNotIgnorable() { |
|
159 |
+ |
return !mIgnore; |
|
160 |
+ |
} |
|
161 |
+ |
|
|
162 |
+ |
public int getDepth() { |
|
163 |
+ |
return mDepth; |
|
164 |
+ |
} |
|
165 |
+ |
|
|
166 |
+ |
public String getWhat() { |
|
167 |
+ |
return mWhat; |
|
168 |
+ |
} |
|
169 |
+ |
|
|
170 |
+ |
public String getData() { |
|
171 |
+ |
return mData; |
|
172 |
+ |
} |
|
173 |
+ |
|
|
174 |
+ |
public boolean isWhat( String pWhat ) { |
|
175 |
+ |
return mWhat.equals( pWhat ); |
|
176 |
+ |
} |
|
177 |
+ |
|
|
178 |
+ |
public void validateWhat( String pWhat ) { |
|
179 |
+ |
if ( !isWhat( pWhat ) ) { |
|
180 |
+ |
throw badWhat( pWhat ); |
|
181 |
+ |
} |
|
182 |
+ |
} |
|
183 |
+ |
|
|
184 |
+ |
public int validateWhat( String[] pWhats ) { |
|
185 |
+ |
for ( int i = 0; i < pWhats.length; i++ ) { |
|
186 |
+ |
if ( isWhat( pWhats[i] ) ) { |
|
187 |
+ |
return i; |
|
188 |
+ |
} |
|
189 |
+ |
} |
|
190 |
+ |
StringBuilder sb = new StringBuilder(); |
|
191 |
+ |
for ( int i = pWhats.length; --i > 0; ) { |
|
192 |
+ |
sb.append( pWhats[i] ); |
|
193 |
+ |
sb.append( ", " ); |
|
194 |
+ |
} |
|
195 |
+ |
sb.append( "or " ); |
|
196 |
+ |
sb.append( pWhats[0] ); |
|
197 |
+ |
throw badWhat( sb.toString() ); |
|
198 |
+ |
} |
|
199 |
+ |
|
|
200 |
+ |
public void validateDepth( int pDepth ) { |
|
201 |
+ |
if ( mDepth != pDepth ) { |
|
202 |
+ |
throw error( "Expected (" + pDepth + ") Depth, but was (" + mDepth + ")" ); |
|
203 |
+ |
} |
|
204 |
+ |
} |
|
205 |
+ |
|
|
206 |
+ |
private ParseException badWhat( String pExpected ) { |
|
207 |
+ |
return error( "Expected (" + pExpected + "), but was (" + mWhat + ")" ); |
|
208 |
+ |
} |
|
209 |
+ |
|
|
210 |
+ |
public void rejectBadFormat() { |
|
211 |
+ |
if ( mBadFormat ) { |
|
212 |
+ |
throw error( "Unacceptable Format" ); |
|
213 |
+ |
} |
|
214 |
+ |
} |
|
215 |
+ |
|
|
216 |
+ |
public ParseException error( String pError ) { |
|
217 |
+ |
return new ParseException( pError + ", line (" + mIndex + ") was: " + mOrigLine ); |
|
218 |
+ |
} |
|
219 |
+ |
} |
|
220 |
+ |
} |