-
Notifications
You must be signed in to change notification settings - Fork 92
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
*/ | ||
|
@@ -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. | ||
*/ | ||
|
@@ -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(); | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
|
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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👍