Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 1 addition & 32 deletions src/Xamarin.Android.Tools.Bytecode/ClassPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public class ClassPath {

public string ApiSource { get; set; }

public JavaDocletType? DocletType { get; set; }

public IEnumerable<string> DocumentationPaths { get; set; }

public string AndroidFrameworkPlatform { get; set; }
Expand Down Expand Up @@ -233,38 +231,9 @@ void FixupParametersFromDocs (XElement api)
}
}

JavaDocletType GetDocletType (string path)
{
var kind = JavaDocletType.DroidDoc;
char [] buf = new char [500];
string packagesHtml = Path.Combine (path, "packages.html");
if (File.Exists (packagesHtml) && File.ReadAllText (packagesHtml).Contains ("<body class=\"gc-documentation develop reference api "))
kind = JavaDocletType.DroidDoc2;
using (var reader = File.OpenText (Path.Combine (path, "index.html")))
reader.ReadBlock (buf, 0, buf.Length);
string rawHTML = new string (buf);
if (rawHTML.Contains ("Generated by javadoc (build 1.6"))
kind = JavaDocletType.Java6;
else if (rawHTML.Contains ("Generated by javadoc (version 1.7"))
kind = JavaDocletType.Java7;
else if (rawHTML.Contains ("Generated by javadoc (1.8"))
kind = JavaDocletType.Java8;

// Check to see if it's an api.xml formatted doc
string rawXML = null;
using (var reader = File.OpenText (path)) {
int len = reader.ReadBlock (buf, 0, buf.Length);
rawXML = new string (buf, 0, len);
}
if (rawXML.Contains ("<api>") && rawXML.Contains ("<package"))
kind = JavaDocletType._ApiXml;

return kind;
}

IAndroidDocScraper CreateDocScraper (string src)
{
switch (DocletType ?? GetDocletType (src)) {
switch (AndroidDocScraper.GetDocletType (src)) {
default: return new DroidDoc2Scraper (src);
case JavaDocletType.DroidDoc: return new DroidDocScraper (src);
case JavaDocletType.Java6: return new JavaDocScraper (src);
Expand Down
36 changes: 36 additions & 0 deletions src/Xamarin.Android.Tools.Bytecode/JavaDocumentScraper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,42 @@ public static void LoadXml (String filename)
Log.Error ("Annotations parser error: " + ex);
}
}

public static JavaDocletType GetDocletType (string path)
{
var kind = JavaDocletType.DroidDoc;
char[] buf = new char[500];

string packagesHtml = Path.Combine (path, "packages.html");
if (File.Exists (packagesHtml) && File.ReadAllText (packagesHtml).Contains ("<body class=\"gc-documentation develop reference api "))
kind = JavaDocletType.DroidDoc2;

string indexHtml = Path.Combine (path, "index.html");
if (File.Exists (indexHtml)) {
using (var reader = File.OpenText (indexHtml))
reader.ReadBlock (buf, 0, buf.Length);
string rawHTML = new string (buf);
if (rawHTML.Contains ("Generated by javadoc (build 1.6"))
kind = JavaDocletType.Java6;
else if (rawHTML.Contains ("Generated by javadoc (version 1.7"))
kind = JavaDocletType.Java7;
else if (rawHTML.Contains ("Generated by javadoc (1.8"))
kind = JavaDocletType.Java8;
}

// Check to see if it's an api.xml formatted doc
if (File.Exists (path)) {
string rawXML = null;
using (var reader = File.OpenText (path)) {
int len = reader.ReadBlock (buf, 0, buf.Length);
rawXML = new string (buf, 0, len);
}
if (rawXML.Contains ("<api>") && rawXML.Contains ("<package"))
kind = JavaDocletType._ApiXml;
}

return kind;
}
}

public interface IAndroidDocScraper
Expand Down
17 changes: 12 additions & 5 deletions src/Xamarin.Android.Tools.Bytecode/Tests/ClassFileFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,29 @@ protected static string LoadString (string resource)
return r.ReadToEnd ();
}

protected static void AssertXmlDeclaration (string classResource, string xmlResource, string documentationPath = null, JavaDocletType? javaDocletType = null)
protected static string LoadToTempFile (string resource)
{
var tempFilePath = Path.GetTempFileName ();

using (var w = File.Create (tempFilePath))
using (var s = Assembly.GetExecutingAssembly ().GetManifestResourceStream (resource))
s.CopyTo (w);

return tempFilePath;
}

