Skip to content

Commit

Permalink
SWEEP: Changed all but the spatial module to throw J2N.Text.ParseExce…
Browse files Browse the repository at this point in the history
…ption whereever Lucene thorws ParseException (see #446)
  • Loading branch information
NightOwl888 committed Apr 26, 2021
1 parent bd05187 commit f032f22
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 145 deletions.
22 changes: 11 additions & 11 deletions src/Lucene.Net.Analysis.Common/Analysis/Hunspell/Dictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
string[] parts = whitespacePattern.Split(line).TrimEnd();
if (parts.Length != 2)
{
throw ParseException.Create("Illegal CIRCUMFIX declaration", lineNumber);
throw new ParseException("Illegal CIRCUMFIX declaration", lineNumber);
}
circumfix = flagParsingStrategy.ParseFlag(parts[1]);
}
Expand All @@ -361,7 +361,7 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
string[] parts = whitespacePattern.Split(line).TrimEnd();
if (parts.Length != 2)
{
throw ParseException.Create("Illegal KEEPCASE declaration");
throw new ParseException("Illegal KEEPCASE declaration", lineNumber);
}
keepcase = flagParsingStrategy.ParseFlag(parts[1]);
}
Expand All @@ -370,7 +370,7 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
string[] parts = whitespacePattern.Split(line).TrimEnd();
if (parts.Length != 2)
{
throw ParseException.Create("Illegal NEEDAFFIX declaration", lineNumber);
throw new ParseException("Illegal NEEDAFFIX declaration", lineNumber);
}
needaffix = flagParsingStrategy.ParseFlag(parts[1]);
}
Expand All @@ -379,7 +379,7 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
string[] parts = whitespacePattern.Split(line).TrimEnd();
if (parts.Length != 2)
{
throw ParseException.Create("Illegal ONLYINCOMPOUND declaration", lineNumber);
throw new ParseException("Illegal ONLYINCOMPOUND declaration", lineNumber);
}
onlyincompound = flagParsingStrategy.ParseFlag(parts[1]);
}
Expand All @@ -388,7 +388,7 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
string[] parts = whitespacePattern.Split(line).TrimEnd();
if (parts.Length != 2)
{
throw ParseException.Create("Illegal IGNORE declaration", lineNumber);
throw new ParseException("Illegal IGNORE declaration", lineNumber);
}
ignore = parts[1].ToCharArray();
Array.Sort(ignore);
Expand All @@ -400,10 +400,10 @@ private void ReadAffixFile(Stream affixStream, Encoding decoder)
string type = parts[0];
if (parts.Length != 2)
{
throw ParseException.Create(string.Format("Illegal {0} declaration", type), lineNumber);
throw new ParseException(string.Format("Illegal {0} declaration", type), lineNumber);
}
int num = int.Parse(parts[1], CultureInfo.InvariantCulture);
FST<CharsRef> res = ParseConversions(reader, num);
FST<CharsRef> res = ParseConversions(reader, num, lineNumber); // LUCENENET: Pass linenumber so we can throw it
if (type.Equals("ICONV", StringComparison.Ordinal))
{
iconv = res;
Expand Down Expand Up @@ -532,7 +532,7 @@ private void ParseAffix(JCG.SortedDictionary<string, IList<int>> affixes,
// condition is optional
if (ruleArgs.Length < 4)
{
throw ParseException.Create("The affix file contains a rule with less than four elements: " + line /*, reader.LineNumber */);// LUCENENET TODO: LineNumberReader
throw new ParseException("The affix file contains a rule with less than four elements: " + line, 0 /*, reader.LineNumber */);// LUCENENET TODO: LineNumberReader
}

char flag = flagParsingStrategy.ParseFlag(ruleArgs[1]);
Expand Down Expand Up @@ -659,7 +659,7 @@ private void ParseAffix(JCG.SortedDictionary<string, IList<int>> affixes,
}
}

private FST<CharsRef> ParseConversions(TextReader reader, int num)
private FST<CharsRef> ParseConversions(TextReader reader, int num, int lineNumber)
{
IDictionary<string, string> mappings = new JCG.SortedDictionary<string, string>(StringComparer.Ordinal);

Expand All @@ -669,7 +669,7 @@ private FST<CharsRef> ParseConversions(TextReader reader, int num)
string[] parts = whitespacePattern.Split(line).TrimEnd();
if (parts.Length != 3)
{
throw ParseException.Create("invalid syntax: " + line /*, reader.LineNumber */); // LUCENENET TODO: LineNumberReader
throw new ParseException("invalid syntax: " + line, lineNumber /*, reader.LineNumber */); // LUCENENET TODO: LineNumberReader
}
if (mappings.Put(parts[1], parts[2]) != null)
{
Expand Down Expand Up @@ -723,7 +723,7 @@ internal static string GetDictionaryEncoding(Stream affix)
// this test only at the end as ineffective but would allow lines only containing spaces:
if (ch < 0)
{
throw ParseException.Create("Unexpected end of affix file.", 0);
throw new ParseException("Unexpected end of affix file.", 0);
}
continue;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Lucene version compatibility level 4.8.1
using J2N.Text;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -136,7 +137,7 @@ public override void Parse(TextReader @in)
}
catch (Exception e) when (e.IsIllegalArgumentException())
{
throw ParseException.Create("Invalid synonym rule at line " + lineNumber, 0, e);
throw new ParseException("Invalid synonym rule at line " + lineNumber, 0, e);
//ex.initCause(e);
//throw ex;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Lucene version compatibility level 4.8.1
using J2N.Text;
using Lucene.Net.Util;
using System;
using System.IO;
Expand Down Expand Up @@ -80,7 +81,7 @@ public override void Parse(TextReader @in)
}
catch (Exception e) when (e.IsIllegalArgumentException())
{
throw ParseException.Create("Invalid synonym rule at line " + lineNumber, 0, e);
throw new ParseException("Invalid synonym rule at line " + lineNumber, 0, e);
}
finally
{
Expand Down
3 changes: 2 additions & 1 deletion src/Lucene.Net.Expressions/JS/JavascriptLexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Runtime.Serialization;
#endif
using Antlr.Runtime;
using J2N.Text;
using Lucene.Net.Support;

namespace Lucene.Net.Expressions.JS
Expand Down Expand Up @@ -117,7 +118,7 @@ internal class JavascriptLexer : Lexer
public override void DisplayRecognitionError(string[] tokenNames, RecognitionException re)
{
string message = " unexpected character '" + (char)re.Character + "' at position (" + re.CharPositionInLine + ").";
throw ParseException.Create(message, re.CharPositionInLine, re);
throw new ParseException(message, re.CharPositionInLine, re);
}

// delegates
Expand Down
3 changes: 2 additions & 1 deletion src/Lucene.Net.Expressions/JS/JavascriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

using Antlr.Runtime;
using Antlr.Runtime.Tree;
using J2N.Text;
using Lucene.Net.Support;
using System;
using System.Diagnostics.CodeAnalysis;
Expand Down Expand Up @@ -205,7 +206,7 @@ public override void DisplayRecognitionError(string[] tokenNames, RecognitionExc
}
}
}
throw ParseException.Create(message, re.CharPositionInLine, re);
throw new ParseException(message, re.CharPositionInLine, re);
}

