-
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathXMLReferenceWriter.java
More file actions
256 lines (214 loc) · 8.31 KB
/
XMLReferenceWriter.java
File metadata and controls
256 lines (214 loc) · 8.31 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package writers;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
public class XMLReferenceWriter extends BaseWriter {
public static void write(String sourceDir, IndexWriter indexWriter) throws IOException
{
write(sourceDir, "", indexWriter);
}
public static void write(String sourceDir, String dstDir, IndexWriter indexWriter) throws IOException
{
File directory = new File(sourceDir);
File[] files = directory.listFiles();
if(files == null ){
return;
}
for(File f : files )
{
if(f.getAbsolutePath().endsWith(".xml"))
{
parseFile(f, dstDir, indexWriter);
}
}
}
public static void parseFile(File f, String dst, IndexWriter indexWriter)
{
Document doc = Shared.loadXmlDocument( f.getPath() );
if( doc != null )
{
TemplateWriter templateWriter = new TemplateWriter();
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
try {
HashMap<String, String> vars = new HashMap<String, String>();
String category = (String) xpath.evaluate("//category", doc, XPathConstants.STRING);
String subcategory = (String) xpath.evaluate("//subcategory", doc, XPathConstants.STRING);
String name = (String) xpath.evaluate("//name", doc, XPathConstants.STRING);
String description = getXMLDescription(doc); //(String) xpath.evaluate("//description", doc, XPathConstants.STRING);
String syntax = (String) xpath.evaluate("//syntax", doc, XPathConstants.STRING);
String classname = (String) xpath.evaluate("//classname", doc, XPathConstants.STRING);
String classAnchor = "";
String um_raw = (String) xpath.evaluate("//unsupported_modes", doc, XPathConstants.STRING);
String[] unsupported_modes = new String[0];
String link_classes = "";
if ( um_raw != null && !um_raw.equals("") ) {
unsupported_modes = um_raw.trim().split(",");
for ( String umode : unsupported_modes ) {
if ( umode.equals("") ) continue;
link_classes += " no-" + umode.trim();
}
}
if ( subcategory.equals("Method") )
{
classname = category;
classAnchor = getAnchorFromName( classname );
}
String constructors = getConstructors( xpath, doc );
// get anchor from original filename
String path = f.getAbsolutePath();
String anchorBase = path.substring( path.lastIndexOf("/")+1, path.indexOf(".xml"));
if ( name.endsWith("()") )
{
if ( !anchorBase.endsWith("_") )
{
anchorBase += "_";
}
}
String anchor = anchorBase + ".html";
String usage = (String) xpath.evaluate("//usage", doc, XPathConstants.STRING);
if ( indexWriter instanceof LibraryIndexWriter )
{
((LibraryIndexWriter) indexWriter).addEvent(name, anchor);
vars.put("csspath", "../../");
}
else if ( !subcategory.equals("Method") )
{
indexWriter.addItem(category, subcategory, name, anchor, link_classes);
}
vars.put("examples", getExamples(doc));
vars.put("name", name);
vars.put("description", description);
vars.put("syntax", syntax);
vars.put("usage", usage);
vars.put("parameters", getParameters(doc, "" ));
vars.put("related", getRelated(doc));
vars.put( "constructors", constructors );
vars.put( "classname", classname );
vars.put( "classanchor", classAnchor );
vars.put( "category", category );
vars.put( "subcategory", subcategory );
if( constructors != "" )
{ // we are documenting a class
vars.put("classname", name);
// TODO: pull in methods and other things
ArrayList< HashMap<String, String> > methodSet = getPropertyInfo( doc, xpath, "method", anchorBase + "_" );
ArrayList< HashMap<String, String> > fieldSet = getPropertyInfo( doc, xpath, "field", anchorBase + "_" );
vars.put( "methods", templateWriter.writeLoop("property.partial.html", methodSet) );
vars.put( "fields", templateWriter.writeLoop("property.partial.html", fieldSet) );
if( vars.get("parameters") == "" )
{ // get constructor parameters
vars.put("parameters", getParameters(doc, "c" ));
}
templateWriter.write("class.template.html", vars, dst + anchor);
}
else
{
templateWriter.write("generic.template.html", vars, dst + anchor);
}
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
System.out.println("Failed to parse " + f.getAbsolutePath());
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Failed to parse " + f.getAbsolutePath());
}
}
}
private static String getConstructors( XPath xpath, Document doc)
{
String constructors = "";
try {
constructors = (String) xpath.evaluate("//constructor", doc, XPathConstants.STRING );
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return constructors;
}
protected static ArrayList< HashMap<String, String > > getPropertyInfo( Document doc, XPath xpath, String tag, String anchorBase )
{
ArrayList<HashMap<String, String>> ret = new ArrayList< HashMap<String, String> >();
try
{
String prefix = tag.substring(0, 1);
XPathExpression expr = xpath.compile("//" + tag);
Object result = expr.evaluate( doc, XPathConstants.NODESET);
NodeList properties = (NodeList) result;
for (int i = 0; i < properties.getLength(); i++ )
{
HashMap<String, String> property = new HashMap<String, String>();
expr = xpath.compile( prefix + "name" );
String name = (String) expr.evaluate( properties.item(i), XPathConstants.STRING );
String anchor = anchorBase + getAnchorFromName( name );
String description = (String) xpath.evaluate( prefix + "description", properties.item(i), XPathConstants.STRING );
property.put("name", name );
property.put("anchor", anchor );
property.put("desc", description );
ret.add( property );
}
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
protected static String getParameters(Document doc, String tagPrefix) throws IOException{
ArrayList<HashMap<String, String>> ret = new ArrayList<HashMap<String,String>>();
//get parameters for this methos
XPath xpath = getXPath();
try{
XPathExpression expr = xpath.compile("//" + tagPrefix + "parameter");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList parameters = (NodeList) result;
for (int i = 0; i < parameters.getLength(); i++) {
String name = (String) xpath.evaluate( tagPrefix + "label", parameters.item(i), XPathConstants.STRING);
String desc = (String) xpath.evaluate( tagPrefix + "description", parameters.item(i), XPathConstants.STRING);
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", name);
map.put("description", desc);
ret.add(map);
}
} catch (XPathExpressionException e) {
// TODO: handle exception
}
TemplateWriter templateWriter = new TemplateWriter();
//write out all parameters with a short loop
return templateWriter.writeLoop("parameter.partial.html", ret);
}
protected static String getRelated(Document doc) throws IOException{
TemplateWriter templateWriter = new TemplateWriter();
ArrayList<HashMap<String, String>> vars = new ArrayList<HashMap<String,String>>();
try{
XPath xpath = getXPath();
String relatedS = (String) xpath.evaluate("//related", doc, XPathConstants.STRING);
if(relatedS.equals("")){
return "";
}
String[] related = relatedS.split("\\n");
for(int i=0; i < related.length; i++ ){
HashMap<String, String> map = new HashMap<String, String>();
String name = related[i];
if(!name.equals("")){
map.put("name", name);
map.put("anchor", getAnchorFromName(name) );
vars.add(map);
}
}
}catch(XPathExpressionException e){
}
return templateWriter.writeLoop("related.partial.html", vars);
}
static protected XPath getXPath(){
XPathFactory xpathFactory = XPathFactory.newInstance();
return xpathFactory.newXPath();
}
}