Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Nov 6, 2023
1 parent b67ea79 commit 2bf0475
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 143 deletions.
1 change: 0 additions & 1 deletion src/it/it8/invoker.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
invoker.goals = clean xml:transform
invoker.java.version = 1.6+
1 change: 0 additions & 1 deletion src/it/mojo-1438-validate/invoker.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
invoker.goals = clean test
invoker.java.version = 1.6+
12 changes: 6 additions & 6 deletions src/main/java/org/codehaus/mojo/xml/AbstractXmlMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ protected void setCatalogs(List<File> pCatalogFiles, List<URL> pCatalogUrls) thr
return;
}

for (int i = 0; i < catalogs.length; i++) {
for (String catalog : catalogs) {
try {
URL url = new URL(catalogs[i]);
URL url = new URL(catalog);
pCatalogUrls.add(url);
} catch (MalformedURLException e) {
File absoluteCatalog = asAbsoluteFile(new File(catalogs[i]));
File absoluteCatalog = asAbsoluteFile(new File(catalog));
if (!absoluteCatalog.exists() || !absoluteCatalog.isFile()) {
throw new MojoExecutionException("That catalog does not exist:" + absoluteCatalog.getPath(), e);
}
Expand All @@ -164,8 +164,8 @@ protected void setCatalogs(List<File> pCatalogFiles, List<URL> pCatalogUrls) thr
* Creates a new resolver.
*/
protected Resolver getResolver() throws MojoExecutionException {
List<File> catalogFiles = new ArrayList<File>();
List<URL> catalogUrls = new ArrayList<URL>();
List<File> catalogFiles = new ArrayList<>();
List<URL> catalogUrls = new ArrayList<>();
setCatalogs(catalogFiles, catalogUrls);

return new Resolver(
Expand Down Expand Up @@ -269,7 +269,7 @@ protected Object activateProxy() {
return null;
}

final List<String> properties = new ArrayList<String>();
final List<String> properties = new ArrayList<>();
final String protocol = proxy.getProtocol();
final String prefix = isEmpty(protocol) ? "" : (protocol + ".");

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/org/codehaus/mojo/xml/CheckFormatMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
import javax.xml.parsers.SAXParserFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -54,16 +54,13 @@
public class CheckFormatMojo extends AbstractXmlMojo {

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

@Override
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);
}

List<XmlFormatViolation> list =
violations.computeIfAbsent(violation.getFile().getAbsolutePath(), k -> new ArrayList<>());
list.add(violation);
if (failOnFormatViolation) {
getLog().error(violation.toString());
Expand Down Expand Up @@ -107,7 +104,7 @@ public boolean hasViolations(File file) {
* @since 1.0.1
*/
@Parameter
private List<FormatFileSet> formatFileSets = new ArrayList<FormatFileSet>();
private List<FormatFileSet> formatFileSets = new ArrayList<>();

/**
* The number of spaces expected for indentation. Note that {@code indentSize} can be configuread also per
Expand Down Expand Up @@ -153,7 +150,7 @@ private void check(File file, String encoding, XmlFormatViolationHandler violati

Reader in = null;
try {
in = new InputStreamReader(new FileInputStream(file), encoding);
in = new InputStreamReader(Files.newInputStream(file.toPath()), encoding);
SAXParser saxParser = saxParserFactory.newSAXParser();
IndentCheckSaxHandler handler = new IndentCheckSaxHandler(file, indentSize, violationHandler);
saxParser.parse(new InputSource(in), handler);
Expand Down
24 changes: 8 additions & 16 deletions src/main/java/org/codehaus/mojo/xml/Resolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,17 @@ public class Resolver implements EntityResolver2, URIResolver, LSResourceResolve
manager.setVerbosity(Integer.MAX_VALUE);
}
resolver = new CatalogResolver(manager);
for (int i = 0; i < pFiles.size(); i++) {
File file = (File) pFiles.get(i);
for (File pFile : pFiles) {
File file = pFile;
try {
resolver.getCatalog().parseCatalog(file.getPath());
} catch (IOException e) {
throw new MojoExecutionException(
"Failed to parse catalog file: " + file.getPath() + ": " + e.getMessage(), e);
}
}
for (int i = 0; i < pUrls.size(); i++) {
URL url = (URL) pUrls.get(i);
for (URL pUrl : pUrls) {
URL url = pUrl;
try {
resolver.getCatalog().parseCatalog(url);
} catch (IOException e) {
Expand Down Expand Up @@ -163,11 +163,7 @@ public Source resolve(String pHref, String pBase) throws TransformerException {
if (url != null) {
try {
return asSaxSource(asInputSource(url));
} catch (IOException e) {
throw new TransformerException(e);
} catch (SAXException e) {
throw new TransformerException(e);
} catch (ParserConfigurationException e) {
} catch (IOException | ParserConfigurationException | SAXException e) {
throw new TransformerException(e);
}
}
Expand All @@ -184,7 +180,7 @@ private Source asSaxSource(InputSource isource) throws SAXException, ParserConfi
return new SAXSource(xmlReader, isource);
}

private final LSInput newLSInput(InputSource pSource) {
private LSInput newLSInput(InputSource pSource) {
final LSInputImpl lsInput = new LSInputImpl();
lsInput.setByteStream(pSource.getByteStream());
lsInput.setCharacterStream(pSource.getCharacterStream());
Expand Down Expand Up @@ -298,9 +294,7 @@ private URL resolveAsURL(String pResource, URI pBaseURI) {
stream = null;
return url;
}
} catch (URISyntaxException ex) {
// ignore
} catch (IOException e) {
} catch (URISyntaxException | IOException ex) {
// ignore
} finally {
if (stream != null) {
Expand Down Expand Up @@ -349,9 +343,7 @@ private URL resolve(String pResource, URI pBaseURI) {
}
try {
return locator.getResource(url.toExternalForm()).getURL();
} catch (ResourceNotFoundException e) {
return null;
} catch (IOException e) {
} catch (ResourceNotFoundException | IOException e) {
return null;
}
}
Expand Down

0 comments on commit 2bf0475

Please sign in to comment.