protected static void AssertXmlDeclaration (string classResource, string xmlResource, string documentationPath = null)
{
var classPathBuilder = new ClassPath () {
ApiSource = "class-parse",
DocumentationPaths = new string[] {
documentationPath,
},
};
if (javaDocletType.HasValue)
classPathBuilder.DocletType = javaDocletType.Value;
classPathBuilder.Add (LoadClassFile (classResource));

var actual = new StringWriter ();
classPathBuilder.ApiSource = "class-parse";
if (javaDocletType.HasValue)
classPathBuilder.DocletType = javaDocletType.Value;
classPathBuilder.SaveXmlDescription (actual);

var expected = LoadString (xmlResource);
Expand Down
58 changes: 41 additions & 17 deletions src/Xamarin.Android.Tools.Bytecode/Tests/ParameterFixupTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO;
using NUnit.Framework;
using Xamarin.Android.Tools.Bytecode;

namespace Xamarin.Android.Tools.BytecodeTests
{
Expand Down Expand Up @@ -30,27 +31,17 @@ public void XmlDeclaration_FixedUpFromDocumentation()
}

[Test]
public void XmlDeclaration_FixedUpFromApiXmlDocumentation()
public void XmlDeclaration_FixedUpFromApiXmlDocumentation ()
{
string tempFile = null;

try
{
tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, LoadString("ParameterFixupApiXmlDocs.xml"));

AssertXmlDeclaration("Collection.class", "ParameterFixupFromDocs.xml", tempFile, Bytecode.JavaDocletType._ApiXml);
}
catch (Exception ex)
{
try
{
if (File.Exists(tempFile))
File.Delete(tempFile);
}
catch { }
try {
tempFile = LoadToTempFile ("ParameterFixupApiXmlDocs.xml");

Assert.Fail("An unexpected exception was thrown : {0}", ex);
AssertXmlDeclaration ("Collection.class", "ParameterFixupFromDocs.xml", tempFile);
} finally {
if (File.Exists (tempFile))
File.Delete (tempFile);
}
}

Expand All @@ -63,6 +54,39 @@ public void XmlDeclaration_DoesNotThrowAnExceptionIfDocumentationNotFound ()
Assert.Fail ("An unexpected exception was thrown : {0}", ex);
}
}

[Test]
public void DocletType_ShouldDetectApiXml ()
{
string tempFile = null;

try {
tempFile = LoadToTempFile ("ParameterFixupApiXmlDocs.xml");

Assert.AreEqual (JavaDocletType._ApiXml, AndroidDocScraper.GetDocletType (tempFile));
} finally {
if (File.Exists (tempFile))
File.Delete (tempFile);
}
}

[Test]
public void DocletType_ShouldDetectDroidDocs ()
{
var androidSdkPath = Environment.GetEnvironmentVariable ("ANDROID_SDK_PATH");
if (string.IsNullOrEmpty (androidSdkPath)) {
Assert.Ignore("The `ANDROID_SDK_PATH` environment variable isn't set; " +
"cannot test importing parameter names from HTML. Skipping...");
return;
}

var droidDocsPath = Path.Combine (androidSdkPath, "docs", "reference");

if (!Directory.Exists (droidDocsPath))
Assert.Fail("The Android SDK Documentation path `{0}` was not found.", droidDocsPath);

Assert.AreEqual(JavaDocletType.DroidDoc2, AndroidDocScraper.GetDocletType(droidDocsPath));
}
}
}

28 changes: 5 additions & 23 deletions tools/class-parse/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ class App {

public static void Main (string[] args)
{
JavaDocletType docsType = 0;

bool dump = false;
bool help = false;
bool docsType = false;
int verbosity = 0;
bool autorename = false;
var outputFile = (string) null;
Expand All @@ -40,9 +39,8 @@ public static void Main (string[] args)
"Documentation {PATH} for parameter fixup",
doc => docsPaths.Add (doc) },
{ "docstype=",
"{TYPE} of the docs within --docspath. Values:\n " +
string.Join ("\n ", JavaDocletTypeMapping.Keys.OrderBy (s => s)),
t => docsType = GetJavaDocletType (t) },
"OBSOLETE: Previously used to specify a doc type (now auto detected).",
t => docsType = t != null },
{ "v|verbose:",
"See stack traces on error.",
(int? v) => verbosity = v.HasValue ? v.Value : verbosity + 1 },
Expand All @@ -61,6 +59,8 @@ public static void Main (string[] args)
p.WriteOptionDescriptions (Console.Out);
return;
}
if (docsType)
Console.WriteLine ("class-parse: --docstype is obsolete and no longer a valid option.");
var output = outputFile == null
? Console.Out
: (TextWriter) new StreamWriter (outputFile, append: false, encoding: new UTF8Encoding (encoderShouldEmitUTF8Identifier: false));
Expand All @@ -71,7 +71,6 @@ public static void Main (string[] args)
ApiSource = "class-parse",
AndroidFrameworkPlatform = platform,
DocumentationPaths = docsPaths.Count == 0 ? null : docsPaths,
DocletType = docsType,
AutoRename = autorename
};
foreach (var file in files) {
Expand All @@ -93,23 +92,6 @@ public static void Main (string[] args)
output.Close ();
}

static Dictionary<string, JavaDocletType> JavaDocletTypeMapping = new Dictionary<string, JavaDocletType> {
{ "droiddoc", JavaDocletType.DroidDoc },
{ "droiddoc2", JavaDocletType.DroidDoc2 },
{ "java6", JavaDocletType.Java6 },
{ "java7", JavaDocletType.Java7 },
{ "java8", JavaDocletType.Java8 },
{ "apixml", JavaDocletType._ApiXml },
};

static JavaDocletType GetJavaDocletType (string value)
{
JavaDocletType type;
if (value != null && JavaDocletTypeMapping.TryGetValue (value.ToLowerInvariant (), out type))
return type;
return JavaDocletType.DroidDoc;
}

static void DumpFileToXml (ClassPath jar, string file)
{
using (var s = File.OpenRead (file)) {
Expand Down