[MSITE-842] $currentFileName and $alignedFileName are incorrect for Maven Report plugins that use several Sink instances

* Ensured that the actual filename of the Sink being rendered is exposed in RenderingContext for multi-page reports
* Subpages (or subsinks) in subdirectories are now supported

This closes #8
diff --git a/src/it/projects/MSITE-842/invoker.properties b/src/it/projects/MSITE-842/invoker.properties
new file mode 100644
index 0000000..ed62a61
--- /dev/null
+++ b/src/it/projects/MSITE-842/invoker.properties
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+invoker.goals.1 = clean install
+invoker.debug.1 = false
+invoker.goals.2 = site
+invoker.maven.version = 3.0+
diff --git a/src/it/projects/MSITE-842/pom.xml b/src/it/projects/MSITE-842/pom.xml
new file mode 100644
index 0000000..470ef86
--- /dev/null
+++ b/src/it/projects/MSITE-842/pom.xml
@@ -0,0 +1,93 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+
+  <groupId>org.apache.maven.plugins.site.its</groupId>
+  <artifactId>MSITE-842</artifactId>
+  <version>1.0-SNAPSHOT</version>
+  <packaging>maven-plugin</packaging>
+
+  <name>it-plugin-test Maven Mojo</name>
+  <url>http://maven.apache.org</url>
+
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.maven.reporting</groupId>
+      <artifactId>maven-reporting-impl</artifactId>
+      <version>3.0.0</version>
+    </dependency>
+  </dependencies>
+
+  <build>
+
+    <pluginManagement>
+      <plugins>
+        <plugin>
+          <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-site-plugin</artifactId>
+          <version>@project.version@</version>
+        </plugin>
+      </plugins>
+    </pluginManagement>
+
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-site-plugin</artifactId>
+        <configuration>
+          <templateFile>${basedir}/src/site/show-properties.vm</templateFile>
+        </configuration>
+      </plugin>
+    </plugins>
+
+  </build>
+
+  <reporting>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins.site.its</groupId>
+        <artifactId>MSITE-842</artifactId>
+        <version>${project.version}</version>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-project-info-reports-plugin</artifactId>
+        <version>2.9</version>
+        <reportSets>
+          <reportSet>
+            <configuration>
+              <skip>true</skip>
+            </configuration>
+          </reportSet>
+        </reportSets>
+      </plugin>
+    </plugins>
+  </reporting>
+
+</project>
diff --git a/src/it/projects/MSITE-842/src/it/MSITE-842.html b/src/it/projects/MSITE-842/src/it/MSITE-842.html
new file mode 100644
index 0000000..4aea73d
--- /dev/null
+++ b/src/it/projects/MSITE-842/src/it/MSITE-842.html
@@ -0,0 +1,26 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+alignedFileName = MSITE-842.html
+
+currentFileName = MSITE-842.html
+
+getRelativePath() = .
+
+content = Main Sink
diff --git a/src/it/projects/MSITE-842/src/it/another-page.html b/src/it/projects/MSITE-842/src/it/another-page.html
new file mode 100644
index 0000000..b95cde3
--- /dev/null
+++ b/src/it/projects/MSITE-842/src/it/another-page.html
@@ -0,0 +1,26 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+alignedFileName = another-page.html
+
+currentFileName = another-page.html
+
+getRelativePath() = .
+
+content = Another Sink
diff --git a/src/it/projects/MSITE-842/src/it/sub/sub.html b/src/it/projects/MSITE-842/src/it/sub/sub.html
new file mode 100644
index 0000000..94838ae
--- /dev/null
+++ b/src/it/projects/MSITE-842/src/it/sub/sub.html
@@ -0,0 +1,26 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+alignedFileName = ../sub/sub.html
+
+currentFileName = sub/sub.html
+
+getRelativePath() = ..
+
+content = Subdirectory Sink
diff --git a/src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java b/src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java
new file mode 100644
index 0000000..c00c6fc
--- /dev/null
+++ b/src/it/projects/MSITE-842/src/main/java/org/apache/maven/plugins/it/MyReport.java
@@ -0,0 +1,100 @@
+package org.apache.maven.plugins.it;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.doxia.sink.Sink;
+import org.apache.maven.reporting.AbstractMavenReport;
+import org.apache.maven.reporting.MavenReportException;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Locale;
+
+/**
+ * Goal which creates several pages in a report.
+ *
+ * @goal test
+ * @phase site
+ */
+public class MyReport
+    extends AbstractMavenReport
+{
+
+    public String getOutputName()
+    {
+        return "MSITE-842";
+    }
+
+    public String getName( Locale locale )
+    {
+        return "MSITE-842";
+    }
+
+    public String getDescription( Locale locale )
+    {
+        return "Test Report for MSITE-842";
+    }
+
+    @Override
+    protected void executeReport( Locale locale ) throws MavenReportException
+    {
+        // Main Sink
+        Sink mainSink = getSink();
+        if ( mainSink == null )
+        {
+            throw new MavenReportException("Could not get the main Sink");
+        }
+
+        // Write to the main Sink
+        mainSink.text( "Main Sink" );
+
+        // Create a new sink!
+        Sink anotherSink;
+        try
+        {
+            anotherSink = getSinkFactory().createSink( outputDirectory, "another-page.html" );
+        }
+        catch ( IOException e )
+        {
+            throw new MavenReportException( "Could not create sink for another-page.html in " +
+                    outputDirectory.getAbsolutePath(), e );
+        }
+
+        // Write to the other Sink
+        anotherSink.text( "Another Sink" );
+
+        // Create a new sink, in a subdirectory
+        File subDirectory = new File( outputDirectory, "sub" );
+        Sink subDirectorySink;
+        try
+        {
+            subDirectorySink = getSinkFactory().createSink( subDirectory, "sub.html" );
+        }
+        catch ( IOException e )
+        {
+            throw new MavenReportException( "Could not create sink for sub.html in " +
+                    subDirectory.getAbsolutePath(), e );
+        }
+
+        // Write to the sink in the subdirectory
+        subDirectorySink.text( "Subdirectory Sink" );
+
+    }
+}
diff --git a/src/it/projects/MSITE-842/src/site/show-properties.vm b/src/it/projects/MSITE-842/src/site/show-properties.vm
new file mode 100644
index 0000000..f51d49e
--- /dev/null
+++ b/src/it/projects/MSITE-842/src/site/show-properties.vm
@@ -0,0 +1,26 @@
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied.  See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+alignedFileName = $alignedFileName
+
+currentFileName = $currentFileName
+
+getRelativePath() = $docRenderingContext.getRelativePath()
+
+content = $bodyContent
diff --git a/src/it/projects/MSITE-842/verify.groovy b/src/it/projects/MSITE-842/verify.groovy
new file mode 100644
index 0000000..508edb6
--- /dev/null
+++ b/src/it/projects/MSITE-842/verify.groovy
@@ -0,0 +1,44 @@
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+File resultFile;
+File expectedFile;
+
+// Check MSITE-842.html (must exist and be exactly like the model
+resultFile = new File(basedir, "target/site/MSITE-842.html");
+expectedFile = new File(basedir, "src/it/MSITE-842.html");
+
+assert resultFile.exists() && resultFile.isFile()
+assert resultFile.text.normalize().equals(expectedFile.text.normalize())
+
+// Check another-page.html (must exist and be exactly like the model
+resultFile = new File(basedir, "target/site/another-page.html");
+expectedFile = new File(basedir, "src/it/another-page.html");
+
+assert resultFile.exists() && resultFile.isFile()
+assert resultFile.text.normalize().equals(expectedFile.text.normalize())
+
+// Check sub/sub.html (must exist and be exactly like the model
+resultFile = new File(basedir, "target/site/sub/sub.html");
+expectedFile = new File(basedir, "src/it/sub/sub.html");
+
+assert resultFile.exists() && resultFile.isFile()
+assert resultFile.text.normalize().equals(expectedFile.text.normalize())
+
diff --git a/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java b/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java
index a5e8201..dcf6e07 100644
--- a/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java
+++ b/src/main/java/org/apache/maven/plugins/site/render/ReportDocumentRenderer.java
@@ -46,6 +46,7 @@
 import org.apache.maven.reporting.MavenReport;
 import org.apache.maven.reporting.MavenReportException;
 import org.apache.maven.reporting.exec.MavenReportExecution;
