Skip to content

Commit

Permalink
[ARCHETYPE-678] Code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Sep 20, 2024
1 parent 1e32a01 commit 4f652a7
Show file tree
Hide file tree
Showing 42 changed files with 161 additions and 301 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
Expand Down Expand Up @@ -95,7 +95,7 @@ public void zip(File sourceDirectory, File archive) throws IOException {
getLogger().warn("Could not create new file \"" + archive.getPath() + "\" or the file already exists.");
}

try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(archive))) {
try (ZipOutputStream zos = new ZipOutputStream(Files.newOutputStream(archive.toPath()))) {
zos.setLevel(9);

zipper(zos, sourceDirectory.getAbsolutePath().length(), sourceDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.eclipse.aether.repository.RemoteRepository;

public interface ArchetypeArtifactManager {
String ROLE = ArchetypeArtifactManager.class.getName();

Model getArchetypePom(File jar) throws XmlPullParserException, UnknownArchetype, IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

/** @author rafale */
public interface ArchetypeFilesResolver {
String ROLE = ArchetypeFilesResolver.class.getName();

List<String> getFilesWithExtension(List<String> files, String extension);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ public interface Constants {

String EXCLUDE_PATTERNS = "excludePatterns";

List<String> DEFAULT_FILTERED_EXTENSIONS = Arrays.asList(new String[] {
"java",
"xml",
"txt",
"groovy",
"cs",
"mdo",
"aj",
"jsp",
"gsp",
"vm",
"html",
"xhtml",
"properties",
".classpath",
".project"
});

List<String> DEFAULT_LANGUAGES = Arrays.asList(new String[] {"java", "groovy", "csharp", "aspectj"});
List<String> DEFAULT_FILTERED_EXTENSIONS = Arrays.asList(
"java",
"xml",
"txt",
"groovy",
"cs",
"mdo",
"aj",
"jsp",
"gsp",
"vm",
"html",
"xhtml",
"properties",
".classpath",
".project");

List<String> DEFAULT_LANGUAGES = Arrays.asList("java", "groovy", "csharp", "aspectj");

String GROUP_ID = "groupId";

Expand All @@ -91,12 +90,8 @@ public interface Constants {

String PARENT_ARTIFACT_ID = "parentArtifactId";

String POM_PATH = Constants.ARCHETYPE_RESOURCES + "/" + Constants.ARCHETYPE_POM;

String RESOURCES = "resources";

String SITE = "site";

String SRC = "src";

String TEST = "test";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -46,7 +48,6 @@
import org.apache.maven.archetype.old.descriptor.ArchetypeDescriptorBuilder;
import org.apache.maven.model.Model;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.repository.RemoteRepository;
Expand Down Expand Up @@ -265,7 +266,7 @@ private void setArchetype(
}

private boolean isFileSetArchetype(ZipFile zipFile) throws IOException {
try (Reader reader = getArchetypeDescriptorReader(zipFile); ) {
try (Reader reader = getArchetypeDescriptorReader(zipFile)) {
return (reader != null);
}
}
Expand Down Expand Up @@ -330,7 +331,7 @@ private Reader getDescriptorReader(ZipFile zipFile, String descriptor) throws IO
throw new IOException("The " + descriptor + " descriptor cannot be read in " + zipFile.getName() + ".");
}

return ReaderFactory.newReader(is, ReaderFactory.UTF_8);
return new InputStreamReader(is, StandardCharsets.UTF_8);
}

private ZipEntry searchEntry(ZipFile zipFile, String searchString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -271,14 +270,14 @@ private String getCommonPackage(String packageName, String templatePackage) {
}

private List<String> resolveFiles(File basedir, List<String> languages) throws IOException {
String[] languagesArray = languages.toArray(new String[languages.size()]);
String[] languagesArray = languages.toArray(new String[0]);
String[] languagesPathesArray = new String[languagesArray.length];
for (int i = 0; i < languagesArray.length; i++) {
languagesPathesArray[i] = "**/src/**/" + languagesArray[i] + "/**";
}

String excludes = "target";
for (String defaultExclude : Arrays.asList(ListScanner.DEFAULTEXCLUDES)) {
for (String defaultExclude : ListScanner.DEFAULTEXCLUDES) {
excludes += "," + defaultExclude + "/**";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import javax.xml.transform.TransformerException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand All @@ -33,6 +32,7 @@
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -56,8 +56,8 @@
import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
import org.codehaus.plexus.logging.AbstractLogEnabled;
import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.ReaderFactory;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.xml.XmlStreamReader;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.codehaus.plexus.util.xml.Xpp3DomUtils;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
Expand All @@ -73,7 +73,7 @@ public void addModule(File pom, String artifactId)
throws IOException, ParserConfigurationException, TransformerException, SAXException, InvalidPackaging,
ArchetypeTemplateProcessingException {
StringWriter out = new StringWriter();
boolean found = PomUtils.addNewModule(artifactId, ReaderFactory.newXmlReader(pom), out);
boolean found = PomUtils.addNewModule(artifactId, new XmlStreamReader(pom), out);
if (found) {
FileUtils.fileWrite(pom.getAbsolutePath(), out.toString());
}
Expand Down Expand Up @@ -182,7 +182,7 @@ public void mergePoms(File pom, File temporaryPom) throws IOException, XmlPullPa

@Override
public Model readPom(final File pomFile) throws IOException, XmlPullParserException {
try (Reader pomReader = ReaderFactory.newXmlReader(pomFile)) {
try (Reader pomReader = new XmlStreamReader(pomFile)) {
MavenXpp3Reader reader = new MavenXpp3Reader();

return reader.read(pomReader);
Expand All @@ -191,7 +191,7 @@ public Model readPom(final File pomFile) throws IOException, XmlPullParserExcept

@Override
public Model readPom(InputStream pomStream) throws IOException, XmlPullParserException {
try (Reader pomReader = ReaderFactory.newXmlReader(pomStream)) {
try (Reader pomReader = new XmlStreamReader(pomStream)) {
MavenXpp3Reader reader = new MavenXpp3Reader();

return reader.read(pomReader);
Expand All @@ -203,7 +203,7 @@ public void writePom(final Model model, final File pomFile, final File initialPo
String fileEncoding = StringUtils.isEmpty(model.getModelEncoding()) ? "UTF-8" : model.getModelEncoding();

org.jdom2.Document doc;
try (InputStream inputStream = new FileInputStream(initialPomFile)) {
try (InputStream inputStream = Files.newInputStream(initialPomFile.toPath())) {
SAXBuilder builder = new SAXBuilder();
doc = builder.build(inputStream);
} catch (JDOMException exc) {
Expand All @@ -225,7 +225,7 @@ public void writePom(final Model model, final File pomFile, final File initialPo
} catch (FileNotFoundException e) {
getLogger().debug("Creating pom file " + pomFile);

try (Writer pomWriter = new OutputStreamWriter(new FileOutputStream(pomFile), fileEncoding)) {
try (Writer pomWriter = new OutputStreamWriter(Files.newOutputStream(pomFile.toPath()), fileEncoding)) {
MavenXpp3Writer writer = new MavenXpp3Writer();
writer.write(pomWriter, model);
}
Expand Down Expand Up @@ -253,13 +253,13 @@ private void mergeModelBuild(Model model, Model generatedModel) {

private void mergeProfiles(Model model, Model generatedModel) {
List<Profile> generatedProfiles = generatedModel.getProfiles();
if (generatedProfiles != null && generatedProfiles.size() > 0) {
if (generatedProfiles != null && !generatedProfiles.isEmpty()) {
List<Profile> modelProfiles = model.getProfiles();
Map<String, Profile> modelProfileIdMap = new HashMap<>();
if (modelProfiles == null) {
modelProfiles = new ArrayList<>();
model.setProfiles(modelProfiles);
} else if (modelProfiles.size() > 0) {
} else if (!modelProfiles.isEmpty()) {
// add profile ids from the model for later lookups to the modelProfileIds set
for (Profile modelProfile : modelProfiles) {
modelProfileIdMap.put(modelProfile.getId(), modelProfile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public void setCaseSensitive(boolean isCaseSensitive) {
* elements must be non-<code>null</code>.
*/
public void setExcludes(List<String> excludesList) {
String[] excludes = excludesList.toArray(new String[excludesList.size()]);
String[] excludes = excludesList.toArray(new String[0]);
if (excludes == null) {
this.excludes = null;
} else {
Expand Down Expand Up @@ -362,7 +362,7 @@ public void setExcludes(String excludes) {
* elements must be non-<code>null</code>.
*/
public void setIncludes(List<String> includesList) {
String[] includes = includesList.toArray(new String[includesList.size()]);
String[] includes = includesList.toArray(new String[0]);
if (includes == null) {
this.includes = null;
} else {
Expand Down Expand Up @@ -449,7 +449,7 @@ protected boolean matchesPatterns(String name, String[] patterns) {
String path = null;

String baseDir = getBasedir();
if (baseDir.length() > 0) {
if (!baseDir.isEmpty()) {
baseDir = baseDir.concat(File.separator);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ public String getURI(String prefix) {
if (index == -1) {
return null;
}
String uri = uris.elementAt(index);
return uri;
return uris.elementAt(index);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,7 @@ private Writer makeWriter(OutputStream out) throws java.io.UnsupportedEncodingEx

/** Get an OutputStreamWriter, use specified encoding. */
private static Writer makeWriter(OutputStream out, String enc) throws java.io.UnsupportedEncodingException {
Writer writer = new BufferedWriter((new OutputStreamWriter(new BufferedOutputStream(out), enc)));
return writer;
return new BufferedWriter((new OutputStreamWriter(new BufferedOutputStream(out), enc)));
}

// * * * * * * * * * * Output to a Writer * * * * * * * * * *
Expand Down Expand Up @@ -1271,12 +1270,12 @@ private boolean isAllWhitespace(Object obj) {

// Determine if a string starts with a XML whitespace.
private boolean startsWithWhite(String str) {
return ((str != null) && (str.length() > 0) && isWhitespace(str.charAt(0)));
return ((str != null) && (!str.isEmpty()) && isWhitespace(str.charAt(0)));
}

// Determine if a string ends with a XML whitespace.
private boolean endsWithWhite(String str) {
return ((str != null) && (str.length() > 0) && isWhitespace(str.charAt(str.length() - 1)));
return ((str != null) && (!str.isEmpty()) && isWhitespace(str.charAt(str.length() - 1)));
}

// Determine if a character is a XML whitespace.
Expand Down Expand Up @@ -1486,7 +1485,7 @@ public String toString() {
+ "omitEncoding = " + userFormat.omitEncoding + ", "
+ "indent = '" + userFormat.indent + "'" + ", "
+ "expandEmptyElements = " + userFormat.expandEmptyElements + ", "
+ "lineSeparator = '" + buffer.toString() + "', "
+ "lineSeparator = '" + buffer + "', "
+ "textMode = " + userFormat.mode + "]");
}

Expand All @@ -1508,12 +1507,12 @@ private NamespaceStack createNamespaceStack() {
* declare a NamespaceStack parameter, but we don't want to
* declare the parent NamespaceStack class as public.
*/
protected class NamespaceStack extends org.apache.maven.archetype.common.util.NamespaceStack {}
protected static class NamespaceStack extends org.apache.maven.archetype.common.util.NamespaceStack {}

// Support method to print a name without using elt.getQualifiedName()
// and thus avoiding a StringBuilder creation and memory churn
private void printQualifiedName(Writer out, Element e) throws IOException {
if (e.getNamespace().getPrefix().length() == 0) {
if (e.getNamespace().getPrefix().isEmpty()) {
out.write(e.getName());
} else {
out.write(e.getNamespace().getPrefix());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
* Create an archetype from a project.
*/
public interface ArchetypeCreator {
String ROLE = ArchetypeCreator.class.getName();

void createArchetype(ArchetypeCreationRequest request, ArchetypeCreationResult result);
}
Loading

0 comments on commit 4f652a7

Please sign in to comment.