forked from xamarin/jar2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidDocScraper.java
197 lines (170 loc) · 6.77 KB
/
AndroidDocScraper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* Copyright (c) 2011 Xamarin Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package jar2xml;
import java.io.*;
import java.lang.reflect.*;
import java.util.regex.*;
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.objectweb.asm.tree.*;
class DroidDocScraper extends AndroidDocScraper {
static final String pattern_head_droiddoc = "<span class=\"sympad\"><a href=\".*";
public DroidDocScraper (File dir) throws IOException {
super (dir, pattern_head_droiddoc, null, null);
}
}
class JavaDocScraper extends AndroidDocScraper {
static final String pattern_head_javadoc = "<TD><CODE><B><A HREF=\"[./]*"; // I'm not sure how path could be specified... (./ , ../ , or even /)
static final String reset_pattern_head_javadoc = "<TD><CODE>";
static final String parameter_pair_splitter_javadoc = " ";
public JavaDocScraper (File dir) throws IOException {
super (dir, pattern_head_javadoc, reset_pattern_head_javadoc, parameter_pair_splitter_javadoc);
}
}
public abstract class AndroidDocScraper implements IDocScraper {
final String pattern_head;
final String reset_pattern_head;
final String parameter_pair_splitter;
File root;
protected AndroidDocScraper (File dir, String patternHead, String resetPatternHead, String parameterPairSplitter) throws IOException {
if (dir == null)
throw new IllegalArgumentException ();
pattern_head = patternHead;
reset_pattern_head = resetPatternHead;
parameter_pair_splitter = parameterPairSplitter != null ? parameterPairSplitter : "\\s+";
if (!dir.exists())
throw new FileNotFoundException (dir.getAbsolutePath());
if (!dir.isDirectory())
throw new IllegalArgumentException (dir.getAbsolutePath() + " is not a directory.");
root = dir;
if (!new File (dir.getAbsolutePath() + "/package-list").isFile() &&
!new File (dir.getAbsolutePath() + "/packages.html").isFile())
throw new IllegalArgumentException (dir.getAbsolutePath() + " does not appear to be an android doc reference directory.");
}
public String[] getParameterNames (ClassNode asm, String name, Type[] ptypes, boolean isVarArgs)
{
String path = asm.name.replace ('$', '.') + ".html";
File file = new File(root.getPath() + "/" + path);
if (!file.isFile ()) {
// System.err.println ("Warning: no document found : " + file);
return null;
}
StringBuffer buffer = new StringBuffer ();
buffer.append (pattern_head);
buffer.append (path);
buffer.append ("#");
buffer.append (name);
buffer.append ("\\(\\Q");
for (int i = 0; i < ptypes.length; i++) {
if (i != 0)
buffer.append (", ");
String type = JavaClass.getGenericTypeName (ptypes[i]);
if (isVarArgs && i == ptypes.length - 1)
type = type.replace ("[]", "...");
buffer.append (type);
}
buffer.append("\\E\\)\".*\\((.*)\\)");
Pattern pattern = Pattern.compile (buffer.toString());
try {
FileInputStream stream = new FileInputStream (file);
try {
InputStreamReader rdr;
rdr = new InputStreamReader (stream, "UTF-8");
BufferedReader br = new BufferedReader (rdr);
String text = "";
String prev = null;
while ((text = br.readLine ()) != null) {
if (prev != null)
prev = text = prev + text;
Matcher matcher = pattern.matcher (text);
if (matcher.find ()) {
String plist = matcher.group (1);
String[] parms = plist.split (", ");
if (parms.length != ptypes.length)
System.err.println ("failed matching " + buffer.toString ());
String[] result = new String [ptypes.length];
for (int i = 0; i < ptypes.length; i++) {
String[] toks = parms [i].split (parameter_pair_splitter);
result [i] = toks [toks.length - 1];
}
stream.close();
return result;
}
// sometimes we get incomplete tag, so cache it until it gets complete or matched.
// I *know* this is a hack.
if (reset_pattern_head == null || text.endsWith (">") || !text.startsWith (reset_pattern_head))
prev = null;
else
prev = text;
}
} finally {
stream.close();
}
} catch (Exception e) {
// System.err.println ("ERROR " + e);
return new String [0];
}
// System.err.println ("Warning : no match for " + asm.name + " :: " + name);
return new String [0];
}
static Map<String,List<String>> deprecatedFields;
static Map<String,List<String>> deprecatedMethods;
public static void loadXml (String filename)
{
try {
Document doc = DocumentBuilderFactory.newInstance ().newDocumentBuilder ().parse (filename);
deprecatedFields = new HashMap<String,List<String>> ();
deprecatedMethods = new HashMap<String,List<String>> ();
NodeList files = doc.getDocumentElement ().getElementsByTagName ("file");
for (int i = 0; i < files.getLength (); i++) {
Element file = (Element) files.item (i);
ArrayList<String> f = new ArrayList<String> ();
deprecatedFields.put (file.getAttribute ("name"), f);
NodeList fields = file.getElementsByTagName ("field");
for (int j = 0; j < fields.getLength (); j++)
f.add (fields.item (j).getTextContent ());
ArrayList<String> m = new ArrayList<String> ();
deprecatedMethods.put (file.getAttribute ("name"), m);
NodeList methods = file.getElementsByTagName ("method");
for (int j = 0; j < methods.getLength (); j++)
m.add (methods.item (j).getTextContent ());
}
} catch (Exception ex) {
System.err.println ("Annotations parser error: " + ex);
}
}
public static List<String> getDeprecatedFields (ClassNode asm)
{
if (deprecatedFields == null)
return null;
return deprecatedFields.get (asm.name.replace ('$', '.'));
}
public static List<String> getDeprecatedMethods (ClassNode asm)
{
if (deprecatedMethods == null)
return null;
return deprecatedMethods.get (asm.name.replace ('$', '.'));
}
}