Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a different XML formatter #294

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@
<exclude>LICENSE</exclude>
<exclude>NOTICE</exclude>
<exclude>.gitattributes</exclude>
<exclude>**/xml/eclipse/*.java</exclude>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than suppress the header applied to this project, please contribute your code under the project's Apache 2.0 license, and add any copyright notices to the NOTICE file, if you wish to do so.

If you wish to also make your code available under additional licenses, you're obviously free to do so, but please conform to the project's documentation standards when contributing here. A class-level comment (below the license header), for example, could serve as a notice to users that the code is also available under the EPL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class level comment sounds good to me, I could add the attribution to original authors there and allow mycila to write the Apache header 👍

</excludes>
<mapping>
<java>SLASHSTAR_STYLE</java>
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/net/revelc/code/formatter/FormatterMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,17 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
private String configHtmlFile;

/**
* File or classpath location of a properties file to use in xml formatting.
* File or classpath location of a properties file to use in jsoup xml formatting.
*/
@Parameter(defaultValue = "formatter-maven-plugin/jsoup/xml.properties", property = "configxmlfile", required = true)
private String configXmlFile;

/**
* File or classpath location of a properties file to use in eclipse xml formatting.
*/
@Parameter(defaultValue = "formatter-maven-plugin/eclipse/xml.properties", property = "configexmlfile", required = true)
private String configEXmlFile;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the Eclipse XML formatter makes a lot more sense to me, since this project's main purpose is to automate the application of Eclipse formatting rules. I don't know much about jsoup, but I've yet to find its formatting useful.

Rather than create a new formatter option like this, I can think of two possible alternatives:

  1. This replaces the jsoup implementation
  2. We do something like what I proposed in Create separate mojos/goals for separate formatters #254 to create separate mojos for each formatter, rather than overload the one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think Eclipse's formatting is much more useful thatn jsoup's, I only left it as default in order to prvent breaking existing users. So if you're ok with a replacement I'm 100% good with that.

Copy link
Member

@hazendaz hazendaz Mar 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree Jsoup solution was never actually useful. What was really wanted was what we could do in Eclipse directly and that was a 'beta' type attempt to get something that eventually might evolve so if we have a better solution, jsoup certain can go. I would highly doubt anyone is actually using the jsoup solution provided here as it is almost entirely not readable at a human level how we would expect things. So I don't think it even warrants deprecation but straight replacement.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. If we resolve the other issues raised I'll change the PR to replace jsoup entirely.


/**
* File or classpath location of a properties file to use in json formatting.
*/
Expand Down Expand Up @@ -244,6 +250,12 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
@Parameter(defaultValue = "false", property = "formatter.xml.skip")
private Boolean skipXmlFormatting;

/**
* Whether to use the jsoup based formatter.
*/
@Parameter(defaultValue = "true", property = "formatter.xml.jsoup")
private Boolean useJsoupXmlFormatter;

