1
2
3
4
5
6
7
8
9
10 package de.funfried.netbeans.plugins.external.formatter.html.jsoup;
11
12 import java.nio.charset.Charset;
13 import java.util.prefs.Preferences;
14
15 import javax.swing.SwingUtilities;
16 import javax.swing.text.BadLocationException;
17 import javax.swing.text.StyledDocument;
18
19 import org.jsoup.nodes.Document;
20 import org.netbeans.api.queries.FileEncodingQuery;
21 import org.netbeans.editor.BaseDocument;
22 import org.netbeans.modules.editor.NbEditorUtilities;
23 import org.openide.awt.NotificationDisplayer;
24 import org.openide.awt.StatusDisplayer;
25 import org.openide.filesystems.FileObject;
26
27 import de.funfried.netbeans.plugins.external.formatter.AbstractFormatJob;
28 import de.funfried.netbeans.plugins.external.formatter.ui.Icons;
29 import de.funfried.netbeans.plugins.external.formatter.ui.options.Settings;
30
31
32
33
34
35
36
37 class JsoupHtmlFormatJob extends AbstractFormatJob {
38
39 private final JsoupHtmlFormatterWrapper formatter;
40
41
42
43
44
45
46
47 JsoupHtmlFormatJob(StyledDocument document, JsoupHtmlFormatterWrapper formatter) {
48 super(document, null);
49
50 this.formatter = formatter;
51 }
52
53
54
55
56 @Override
57 public void format() throws BadLocationException {
58 Preferences pref = Settings.getActivePreferences(document);
59
60 boolean prettyPrint = pref.getBoolean(JsoupHtmlFormatterSettings.PRETTY_PRINT, true);
61 boolean outline = pref.getBoolean(JsoupHtmlFormatterSettings.OUTLINE, false);
62 int indentSize = pref.getInt(JsoupHtmlFormatterSettings.INDENT_SIZE, 1);
63 String lineFeedSetting = pref.get(JsoupHtmlFormatterSettings.LINEFEED, "");
64
65 Document.OutputSettings options = new Document.OutputSettings();
66 options.indentAmount(indentSize);
67 options.outline(outline);
68 options.prettyPrint(prettyPrint);
69
70 FileObject fileObj = NbEditorUtilities.getFileObject(document);
71 if (fileObj != null) {
72 Charset charset = FileEncodingQuery.getEncoding(fileObj);
73 if (charset != null) {
74 options.charset(charset);
75 }
76 }
77
78
79 String lineFeed = Settings.getLineFeed(lineFeedSetting, System.getProperty("line.separator"));
80 if (null != lineFeed) {
81 document.putProperty(BaseDocument.READ_LINE_SEPARATOR_PROP, lineFeed);
82 document.putProperty(BaseDocument.WRITE_LINE_SEPARATOR_PROP, lineFeed);
83 }
84
85 String code = getCode();
86
87 String formattedContent = formatter.format(code, lineFeed, options);
88 if (setFormattedCode(code, formattedContent)) {
89 SwingUtilities.invokeLater(() -> {
90 if (pref.getBoolean(Settings.SHOW_NOTIFICATIONS, false)) {
91 NotificationDisplayer.getDefault().notify("Format using Jsoup HTML formatter", Icons.ICON_JSOUP, "", null);
92 }
93
94 StatusDisplayer.getDefault().setStatusText("Format using Jsoup HTML formatter");
95 });
96 }
97 }
98 }