| 1 |
/****************************************************************************** |
| 2 |
* The MIT License |
| 3 |
* Copyright (c) 2003 Novell Inc. www.novell.com |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
* of this software and associated documentation files (the Software), to deal |
| 7 |
* in the Software without restriction, including without limitation the rights |
| 8 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the Software is |
| 10 |
* furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 |
* SOFTWARE. |
| 22 |
*******************************************************************************/ |
| 23 |
// |
| 24 |
// Novell.Directory.Ldap.Utilclass.SchemaTokenCreator.cs |
| 25 |
// |
| 26 |
// Author: |
| 27 |
// Sunil Kumar ([email protected]) |
| 28 |
// |
| 29 |
// (C) 2003 Novell, Inc (http://www.novell.com) |
| 30 |
// |
| 31 |
|
| 32 |
using System; |
| 33 |
|
| 34 |
namespace Novell.Directory.Ldap.Utilclass |
| 35 |
{ |
| 36 |
|
| 37 |
public class SchemaTokenCreator |
| 38 |
{ |
| 39 |
private string basestring; |
| 40 |
private bool cppcomments=false; // C++ style comments enabled |
| 41 |
private bool ccomments=false; // C style comments enabled |
| 42 |
private bool iseolsig=false; |
| 43 |
private bool cidtolower; |
| 44 |
private bool pushedback; |
| 45 |
private int peekchar; |
| 46 |
private sbyte[] ctype; |
| 47 |
private int linenumber=1; |
| 48 |
private int ichar=1; |
| 49 |
private char[] buf; |
| 50 |
|
| 51 |
private System.IO.StreamReader reader = null; |
| 52 |
private System.IO.StringReader sreader = null; |
| 53 |
private System.IO.Stream input = null; |
| 54 |
|
| 55 |
public System.String StringValue; |
| 56 |
public double NumberValue; |
| 57 |
public int lastttype; |
| 58 |
|
| 59 |
private void Initialise() |
| 60 |
{ |
| 61 |
ctype = new sbyte[256]; |
| 62 |
buf = new char[20]; |
| 63 |
peekchar=System.Int32.MaxValue; |
| 64 |
WordCharacters('a', 'z'); |
| 65 |
WordCharacters('A', 'Z'); |
| 66 |
WordCharacters(128 + 32, 255); |
| 67 |
WhitespaceCharacters(0, ' '); |
| 68 |
CommentCharacter('/'); |
| 69 |
QuoteCharacter('"'); |
| 70 |
QuoteCharacter('\''); |
| 71 |
parseNumbers(); |
| 72 |
} |
| 73 |
|
| 74 |
public SchemaTokenCreator(System.IO.Stream instream) |
| 75 |
{ |
| 76 |
Initialise(); |
| 77 |
if (instream == null) |
| 78 |
{ |
| 79 |
throw new System.NullReferenceException(); |
| 80 |
} |
| 81 |
input = instream; |
| 82 |
} |
| 83 |
|
| 84 |
public SchemaTokenCreator(System.IO.StreamReader r) |
| 85 |
{ |
| 86 |
Initialise(); |
| 87 |
if (r == null) |
| 88 |
{ |
| 89 |
throw new System.NullReferenceException(); |
| 90 |
} |
| 91 |
reader = r; |
| 92 |
} |
| 93 |
|
| 94 |
public SchemaTokenCreator(System.IO.StringReader r) |
| 95 |
{ |
| 96 |
Initialise(); |
| 97 |
if (r == null) |
| 98 |
{ |
| 99 |
throw new System.NullReferenceException(); |
| 100 |
} |
| 101 |
sreader = r; |
| 102 |
} |
| 103 |
|
| 104 |
public void pushBack() |
| 105 |
{ |
| 106 |
pushedback = true; |
| 107 |
} |
| 108 |
|
| 109 |
public int CurrentLine |
| 110 |
{ |
| 111 |
get |
| 112 |
{ |
| 113 |
return linenumber; |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
public System.String ToStringValue() |
| 118 |
{ |
| 119 |
System.String strval; |
| 120 |
switch (lastttype) |
| 121 |
{ |
| 122 |
|
| 123 |
case (int)TokenTypes.EOF: |
| 124 |
strval = "EOF"; |
| 125 |
break; |
| 126 |
|
| 127 |
case (int) TokenTypes.EOL: |
| 128 |
strval = "EOL"; |
| 129 |
break; |
| 130 |
|
| 131 |
case (int) TokenTypes.WORD: |
| 132 |
strval = StringValue; |
| 133 |
break; |
| 134 |
|
| 135 |
case (int) TokenTypes.STRING: |
| 136 |
strval = StringValue; |
| 137 |
break; |
| 138 |
|
| 139 |
case (int) TokenTypes.NUMBER: |
| 140 |
case (int) TokenTypes.REAL: |
| 141 |
strval = "n=" + NumberValue; |
| 142 |
break; |
| 143 |
|
| 144 |
default: |
| 145 |
{ |
| 146 |
if (lastttype < 256 && ((ctype[lastttype] & (sbyte)CharacterTypes.STRINGQUOTE) != 0)) |
| 147 |
{ |
| 148 |
strval = StringValue; |
| 149 |
break; |
| 150 |
} |
| 151 |
|
| 152 |
char[] s = new char[3]; |
| 153 |
s[0] = s[2] = '\''; |
| 154 |
s[1] = (char) lastttype; |
| 155 |
strval = new System.String(s); |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
return strval; |
| 161 |
} |
| 162 |
|
| 163 |
public void WordCharacters(int min, int max) |
| 164 |
{ |
| 165 |
if (min < 0) |
| 166 |
min = 0; |
| 167 |
if (max >= ctype.Length) |
| 168 |
max = ctype.Length - 1; |
| 169 |
while (min <= max) |
| 170 |
ctype[min++] |= (sbyte)CharacterTypes.ALPHABETIC; |
| 171 |
} |
| 172 |
|
| 173 |
public void WhitespaceCharacters(int min, int max) |
| 174 |
{ |
| 175 |
if (min < 0) |
| 176 |
min = 0; |
| 177 |
if (max >= ctype.Length) |
| 178 |
max = ctype.Length - 1; |
| 179 |
while (min <= max) |
| 180 |
ctype[min++] = (sbyte)CharacterTypes.WHITESPACE; |
| 181 |
} |
| 182 |
|
| 183 |
public void OrdinaryCharacters(int min, int max) |
| 184 |
{ |
| 185 |
if (min < 0) |
| 186 |
min = 0; |
| 187 |
if (max >= ctype.Length) |
| 188 |
max = ctype.Length - 1; |
| 189 |
while (min <= max) |
| 190 |
ctype[min++] = 0; |
| 191 |
} |
| 192 |
|
| 193 |
public void OrdinaryCharacter(int ch) |
| 194 |
{ |
| 195 |
if (ch >= 0 && ch < ctype.Length) |
| 196 |
ctype[ch] = 0; |
| 197 |
} |
| 198 |
|
| 199 |
public void CommentCharacter(int ch) |
| 200 |
{ |
| 201 |
if (ch >= 0 && ch < ctype.Length) |
| 202 |
ctype[ch] = (sbyte)CharacterTypes.COMMENTCHAR; |
| 203 |
} |
| 204 |
|
| 205 |
public void InitTable() |
| 206 |
{ |
| 207 |
for (int i = ctype.Length; --i >= 0; ) |
| 208 |
ctype[i] = 0; |
| 209 |
} |
| 210 |
|
| 211 |
public void QuoteCharacter(int ch) |
| 212 |
{ |
| 213 |
if (ch >= 0 && ch < ctype.Length) |
| 214 |
ctype[ch] = (sbyte)CharacterTypes.STRINGQUOTE; |
| 215 |
} |
| 216 |
|
| 217 |
public void parseNumbers() |
| 218 |
{ |
| 219 |
for (int i = '0'; i <= '9'; i++) |
| 220 |
ctype[i] |= (sbyte)CharacterTypes.NUMERIC; |
| 221 |
ctype['.'] |= (sbyte)CharacterTypes.NUMERIC; |
| 222 |
ctype['-'] |= (sbyte)CharacterTypes.NUMERIC; |
| 223 |
} |
| 224 |
|
| 225 |
private int read() |
| 226 |
{ |
| 227 |
if (sreader !=null ) |
| 228 |
{ |
| 229 |
return sreader.Read(); |
| 230 |
} |
| 231 |
else if (reader != null) |
| 232 |
{ |
| 233 |
return reader.Read(); |
| 234 |
} |
| 235 |
else if (input != null) |
| 236 |
return input.ReadByte(); |
| 237 |
else |
| 238 |
throw new System.SystemException(); |
| 239 |
} |
| 240 |
|
| 241 |
public int nextToken() |
| 242 |
{ |
| 243 |
if (pushedback) |
| 244 |
{ |
| 245 |
pushedback = false; |
| 246 |
return lastttype; |
| 247 |
} |
| 248 |
|
| 249 |
StringValue = null; |
| 250 |
|
| 251 |
int curc = peekchar; |
| 252 |
if (curc < 0) |
| 253 |
curc = System.Int32.MaxValue; |
| 254 |
if (curc == (System.Int32.MaxValue - 1)) |
| 255 |
{ |
| 256 |
curc = read(); |
| 257 |
if (curc < 0) |
| 258 |
return lastttype = (int) TokenTypes.EOF; |
| 259 |
if (curc == '\n') |
| 260 |
curc = System.Int32.MaxValue; |
| 261 |
} |
| 262 |
if (curc == System.Int32.MaxValue) |
| 263 |
{ |
| 264 |
curc = read(); |
| 265 |
if (curc < 0) |
| 266 |
return lastttype = (int) TokenTypes.EOF; |
| 267 |
} |
| 268 |
lastttype = curc; |
| 269 |
peekchar = System.Int32.MaxValue; |
| 270 |
|
| 271 |
int ctype = curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC; |
| 272 |
while ((ctype & (sbyte)CharacterTypes.WHITESPACE) != 0) |
| 273 |
{ |
| 274 |
if (curc == '\r') |
| 275 |
{ |
| 276 |
linenumber++; |
| 277 |
if (iseolsig) |
| 278 |
{ |
| 279 |
peekchar = (System.Int32.MaxValue - 1); |
| 280 |
return lastttype = (int) TokenTypes.EOL; |
| 281 |
} |
| 282 |
curc = read(); |
| 283 |
if (curc == '\n') |
| 284 |
curc = read(); |
| 285 |
} |
| 286 |
else |
| 287 |
{ |
| 288 |
if (curc == '\n') |
| 289 |
{ |
| 290 |
linenumber++; |
| 291 |
if (iseolsig) |
| 292 |
{ |
| 293 |
return lastttype = (int) TokenTypes.EOL; |
| 294 |
} |
| 295 |
} |
| 296 |
curc = read(); |
| 297 |
} |
| 298 |
if (curc < 0) |
| 299 |
return lastttype = (int) TokenTypes.EOF; |
| 300 |
ctype = curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC; |
| 301 |
} |
| 302 |
|
| 303 |
if ((ctype & (sbyte)CharacterTypes.NUMERIC) != 0) |
| 304 |
{ |
| 305 |
bool checkb = false; |
| 306 |
if (curc == '-') |
| 307 |
{ |
| 308 |
curc = read(); |
| 309 |
if (curc != '.' && (curc < '0' || curc > '9')) |
| 310 |
{ |
| 311 |
peekchar = curc; |
| 312 |
return lastttype = '-'; |
| 313 |
} |
| 314 |
checkb = true; |
| 315 |
} |
| 316 |
double dvar = 0; |
| 317 |
int tempvar = 0; |
| 318 |
int checkdec = 0; |
| 319 |
while (true) |
| 320 |
{ |
| 321 |
if (curc == '.' && checkdec == 0) |
| 322 |
checkdec = 1; |
| 323 |
else if ('0' <= curc && curc <= '9') |
| 324 |
{ |
| 325 |
dvar = dvar * 10 + (curc - '0'); |
| 326 |
tempvar += checkdec; |
| 327 |
} |
| 328 |
else |
| 329 |
break; |
| 330 |
curc = read(); |
| 331 |
} |
| 332 |
peekchar = curc; |
| 333 |
if (tempvar != 0) |
| 334 |
{ |
| 335 |
double divby = 10; |
| 336 |
tempvar--; |
| 337 |
while (tempvar > 0) |
| 338 |
{ |
| 339 |
divby *= 10; |
| 340 |
tempvar--; |
| 341 |
} |
| 342 |
dvar = dvar / divby; |
| 343 |
} |
| 344 |
NumberValue = checkb?- dvar:dvar; |
| 345 |
return lastttype = (int) TokenTypes.NUMBER; |
| 346 |
} |
| 347 |
|
| 348 |
if ((ctype & (sbyte)CharacterTypes.ALPHABETIC) != 0) |
| 349 |
{ |
| 350 |
int i = 0; |
| 351 |
do |
| 352 |
{ |
| 353 |
if (i >= buf.Length) |
| 354 |
{ |
| 355 |
char[] nb = new char[buf.Length * 2]; |
| 356 |
Array.Copy((System.Array) buf, 0, (System.Array) nb, 0, buf.Length); |
| 357 |
buf = nb; |
| 358 |
} |
| 359 |
buf[i++] = (char) curc; |
| 360 |
curc = read(); |
| 361 |
ctype = curc < 0?(sbyte)CharacterTypes.WHITESPACE:curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC; |
| 362 |
} |
| 363 |
while ((ctype & ((sbyte)CharacterTypes.ALPHABETIC | (sbyte)CharacterTypes.NUMERIC)) != 0); |
| 364 |
peekchar = curc; |
| 365 |
StringValue = new String(buf, 0, i); |
| 366 |
if (cidtolower) |
| 367 |
StringValue = StringValue.ToLower(); |
| 368 |
return lastttype = (int) TokenTypes.WORD; |
| 369 |
} |
| 370 |
|
| 371 |
if ((ctype & (sbyte)CharacterTypes.STRINGQUOTE) != 0) |
| 372 |
{ |
| 373 |
lastttype = curc; |
| 374 |
int i = 0; |
| 375 |
int rc = read(); |
| 376 |
while (rc >= 0 && rc != lastttype && rc != '\n' && rc != '\r') |
| 377 |
{ |
| 378 |
if (rc == '\\') |
| 379 |
{ |
| 380 |
curc = read(); |
| 381 |
int first = curc; |
| 382 |
if (curc >= '0' && curc <= '7') |
| 383 |
{ |
| 384 |
curc = curc - '0'; |
| 385 |
int loopchar = read(); |
| 386 |
if ('0' <= loopchar && loopchar <= '7') |
| 387 |
{ |
| 388 |
curc = (curc << 3) + (loopchar - '0'); |
| 389 |
loopchar = read(); |
| 390 |
if ('0' <= loopchar && loopchar <= '7' && first <= '3') |
| 391 |
{ |
| 392 |
curc = (curc << 3) + (loopchar - '0'); |
| 393 |
rc = read(); |
| 394 |
} |
| 395 |
else |
| 396 |
rc = loopchar; |
| 397 |
} |
| 398 |
else |
| 399 |
rc = loopchar; |
| 400 |
} |
| 401 |
else |
| 402 |
{ |
| 403 |
switch (curc) |
| 404 |
{ |
| 405 |
|
| 406 |
case 'f': |
| 407 |
curc = 0xC; |
| 408 |
break; |
| 409 |
|
| 410 |
case 'a': |
| 411 |
curc = 0x7; |
| 412 |
break; |
| 413 |
|
| 414 |
case 'b': |
| 415 |
curc = '\b'; |
| 416 |
break; |
| 417 |
|
| 418 |
case 'v': |
| 419 |
curc = 0xB; |
| 420 |
break; |
| 421 |
|
| 422 |
case 'n': |
| 423 |
curc = '\n'; |
| 424 |
break; |
| 425 |
|
| 426 |
case 'r': |
| 427 |
curc = '\r'; |
| 428 |
break; |
| 429 |
|
| 430 |
case 't': |
| 431 |
curc = '\t'; |
| 432 |
break; |
| 433 |
|
| 434 |
default: |
| 435 |
break; |
| 436 |
|
| 437 |
} |
| 438 |
rc = read(); |
| 439 |
} |
| 440 |
} |
| 441 |
else |
| 442 |
{ |
| 443 |
curc = rc; |
| 444 |
rc = read(); |
| 445 |
} |
| 446 |
if (i >= buf.Length) |
| 447 |
{ |
| 448 |
char[] nb = new char[buf.Length * 2]; |
| 449 |
Array.Copy((System.Array) buf, 0, (System.Array) nb, 0, buf.Length); |
| 450 |
buf = nb; |
| 451 |
} |
| 452 |
buf[i++] = (char) curc; |
| 453 |
} |
| 454 |
|
| 455 |
peekchar = (rc == lastttype)?System.Int32.MaxValue:rc; |
| 456 |
|
| 457 |
StringValue = new String(buf, 0, i); |
| 458 |
return lastttype; |
| 459 |
} |
| 460 |
|
| 461 |
if (curc == '/' && (cppcomments || ccomments)) |
| 462 |
{ |
| 463 |
curc = read(); |
| 464 |
if (curc == '*' && ccomments) |
| 465 |
{ |
| 466 |
int prevc = 0; |
| 467 |
while ((curc = read()) != '/' || prevc != '*') |
| 468 |
{ |
| 469 |
if (curc == '\r') |
| 470 |
{ |
| 471 |
linenumber++; |
| 472 |
curc = read(); |
| 473 |
if (curc == '\n') |
| 474 |
{ |
| 475 |
curc = read(); |
| 476 |
} |
| 477 |
} |
| 478 |
else |
| 479 |
{ |
| 480 |
if (curc == '\n') |
| 481 |
{ |
| 482 |
linenumber++; |
| 483 |
curc = read(); |
| 484 |
} |
| 485 |
} |
| 486 |
if (curc < 0) |
| 487 |
return lastttype = (int) TokenTypes.EOF; |
| 488 |
prevc = curc; |
| 489 |
} |
| 490 |
return nextToken(); |
| 491 |
} |
| 492 |
else if (curc == '/' && cppcomments) |
| 493 |
{ |
| 494 |
while ((curc = read()) != '\n' && curc != '\r' && curc >= 0) |
| 495 |
; |
| 496 |
peekchar = curc; |
| 497 |
return nextToken(); |
| 498 |
} |
| 499 |
else |
| 500 |
{ |
| 501 |
if ((this.ctype['/'] & (sbyte)CharacterTypes.COMMENTCHAR) != 0) |
| 502 |
{ |
| 503 |
while ((curc = read()) != '\n' && curc != '\r' && curc >= 0) |
| 504 |
; |
| 505 |
peekchar = curc; |
| 506 |
return nextToken(); |
| 507 |
} |
| 508 |
else |
| 509 |
{ |
| 510 |
peekchar = curc; |
| 511 |
return lastttype = '/'; |
| 512 |
} |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
if ((ctype & (sbyte)CharacterTypes.COMMENTCHAR) != 0) |
| 517 |
{ |
| 518 |
while ((curc = read()) != '\n' && curc != '\r' && curc >= 0) |
| 519 |
; |
| 520 |
peekchar = curc; |
| 521 |
return nextToken(); |
| 522 |
} |
| 523 |
|
| 524 |
return lastttype = curc; |
| 525 |
} |
| 526 |
|
| 527 |
} |
| 528 |
} |