Skip to content

Commit f6a5394

Browse files
committed
除变量外的高亮字段可成功转换
1 parent 88a2d22 commit f6a5394

File tree

6 files changed

+315
-0
lines changed

6 files changed

+315
-0
lines changed

.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Java2Html</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding//src/totoro/top/java2html/ChangeFrame.java=UTF-8
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 255 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
package totoro.top.java2html;
2+
3+
import java.awt.event.KeyEvent;
4+
import java.awt.event.KeyListener;
5+
6+
import javax.swing.JButton;
7+
import javax.swing.JFrame;
8+
import javax.swing.JLabel;
9+
import javax.swing.JPanel;
10+
import javax.swing.JScrollPane;
11+
import javax.swing.JTextArea;
12+
13+
public class ChangeFrame extends JFrame implements KeyListener{
14+
15+
private static final long serialVersionUID = 1L;
16+
17+
public static void main(String[] args) {
18+
new ChangeFrame();
19+
}
20+
21+
private JLabel javaLabel = new JLabel("Java:粘贴JAVA代码至下面代码框");
22+
private JLabel htmlLabel = new JLabel("Html:复制代码框代码到HTML页面");
23+
24+
private JTextArea javaArea = new JTextArea();
25+
private JTextArea htmlArea = new JTextArea();
26+
// 给代码框添加滚动�
27+
private JScrollPane javaScroll = new JScrollPane(javaArea);
28+
private JScrollPane htmlScroll = new JScrollPane(htmlArea);
29+
private JPanel javaPanel = new JPanel();
30+
private JPanel htmlPanel = new JPanel();
31+
32+
private JButton to = new JButton("转换");
33+
34+
private String t1 = " ";
35+
private String javaCode = "";
36+
37+
// 因为代码转换时会有大量的字符串拼接,使用StringBuilder提高拼接效率
38+
private StringBuilder htmlCode = new StringBuilder(Constants.START_PRE);
39+
40+
public ChangeFrame() {
41+
super("Java代码转换为Html代码");
42+
setBounds(100, 50, 1140, 670);
43+
setDefaultCloseOperation(EXIT_ON_CLOSE);
44+
setVisible(true);
45+
setLayout(null);
46+
javaPanel.setLayout(null);
47+
htmlPanel.setLayout(null);
48+
49+
// 代码框自动换�
50+
javaArea.setLineWrap(true);
51+
htmlArea.setLineWrap(true);
52+
// 滚动条一直显示,即使内容没有超过文本�
53+
javaScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
54+
htmlScroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
55+
56+
javaLabel.setBounds(5, 0, 500, 20);
57+
htmlLabel.setBounds(5, 0, 500, 20);
58+
javaScroll.setBounds(5, 20, 500, 600);
59+
htmlScroll.setBounds(5, 20, 500, 600);
60+
javaPanel.setBounds(0, 10, 500, 620);
61+
htmlPanel.setBounds(615, 10, 500, 620);
62+
to.setBounds(520, 310, 80, 20);
63+
64+
javaPanel.add(javaLabel);
65+
javaPanel.add(javaScroll);
66+
htmlPanel.add(htmlLabel);
67+
htmlPanel.add(htmlScroll);
68+
add(javaPanel);
69+
add(htmlPanel);
70+
add(to);
71+
repaint();
72+
73+
to.addActionListener((e) -> {
74+
toHtml();
75+
});
76+
javaArea.addKeyListener(this);
77+
}
78+
79+
private void toHtml() {
80+
String[] lines = javaCode.split("\n");
81+
for (String line : lines) {
82+
String[] ts = line.split("\t");
83+
for (String t : ts) {
84+
if (t.length()==0) {
85+
htmlCode.append(t1);
86+
}else {
87+
// 按空格提取代码词
88+
String[] words = t.split(" ");
89+
for (String word : words) {
90+
if (isCom(word)) {
91+
continue;
92+
}
93+
if (isNote(word)) {
94+
continue;
95+
}
96+
if (isAnno(word)) {
97+
continue;
98+
}
99+
if (isString(word)) {
100+
continue;
101+
}
102+
htmlCode.append(" "+word);
103+
}
104+
}
105+
}
106+
htmlCode.append(Constants.BR);
107+
}
108+
htmlCode.append(Constants.END_PRE);
109+
htmlArea.setText(htmlCode.toString());
110+
}
111+
112+
private boolean isString(String word) {
113+
if (word.startsWith("\"") || word.endsWith("\"")) {
114+
htmlCode.append(Constants.START_NOBR_STRING+word+Constants.END_NOBR);
115+
return true;
116+
}
117+
return false;
118+
}
119+
120+
private boolean isAnno(String word) {
121+
if (word.startsWith("@")) {
122+
htmlCode.append(Constants.START_NOBR_ANNOTATION+word+Constants.END_NOBR);
123+
return true;
124+
}
125+
return false;
126+
}
127+
128+
private boolean isNote(String word) {
129+
boolean a = word.startsWith("//");
130+
boolean b = word.startsWith("/*");
131+
boolean c = word.startsWith("/**");
132+
boolean d = word.startsWith("*");
133+
boolean e = word.startsWith("*/");
134+
if (a) {
135+
htmlCode.append(Constants.START_NOBR_NOTE_GREEN+word+Constants.END_NOBR);
136+
return true;
137+
}else if (b && !c) {
138+
htmlCode.append(Constants.START_NOBR_NOTE_GREEN+word+Constants.END_NOBR);
139+
return true;
140+
}else if (c||d||e) {
141+
htmlCode.append(Constants.START_NOBR_NOTE_BLUE+word+Constants.END_NOBR);
142+
return true;
143+
}
144+
return false;
145+
}
146+
147+
private boolean isCom(String word) {
148+
switch (word) {
149+
case "package":
150+
case "import":
151+
case "public":
152+
case "protected":
153+
case "private":
154+
case "class":
155+
case "interface":
156+
case "enum":
157+
case "@interface":
158+
case "extends":
159+
case "static":
160+
case "final":
161+
case "int":
162+
case "short":
163+
case "long":
164+
case "double":
165+
case "float":
166+
case "void":
167+
case "true":
168+
case "(true":
169+
case "true)":
170+
case "(true)":
171+
case "false":
172+
case "this":
173+
case "super(":
174+
case "null":
175+
case "(null":
176+
case "null)":
177+
case "(null)":
178+
case "if":
179+
case "if{":
180+
case "else":
181+
case "}else":
182+
case "else{":
183+
case "}else{":
184+
case "for":
185+
case "while":
186+
case "do":
187+
case "switch":
188+
case "case":
189+
case "default:":
190+
case "break;":
191+
case "continue;":
192+
case "return;":
193+
case "new":
194+
htmlCode.append(Constants.START_NOBR_KEY_WORD);
195+
htmlCode.append(word + Constants.END_NOBR);
196+
return true;
197+
198+
}
199+
return false;
200+
}
201+
202+
// 监控键盘是否使用粘贴功能,为1时按下ctrl键,�2时,使用粘贴(ctrl+v�
203+
private int ctrl_v = 0;
204+
205+
@Override
206+
public void keyReleased(KeyEvent e) {
207+
int keyCode = e.getKeyCode();
208+
if ((keyCode == KeyEvent.VK_CONTROL || keyCode == KeyEvent.VK_V) && ctrl_v == 2) {
209+
ctrl_v = 0;
210+
new Thread(()->{
211+
javaCode = javaArea.getText();
212+
pick(javaCode);
213+
}).start();;
214+
}
215+
}
216+
217+
@Override
218+
public void keyPressed(KeyEvent e) {
219+
if(e.getKeyCode() == KeyEvent.VK_CONTROL){
220+
ctrl_v = 1;
221+
}
222+
if(e.getKeyCode() == KeyEvent.VK_V && ctrl_v == 1){
223+
ctrl_v = 2;
224+
}
225+
}
226+
227+
@Override
228+
public void keyTyped(KeyEvent e) {
229+
}
230+
231+
/**
232+
* 整理粘贴的内容,以�配文本�
233+
* 因为原始的文本框内,制表符的宽度太宽,代码排版相当丑�
234+
* �以需要对粘贴的内容进行制表符的处�
235+
* @param javaCode
236+
*/
237+
private void pick(String javaCode) {
238+
StringBuilder pick = new StringBuilder();
239+
String[] lines = javaCode.split("\n");
240+
for (String line : lines) {
241+
String[] ts = line.split("\t");
242+
for (String t : ts) {
243+
if (t.length()==0) { // 当前字段为制表符"\t"
244+
// 将制表符转化�8个小空格,�过测试�8个空格的排版较合�
245+
pick.append(t1+t1);
246+
}else {
247+
pick.append(t);
248+
}
249+
}
250+
pick.append("\n");
251+
}
252+
javaArea.setText(pick.toString());
253+
}
254+
255+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package totoro.top.java2html;
2+
3+
public class Constants {
4+
// 带特定CSS样式的HTML标签,tips:<nobr></nobr>为强制不换行标签
5+
6+
/* Java代码块 */
7+
public static final String START_PRE = "<pre>";
8+
public static final String END_PRE = "</pre>";
9+
10+
/* Java关键字紫色加粗 */
11+
public static final String START_NOBR_KEY_WORD= " <nobr class=\"key\">";
12+
/* Java绿色字体注释 */
13+
public static final String START_NOBR_NOTE_GREEN= " <nobr class=\"note-green\">";
14+
/* Java蓝色字体注释 */
15+
public static final String START_NOBR_NOTE_BLUE= " <nobr class=\"note-blue\">";
16+
/* Java灰色注解 */
17+
public static final String START_NOBR_ANNOTATION= " <nobr class=\"anno\">";
18+
/* Java蓝色字符串 */
19+
public static final String START_NOBR_STRING= " <nobr class=\"string\">";
20+
21+
public static final String END_NOBR = "</nobr>";
22+
public static final String BR = "<br>";
23+
24+
}

0 commit comments

Comments
 (0)