View Javadoc
1   /*
2    * Copyright (c) 2020 bahlef.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v2.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v20.html
7    * Contributors:
8    * bahlef - initial API and implementation and/or initial documentation
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   * Jsoup formatter implementation of the {@link AbstractFormatJob} to
33   * format a given document using the {@link JsoupHtmlFormatterWrapper}.
34   *
35   * @author bahlef
36   */
37  class JsoupHtmlFormatJob extends AbstractFormatJob {
38  	/** * The {@link JsoupHtmlFormatterWrapper} implementation. */
39  	private final JsoupHtmlFormatterWrapper formatter;
40  
41  	/**
42  	 * Package private constructor to create a new instance of {@link JsoupFormatJob}.
43  	 *
44  	 * @param document the {@link StyledDocument} which sould be formatted
45  	 * @param formatter the {@link JsoupHtmlFormatterWrapper} to use
46  	 */
47  	JsoupHtmlFormatJob(StyledDocument document, JsoupHtmlFormatterWrapper formatter) {
48  		super(document, null);
49  
50  		this.formatter = formatter;
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
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  		//save with configured linefeed
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  }