/// <summary> /// Converts a Well-known Text representation to a <c>Geometry</c>. /// </summary> /// <param name="reader"> /// A Reader which will return a "Geometry Tagged Text" /// string (see the OpenGIS Simple Features Specification). /// </param> /// <returns>A <c>Geometry</c> read from <c>reader</c>. /// </returns> public IGeometry Read(TextReader reader) { StreamTokenizer tokenizer = new StreamTokenizer(reader); ArrayList tokens = new ArrayList(); tokenizer.Tokenize(tokens); // Read directly all tokens index = 0; // Reset pointer to start of tokens try { return ReadGeometryTaggedText(tokens); } catch (IOException e) { throw new ParseException(e.ToString()); } }
internal IList<Token> Tokenize(TextReader reader) { var tokenizer = new StreamTokenizer(reader); IList<Token> tokens = new List<Token>(); tokenizer.Tokenize(tokens); // Read directly all tokens return tokens; }
/// <summary> /// Simple test of some ParseUtil methods. /// </summary> /// <returns>bool - true for all passed, false otherwise</returns> public static bool TestSelf() { Logger log = new Logger("ParseUtil: TestSelf"); log.Info("Starting..."); StreamTokenizer tokenizer = new StreamTokenizer(); tokenizer.Verbosity = VerbosityLevel.Warn; // FindMatch ArrayList alist = new ArrayList(); tokenizer.TokenizeString("{ [ ] '}' }", alist); foreach(Token t in alist) log.Debug("Token = {0}", t); Token[] tarray = (Token[])alist.ToArray(typeof(Token)); int i = 0; if (!FindMatch(tarray, ref i, '{')) { log.Error("FindMatch failed to match { char"); return(false); } if (i != 4) { log.Error("FindMatch got the wrong answer {0}", i); return(false); } else log.Info("FindMatch worked."); // // try BuildArray // ArrayList tokens = new ArrayList(); tokenizer.TokenizeString("1 2 3 4 5", tokens); foreach(Token t in tokens) log.Debug("Token = {0}", t); i = 0; Int16[] shorts = (short[])BuildArray(tokens, ref i, typeof(Int16), null, -1, log); if (shorts == null) { log.Error("Unable to BuildArray of shorts."); return(false); } log.Info("Parsed shorts:"); foreach(Int16 s in shorts) { log.Write("{0}, ", s); } log.WriteLine(String.Empty); // // try BuildArray of floats, char terminated // tokens.Clear(); tokenizer.TokenizeString("1 2 ; 3 4", tokens); foreach(Token t in tokens) log.Debug("Token = {0}", t); i = 0; Single[] floats = (float[])BuildArray(tokens, ref i, typeof(Single), new CharToken(';'), -1, log); if (floats == null) { log.Error("Unable to BuildArray of floats."); return(false); } log.Info("Parsed floats:"); foreach(float f in floats) { log.Write("{0}, ", f); } log.WriteLine(String.Empty); if (i != 2) { log.Error("BuildArray left i = {0} which is incorrect"); return(false); } // // try BuildArray on high-precision floats // tokens.Clear(); float f1 = 1.23456f; float f2 = 2.34567f; tokenizer.TokenizeString(String.Format("{0:f6} {1:f6}", f1,f2), tokens); foreach(Token t in tokens) log.Debug("Token = {0}", t); i = 0; floats = (float[])BuildArray(tokens, ref i, typeof(Single), null, -1, log); if (floats == null) { log.Error("Unable to BuildArray of floats."); return(false); } log.Info("Parsed floats:"); foreach(float f in floats) { log.Write("{0}, ", f); } log.WriteLine(String.Empty); if (floats[0] != f1) { log.Error("BuildArray produced float {0:f6} instead of {1:f6}", floats[0], f1); return(false); } // // try BuildArray of chars, maxLength terminated // log.Info("Chars, terminated by maxLength"); tokens.Clear(); tokenizer.TokenizeString("12 2 ; 3 4", tokens); foreach(Token t in tokens) log.Debug("Token = {0}", t); i = 0; char[] chars = (char[])BuildArray(tokens, ref i, typeof(Char), null, 3, log); if (chars == null) { log.Error("Unable to BuildArray of chars."); return(false); } log.Info("Parsed chars:"); foreach(char f in chars) { log.Write("{0}, ", f); } log.WriteLine(String.Empty); if (i != 4) { log.Error("BuildArray left i = {0} which is incorrect", i); return(false); } // // try BuildArray of hex numbers // log.Info("Hex numbers"); tokens.Clear(); tokenizer.Settings.ParseHexNumbers = true; tokenizer.TokenizeString("0xfff, 0xffe", tokens); foreach(Token t in tokens) log.Debug("Token = {0}", t); i = 0; ushort[] ushorts = (ushort[])BuildArray(tokens, ref i, typeof(ushort), null, 3, log); if (ushorts == null) { log.Error("Unable to BuildArray of ushorts."); return(false); } log.Info("Parsed ushorts:"); foreach(ushort us in ushorts) { log.Write("{0}, ", us); } log.WriteLine(String.Empty); // if (i != 4) // { // log.Error("BuildArray left i = {0} which is incorrect", i); // return(false); // } log.Info("All PASSED"); return(true); }
/// <summary> /// Converts a Well-known Text representation to a <c>Geometry</c>. /// </summary> /// <param name="reader"> /// A Reader which will return a "Geometry Tagged Text" /// string (see the OpenGIS Simple Features Specification). /// </param> /// <returns>A <c>Geometry</c> read from <c>reader</c>. /// </returns> public IGeometry Read(TextReader reader) { /* var tokens = Tokenize(reader); StreamTokenizer tokenizer = new StreamTokenizer(reader); IList<Token> tokens = new List<Token>(); tokenizer.Tokenize(tokens); // Read directly all tokens */ //_index = 0; // Reset pointer to start of tokens try { var enumerator = new StreamTokenizer(reader).GetEnumerator(); enumerator.MoveNext(); return ReadGeometryTaggedText(enumerator); } catch (IOException e) { throw new GeoAPI.IO.ParseException(e.ToString()); } }
/// <summary> /// Tests if reader is at EOF. /// </summary> private static bool IsAtEndOfFile(StreamReader bufferedReader) { var position = bufferedReader.BaseStream.Position; var tokenizer = new StreamTokenizer(bufferedReader); Token t; if (!tokenizer.NextToken(out t) || t is EofToken) return true; bufferedReader.BaseStream.Seek(position, SeekOrigin.Begin); return false; }