-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjavatest.java
173 lines (147 loc) · 4.8 KB
/
javatest.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
package CodeTransform;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class HTMLWriter {
private CodeParser parser_;
public HTMLWriter(CodeParser parser) {
parser_ = parser;
}
void write(File file) {
try {
if (file.exists() == false) {
file.createNewFile();
}
FileOutputStream fileOutputStream = new FileOutputStream(file);
writeHeader(fileOutputStream, file.getName());
writeBody(fileOutputStream);
writeFooter(fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeFooter(FileOutputStream fileOutputStream) {
String footerString = "</body>\r\n</html>";
try {
fileOutputStream.write(footerString.getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
private String replaceEscapeChars(String src) {
src = src.replace("&", "&");
src = src.replace(" ", " ");
src = src.replace("\r\n", " \r\n");
src = src.replace("\t", " ");
src = src.replace("<", "<");
src = src.replace(">", ">");
return src;
}
private void writeBody(FileOutputStream fileOutputStream) {
ArrayList<ParsedCode> parsedCodes = parser_.getParserResult();
try {
fileOutputStream
.write(" <div class=\"code\" style=\"background-color:#E7E5DC\">\r\n"
.getBytes());
fileOutputStream
.write(" <ol start=\"1\" style=\"margin:0 0 1px 45px !important;\">\r\n"
.getBytes());
for (int i = 0; i < parsedCodes.size(); i++) {
ParsedCode parsedCode = parsedCodes.get(i);
fileOutputStream.write(" <li>\r\n".getBytes());
String colorString = ColorConverter
.Color2String(parsedCode.codeColor_);
String fontString = parsedCode.codeFont_;
String spanString = " <span style=\"color:" + colorString
+ "; font:" + fontString + "\" >";
fileOutputStream.write(spanString.getBytes());
do {
String codeString = parsedCode.codeString_;
//检测换行个数,两个以上的话拆分成开
int index = codeString.indexOf("\n");
if (index >= 0) {
index++;
if (index < codeString.length()) {
// 至少有两个换行
String rightString = codeString.substring(index);
ParsedCode parsedCodeNew = new ParsedCode();
parsedCodeNew.codeColor_ = parsedCode.codeColor_;
parsedCodeNew.codeString_ = rightString;
parsedCodeNew.codeFont_ = parsedCode.codeFont_;
parsedCodes.add(i+1, parsedCodeNew);
String leftString = codeString.substring(0, index);
parsedCode.codeString_ = leftString;
parsedCodes.set(i, parsedCode);
codeString = leftString;
}
}
codeString = replaceEscapeChars(codeString);
fileOutputStream.write(codeString.getBytes());
if (codeString.indexOf("\n") > 0) {
break;
}
if (i < parsedCodes.size() - 1) {
ParsedCode nextCode = parsedCodes.get(i + 1);
if (nextCode.codeColor_.equals(parsedCode.codeColor_) == false
&& nextCode.codeString_.indexOf('\n') < 0) {
// 颜色不一样,又不是回车
colorString = ColorConverter
.Color2String(nextCode.codeColor_);
fontString = nextCode.codeFont_;
spanString = "</span><span style=\"color:" + colorString
+ "; font:" + fontString + "\" >";
fileOutputStream.write(spanString.getBytes());
parsedCode = nextCode;
++i;
} else if (nextCode.codeColor_
.equals(parsedCode.codeColor_)) {
parsedCode = nextCode;
++i;
} else {
parsedCode = nextCode;
++i;
}
} else {
break;
}
} while (i < parsedCodes.size());
spanString = " </span>\r\n";
fileOutputStream.write(spanString.getBytes());
fileOutputStream.write(" </li>\r\n".getBytes());
}
fileOutputStream.write(" </ol>\r\n".getBytes());
fileOutputStream.write(" </div>\r\n".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
}
private void writeHeader(FileOutputStream fileOutputStream, String title) {
String headerString = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" "
+ "\"http://www.w3.org/TR/html4/loose.dtd\">\r\n"
+ "<html>\r\n"
+ "<head>\r\n"
+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"
+ "<title>"
+ title
+ "</title>\r\n"
+ "</head>\r\n"
+ "<body>\r\n";
try {
fileOutputStream.write(headerString.getBytes());
System.out.println("HTMLWriter.writeHeader()");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
JavaParser parser = new JavaParser();
parser.init(new File("temp/javatest.java"));
parser.parse();
HTMLWriter writer = new HTMLWriter(parser);
writer.write(new File("Test.html"));
}
}