public static string GetReadableTokenString(IToken token)
Expand Down
6 changes: 3 additions & 3 deletions src/Lucene.Net.Spatial/Query/SpatialArgsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static string WriteSpatialArgs(SpatialArgs args)
/// <param name="ctx">The spatial context. Mandatory.</param>
/// <returns>Not null.</returns>
/// <exception cref="ArgumentException">if the parameters don't make sense or an add-on parameter is unknown</exception>
/// <exception cref="ParseException">If there is a problem parsing the string</exception>
/// <exception cref="Spatial4n.Core.Exceptions.ParseException">If there is a problem parsing the string</exception>
/// <exception cref="InvalidShapeException">When the coordinates are invalid for the shape</exception>
public virtual SpatialArgs Parse(string v, SpatialContext ctx)
{
Expand All @@ -80,7 +80,7 @@ public virtual SpatialArgs Parse(string v, SpatialContext ctx)

if (idx < 0 || idx > edx)
{
throw ParseException.Create("missing parens: " + v, -1);
throw new Spatial4n.Core.Exceptions.ParseException("missing parens: " + v, -1);
}

SpatialOperation op = SpatialOperation.Get(v.Substring(0, idx - 0).Trim());
Expand All @@ -90,7 +90,7 @@ public virtual SpatialArgs Parse(string v, SpatialContext ctx)
string body = v.Substring(idx + 1, edx - (idx + 1)).Trim();
if (body.Length < 1)
{
throw ParseException.Create("missing body : " + v, idx + 1);
throw new Spatial4n.Core.Exceptions.ParseException("missing body : " + v, idx + 1);
}

var shape = ParseShape(body, ctx);
Expand Down
16 changes: 4 additions & 12 deletions src/Lucene.Net.Suggest/Suggest/FileDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Search.Spell;
using J2N.Text;
using Lucene.Net.Search.Spell;
using Lucene.Net.Util;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -248,17 +249,8 @@ internal void ReadWeight(string weight)
// LUCENENET specific - don't use exception, use TryParse
if (!long.TryParse(weight, NumberStyles.Integer, CultureInfo.InvariantCulture, out curWeight))
{
try
{
// keep reading floats for bw compat
curWeight = (long)double.Parse(weight, NumberStyles.Float, CultureInfo.InvariantCulture);
}
catch (Exception e) when (e.IsNumberFormatException())
{
// LUCENENET: This is just so we can see what string and what culture was being tested when parsing failed,
// to try to reproduce the conditions of the failure.
throw ParseException.Create($"Weight '{weight}' could not be parsed to long or double in culture '{CultureInfo.CurrentCulture.Name}'.", e);
}
// keep reading floats for bw compat
curWeight = (long)double.Parse(weight, NumberStyles.Float, CultureInfo.InvariantCulture);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/Lucene.Net/Document/DateTools.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using J2N.Text;
using System;
using System.Globalization;

namespace Lucene.Net.Documents
Expand Down Expand Up @@ -192,7 +193,7 @@ public static DateTime StringToDate(string dateString)
}
else
{
throw ParseException.Create("Input is not valid date string: " + dateString, 0);
throw new ParseException("Input is not valid date string: " + dateString, 0);
}
return date;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Lucene.Net.Diagnostics;
using J2N.Text;
using Lucene.Net.Diagnostics;
using System;
using System.Configuration;
using System.IO;
Expand Down Expand Up @@ -281,7 +282,7 @@ public static bool IsNoSuchFileExceptionOrFileNotFoundException(this Exception e
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsParseException(this Exception e)
{
return e is FormatException;
return e is ParseException;
}

/// <summary>
Expand Down
111 changes: 0 additions & 111 deletions src/Lucene.Net/Support/ExceptionHandling/Exceptions/ParseException.cs

This file was deleted.

0 comments on commit f032f22

Please sign in to comment.