This repository was archived by the owner on Feb 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocClass.java
More file actions
184 lines (163 loc) · 5.97 KB
/
DocClass.java
File metadata and controls
184 lines (163 loc) · 5.97 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
//
// Copyright 2016, Yahoo Inc.
// Copyrights licensed under the New BSD License.
// See the accompanying LICENSE file for terms.
//
package github.com.jminusminus.doc;
import github.com.jminusminus.core.io.BufferedInputStreamReader;
import github.com.jminusminus.core.io.InputStreamMock;
// Container for class documentation.
// ```
// new DocClass();
// ```
public class DocClass {
// The source file.
public String file = "";
// The package name.
public String packageName = "";
// The description for the package.
public String packageDesc = "";
// The description for main.
public String mainDesc = "";
// The class name.
public String className = "";
// The description for the class.
public String classDesc = "";
// The class attributes.
public DocAttribute[] attributes = new DocAttribute[0];
// The class methods.
public DocMethod[] methods = new DocMethod[0];
DocClass(String file) {
this.file = file;
this.parse();
}
public String classPath() {
return this.packageName + "." + this.className;
}
// Returns Markdown.
public String toString() {
String md = "# " + this.className + Doc.CRLF;
md += "#### Package: " + this.packageName + Doc.CRLF;
if (this.packageDesc.length() > 0) {
md += this.packageDesc + Doc.CRLF;
}
md += "## Install" + Doc.CRLF + "```" + Doc.CRLF + "jmm get " + this.getInstallPath() + Doc.CRLF + "```" + Doc.CRLF;
if (this.mainDesc.length() > 0) {
md += this.mainDesc + Doc.CRLF;
}
md += "## Class: " + this.classPath() + Doc.CRLF;
md += "```" + Doc.CRLF + "import " + this.classPath() + ";" + Doc.CRLF + "```" + Doc.CRLF;
if (this.classDesc.length() > 0) {
md += this.classDesc + Doc.CRLF;
}
md += "## Attributes" + Doc.CRLF;
for (DocAttribute i : this.attributes) {
md += i.toString();
}
md += "## Methods" + Doc.CRLF;
for (DocMethod i : this.methods) {
md += i.toString();
}
return md;
}
// Parse a .java file and return markdown documentation.
protected boolean parse() {
byte[] data = new byte[0];
try {
data = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(this.file));
} catch (Exception e) {
return false;
}
BufferedInputStreamReader reader = new BufferedInputStreamReader(new InputStreamMock(data));
byte[] buf = reader.readTo(Doc.LF);
String line;
String comment = "";
while (buf.length > 0) {
line = new String(buf).trim();
if (line.isEmpty() == false) {
comment = this.parseLine(line, comment);
} else {
comment = "";
}
// Read the next line into the buffer.
buf = reader.readTo(Doc.LF);
}
return true;
}
protected String parseLine(String line, String comment) {
if (line.startsWith("//")) {
// Build up the comment.
comment += this.removeCommentSpace(line.substring(2)) + Doc.CRLF;
} else if (line.startsWith("protected")) {
comment = "";
} else if (line.startsWith("public")) {
comment = this.parseLinePublic(line, comment);
} else if (line.startsWith("package") && this.packageName.length() == 0) {
this.packageName = line.substring(7, line.length() - 1).trim();
this.packageDesc = comment;
comment = "";
}
return comment;
}
protected String parseLinePublic(String line, String comment) {
if (line.startsWith("public") && line.endsWith(";")) {
// Add attribute.
DocAttribute a = new DocAttribute();
a.name = line.substring(6, line.length() - 1).trim();
a.desc = comment;
this.addAttr(a);
comment = "";
} else if (line.startsWith("public class") && line.endsWith("{")) {
// Add class.
this.classDesc = comment;
this.className = line.substring(12, line.length() - 1).trim();
comment = "";
} else if (line.startsWith("public static void main(")) {
// Add main method description.
this.mainDesc = comment;
comment = "";
} else if (line.startsWith("public") && line.endsWith("{")) {
// Add method.
DocMethod m = new DocMethod();
m.name = line.substring(6, line.length() - 1).trim();
m.desc = comment;
this.addMethod(m);
comment = "";
}
return comment;
}
// Is there is a space before the coment text starts [// abc]?
// Or, are there spaces in comment code [// some code]?
protected String removeCommentSpace(String comment) {
if (comment.length() == 0) {
return "";
}
int index = 0;
// Count the numer of spaces before a comment starts.
char token = comment.charAt(index);
while (token == ' ') {
index++;
token = comment.charAt(index);
}
if (index == 1 || index == 5) {
return comment.substring(1);
}
return comment;
}
protected void addAttr(DocAttribute a) {
DocAttribute[] n = new DocAttribute[this.attributes.length + 1];
System.arraycopy(this.attributes, 0, n, 0, this.attributes.length);
n[this.attributes.length] = a;
this.attributes = n;
}
protected void addMethod(DocMethod m) {
DocMethod[] n = new DocMethod[this.methods.length + 1];
System.arraycopy(this.methods, 0, n, 0, this.methods.length);
n[this.methods.length] = m;
this.methods = n;
}
// This needs updating when more than github.com is supported.
protected String getInstallPath() {
return this.packageName.replace('.', '/').replaceFirst("/", ".");
}
}