Skip to content

Commit 7377d37

Browse files
committed
[Java.Interop.Tools.JavaSource] Handle common parsing exceptions
Fixes the 91 instances of exceptions being thrown when generating API docs for the API 33 version of Mono.Android. The two most common instances were: System.IndexOutOfRangeException: Index was outside the bounds of the array. at Java.Interop.Tools.JavaSource.UnknownHtmlElementStartTerminal.TryMatch (Irony.Parsing.ParsingContext context, Irony.Parsing.ISourceStream source) [0x0003e] in /Users/builder/azdo/_work/4/s/external/Java.Interop/src/Java.Interop.Tools.JavaSource/Java.Interop.Tools.JavaSource/SourceJavadocToXmldocGrammar.HtmlBnfTerms.cs:390 System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.Collections.Generic.List`1[T].get_Item (System.Int32 index) [0x00009] in <08f46039e5064c628bf7795f9b970b7b>:0 at Java.Interop.Tools.JavaSource.SourceJavadocToXmldocGrammar+BlockTagsBnfTerms+<>c__DisplayClass1_0.<CreateRules>b__8 (Irony.Ast.AstContext context, Irony.Parsing.ParseTreeNode parseNode) [0x00025] in /Users/builder/azdo/_work/4/s/external/Java.Interop/src/Java.Interop.Tools.JavaSource/Java.Interop.Tools.JavaSource/SourceJavadocToXmldocGrammar.BlockTagsBnfTerms.cs:151
1 parent 984711a commit 7377d37

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/Java.Interop.Tools.JavaSource/Java.Interop.Tools.JavaSource/SourceJavadocToXmldocGrammar.BlockTagsBnfTerms.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,12 @@ internal void CreateRules (SourceJavadocToXmldocGrammar grammar)
145145
if (!grammar.ShouldImport (ImportJavadoc.ReturnTag)) {
146146
return;
147147
}
148-
// When encountering multiple @return keys in a line, append subsequent @return key content to the original <returns> element.
149148
var jdi = FinishParse (context, parseNode);
149+
// If we have no return value, continue
150+
if (parseNode.ChildNodes.Count < 2) {
151+
return;
152+
}
153+
// When encountering multiple @return keys in a line, append subsequent @return key content to the original <returns> element.
150154
if (jdi.Returns.Count == 0) {
151155
var r = new XElement ("returns",
152156
AstNodeToXmlContent (parseNode.ChildNodes [1]));

src/Java.Interop.Tools.JavaSource/Java.Interop.Tools.JavaSource/SourceJavadocToXmldocGrammar.HtmlBnfTerms.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Net;
6+
using System.Xml;
57
using System.Xml.Linq;
68

79
using Irony.Ast;
@@ -113,7 +115,8 @@ internal void CreateRules (SourceJavadocToXmldocGrammar grammar)
113115

114116
XNode astNodeElement = new XText (unparsedAElementValue);
115117
try {
116-
var seeElement = XElement.Parse ($"<see href={unparsedAElementValue}</see>");
118+
var decoded = WebUtility.HtmlDecode (unparsedAElementValue);
119+
var seeElement = XElement.Parse ($"<see href={decoded}</see>");
117120
var hrefValue = seeElement.Attribute ("href")?.Value ?? string.Empty;
118121
if (!string.IsNullOrEmpty (hrefValue) &&
119122
(hrefValue.StartsWith ("http", StringComparison.OrdinalIgnoreCase) || hrefValue.StartsWith ("www", StringComparison.OrdinalIgnoreCase))) {
@@ -122,8 +125,9 @@ internal void CreateRules (SourceJavadocToXmldocGrammar grammar)
122125
// TODO: Need to convert relative paths or code references to appropriate CREF value.
123126
parseNode.AstNode = astNodeElement;
124127
}
125-
} catch (Exception) {
128+
} catch (Exception ex) {
126129
Console.Error.WriteLine ($"# Unable to parse HTML element: <see href={unparsedAElementValue}</see>");
130+
Console.Error.WriteLine ($"# {ex.Message}");
127131
parseNode.AstNode = astNodeElement;
128132
}
129133
};
@@ -387,7 +391,7 @@ public override void Init (GrammarData grammarData)
387391
source.PreviewPosition += 1;
388392
int start = source.Location.Position;
389393
int stop = start;
390-
while (source.Text [stop] != '>' && stop < source.Text.Length)
394+
while (stop < source.Text.Length && source.Text [stop] != '>' )
391395
stop++;
392396
if (addingRemarks) {
393397
Console.Error.WriteLine ($"# Unsupported HTML element: {source.Text.Substring (start, stop - start)}");

tests/Java.Interop.Tools.JavaSource-Tests/SourceJavadocToXmldocGrammar.HtmlBnfTermsTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public void HyperLinkDeclaration ()
6262
Assert.AreEqual ("<see href=\"https://developer.android.com/guide/topics/manifest/application-element.html\">application</see>",
6363
r.Root.AstNode.ToString ());
6464

65+
r = p.Parse ("<a href=\"http://www.ietf.org/rfc/rfc2396.txt\">RFC&nbsp;2396: Uniform Resource Identifiers (URI): Generic Syntax</a>");
66+
Assert.IsFalse (r.HasErrors (), DumpMessages (r, p));
67+
Assert.AreEqual ("<see href=\"http://www.ietf.org/rfc/rfc2396.txt\">RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax</see>",
68+
r.Root.AstNode.ToString ());
69+
6570
r = p.Parse ("<a href=\"AutofillService.html#FieldClassification\">field classification</a>");
6671
Assert.IsFalse (r.HasErrors (), DumpMessages (r, p));
6772
Assert.AreEqual ("\"AutofillService.html#FieldClassification\"&gt;field classification",

0 commit comments

Comments
 (0)