Skip to content

Commit

Permalink
[Misc] Fix code style and use try-with-resource in xwiki-platform-too…
Browse files Browse the repository at this point in the history
…l-xmldoc-update-plugin
  • Loading branch information
michitux committed Sep 5, 2024
1 parent c930891 commit f40976f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,23 +122,6 @@
</dependencies>
<build>
<plugins>
<plugin>
<!-- Apply the Checkstyle configurations defined in the top level pom.xml file -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<executions>
<execution>
<!-- Specify the "default" execution id so that the "blocker" one is always executed -->
<id>default</id>
<configuration>
<excludes>
com/xpn/xwiki/tool/doc/AbstractDocumentMojo.java,
com/xpn/xwiki/tool/doc/AttachMojo.java
</excludes>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
Expand All @@ -48,20 +47,20 @@
import com.xpn.xwiki.web.Utils;

/**
* An abstract Mojo that knows how to load a XWikiDocument from XML and to write XML from a XWikiDocument
* An abstract Mojo that knows how to load a XWikiDocument from XML and to write XML from a XWikiDocument.
*
* @version $Id$
*/
public abstract class AbstractDocumentMojo extends AbstractMojo
{
/**
* The document to perform the update on
* The document to perform the update on.
*/
@Parameter(defaultValue = "${basedir}/src/main/resources/XWiki/XWikiPreferences")
protected File sourceDocument;

/**
* The target directory to write the document back to
* The target directory to write the document back to.
*/
@Parameter(defaultValue = "${project.build.outputDirectory}")
protected File outputDirectory;
Expand All @@ -70,9 +69,9 @@ public abstract class AbstractDocumentMojo extends AbstractMojo
* An empty context that will hold the base classes encountered in the passed XML document. This is needed in order
* not to lose the class definition when writing back to XML.
*/
private XWikiContext context;
private final XWikiContext context;

public AbstractDocumentMojo() throws MojoExecutionException
AbstractDocumentMojo() throws MojoExecutionException
{
this.context = createXWikiContext();
}
Expand Down Expand Up @@ -108,38 +107,38 @@ protected XWikiContext createXWikiContext() throws MojoExecutionException
}

/**
* Loads a XWikiDocument from a XML file
* Loads a XWikiDocument from a XML file.
*
* @param file the xml file to load
* @return the XWiki document loaded from XML
* @throws MojoExecutionException
*/
protected XWikiDocument loadFromXML(File file) throws MojoExecutionException
{
XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "WebHome"));
FileInputStream fis;
try {
// Load the document as a XWikiDocument from XML
fis = new FileInputStream(file);
doc.fromXML(fis);
XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "WebHome"));
try (FileInputStream fis = new FileInputStream(file)) {
// Load the document as a XWikiDocument from XML
doc.fromXML(fis);
}

// get XML tree
FileReader fr = new FileReader(file);
// This is not subject to XXE attacks since we control the input files and it's used only at build time.
SAXReader reader = new SAXReader();
Document domdoc;
domdoc = reader.read(fr);
try (FileReader fr = new FileReader(file)) {
domdoc = reader.read(fr);
}
Element root = domdoc.getRootElement();

// Lookup all class nodes, and add them to our xwiki context as BaseClass definitions
List<Node> classNodes = root.selectNodes("//xwikidoc/object/class");
for (Iterator<Node> it = classNodes.iterator(); it.hasNext();) {
for (Node classNode : classNodes) {
BaseClass bClass = new BaseClass();
bClass.fromXML((Element) it.next());
context.addBaseClass(bClass);
bClass.fromXML((Element) classNode);
this.context.addBaseClass(bClass);
}

fis.close();
return doc;
} catch (Exception e) {
throw new MojoExecutionException("Error loading XWikiDocument [" + file + "]", e);
Expand All @@ -155,26 +154,24 @@ protected XWikiDocument loadFromXML(File file) throws MojoExecutionException
*/
protected void writeToXML(XWikiDocument doc, File file) throws MojoExecutionException
{
try {
FileWriter fw = new FileWriter(file);
try (FileWriter fw = new FileWriter(file)) {

context.setWiki(new XWiki());
this.context.setWiki(new XWiki());

// Write to XML the document and attachments but without rendering and without versions.
// The passed xwiki context contains the XClass definitions (as BaseClass) all the objects that
// has been previously loaded from XML, so that the class definition appears again in the XML output.
String xml = doc.toXML(true, false, true, false, context);
String xml = doc.toXML(true, false, true, false, this.context);

fw.write(xml);
fw.close();
} catch (Exception e) {
throw new MojoExecutionException("Error writing XML for XWikiDocument [" + file + "]", e);
}
}

/**
* Return the space directory as a File for a given document in a given directory, creating the directories on the
* fly if the do not exists
* fly if the do not exists.
*
* @param document the document to get space for
* @param directory the directory in which the space will be written
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,21 @@
import com.xpn.xwiki.doc.XWikiDocument;

/**
* Attach a file to a XWiki document
* Attach a file to a XWiki document.
*
* @version $Id$
*/
@Mojo(name = "attach")
public class AttachMojo extends AbstractDocumentMojo
{
/**
* The attachment author
* The attachment author.
*/
@Parameter(defaultValue="XWiki.Admin")
@Parameter(defaultValue = "XWiki.Admin")
private String author;

/**
* The file to attach
* The file to attach.
*/
@Parameter
private File file;
Expand All @@ -57,41 +57,48 @@ public class AttachMojo extends AbstractDocumentMojo
@Parameter
private File[] files;

/**
* Default constructor.
*
* @throws MojoExecutionException when there is an error initializing the context
*/
public AttachMojo() throws MojoExecutionException
{
super();
}

@Override
public void execute() throws MojoExecutionException, MojoFailureException
{
try {
XWikiDocument doc = loadFromXML(sourceDocument);
XWikiDocument doc = loadFromXML(this.sourceDocument);
List<XWikiAttachment> attachments = doc.getAttachmentList();

// Create an XWiki attachment from each of the specified files.
if (this.file != null) {
attachments.add(createAttachment(this.file, this.author));
}
if (this.files != null) {
for (File file : this.files) {
attachments.add(createAttachment(file, this.author));
for (File inputFile : this.files) {
attachments.add(createAttachment(inputFile, this.author));
}
}

// Update the list of attachments.
doc.setAttachmentList(attachments);

// output the file
File outputFile = new File(getSpaceDirectory(outputDirectory, sourceDocument), sourceDocument.getName());
File outputFile =
new File(getSpaceDirectory(this.outputDirectory, this.sourceDocument), this.sourceDocument.getName());
writeToXML(doc, outputFile);
} catch (Exception e) {
throw new MojoExecutionException("Error while attaching files on document ["
+ sourceDocument.getParentFile().getName() + "." + sourceDocument.getName() + "]", e);
+ this.sourceDocument.getParentFile().getName() + "." + this.sourceDocument.getName() + "]", e);
}
}

/**
* Create a XWikiAttachment from a File
* Create a XWikiAttachment from a File.
*
* @param file the file to create the attachment from
* @return the attachment
Expand All @@ -104,11 +111,8 @@ private XWikiAttachment createAttachment(File file, String author) throws MojoEx
XWikiAttachment attachment = new XWikiAttachment();

// Feed the attachment
FileInputStream fis = new FileInputStream(file);
try {
try (FileInputStream fis = new FileInputStream(file)) {
attachment.setContent(fis);
} finally {
fis.close();
}

attachment.setAuthor(author);
Expand Down

0 comments on commit f40976f

Please sign in to comment.