+import org.codehaus.plexus.util.PathTool;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.WriterFactory;
 
@@ -65,7 +66,7 @@
     private final String reportMojoInfo;
 
     private final ClassLoader classLoader;
-    
+
     private final Log log;
 
     public ReportDocumentRenderer( MavenReportExecution mavenReportExecution, RenderingContext renderingContext,
@@ -95,7 +96,7 @@
 
     /**
      * Get plugin information from report's Manifest.
-     * 
+     *
      * @param report the Maven report
      * @return plugin information as Specification Title followed by Specification Version if set in Manifest and
      *         supported by JVM
@@ -108,7 +109,7 @@
         {
             String title = pkg.getSpecificationTitle();
             String version = pkg.getSpecificationVersion();
-            
+
             if ( title == null )
             {
                 return version;
@@ -133,9 +134,9 @@
 
         private String outputName;
 
-        MultiPageSubSink( File outputDir, String outputName, RenderingContext ctx )
+        MultiPageSubSink( File outputDir, String outputName, RenderingContext context )
         {
-            super( ctx );
+            super( context );
             this.outputName = outputName;
             this.outputDir = outputDir;
         }
@@ -155,22 +156,56 @@
     private static class MultiPageSinkFactory
         implements SinkFactory
     {
+        /**
+         * The report that is (maybe) generating multiple pages
+         */
+        private MavenReport report;
+
+        /**
+         * The main RenderingContext, which is the base for the RenderingContext of subpages
+         */
         private RenderingContext context;
 
+        /**
+         * List of sinks (subpages) associated to this report
+         */
         private List<MultiPageSubSink> sinks = new ArrayList<MultiPageSubSink>();
 
-        MultiPageSinkFactory( RenderingContext ctx )
+        MultiPageSinkFactory( MavenReport report, RenderingContext context )
         {
-            this.context = ctx;
+            this.report = report;
+            this.context = context;
         }
 
+        @Override
         public Sink createSink( File outputDir, String outputName )
         {
-            MultiPageSubSink sink = new MultiPageSubSink( outputDir, outputName, context );
+            // Create a new context, similar to the main one, but with a different output name
+            String outputRelativeToTargetSite = PathTool.getRelativeFilePath(
+                report.getReportOutputDirectory().getPath(),
+                new File( outputDir, outputName ).getPath()
+            );
+
+            RenderingContext subSinkContext = new RenderingContext(
+                context.getBasedir(),
+                context.getBasedirRelativePath(),
+                outputRelativeToTargetSite,
+                context.getParserId(),
+                context.getExtension(),
+                context.isEditable(),
+                context.getGenerator()
+            );
+
+            // Create a sink for this subpage, based on this new context
+            MultiPageSubSink sink = new MultiPageSubSink( outputDir, outputName, subSinkContext );
+
+            // Add it to the list of sinks associated to this report
             sinks.add( sink );
+
             return sink;
         }
 
+        @Override
         public Sink createSink( File arg0, String arg1, String arg2 )
             throws IOException
         {
@@ -178,6 +213,7 @@
             return null;
         }
 
+        @Override
         public Sink createSink( OutputStream arg0 )
             throws IOException
         {
@@ -185,6 +221,7 @@
             return null;
         }
 
+        @Override
         public Sink createSink( OutputStream arg0, String arg1 )
             throws IOException
         {
@@ -198,6 +235,7 @@
         }
     }
 
