Skip to content

Commit 806082f

Browse files
Redthatsushieno
authored andcommitted
api.xml doc parser (#97)
* Added ApiXml file doc parser This will parse parameter names from an xml file in the api.xml format. * Added test for ApiXml doc parser * Make name more obviously for internal use
1 parent b4e151e commit 806082f

File tree

7 files changed

+103
-10
lines changed

7 files changed

+103
-10
lines changed

src/Xamarin.Android.Tools.Bytecode/ClassPath.cs

+9-7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public enum JavaDocletType {
1717
Java6,
1818
Java7,
1919
Java8,
20+
_ApiXml
2021
}
2122

2223
public class ClassPath {
@@ -217,20 +218,21 @@ void FixupParametersFromDocs (XElement api)
217218
if (DocumentationPaths == null)
218219
return;
219220
foreach (var path in DocumentationPaths) {
220-
if (!Directory.Exists (path))
221+
if (!Directory.Exists (path) && !File.Exists (path))
221222
continue;
222223
FixupParametersFromDocs (api, path);
223224
}
224225
}
225226

226-
AndroidDocScraper CreateDocScraper (string dir)
227+
IAndroidDocScraper CreateDocScraper (string src)
227228
{
228229
switch (DocletType) {
229-
default: return new DroidDoc2Scraper (dir);
230-
case JavaDocletType.DroidDoc: return new DroidDocScraper (dir);
231-
case JavaDocletType.Java6: return new JavaDocScraper (dir);
232-
case JavaDocletType.Java7: return new Java7DocScraper (dir);
233-
case JavaDocletType.Java8: return new Java8DocScraper (dir);
230+
default: return new DroidDoc2Scraper (src);
231+
case JavaDocletType.DroidDoc: return new DroidDocScraper (src);
232+
case JavaDocletType.Java6: return new JavaDocScraper (src);
233+
case JavaDocletType.Java7: return new Java7DocScraper (src);
234+
case JavaDocletType.Java8: return new Java8DocScraper (src);
235+
case JavaDocletType._ApiXml: return new ApiXmlDocScraper (src);
234236
}
235237
}
236238

src/Xamarin.Android.Tools.Bytecode/JavaDocumentScraper.cs

+50-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public Java8DocScraper (string dir)
125125
}
126126
}
127127

128-
public abstract class AndroidDocScraper
128+
public abstract class AndroidDocScraper : IAndroidDocScraper
129129
{
130130
readonly String pattern_head;
131131
readonly String reset_pattern_head;
@@ -269,6 +269,54 @@ public static void LoadXml (String filename)
269269
} catch (Exception ex) {
270270
Log.Error ("Annotations parser error: " + ex);
271271
}
272-
}
272+
}
273+
}
274+
275+
public interface IAndroidDocScraper
276+
{
277+
String[] GetParameterNames (string package, string type, string method, string[] ptypes, bool isVarArgs);
278+
}
279+
280+
public class ApiXmlDocScraper : IAndroidDocScraper
281+
{
282+
public ApiXmlDocScraper (string apiXmlFile)
283+
{
284+
xdoc = XDocument.Load (apiXmlFile);
285+
}
286+
287+
XDocument xdoc;
288+
289+
public string[] GetParameterNames (string package, string type, string method, string[] ptypes, bool isVarArgs)
290+
{
291+
var methodOrCtor = method == "constructor" ?
292+
"constructor[" : $"method[@name='{method}'";
293+
294+
var pcount = ptypes.Length;
295+
296+
var xpath = new StringBuilder ();
297+
298+
xpath.Append ($"/api/package[@name='{package}']/*[self::class or self::interface]/");
299+
300+
if (method == "constructor")
301+
xpath.Append ("constructor[");
302+
else
303+
xpath.Append ($"method[@name='{method}'");
304+
305+
xpath.Append ($" and count(parameter)={pcount}");
306+
307+
if (pcount > 0) {
308+
xpath.Append (" and ");
309+
xpath.Append (string.Join (" and ", ptypes.Select ((pt, pindex) => $"parameter[{pindex + 1}][@type='{pt}']")));
310+
}
311+
312+
xpath.Append ("]");
313+
314+
var methodElem = xdoc.XPathSelectElement (xpath.ToString ());
315+
316+
if (methodElem != null)
317+
return methodElem.Elements ("parameter").Select (pe => pe.Attribute ("name")?.Value).ToArray ();
318+
319+
return new string[0];
320+
}
273321
}
274322
}

