Skip to content

Commit

Permalink
[Misc] Fix some issues reported by sonarcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
vmassol committed Jan 4, 2019
1 parent e4f80bb commit 71bbc48
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ protected Collection<File> getXARXMLFiles() throws MojoFailureException
// Find all files in the resources dir
File resourcesDir = getResourcesDirectory();

Collection<File> files = new ArrayList<File>();
Collection<File> files = new ArrayList<>();
if (resourcesDir.exists()) {
PlexusIoFileResourceCollection collection = new PlexusIoFileResourceCollection();
collection.setBaseDir(resourcesDir);
Expand Down Expand Up @@ -321,10 +321,8 @@ protected boolean isTechnicalPage(String fileName)
protected boolean isTitlesMatching(String documentReference, String title)
{
for (Map.Entry<Pattern, Pattern> entry : this.titlePatterns.entrySet()) {
if (entry.getKey().matcher(documentReference).matches()) {
if (!entry.getValue().matcher(title).matches()) {
return false;
}
if (entry.getKey().matcher(documentReference).matches() && !entry.getValue().matcher(title).matches()) {
return false;
}
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public MavenProject getProject()
* @throws InvalidDependencyVersionException error
*/
protected Set<Artifact> resolveArtifactDependencies(Artifact artifact) throws ArtifactResolutionException,
ArtifactNotFoundException, ProjectBuildingException, InvalidDependencyVersionException
ArtifactNotFoundException, ProjectBuildingException
{
Artifact pomArtifact =
this.factory.createArtifact(artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), "",
Expand Down Expand Up @@ -348,7 +348,6 @@ protected File getResourcesDirectory()
{
String resourcesLocation =
(this.project.getBasedir().getAbsolutePath() + "/src/main/resources").replace("/", File.separator);
File resourcesDir = new File(resourcesLocation);
return resourcesDir;
return new File(resourcesLocation);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ private void format(String fileName, Document domdoc, String defaultLanguage)
}

// Also update the attachment authors
for (Object attachmentAuthorNode : domdoc.selectNodes("xwikidoc/attachment/author")) {
((Node) attachmentAuthorNode).setText(AUTHOR);
for (Node attachmentAuthorNode : domdoc.selectNodes("xwikidoc/attachment/author")) {
attachmentAuthorNode.setText(AUTHOR);
}

// Set the default language
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private Artifact findArtifact() throws MojoExecutionException
* @throws ArchiverException error when unzip package.O
* @throws MojoExecutionException error when unzip package.
*/
private void performUnArchive() throws ArchiverException, MojoExecutionException
private void performUnArchive() throws MojoExecutionException
{
Artifact artifact = findArtifact();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void execute() throws MojoExecutionException, MojoFailureException
for (File file : xmlFiles) {
String parentName = file.getParentFile().getName();
XWikiDocument xdoc = getDocFromXML(file);
List<String> errors = new ArrayList<String>();
List<String> errors = new ArrayList<>();

// Verification 1: Verify Encoding is UTF8
if (!xdoc.getEncoding().equals("UTF-8")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class XARMojo extends AbstractXARMojo
@Override
public void execute() throws MojoExecutionException
{
if (this.project.getResources().size() < 1) {
if (this.project.getResources().isEmpty()) {
getLog().warn("No XAR created as no resources were found");
return;
}
Expand Down Expand Up @@ -242,13 +242,11 @@ private void unpackTransformedXARs() throws MojoExecutionException
Set<Artifact> artifacts = this.project.getArtifacts();
if (artifacts != null) {
for (Artifact artifact : artifacts) {
if (!artifact.isOptional()) {
if ("xar".equals(artifact.getType())) {
String id = String.format("%s:%s", artifact.getGroupId(), artifact.getArtifactId());
if (id.equals(transformation.getArtifact())) {
unpackXARToOutputDirectory(artifact, new String[] { transformation.getFile() },
new String[] {});
}
if (!artifact.isOptional() && "xar".equals(artifact.getType())) {
String id = String.format("%s:%s", artifact.getGroupId(), artifact.getArtifactId());
if (id.equals(transformation.getArtifact())) {
unpackXARToOutputDirectory(artifact, new String[] { transformation.getFile() },
new String[] {});
}
}
}
Expand All @@ -266,10 +264,8 @@ private void unpackDependentXARs() throws MojoExecutionException
Set<Artifact> artifacts = this.project.getArtifacts();
if (artifacts != null) {
for (Artifact artifact : artifacts) {
if (!artifact.isOptional()) {
if ("xar".equals(artifact.getType())) {
unpackXARToOutputDirectory(artifact, getIncludes(), getExcludes());
}
if (!artifact.isOptional() && "xar".equals(artifact.getType())) {
unpackXARToOutputDirectory(artifact, getIncludes(), getExcludes());
}
}
}
Expand Down Expand Up @@ -381,11 +377,8 @@ private void addFileElements(Collection<ArchiveEntry> files, Element filesElemen

// Add configured properties
XAREntry cfgEntry = getEntryMap().get(reference);
if (cfgEntry != null) {
// Entry type
if (cfgEntry.getType() != null) {
element.addAttribute("type", cfgEntry.getType());
}
if (cfgEntry != null && cfgEntry.getType() != null) {
element.addAttribute("type", cfgEntry.getType());
}

filesElement.add(element);
Expand Down Expand Up @@ -504,7 +497,7 @@ private void addFilesToArchive(ZipArchiver archiver, File sourceDir, File packag

// Next, we scan the hole directory and subdirectories for documents.

Queue<File> fileQueue = new LinkedList<File>();
Queue<File> fileQueue = new LinkedList<>();
addContentsToQueue(fileQueue, sourceDir);
while (!fileQueue.isEmpty() && !documentNames.isEmpty()) {
File currentFile = fileQueue.poll();
Expand All @@ -517,8 +510,8 @@ private void addFilesToArchive(ZipArchiver archiver, File sourceDir, File packag
//
// Note: DO NOT USE String.split since it requires a regexp. Under Windows XP, the FileSeparator is
// '\' when not escaped is a special character of the regexp
// String archivedFilePath =
// currentFile.getAbsolutePath().split(sourceDir.getAbsolutePath() + File.separator)[1];
// String archivedFilePath =
// currentFile.getAbsolutePath().split(sourceDir.getAbsolutePath() + File.separator)[1];
String archivedFilePath = currentFile.getAbsolutePath()
.substring((sourceDir.getAbsolutePath() + File.separator).length());
archivedFilePath = archivedFilePath.replace(File.separatorChar, '/');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ protected void writeNodeText(Node node) throws IOException
if (this.useFormat && node.getText().trim().length() == 0) {
// Check if parent node contains non text nodes
boolean containsNonTextNode = false;
for (Object object : node.getParent().content()) {
Node objectNode = (Node) object;
for (Node objectNode : node.getParent().content()) {
if (objectNode.getNodeType() != Node.TEXT_NODE) {
containsNonTextNode = true;
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void fromXML(Document domdoc) throws DocumentException
this.attachmentDatePresent = rootElement.selectSingleNode("//attachment/date") != null;

// Does this document contain a XWiki.TranslationDocumentClass xobject?
if (rootElement.selectNodes("//object/className[text() = 'XWiki.TranslationDocumentClass']").size() > 0) {
if (!rootElement.selectNodes("//object/className[text() = 'XWiki.TranslationDocumentClass']").isEmpty()) {
this.containsTranslations = true;
// Record the visibility
for (Node node : rootElement
Expand Down Expand Up @@ -252,7 +252,7 @@ public static String readDocumentReference(Document domdoc) throws DocumentExcep
// to a wiki page.
if (name == null && space == null) {
throw new DocumentException(
String.format("Content doesn't point to valid wiki page XML", domdoc.getName()));
String.format("Content doesn't point to valid wiki page XML [%s]", domdoc.getName()));
}

result = space == null ? name : escapeSpaceOrPageName(space) + '.' + escapeSpaceOrPageName(name);
Expand Down Expand Up @@ -306,17 +306,17 @@ public static String readElement(Element rootElement, String elementName) throws
public static List<Map<String, String>> readAttachmentData(Element rootElement) throws DocumentException
{
List<Map<String, String>> data = new ArrayList<>();
for (Object attachmentNode : rootElement.elements("attachment")) {
for (Element attachmentNode : rootElement.elements("attachment")) {
Map<String, String> map = new HashMap<>();
String authorValue = readElement((Element) attachmentNode, AUTHOR_TAG);
String authorValue = readElement(attachmentNode, AUTHOR_TAG);
if (authorValue != null) {
map.put(AUTHOR_TAG, authorValue);
}
String mimetypeValue = readElement((Element) attachmentNode, MIMETYPE_TAG);
String mimetypeValue = readElement(attachmentNode, MIMETYPE_TAG);
if (mimetypeValue != null) {
map.put(MIMETYPE_TAG, mimetypeValue);
}
String filenameValue = readElement((Element) attachmentNode, FILENAME_TAG);
String filenameValue = readElement(attachmentNode, FILENAME_TAG);
if (filenameValue != null) {
map.put(FILENAME_TAG, filenameValue);
}
Expand Down

0 comments on commit 71bbc48

Please sign in to comment.