+    @Override
     public void renderDocument( Writer writer, Renderer renderer, SiteRenderingContext siteRenderingContext )
         throws RendererException, FileNotFoundException
     {
@@ -213,7 +251,7 @@
         // main sink
         SiteRendererSink mainSink = new SiteRendererSink( renderingContext );
         // sink factory, for multi-page reports that need sub-sinks
-        MultiPageSinkFactory multiPageSinkFactory = new MultiPageSinkFactory( renderingContext );
+        MultiPageSinkFactory multiPageSinkFactory = new MultiPageSinkFactory( report, renderingContext );
 
         ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
         try
@@ -288,6 +326,12 @@
                 outputName = mySink.getOutputName();
                 log.debug( "  Rendering " + outputName );
 
+                // Create directories if necessary
+                if ( !mySink.getOutputDir().exists() )
+                {
+                    mySink.getOutputDir().mkdirs();
+                }
+
                 File outputFile = new File( mySink.getOutputDir(), outputName );
 
                 try ( Writer out = WriterFactory.newWriter( outputFile, siteRenderingContext.getOutputEncoding() ) )
@@ -313,7 +357,7 @@
 
     /**
      * Try to generate report with extended multi-page API.
-     * 
+     *
      * @return <code>true</code> if the report was compatible with the extended API
      */
     private boolean generateMultiPage( Locale locale, SinkFactory sf, Sink sink )
@@ -344,16 +388,19 @@
         }
     }
 
+    @Override
     public String getOutputName()
     {
         return renderingContext.getOutputName();
     }
 
+    @Override
     public RenderingContext getRenderingContext()
     {
         return renderingContext;
     }
 
+    @Override
     public boolean isOverwrite()
     {
         // TODO: would be nice to query the report to see if it is modified
@@ -363,6 +410,7 @@
     /**
      * @return true if the current report is external, false otherwise
      */
+    @Override
     public boolean isExternalReport()
     {
         return report.isExternalReport();