1 /******************************************************************************
3 * Copyright (c) 2003 Novell Inc. www.novell.com
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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
22 *******************************************************************************/
24 // Novell.Directory.Ldap.Utilclass.SchemaTokenCreator.cs
27 // Sunil Kumar (Sunilk@novell.com)
29 // (C) 2003 Novell, Inc (http://www.novell.com)
34 namespace Novell.Directory.Ldap.Utilclass
37 public class SchemaTokenCreator
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;
46 private sbyte[] ctype;
47 private int linenumber=1;
51 private System.IO.StreamReader reader = null;
52 private System.IO.StringReader sreader = null;
53 private System.IO.Stream input = null;
55 public System.String StringValue;
56 public double NumberValue;
59 private void Initialise()
61 ctype = new sbyte[256];
63 peekchar=System.Int32.MaxValue;
64 WordCharacters('a', 'z');
65 WordCharacters('A', 'Z');
66 WordCharacters(128 + 32, 255);
67 WhitespaceCharacters(0, ' ');
68 CommentCharacter('/');
74 public SchemaTokenCreator(System.IO.Stream instream)
79 throw new System.NullReferenceException();
84 public SchemaTokenCreator(System.IO.StreamReader r)
89 throw new System.NullReferenceException();
94 public SchemaTokenCreator(System.IO.StringReader r)
99 throw new System.NullReferenceException();
104 public void pushBack()
109 public int CurrentLine
117 public System.String ToStringValue()
119 System.String strval;
123 case (int)TokenTypes.EOF:
127 case (int) TokenTypes.EOL:
131 case (int) TokenTypes.WORD:
132 strval = StringValue;
135 case (int) TokenTypes.STRING:
136 strval = StringValue;
139 case (int) TokenTypes.NUMBER:
140 case (int) TokenTypes.REAL:
141 strval = "n=" + NumberValue;
146 if (lastttype < 256 && ((ctype[lastttype] & (sbyte)CharacterTypes.STRINGQUOTE) != 0))
148 strval = StringValue;
152 char[] s = new char[3];
154 s[1] = (char) lastttype;
155 strval = new System.String(s);
163 public void WordCharacters(int min, int max)
167 if (max >= ctype.Length)
168 max = ctype.Length - 1;
170 ctype[min++] |= (sbyte)CharacterTypes.ALPHABETIC;
173 public void WhitespaceCharacters(int min, int max)
177 if (max >= ctype.Length)
178 max = ctype.Length - 1;
180 ctype[min++] = (sbyte)CharacterTypes.WHITESPACE;
183 public void OrdinaryCharacters(int min, int max)
187 if (max >= ctype.Length)
188 max = ctype.Length - 1;
193 public void OrdinaryCharacter(int ch)
195 if (ch >= 0 && ch < ctype.Length)
199 public void CommentCharacter(int ch)
201 if (ch >= 0 && ch < ctype.Length)
202 ctype[ch] = (sbyte)CharacterTypes.COMMENTCHAR;
205 public void InitTable()
207 for (int i = ctype.Length; --i >= 0; )
211 public void QuoteCharacter(int ch)
213 if (ch >= 0 && ch < ctype.Length)
214 ctype[ch] = (sbyte)CharacterTypes.STRINGQUOTE;
217 public void parseNumbers()
219 for (int i = '0'; i <= '9'; i++)
220 ctype[i] |= (sbyte)CharacterTypes.NUMERIC;
221 ctype['.'] |= (sbyte)CharacterTypes.NUMERIC;
222 ctype['-'] |= (sbyte)CharacterTypes.NUMERIC;
229 return sreader.Read();
231 else if (reader != null)
233 return reader.Read();
235 else if (input != null)
236 return input.ReadByte();
238 throw new System.SystemException();
241 public int nextToken()
253 curc = System.Int32.MaxValue;
254 if (curc == (System.Int32.MaxValue - 1))
258 return lastttype = (int) TokenTypes.EOF;
260 curc = System.Int32.MaxValue;
262 if (curc == System.Int32.MaxValue)
266 return lastttype = (int) TokenTypes.EOF;
269 peekchar = System.Int32.MaxValue;
271 int ctype = curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC;
272 while ((ctype & (sbyte)CharacterTypes.WHITESPACE) != 0)
279 peekchar = (System.Int32.MaxValue - 1);
280 return lastttype = (int) TokenTypes.EOL;
293 return lastttype = (int) TokenTypes.EOL;
299 return lastttype = (int) TokenTypes.EOF;
300 ctype = curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC;
303 if ((ctype & (sbyte)CharacterTypes.NUMERIC) != 0)
309 if (curc != '.' && (curc < '0' || curc > '9'))
312 return lastttype = '-';
321 if (curc == '.' && checkdec == 0)
323 else if ('0' <= curc && curc <= '9')
325 dvar = dvar * 10 + (curc - '0');
344 NumberValue = checkb?- dvar:dvar;
345 return lastttype = (int) TokenTypes.NUMBER;
348 if ((ctype & (sbyte)CharacterTypes.ALPHABETIC) != 0)
355 char[] nb = new char[buf.Length * 2];
356 Array.Copy((System.Array) buf, 0, (System.Array) nb, 0, buf.Length);
359 buf[i++] = (char) curc;
361 ctype = curc < 0?(sbyte)CharacterTypes.WHITESPACE:curc < 256?this.ctype[curc]:(sbyte)CharacterTypes.ALPHABETIC;
363 while ((ctype & ((sbyte)CharacterTypes.ALPHABETIC | (sbyte)CharacterTypes.NUMERIC)) != 0);
365 StringValue = new String(buf, 0, i);
367 StringValue = StringValue.ToLower();
368 return lastttype = (int) TokenTypes.WORD;
371 if ((ctype & (sbyte)CharacterTypes.STRINGQUOTE) != 0)
376 while (rc >= 0 && rc != lastttype && rc != '\n' && rc != '\r')
382 if (curc >= '0' && curc <= '7')
385 int loopchar = read();
386 if ('0' <= loopchar && loopchar <= '7')
388 curc = (curc << 3) + (loopchar - '0');
390 if ('0' <= loopchar && loopchar <= '7' && first <= '3')
392 curc = (curc << 3) + (loopchar - '0');
448 char[] nb = new char[buf.Length * 2];
449 Array.Copy((System.Array) buf, 0, (System.Array) nb, 0, buf.Length);
452 buf[i++] = (char) curc;
455 peekchar = (rc == lastttype)?System.Int32.MaxValue:rc;
457 StringValue = new String(buf, 0, i);
461 if (curc == '/' && (cppcomments || ccomments))
464 if (curc == '*' && ccomments)
467 while ((curc = read()) != '/' || prevc != '*')
487 return lastttype = (int) TokenTypes.EOF;
492 else if (curc == '/' && cppcomments)
494 while ((curc = read()) != '\n' && curc != '\r' && curc >= 0)
501 if ((this.ctype['/'] & (sbyte)CharacterTypes.COMMENTCHAR) != 0)
503 while ((curc = read()) != '\n' && curc != '\r' && curc >= 0)
511 return lastttype = '/';
516 if ((ctype & (sbyte)CharacterTypes.COMMENTCHAR) != 0)
518 while ((curc = read()) != '\n' && curc != '\r' && curc >= 0)
524 return lastttype = curc;