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.xml.jsoup;
11  
12  import java.util.Objects;
13  
14  import org.apache.commons.lang3.StringUtils;
15  import org.jsoup.Jsoup;
16  import org.jsoup.nodes.Document;
17  import org.jsoup.nodes.Entities;
18  import org.jsoup.parser.Parser;
19  import org.netbeans.api.annotations.common.CheckForNull;
20  import org.netbeans.api.annotations.common.NonNull;
21  
22  /**
23   * Wrapper class to the Jsoup formatter implementation.
24   *
25   * @author bahlef
26   */
27  public final class JsoupXmlFormatterWrapper {
28  	/** Default system line separator. */
29  	private static final String DEFAULT_LINE_SEPARATOR = "\n";
30  
31  	/**
32  	 * Package private Constructor for creating a new instance of {@link JsoupXmlFormatterWrapper}.
33  	 */
34  	JsoupXmlFormatterWrapper() {
35  	}
36  
37  	/**
38  	 * Formats the given {@code code} with the given configurations and returns
39  	 * the formatted code.
40  	 *
41  	 * @param code the unformatted code
42  	 * @param lineFeed the line feed to use for formatting
43  	 * @param options the {@link Document.OutputSettings}
44  	 *
45  	 * @return the formatted code
46  	 */
47  	@CheckForNull
48  	public String format(String code, String lineFeed, Document.OutputSettings options) {
49  		if (code == null) {
50  			return null;
51  		}
52  
53  		if (lineFeed == null) {
54  			lineFeed = System.getProperty("line.separator");
55  		}
56  
57  		if (options == null) {
58  			options = new Document.OutputSettings();
59  		}
60  
61  		options.escapeMode(Entities.EscapeMode.xhtml);
62  		options.syntax(Document.OutputSettings.Syntax.xml);
63  
64  		return format(options, code, lineFeed);
65  	}
66  
67  	/**
68  	 * Formats the given {@code code} with the given configurations and returns
69  	 * the formatted code.
70  	 *
71  	 * @param options the {@link Document.OutputSettings}
72  	 * @param code the unformatted code
73  	 * @param lineFeed the line feed to use for formatting
74  	 *
75  	 * @return the formatted code
76  	 */
77  	@CheckForNull
78  	private String format(Document.OutputSettings options, @NonNull String code, String lineFeed) {
79  		Document document = Jsoup.parse(code, "", Parser.xmlParser());
80  		document.outputSettings(options);
81  
82  		String formattedCode = document.outerHtml();
83  		if (Objects.equals(code, formattedCode)) {
84  			return null;
85  		} else if (!DEFAULT_LINE_SEPARATOR.equals(lineFeed)) {
86  			formattedCode = StringUtils.replace(formattedCode, DEFAULT_LINE_SEPARATOR, lineFeed);
87  		}
88  
89  		return formattedCode;
90  	}
91  }