44import com .zuicoding .platform .plugins .jsonformater .JsonUtils ;
55import org .jetbrains .annotations .NotNull ;
66import 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 ;
711
812import javax .swing .*;
13+ import javax .swing .text .html .HTMLEditorKit ;
914import java .awt .event .ActionEvent ;
15+ import java .awt .event .ActionListener ;
1016
1117/**
1218 * Created by <a href="mailto:stephen.linicoding@gmail.com">Stephen.lin</a> on 2017/12/7
1521 */
1622public class JSONViewer extends DialogWrapper {
1723 private JPanel container ;
18- private JTextArea jsonText ;
24+ private JTextField queryField ;
25+ private JEditorPane jsonArea ;
26+ private JButton queryBtn ;
27+
1928 public JSONViewer () {
2029 super (false );
2130 init ();
2231 setTitle ("JSON 格式化" );
2332 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 );
2452
53+ element .html (text );
54+
55+ setJsonArea (element .toString ());
56+ }
57+ });
2558 }
2659
2760 public JPanel getContainer () {
2861 return container ;
2962 }
3063
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+ }
3180 @ NotNull
3281 @ Override
3382 protected Action [] createActions () {
@@ -36,10 +85,30 @@ protected Action[] createActions() {
3685
3786 @ Override
3887 protected void doAction (ActionEvent actionEvent ) {
39- String text = jsonText .getText ();
88+ String text = jsonArea .getText ();
4089 if (text == null || text .trim ().isEmpty ()) return ;
4190 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+
43112 }
44113 };
45114
0 commit comments