src/Xamarin.Android.Tools.Bytecode/Tests/ClassFileFixture.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ protected static string LoadString (string resource)
2525
return r.ReadToEnd ();
2626
}
2727

28-
protected static void AssertXmlDeclaration (string classResource, string xmlResource, string documentationPath = null)
28+
protected static void AssertXmlDeclaration (string classResource, string xmlResource, string documentationPath = null, JavaDocletType? javaDocletType = null)
2929
{
3030
var classPathBuilder = new ClassPath () {
3131
ApiSource = "class-parse",
3232
DocumentationPaths = new string[] {
3333
documentationPath,
3434
},
3535
};
36+
if (javaDocletType.HasValue)
37+
classPathBuilder.DocletType = javaDocletType.Value;
3638
classPathBuilder.Add (LoadClassFile (classResource));
3739

3840
var actual = new StringWriter ();
3941
classPathBuilder.ApiSource = "class-parse";
42+
if (javaDocletType.HasValue)
43+
classPathBuilder.DocletType = javaDocletType.Value;
4044
classPathBuilder.SaveXmlDescription (actual);
4145

4246
var expected = LoadString (xmlResource);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
2+
<api>
3+
<package name="java.util">
4+
<class name="Collection">
5+
<method name="add">
6+
<parameter name="object" type="E" />
7+
</method>
8+
</class>
9+
</package>
10+
</api>

src/Xamarin.Android.Tools.Bytecode/Tests/ParameterFixupTests.cs

+25
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,31 @@ public void XmlDeclaration_FixedUpFromDocumentation()
2929
}
3030
}
3131

32+
[Test]
33+
public void XmlDeclaration_FixedUpFromApiXmlDocumentation()
34+
{
35+
string tempFile = null;
36+
37+
try
38+
{
39+
tempFile = Path.GetTempFileName();
40+
File.WriteAllText(tempFile, LoadString("ParameterFixupApiXmlDocs.xml"));
41+
42+
AssertXmlDeclaration("Collection.class", "ParameterFixupFromDocs.xml", tempFile, Bytecode.JavaDocletType._ApiXml);
43+
}
44+
catch (Exception ex)
45+
{
46+
try
47+
{
48+
if (File.Exists(tempFile))
49+
File.Delete(tempFile);
50+
}
51+
catch { }
52+
53+
Assert.Fail("An unexpected exception was thrown : {0}", ex);
54+
}
55+
}
56+
3257
[Test]
3358
public void XmlDeclaration_DoesNotThrowAnExceptionIfDocumentationNotFound ()
3459
{

src/Xamarin.Android.Tools.Bytecode/Tests/Xamarin.Android.Tools.Bytecode-Tests.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
<EmbeddedResource Include="$(IntermediateOutputPath)classes\java\util\Collection.class">
152152
<LogicalName>Collection.class</LogicalName>
153153
</EmbeddedResource>
154+
<EmbeddedResource Include="ParameterFixupApiXmlDocs.xml">
155+
<LogicalName>ParameterFixupApiXmlDocs.xml</LogicalName>
156+
</EmbeddedResource>
154157
</ItemGroup>
155158
<ItemGroup>
156159
<None Include="packages.config" />

tools/class-parse/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static void Main (string[] args)
9494
{ "java6", JavaDocletType.Java6 },
9595
{ "java7", JavaDocletType.Java7 },
9696
{ "java8", JavaDocletType.Java8 },
97+
{ "apixml", JavaDocletType.ApiXml },
9798
};
9899

99100
static JavaDocletType GetJavaDocletType (string value)

0 commit comments

Comments
 (0)