/**
* Whether the json formatting is skipped.
*/
Expand Down Expand Up @@ -276,7 +288,7 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {

private HTMLFormatter htmlFormatter = new HTMLFormatter();

private XMLFormatter xmlFormatter = new XMLFormatter();
private XMLFormatter xmlFormatter;

private JsonFormatter jsonFormatter = new JsonFormatter();

Expand All @@ -291,6 +303,8 @@ public class FormatterMojo extends AbstractMojo implements ConfigurationSource {
*/
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
xmlFormatter = new XMLFormatter(useJsoupXmlFormatter);

if (this.skipFormatting) {
getLog().info("Formatting is skipped");
return;
Expand Down Expand Up @@ -369,9 +383,18 @@ public void execute() throws MojoExecutionException, MojoFailureException {
List<File> addCollectionFiles(File newBasedir) {
final DirectoryScanner ds = new DirectoryScanner();
ds.setBasedir(newBasedir);

Log log = getLog();

if (this.includes != null && this.includes.length > 0) {
for (String include : includes) {
log.debug("Including files with pattern " + include + " in directory " + newBasedir);
}
ds.setIncludes(this.includes);
} else {
for (String include : DEFAULT_INCLUDES) {
log.debug("Including files with pattern " + include + " in directory " + newBasedir);
}
ds.setIncludes(DEFAULT_INCLUDES);
}

Expand All @@ -383,6 +406,7 @@ List<File> addCollectionFiles(File newBasedir) {

List<File> foundFiles = new ArrayList<>();
for (String filename : ds.getIncludedFiles()) {
log.debug("Adding file " + filename);
foundFiles.add(new File(newBasedir, filename));
}
return foundFiles;
Expand Down Expand Up @@ -646,8 +670,10 @@ private void createCodeFormatter() throws MojoExecutionException {
if (configHtmlFile != null) {
this.htmlFormatter.init(getOptionsFromPropertiesFile(configHtmlFile), this);
}
if (configXmlFile != null) {
if (useJsoupXmlFormatter && configXmlFile != null) {
this.xmlFormatter.init(getOptionsFromPropertiesFile(configXmlFile), this);
} else if (configEXmlFile != null) {
this.xmlFormatter.init(getOptionsFromPropertiesFile(configEXmlFile), this);
}
if (configJsonFile != null) {
Map<String, String> jsonFormattingOptions = getOptionsFromPropertiesFile(configJsonFile);
Expand Down
83 changes: 77 additions & 6 deletions src/main/java/net/revelc/code/formatter/xml/XMLFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,90 @@
*/
package net.revelc.code.formatter.xml;

import net.revelc.code.formatter.Formatter;
import net.revelc.code.formatter.LineEnding;
import java.io.File;
import java.util.Map;

import net.revelc.code.formatter.*;
import net.revelc.code.formatter.jsoup.JsoupBasedFormatter;
import net.revelc.code.formatter.xml.eclipse.FormattingPreferences;
import net.revelc.code.formatter.xml.eclipse.XmlDocumentFormatter;

/**
* @author yoshiman
*
* @author Jose Montoya
*/
public class XMLFormatter extends JsoupBasedFormatter implements Formatter {
public class XMLFormatter implements Formatter {
private final Formatter delegateFormatter;

public XMLFormatter(Boolean useJsoupFormatting) {
if (useJsoupFormatting)
this.delegateFormatter = new JsoupDelegate();
else
this.delegateFormatter = new EclipseDelegate();

}

@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
delegateFormatter.init(options, cfg);
}

@Override
public Result formatFile(File file, LineEnding ending, boolean dryRun) {
return delegateFormatter.formatFile(file, ending, dryRun);
}

@Override
public String doFormat(String code, LineEnding ending) {
return super.doFormat(code, ending);
public boolean isInitialized() {
return delegateFormatter.isInitialized();
}

// Delegate inner classes
protected class JsoupDelegate extends JsoupBasedFormatter {
@Override
public String doFormat(String code, LineEnding ending) {
return super.doFormat(code, ending);
}
}

protected class EclipseDelegate extends AbstractCacheableFormatter implements Formatter {
XmlDocumentFormatter formatter;

@Override
public void init(Map<String, String> options, ConfigurationSource cfg) {
super.initCfg(cfg);

FormattingPreferences prefs = new FormattingPreferences();
String maxLineLength = options.get("maxLineLength");
String wrapLongLines = options.get("wrapLongLines");
String tabInsteadOfSpaces = options.get("tabInsteadOfSpaces");
String tabWidth = options.get("tabWidth");
String splitMultiAttrs = options.get("splitMultiAttrs");

prefs.setMaxLineLength(maxLineLength != null ? Integer.valueOf(maxLineLength) : null);
prefs.setTabWidth(tabWidth != null ? Integer.valueOf(tabWidth) : null);
prefs.setWrapLongLines(wrapLongLines != null ? Boolean.valueOf(wrapLongLines) : null);
prefs.setTabInsteadOfSpaces(tabInsteadOfSpaces != null ? Boolean.valueOf(tabInsteadOfSpaces) : null);
prefs.setSetSplitMultiAttrs(splitMultiAttrs != null ? Boolean.valueOf(splitMultiAttrs) : null);

this.formatter = new XmlDocumentFormatter(options.getOrDefault("lineending", System.lineSeparator()),
prefs);
}

@Override
protected String doFormat(String code, LineEnding ending) {
String formattedCode = formatter.format(code);

if (code.equals(formattedCode)) {
return null;
}

return formattedCode;
}

@Override
public boolean isInitialized() {
return formatter != null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************************************************************
* Copyright (c) 2004, 2011 John-Mason P. Shackelford and others.,
* 2009 Jose Montoya
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
* which is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*
* Contributors:
* John-Mason P. Shackelford - initial API and implementation
* IBM Corporation - bug fixes
* Jose Montoya - Modified implementation outside Eclipse Platform
*******************************************************************************/
package net.revelc.code.formatter.xml.eclipse;

/**
* Based on Eclipse Ant Formatter and distributed under original Eclipse 2.0 license and compatible Apache 2.0 See:
* https://github.com/eclipse/eclipse.platform/tree/master/ant/org.eclipse.ant.ui/Ant%20Editor/org/eclipse/ant/internal/ui/editor/formatter
*/
public class FormattingPreferences {
private int maxLineLength = 115;
private boolean wrapLongLines = true;
private boolean tabInsteadOfSpaces = true;
private int tabWidth = 4;
private boolean setSplitMultiAttrs = false;

public void setMaxLineLength(Integer maxLineLength) {
if (maxLineLength != null)
this.maxLineLength = maxLineLength;
}

public void setWrapLongLines(Boolean wrapLongLines) {
if (wrapLongLines != null)
this.wrapLongLines = wrapLongLines;
}

public void setTabInsteadOfSpaces(Boolean tabInsteadOfSpaces) {
if (tabInsteadOfSpaces != null)
this.tabInsteadOfSpaces = tabInsteadOfSpaces;
}

public String getCanonicalIndent() {
String canonicalIndent;
if (useTabInsteadOfSpaces()) {
canonicalIndent = "\t"; //$NON-NLS-1$
} else {
String tab = "";
for (int i = 0; i < getTabWidth(); i++) {
tab = tab.concat(" "); //$NON-NLS-1$
}
canonicalIndent = tab;
}

return canonicalIndent;
}

public int getMaximumLineWidth() {
return maxLineLength;
}

public boolean wrapLongTags() {
return wrapLongLines;
}

public int getTabWidth() {
return tabWidth;
}

public void setTabWidth(Integer tabWidth) {
if (tabWidth != null)
this.tabWidth = tabWidth;
}

public boolean useTabInsteadOfSpaces() {
return tabInsteadOfSpaces;
}

public boolean isSetSplitMultiAttrs() {
return setSplitMultiAttrs;
}

public void setSetSplitMultiAttrs(Boolean setSplitMultiAttrs) {
if (setSplitMultiAttrs != null)
this.setSplitMultiAttrs = setSplitMultiAttrs;
}
}
Loading