4
4
import com .zuicoding .platform .plugins .jsonformater .JsonUtils ;
5
5
import org .jetbrains .annotations .NotNull ;
6
6
import org .jetbrains .annotations .Nullable ;
7
+ import org .jsoup .Jsoup ;
8
+ import org .jsoup .nodes .Document ;
9
+ import org .jsoup .nodes .Element ;
10
+ import org .jsoup .select .Elements ;
7
11
8
12
import javax .swing .*;
13
+ import javax .swing .text .html .HTMLEditorKit ;
9
14
import java .awt .event .ActionEvent ;
15
+ import java .awt .event .ActionListener ;
10
16
11
17
/**
12
18
* Created by <a href="mailto:[email protected] ">Stephen.lin</a> on 2017/12/7
15
21
*/
16
22
public class JSONViewer extends DialogWrapper {
17
23
private JPanel container ;
18
- private JTextArea jsonText ;
24
+ private JTextField queryField ;
25
+ private JEditorPane jsonArea ;
26
+ private JButton queryBtn ;
27
+
19
28
public JSONViewer () {
20
29
super (false );
21
30
init ();
22
31
setTitle ("JSON 格式化" );
23
32
setOKButtonText ("格式化" );
33
+ HTMLEditorKit kit = new HTMLEditorKit ();
34
+ jsonArea .setEditorKit (kit );
35
+ queryBtn .addActionListener (new ActionListener () {
36
+ @ Override
37
+ public void actionPerformed (ActionEvent e ) {
38
+ String text = jsonArea .getText ();
39
+ if (JsonUtils .isBank (text )) return ;
40
+ Document doc = Jsoup .parse (text );
41
+ Element element = doc .body ().selectFirst ("p" );
42
+ //已经格式化好了
43
+ if (element == null ){
44
+ element = doc .body ();
45
+ element .html ("<p>" + element .html () + "</p>" );
46
+ element = element .selectFirst ("p" );
47
+ }
48
+
49
+ text = element .html ();
50
+
51
+ text = wrapStyle (text );
24
52
53
+ element .html (text );
54
+
55
+ setJsonArea (element .toString ());
56
+ }
57
+ });
25
58
}
26
59
27
60
public JPanel getContainer () {
28
61
return container ;
29
62
}
30
63
64
+
65
+
66
+ private String wrapStyle (String html ){
67
+ if (html == null || html .trim ().isEmpty ())return html ;
68
+ String value = queryField .getText ();
69
+ if (value == null || value .trim ().isEmpty ()) return html ;
70
+ Document doc = Jsoup .parse (html );
71
+ doc .select ("font[name=\" keyWrapper\" ]" ).unwrap ();
72
+ html = doc .body ().html ().replaceAll (value ,"<font name=\" keyWrapper\" style=\" background-color:yellow;\" >" + value + "</font>" );
73
+
74
+ return html ;
75
+ }
76
+
77
+ private void setJsonArea (String html ){
78
+ jsonArea .setText (html );
79
+ }
31
80
@ NotNull
32
81
@ Override
33
82
protected Action [] createActions () {
@@ -36,10 +85,30 @@ protected Action[] createActions() {
36
85
37
86
@ Override
38
87
protected void doAction (ActionEvent actionEvent ) {
39
- String text = jsonText .getText ();
88
+ String text = jsonArea .getText ();
40
89
if (text == null || text .trim ().isEmpty ()) return ;
41
90
System .err .println (text );
42
- jsonText .setText (JsonUtils .formatJson (text ));
91
+
92
+ try {
93
+ Document doc = Jsoup .parse (text );
94
+
95
+ Element element = doc .body ().selectFirst ("p" );
96
+ if (element == null ) {
97
+ element = doc .body ();
98
+ element .html ("<p>" + element .html () + "</p>" );
99
+ element = element .selectFirst ("p" );
100
+ }
101
+ text = element .html ();
102
+ System .err .println (text );
103
+ text = text .replaceAll ("( |<br/>|\0 |<br>)" ,"" );
104
+ text = JsonUtils .formatJson (text );
105
+ text = wrapStyle (text );
106
+ element .html (text );
107
+ setJsonArea (element .toString ());
108
+ }catch (Exception e ){
109
+ e .printStackTrace ();
110
+ }
111
+
43
112
}
44
113
};
45
114
0 commit comments