-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarValidity.java
More file actions
455 lines (430 loc) · 14.7 KB
/
StarValidity.java
File metadata and controls
455 lines (430 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package EDU.bmrb.starlibj;
import java.lang.*;
import java.util.*;
/** This is a set of some simple utility methods that are used throughout
* the starlibj. Mostly they deal with string syntax checks. These
* routines are used by the starlibj functions to decide if certain
* operations should be rejected. (For example, deciding if a string
* is an acceptable saveframe name or tagname.) They are left public
* so that the caller of this package/library can use them too.
*/
public class StarValidity
{
private static String packageNameStatic = null;
/** Returns the String name of this package. This is needed
* because Java loses the 'import' information at runtime,
* and therefore we have to give full-path names for
* types when looking at the Class object. (e.g. you can't
* just say:
* <PRE>
* Class.forName("Vector")... // (won't work)
* </PRE>
* You have to say:
* <PRE>
* Class.forName("java.lang.Vector")...
* </PRE>
* @return The package name for this class.
*/
public static String pkgName( )
{
// This really silly jumping through hoops is needed
// because Java doesn't let you get the name of the
// class except via an object of that class type.
// It doesn't matter which starlibj Class we pick here,
// because we are only interested in finding the package
// it is inside: I picked "StarNode" because it was
// small and simple.
// To save time, we want to only do this once during the
// life of the program, so stuff the result into a
// static string variable and use it on subsequent
// calls:
if( packageNameStatic == null )
{
StarNode dummyObj = new StarNode();
String className = dummyObj.getClass().getName();
packageNameStatic =
className.substring( 0, className.lastIndexOf('.') );
}
return packageNameStatic;
}
/** Returns true if the string is a valid tag name.
* @return true if valid, false if invalid
*/
public static boolean isValidTagName( String s )
{
int i;
// Invalid if it doesn't start with an underscore.
if( s.charAt(0) != '_' )
return false;
// Invalid if it contains whitespace within.
for( i = 0 ; i < s.length() ; i++ )
if( Character.isWhitespace( s.charAt(i) ) )
return false;
// If it got this far, its good.
return true;
}
/** Returns true if the string is a valid saveframe name.
* @return true if valid, false if invalid
*/
public static boolean isValidSaveName( String s )
{
int i;
// Invalid if it doesn't start with "save_"
if( ! s.substring( 0,5 ).equals("save_") )
return false;
// Invalid if it *only* contains "save_" and nothing more.
if( s.length() <= 5 )
return false;
// Invalid if it contains whitespace within.
for( i = 5 ; i < s.length() ; i++ )
if( Character.isWhitespace( s.charAt(i) ) )
return false;
// If it got this far, its good.
return true;
}
/** Returns true if the string is a valid data/global block name.
* @return true if valid, false if invalid
*/
public static boolean isValidBlockName( String s )
{
int i;
// Special case: okay if it is "global_"
if( s.equals("global_") )
return true;
// Invalid if it doesn't start with "data_"
if( ! s.substring( 0,5 ).equals("data_") )
return false;
// Invalid if it *only* contains "data_" and nothing more.
if( s.length() <= 5 )
return false;
// Invalid if it contains whitespace within.
for( i = 5 ; i < s.length() ; i++ )
if( Character.isWhitespace( s.charAt(i) ) )
return false;
// If it got this far, its good.
return true;
}
/** Returns true if the string given is valid for a
* nondelimited DataValueNode (no whitespace).
* @return true if valid, false if invalid
*/
public static boolean isValidValueForNonDelim( String s )
{
int i;
// Must have at leas some value.
if( s.length() == 0 )
return false;
// Invalid if it starts with ' or " or _ or $
if( s.charAt(0) == '\'' )
return false;
if( s.charAt(0) == '\"' )
return false;
if( s.charAt(0) == '$' )
return false;
if( s.charAt(0) == '_' )
return false;
// Invalid if contains whitespace within.
// Invalid if contains comment-start hashes ('#') within.
for( i = 0 ; i < s.length() ; i++ )
{
if( Character.isWhitespace( s.charAt(i) ) )
return false;
if( s.charAt(i) == '#' )
return false;
}
return true;
}
/** Returns true if the string given is valid for a
* single-quote delimiter in a DataValueNode.
*/
public static boolean isValidValueForSingleDelim( String s )
{
int i;
// Invalid if it contains line breaks within or
// a single-tick quote followed by space.
for( i = 0 ; i < s.length() ; i++ )
{
if( s.charAt(i) == '\n' || s.charAt(i) == '\r' )
return false;
else if( s.charAt(i) == '\'' )
if( i < s.length() - 1 )
{
if( s.charAt(i+1) == ' ' )
return false;
}
else
return true;
}
return true;
}
/** Returns true if the string given is valid for a
* double-quote delimiter in a DataValueNode.
*/
public static boolean isValidValueForDoubleDelim( String s )
{
int i;
// Invalid if it contains line breaks within or
// a double-tick quote followed by space.
for( i = 0 ; i < s.length() ; i++ )
{
if( s.charAt(i) == '\n' || s.charAt(i) == '\r' )
return false;
else if( s.charAt(i) == '\"' )
if( i < s.length() - 1 )
{
if( s.charAt(i+1) == ' ' )
return false;
}
else
return true;
}
return true;
}
/** Returns true if the string given is valid for a
* framecode delimiter (dollar sign) in a DataValueNode,
*/
public static boolean isValidValueForFrameCodeDelim( String s )
{
int i;
// Invalid if it contains whitespace within.
for( i = 0 ; i < s.length() ; i++ )
if( Character.isWhitespace( s.charAt(i) ) )
return false;
return true;
}
/** Determines if the string given is valid for the delimiter
* type given (from DataValueNode).
* @return true = valid, false = invalid.
* @see DataValueNode
*/
public static boolean isValidForDelim( String s, int delim )
{
if( delim == DataValueNode.NON )
return isValidValueForNonDelim(s);
else if( delim == DataValueNode.DOUBLE )
return isValidValueForDoubleDelim(s);
else if( delim == DataValueNode.SINGLE )
return isValidValueForSingleDelim(s);
else if( delim == DataValueNode.SEMICOLON )
return true; // all semicolon strings are valid.
else if( delim == DataValueNode.FRAMECODE )
return isValidValueForNonDelim(s);
// This could probably be something better.
else
return false;
}
/** Return the valid delim type to use
* @return the delim type that is safe to use for this value.
*/
public static short getValidDelimFor( String s )
{
if( isValidValueForNonDelim(s) )
return DataValueNode.NON;
else if( isValidValueForDoubleDelim(s) )
return DataValueNode.DOUBLE;
else if( isValidValueForSingleDelim(s) )
return DataValueNode.SINGLE;
else
return DataValueNode.SEMICOLON;
}
/** Given a string, parse (starting at the first char and extending
* until valid syntax is exhausted) a value string in STAR syntax.
* @return The string and delimiter type in a ParseValFromRetVal
* structure.
* @param str the string to parse through.
* @param makeNew set to true to return a new String, false not to.
* Setting it to false is useful when you want to
* parse for the end of the string, but you don't
* care what the string is (you are skipping past
* the value.)
*/
public static ParseValFromRetVal parseValFrom( String str, boolean makeNew )
{
char curChar;
int rowOffset, colOffset;
int idx; // string index
int startIdx; // index of first char of value (does not
// include delimiters)
int endIdx; // index of last char of value + 1 (plus one one
// because that fits well with Java conventions)
int nextIdx; // The index that would be used for the next
// parse to start from to get more values in this string.
boolean inLeadingWhitespace;
boolean inNonQuoted;
boolean inFramecode;
boolean inSingle;
boolean inDouble;
boolean inSemicolon;
boolean reachedEnd;
boolean found;
ParseValFromRetVal retVal;
idx = 0;
inLeadingWhitespace =true;
inNonQuoted = false;
inFramecode = false;
inSingle = false;
inDouble = false;
inSemicolon = false;
reachedEnd = false;
startIdx = 0;
endIdx = 0;
nextIdx = 0;
found = false;
rowOffset = 0;
colOffset = 0;
while( !reachedEnd )
{
if( idx >= str.length() )
{
reachedEnd = true;
endIdx = idx;
nextIdx = endIdx;
}
else if( inLeadingWhitespace )
{
if( Character.isWhitespace( str.charAt( idx ) ) )
{
idx++;
}
else
{
inLeadingWhitespace = false;
found = true;
// This is the starting character of the string,
// determine its delimiter type.
curChar = str.charAt( idx );
if( curChar == '"' ) // double-quoted delimiter
{ inDouble = true;
idx++;
startIdx = idx;
}
else if( curChar == '\'' )
{ inSingle = true;
idx++;
startIdx = idx;
}
else if( curChar == ';' )
{ inSemicolon = true;
idx++;
// If next char is eoln, eat it up:
while( str.charAt( idx ) == '\n' ||
str.charAt( idx ) == '\r' )
{
idx++;
}
startIdx = idx;
}
else if( curChar == '$' )
{
inFramecode = true;
idx++;
startIdx = idx;
}
else // default: non-quoted string:
{
inNonQuoted = true;
startIdx = idx;
idx++;
}
}
continue;
}
else
{
if( inDouble && str.charAt( idx ) == '"' )
{
reachedEnd = true;
endIdx = idx;
nextIdx = idx + 1;
}
else if( inSingle && str.charAt( idx ) == '\'' )
{
reachedEnd = true;
endIdx = idx;
nextIdx = idx + 1;
}
else if( inSemicolon && str.charAt( idx ) == ';' &&
( str.charAt( idx - 1 ) == '\n' ||
str.charAt( idx - 1 ) == '\r' ) )
{
reachedEnd = true;
endIdx = idx - 1;
nextIdx = idx + 1;
}
else if( ( inNonQuoted || inFramecode ) &&
Character.isWhitespace( str.charAt( idx ) ) )
{
reachedEnd = true;
endIdx = idx;
nextIdx = idx;
}
else
{
idx++;
}
}
}
retVal = new ParseValFromRetVal();
if( makeNew )
{
retVal.delim = inNonQuoted ? DataValueNode.NON :
inDouble ? DataValueNode.DOUBLE :
inSingle ? DataValueNode.SINGLE :
inSemicolon ? DataValueNode.SEMICOLON :
inFramecode ? DataValueNode.FRAMECODE :
/*can't happen*/ DataValueNode.DONT_CARE ;
retVal.str = str.substring( startIdx, endIdx );
}
else
{
retVal.str = null;
}
retVal.endingIdx = endIdx;
retVal.nextIdx = nextIdx;
retVal.found = found;
return retVal;
}
public static String clsNameASCII_CharStream = "EDU.bmrb.starlibj.ASCII_CharStream" ;
public static String clsNameBadValueForDelimiter = "EDU.bmrb.starlibj.BadValueForDelimiter" ;
public static String clsNameBlockListVector = "EDU.bmrb.starlibj.BlockListVector" ;
public static String clsNameBlockNode = "EDU.bmrb.starlibj.BlockNode" ;
public static String clsNameCharStream = "EDU.bmrb.starlibj.CharStream" ;
public static String clsNameDataItemNode = "EDU.bmrb.starlibj.DataItemNode" ;
public static String clsNameDataLoopNameListNode = "EDU.bmrb.starlibj.DataLoopNameListNode" ;
public static String clsNameDataLoopNode = "EDU.bmrb.starlibj.DataLoopNode" ;
public static String clsNameDataNameNode = "EDU.bmrb.starlibj.DataNameNode" ;
public static String clsNameDataValueNode = "EDU.bmrb.starlibj.DataValueNode" ;
public static String clsNameDataValuesVector = "EDU.bmrb.starlibj.DataValuesVector" ;
public static String clsNameHomemadeStringBuffer = "EDU.bmrb.starlibj.HomemadeStringBuffer" ;
public static String clsNameInternalException = "EDU.bmrb.starlibj.InternalException" ;
public static String clsNameLoopNameListNode = "EDU.bmrb.starlibj.LoopNameListNode" ;
public static String clsNameLoopRowNode = "EDU.bmrb.starlibj.LoopRowNode" ;
public static String clsNameLoopRowsVector = "EDU.bmrb.starlibj.LoopRowsVector" ;
public static String clsNameLoopTableNode = "EDU.bmrb.starlibj.LoopTableNode" ;
public static String clsNameNameListVector = "EDU.bmrb.starlibj.NameListVector" ;
public static String clsNameNameViolatesStarSyntax = "EDU.bmrb.starlibj.NameViolatesStarSyntax" ;
public static String clsNameOperationCausesMismatchedLoopData = "EDU.bmrb.starlibj.OperationCausesMismatchedLoopData" ;
public static String clsNameParseException = "EDU.bmrb.starlibj.ParseException" ;
public static String clsNameParseValFromRetVal = "EDU.bmrb.starlibj.ParseValFromRetVal" ;
public static String clsNameRemoteInt = "EDU.bmrb.starlibj.RemoteInt" ;
public static String clsNameSaveFrameNode = "EDU.bmrb.starlibj.SaveFrameNode" ;
public static String clsNameSaveListVector = "EDU.bmrb.starlibj.SaveListVector" ;
public static String clsNameStarFileNode = "EDU.bmrb.starlibj.StarFileNode" ;
public static String clsNameStarListVector = "EDU.bmrb.starlibj.StarListVector" ;
public static String clsNameStarNode = "EDU.bmrb.starlibj.StarNode" ;
public static String clsNameStarParser = "EDU.bmrb.starlibj.StarParser" ;
public static String clsNameStarParserConstants = "EDU.bmrb.starlibj.StarParserConstants" ;
public static String clsNameStarParserTokenManager = "EDU.bmrb.starlibj.StarParserTokenManager" ;
public static String clsNameStarUnparser = "EDU.bmrb.starlibj.StarUnparser" ;
public static String clsNameStarValidity = "EDU.bmrb.starlibj.StarValidity" ;
public static String clsNameStarVectorLike = "EDU.bmrb.starlibj.StarVectorLike" ;
public static String clsNameTagsVector = "EDU.bmrb.starlibj.TagsVector" ;
public static String clsNameTinyAbsDataValueNode = "EDU.bmrb.starlibj.TinyAbsDataValueNode" ;
public static String clsNameTinyRelDataValueNode = "EDU.bmrb.starlibj.TinyRelDataValueNode" ;
public static String clsNameToken = "EDU.bmrb.starlibj.Token" ;
public static String clsNameTokenMgrError = "EDU.bmrb.starlibj.TokenMgrError" ;
public static String clsNameTypesAreFrozen = "EDU.bmrb.starlibj.TypesAreFrozen" ;
public static String clsNameTypesNotFrozenYet = "EDU.bmrb.starlibj.TypesNotFrozenYet" ;
public static String clsNameVectorCheckType = "EDU.bmrb.starlibj.VectorCheckType" ;
public static String clsNameWrongElementType = "EDU.bmrb.starlibj.WrongElementType" ;
}