forked from xamarin/jar2xml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStart.java
142 lines (130 loc) · 5.02 KB
/
Start.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
/*
* 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.File;
import java.io.OutputStreamWriter;
import java.io.FileOutputStream;
import java.util.List;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Start {
public static void main (String[] args)
{
String droiddocs = null;
String javadocs = null;
String annots = null;
List<String> jar_paths = new ArrayList<String> ();
String out_path = null;
List<String> additional_jar_paths = new ArrayList<String> ();
String usage = "Usage: jar2xml --jar=<jarfile> [--ref=<jarfile>] --out=<file> [--javadocpath=<javadoc>] [--droiddocpath=<droiddoc>] [--annotations=<xmlfile>]";
for (String arg : args) {
if (arg.startsWith ("--javadocpath=")) {
javadocs = arg.substring (14);
if (!javadocs.endsWith ("/"))
javadocs += "/";
} else if (arg.startsWith ("--droiddocpath=")) {
droiddocs = arg.substring (15);
if (!droiddocs.endsWith ("/"))
droiddocs += "/";
} else if (arg.startsWith ("--annotations=")) {
annots = arg.substring (14);
} else if (arg.startsWith ("--jar=")) {
jar_paths.add (arg.substring (6));
} else if (arg.startsWith ("--ref=")) {
additional_jar_paths.add (arg.substring (6));
} else if (arg.startsWith ("--out=")) {
out_path = arg.substring (6);
} else {
System.err.println (usage);
System.exit (1);
}
}
if (jar_paths.size() == 0 || out_path == null) {
System.err.println (usage);
System.exit (1);
}
File dir = new File (out_path).getAbsoluteFile ().getParentFile ();
if (!dir.exists ())
dir.mkdirs ();
JavaArchive jar = null;
try {
jar = new JavaArchive (jar_paths, additional_jar_paths);
} catch (Exception e) {
System.err.println ("error J2X0001: Couldn't open java archive : " + e);
System.exit (1);
}
try {
if (annots != null)
AndroidDocScraper.loadXml (annots);
if (droiddocs != null)
JavaClass.addDocScraper (new DroidDocScraper (new File (droiddocs)));
if (javadocs != null)
JavaClass.addDocScraper (new JavaDocScraper (new File (javadocs)));
} catch (Exception e) {
System.err.println ("warning J2X8001: Couldn't access javadocs at specified docpath. Continuing without it...");
}
Document doc = null;
try {
DocumentBuilderFactory builder_factory = DocumentBuilderFactory.newInstance ();
DocumentBuilder builder = builder_factory.newDocumentBuilder ();
doc = builder.newDocument ();
} catch (Exception e) {
System.err.println ("warning J2X8002: Couldn't create xml document - exception occurred:" + e.getMessage ());
}
try {
Element root = doc.createElement ("api");
doc.appendChild (root);
for (JavaPackage pkg : jar.getPackages ())
pkg.appendToDocument (doc, root);
} catch (Exception e) {
e.printStackTrace ();
System.err.println ("error J2X0002: API analyzer failed with java exception. See verbose output for details.");
System.exit (1);
}
try {
TransformerFactory transformer_factory = TransformerFactory.newInstance ();
Transformer transformer = transformer_factory.newTransformer ();
transformer.setOutputProperty (OutputKeys.INDENT, "yes");
FileOutputStream stream = new FileOutputStream(out_path);
OutputStreamWriter writer = new OutputStreamWriter(stream,"UTF-8");
StreamResult result = new StreamResult (writer);
DOMSource source = new DOMSource (doc);
transformer.transform (source, result);
writer.close ();
} catch (Exception e) {
System.err.println ("error J2X0003: Couldn't format xml file - exception occurred:" + e.getMessage ());
System.exit (1);
}
}
}