Skip to content

Commit

Permalink
Reformat code with spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Jun 20, 2023
1 parent 9773f11 commit 5b98282
Show file tree
Hide file tree
Showing 18 changed files with 1,149 additions and 1,723 deletions.
257 changes: 102 additions & 155 deletions src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java

Large diffs are not rendered by default.

170 changes: 65 additions & 105 deletions src/main/java/org/codehaus/mojo/xml/CheckFormatMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
* under the License.
*/

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
Expand All @@ -29,9 +32,6 @@
import java.util.List;
import java.util.Map;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.maven.model.FileSet;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
Expand All @@ -50,63 +50,51 @@
*
* @author <a href="https://github.com/ppalaga">Peter Palaga</a>
*/
@Mojo( defaultPhase = LifecyclePhase.VALIDATE, name = "check-format", threadSafe = true )
public class CheckFormatMojo
extends AbstractXmlMojo
{

private class ViolationCollector
implements XmlFormatViolationHandler
{
@Mojo(defaultPhase = LifecyclePhase.VALIDATE, name = "check-format", threadSafe = true)
public class CheckFormatMojo extends AbstractXmlMojo {

private class ViolationCollector implements XmlFormatViolationHandler {
private final Map<String, List<XmlFormatViolation>> violations =
new LinkedHashMap<String, List<XmlFormatViolation>>();
new LinkedHashMap<String, List<XmlFormatViolation>>();

@Override
public void handle( XmlFormatViolation violation )
{
List<XmlFormatViolation> list = violations.get( violation.getFile().getAbsolutePath() );
if ( list == null )
{
public void handle(XmlFormatViolation violation) {
List<XmlFormatViolation> list = violations.get(violation.getFile().getAbsolutePath());
if (list == null) {
list = new ArrayList<XmlFormatViolation>();
violations.put( violation.getFile().getAbsolutePath(), list );
violations.put(violation.getFile().getAbsolutePath(), list);
}
list.add( violation );
if ( failOnFormatViolation )
{
getLog().error( violation.toString() );
}
else
{
getLog().warn( violation.toString() );
list.add(violation);
if (failOnFormatViolation) {
getLog().error(violation.toString());
} else {
getLog().warn(violation.toString());
}
}

public boolean hasViolations()
{
public boolean hasViolations() {
return !violations.isEmpty();
}

public boolean hasViolations( File file )
{
List<XmlFormatViolation> list = violations.get( file.getAbsolutePath() );
public boolean hasViolations(File file) {
List<XmlFormatViolation> list = violations.get(file.getAbsolutePath());
return list != null && !list.isEmpty();
}

}

/**
* The encoding of files included in {@link #formatFileSets}. Note that the
* {@code encoding can be set also per FormatFileSet}.
*/
@Parameter( property = "xml.encoding", defaultValue = "${project.build.sourceEncoding}" )
@Parameter(property = "xml.encoding", defaultValue = "${project.build.sourceEncoding}")
private String encoding;

/**
* Tells the mojo what to do in case XML formatting violations are found. if {@code true}, all violations will be
* reported on the console as ERRORs and the build will fail. if {@code false}, all violations will be reported on
* the console as WARNs and the build will proceed further.
*/
@Parameter( property = "xml.failOnFormatViolation", defaultValue = "true" )
@Parameter(property = "xml.failOnFormatViolation", defaultValue = "true")
private boolean failOnFormatViolation;

/**
Expand All @@ -119,7 +107,7 @@ public boolean hasViolations( File file )
* The number of spaces expected for indentation. Note that {@code indentSize} can be configuread also per
* {@link FormatFileSet}.
*/
@Parameter( property = "xml.indentSize", defaultValue = "2" )
@Parameter(property = "xml.indentSize", defaultValue = "2")
private int indentSize;

/** A {@link SAXParserFactory} */
Expand All @@ -129,17 +117,16 @@ public boolean hasViolations( File file )
* If set to {@code true}, the result of {@link FormatFileSet#getDefault(String, int)} will be appended to
* {@link #formatFileSets} before the processing.
*/
@Parameter( property = "xml.useDefaultFormatFileSet", defaultValue = "true" )
@Parameter(property = "xml.useDefaultFormatFileSet", defaultValue = "true")
private boolean useDefaultFormatFileSet;

/**
* Creates a new {@link CheckFormatMojo} instance.
*/
public CheckFormatMojo()
{
public CheckFormatMojo() {
super();
this.saxParserFactory = SAXParserFactory.newInstance();
this.saxParserFactory.setValidating( false );
this.saxParserFactory.setValidating(false);
}

/**
Expand All @@ -151,33 +138,23 @@ public CheckFormatMojo()
* @param violationHandler the {@link XmlFormatViolationHandler} to report violations
* @throws MojoExecutionException if there is any lover level exception reading or parsing the file.
*/
private void check( File file, String encoding, XmlFormatViolationHandler violationHandler )
throws MojoExecutionException
{
private void check(File file, String encoding, XmlFormatViolationHandler violationHandler)
throws MojoExecutionException {

Reader in = null;
try
{
in = new InputStreamReader( new FileInputStream( file ), encoding );
try {
in = new InputStreamReader(new FileInputStream(file), encoding);
SAXParser saxParser = saxParserFactory.newSAXParser();
IndentCheckSaxHandler handler = new IndentCheckSaxHandler( file, indentSize, violationHandler );
saxParser.parse( new InputSource( in ), handler );
}
catch ( Exception e )
{
throw new MojoExecutionException( "Could not process file " + file.getAbsolutePath(), e );
}
finally
{
if ( in != null )
{
try
{
IndentCheckSaxHandler handler = new IndentCheckSaxHandler(file, indentSize, violationHandler);
saxParser.parse(new InputSource(in), handler);
} catch (Exception e) {
throw new MojoExecutionException("Could not process file " + file.getAbsolutePath(), e);
} finally {
if (in != null) {
try {
in.close();
}
catch ( IOException e )
{
getLog().error( "Could not close Reader for " + file.getAbsolutePath(), e );
} catch (IOException e) {
getLog().error("Could not close Reader for " + file.getAbsolutePath(), e);
}
}
}
Expand All @@ -189,21 +166,16 @@ private void check( File file, String encoding, XmlFormatViolationHandler violat
* @throws MojoExecutionException Running the Mojo failed.
* @throws MojoFailureException A configuration error was detected.
*/
public void execute()
throws MojoExecutionException, MojoFailureException
{
if ( isSkipping() )
{
getLog().debug( "Skipping execution, as demanded by user." );
public void execute() throws MojoExecutionException, MojoFailureException {
if (isSkipping()) {
getLog().debug("Skipping execution, as demanded by user.");
return;
}

if ( useDefaultFormatFileSet )
{
formatFileSets.add( FormatFileSet.getDefault( getBasedir(), encoding, indentSize ) );
if (useDefaultFormatFileSet) {
formatFileSets.add(FormatFileSet.getDefault(getBasedir(), encoding, indentSize));
}
if ( formatFileSets == null || formatFileSets.isEmpty() )
{
if (formatFileSets == null || formatFileSets.isEmpty()) {
/* nothing to do */
return;
}
Expand All @@ -212,32 +184,26 @@ public void execute()

int processedFileCount = 0;

for ( FormatFileSet formatFileSet : formatFileSets )
{
for (FormatFileSet formatFileSet : formatFileSets) {
String effectiveEncoding = formatFileSet.getEncoding();
if ( effectiveEncoding == null )
{
if (effectiveEncoding == null) {
effectiveEncoding = this.encoding;
}
String[] includedFiles = scan( formatFileSet );
for ( String includedPath : includedFiles )
{
String[] includedFiles = scan(formatFileSet);
for (String includedPath : includedFiles) {
processedFileCount++;
File file = new File( formatFileSet.getDirectory(), includedPath );
check( file, effectiveEncoding, violationCollector );
if ( getLog().isDebugEnabled() && !violationCollector.hasViolations( file ) )
{
getLog().debug( "No XML formatting violations found in file " + file.getAbsolutePath() );
File file = new File(formatFileSet.getDirectory(), includedPath);
check(file, effectiveEncoding, violationCollector);
if (getLog().isDebugEnabled() && !violationCollector.hasViolations(file)) {
getLog().debug("No XML formatting violations found in file " + file.getAbsolutePath());
}
}
}
getLog().debug( "Checked the formatting of " + processedFileCount + " files" );
getLog().debug("Checked the formatting of " + processedFileCount + " files");

if ( failOnFormatViolation && violationCollector.hasViolations() )
{
throw new MojoFailureException( "There are XML formatting violations. Check the above log for details." );
if (failOnFormatViolation && violationCollector.hasViolations()) {
throw new MojoFailureException("There are XML formatting violations. Check the above log for details.");
}

}

/**
Expand All @@ -246,11 +212,9 @@ public void execute()
* @param fileSet {@link FileSet} to scan
* @return the included paths
*/
private String[] scan( FileSet fileSet )
{
File basedir = new File( fileSet.getDirectory() );
if ( !basedir.exists() || !basedir.isDirectory() )
{
private String[] scan(FileSet fileSet) {
File basedir = new File(fileSet.getDirectory());
if (!basedir.exists() || !basedir.isDirectory()) {
return null;
}

Expand All @@ -259,17 +223,15 @@ private String[] scan( FileSet fileSet )
List<String> includes = fileSet.getIncludes();
List<String> excludes = fileSet.getExcludes();

if ( includes != null && includes.size() > 0 )
{
scanner.setIncludes( includes.toArray( new String[0] ) );
if (includes != null && includes.size() > 0) {
scanner.setIncludes(includes.toArray(new String[0]));
}

if ( excludes != null && excludes.size() > 0 )
{
scanner.setExcludes( excludes.toArray( new String[0] ) );
if (excludes != null && excludes.size() > 0) {
scanner.setExcludes(excludes.toArray(new String[0]));
}

scanner.setBasedir( basedir );
scanner.setBasedir(basedir);

scanner.scan();
return scanner.getIncludedFiles();
Expand All @@ -280,9 +242,7 @@ private String[] scan( FileSet fileSet )
*
* @param indentSize the number of spaces
*/
public void setIndentSize( int indentSize )
{
public void setIndentSize(int indentSize) {
this.indentSize = indentSize;
}

}
Loading

0 comments on commit 5b98282

Please sign in to comment.