You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2016/01/18 00:39:57 UTC

svn commit: r1725148 - in /maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src: main/java/org/apache/maven/doxia/tools/ test/java/org/apache/maven/doxia/tools/ test/resources/unit/interpolation-parent-test/src/ test/resources/unit/interpolati...

Author: hboutemy
Date: Sun Jan 17 23:39:57 2016
New Revision: 1725148

URL: http://svn.apache.org/viewvc?rev=1725148&view=rev
Log:
[DOXIASITETOOLS-118] do interpolation after inheritance of all parents

Added:
    maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/
    maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/
    maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml   (with props)
Modified:
    maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
    maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java

Modified: maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java?rev=1725148&r1=1725147&r2=1725148&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/main/java/org/apache/maven/doxia/tools/DefaultSiteTool.java Sun Jan 17 23:39:57 2016
@@ -24,13 +24,13 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.io.StringReader;
+import java.io.StringWriter;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.AbstractMap;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -54,6 +54,7 @@ import org.apache.maven.doxia.site.decor
 import org.apache.maven.doxia.site.decoration.Skin;
 import org.apache.maven.doxia.site.decoration.inheritance.DecorationModelInheritanceAssembler;
 import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
+import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Writer;
 import org.apache.maven.model.DistributionManagement;
 import org.apache.maven.model.Model;
 import org.apache.maven.model.Site;
@@ -382,6 +383,28 @@ public class DefaultSiteTool
         }
     }
 
+    /**
+     * Read site descriptor content from Reader, adding support for deprecated <code>${reports}</code>,
+     * <code>${parentProject}</code> and <code>${modules}</code> tags.
+     *
+     * @param reader
+     * @return the input content interpolated with deprecated tags 
+     * @throws IOException
+     */
+    private static String readSiteDescriptor( Reader reader )
+        throws IOException
+    {
+        String siteDescriptorContent = IOUtil.toString( reader );
+    
+        // This is to support the deprecated ${reports}, ${parentProject} and ${modules} tags.
+        Properties props = new Properties();
+        props.put( "reports", "<menu ref=\"reports\"/>\n" );
+        props.put( "modules", "<menu ref=\"modules\"/>\n" );
+        props.put( "parentProject", "<menu ref=\"parent\"/>" );
+    
+        return StringUtils.interpolate( siteDescriptorContent, props );
+    }
+
     /** {@inheritDoc} */
     public DecorationModel getDecorationModel( File siteDirectory, Locale locale, MavenProject project,
                                                List<MavenProject> reactorProjects, ArtifactRepository localRepository,
@@ -397,16 +420,8 @@ public class DefaultSiteTool
 
         getLogger().debug( "Computing decoration model of " + project.getId() + " for locale " + llocale );
 
-        Map<String, String> props = new HashMap<String, String>( 2 );
-
-        // This is to support the deprecated ${reports} and ${modules} tags.
-        props.put( "reports", "<menu ref=\"reports\"/>\n" );
-        props.put( "modules", "<menu ref=\"modules\"/>\n" );
-
-
         Map.Entry<DecorationModel, MavenProject> result =
-            getDecorationModel( 0, siteDirectory, llocale, project, reactorProjects, localRepository, repositories,
-                                props );
+            getDecorationModel( 0, siteDirectory, llocale, project, reactorProjects, localRepository, repositories );
         DecorationModel decorationModel = result.getKey();
         MavenProject parentProject = result.getValue();
 
@@ -416,12 +431,12 @@ public class DefaultSiteTool
 
             String siteDescriptorContent;
 
-            InputStream in = null;
+            Reader reader = null;
             try
             {
                 // Note the default is not a super class - it is used when nothing else is found
-                in = getClass().getResourceAsStream( "/default-site.xml" );
-                siteDescriptorContent = IOUtil.toString( in, "UTF-8" );
+                reader = ReaderFactory.newXmlReader( getClass().getResourceAsStream( "/default-site.xml" ) );
+                siteDescriptorContent = readSiteDescriptor( reader );
             }
             catch ( IOException e )
             {
@@ -429,14 +444,19 @@ public class DefaultSiteTool
             }
             finally
             {
-                IOUtil.close( in );
+                IOUtil.close( reader );
             }
 
-            siteDescriptorContent = getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent );
-
             decorationModel = readDecorationModel( siteDescriptorContent );
         }
 
+        // DecorationModel back to String to interpolate, then go back to DecorationModel
+        String siteDescriptorContent = decorationModelToString( decorationModel );
+        System.out.println( "inherited : " + siteDescriptorContent );
+        siteDescriptorContent = getInterpolatedSiteDescriptorContent( project, siteDescriptorContent );
+        System.out.println( "interpolated : " + siteDescriptorContent );
+        decorationModel = readDecorationModel( siteDescriptorContent );
+
         if ( parentProject != null )
         {
             populateParentMenu( decorationModel, llocale, project, parentProject, true );
@@ -461,19 +481,16 @@ public class DefaultSiteTool
         throws SiteToolException
     {
         checkNotNull( "props", props );
-        checkNotNull( "aProject", aProject );
-        checkNotNull( "siteDescriptorContent", siteDescriptorContent );
 
-        // MSITE-201: The ObjectBasedValueSource( aProject ) below will match
-        // ${modules} to aProject.getModules(), so we need to interpolate that
-        // first.
-
-        Map<String, String> modulesProps = new HashMap<String, String>( 1 );
-
-        // Legacy for the old ${modules} syntax
-        modulesProps.put( "modules", "<menu ref=\"modules\"/>" );
+        return getInterpolatedSiteDescriptorContent( aProject, siteDescriptorContent );
+    }
 
-        String interpolatedSiteDescriptorContent = StringUtils.interpolate( siteDescriptorContent, modulesProps );
+    private String getInterpolatedSiteDescriptorContent( MavenProject aProject,
+                                                        String siteDescriptorContent )
+        throws SiteToolException
+    {
+        checkNotNull( "aProject", aProject );
+        checkNotNull( "siteDescriptorContent", siteDescriptorContent );
 
         RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
 
@@ -495,21 +512,12 @@ public class DefaultSiteTool
         try
         {
             // FIXME: this does not escape xml entities, see MSITE-226, PLXCOMP-118
-            interpolatedSiteDescriptorContent = interpolator.interpolate( interpolatedSiteDescriptorContent,
-                                                                          "project" );
+            return interpolator.interpolate( siteDescriptorContent, "project" );
         }
         catch ( InterpolationException e )
         {
             throw new SiteToolException( "Cannot interpolate site descriptor: " + e.getMessage(), e );
         }
-
-        // Legacy for the old ${parentProject} syntax
-        props.put( "parentProject", "<menu ref=\"parent\"/>" );
-
-        // Legacy for the old ${reports} syntax
-        props.put( "reports", "<menu ref=\"reports\"/>" );
-
-        return StringUtils.interpolate( interpolatedSiteDescriptorContent, props );
     }
 
     /** {@inheritDoc} */
@@ -1055,12 +1063,9 @@ public class DefaultSiteTool
                                                                          MavenProject project,
                                                                          List<MavenProject> reactorProjects,
                                                                          ArtifactRepository localRepository,
-                                                                         List<ArtifactRepository> repositories,
-                                                                         Map<String, String> origProps )
+                                                                         List<ArtifactRepository> repositories )
         throws SiteToolException
     {
-        Map<String, String> props = new HashMap<String, String>( origProps );
-
         // 1. get site descriptor File
         File siteDescriptor;
         if ( project.getBasedir() == null )
@@ -1094,10 +1099,9 @@ public class DefaultSiteTool
 
                 siteDescriptorReader = ReaderFactory.newXmlReader( siteDescriptor );
 
-                String siteDescriptorContent = IOUtil.toString( siteDescriptorReader );
-                String interpolated = getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent );
+                String siteDescriptorContent = readSiteDescriptor( siteDescriptorReader );
 
-                decoration = readDecorationModel( interpolated );
+                decoration = readDecorationModel( siteDescriptorContent );
                 decoration.setLastModified( siteDescriptor.lastModified() );
             }
             else
@@ -1139,7 +1143,7 @@ public class DefaultSiteTool
 
             DecorationModel parentDecoration =
                 getDecorationModel( depth, parentSiteDirectory, locale, parentProject, reactorProjects, localRepository,
-                                    repositories, props ).getKey();
+                                    repositories ).getKey();
 
             // MSHARED-116 requires an empty decoration model (instead of a null one)
             // MSHARED-145 requires us to do this only if there is a parent to merge it with
@@ -1179,10 +1183,9 @@ public class DefaultSiteTool
     private DecorationModel readDecorationModel( String siteDescriptorContent )
         throws SiteToolException
     {
-        DecorationModel decoration;
         try
         {
-            decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
+            return new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
         }
         catch ( XmlPullParserException e )
         {
@@ -1192,7 +1195,26 @@ public class DefaultSiteTool
         {
             throw new SiteToolException( "Error reading site descriptor", e );
         }
-        return decoration;
+    }
+
+    private String decorationModelToString( DecorationModel decoration )
+        throws SiteToolException
+    {
+        StringWriter writer = new StringWriter();
+
+        try
+        {
+            new DecorationXpp3Writer().write( writer, decoration );
+            return writer.toString();
+        }
+        catch ( IOException e )
+        {
+            throw new SiteToolException( "Error reading site descriptor", e );
+        }
+        finally
+        {
+            IOUtil.close( writer );
+        }
     }
 
     private static String buildRelativePath( final String toPath,  final String fromPath, final char separatorChar )

Modified: maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java?rev=1725148&r1=1725147&r2=1725148&view=diff
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java (original)
+++ maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/java/org/apache/maven/doxia/tools/SiteToolTest.java Sun Jan 17 23:39:57 2016
@@ -338,7 +338,7 @@ public class SiteToolTest
     }
     
     // MSHARED-217 -> DOXIATOOLS-34 -> DOXIASITETOOLS-118
-    public void testMultiModuleInterpolation()
+    public void testDecorationModelInheritanceAndInterpolation()
         throws Exception
     {
         SiteTool tool = (SiteTool) lookup( SiteTool.ROLE );
@@ -348,24 +348,22 @@ public class SiteToolTest
         parentProject.setGroupId( "org.apache.maven.shared.its" );
         parentProject.setArtifactId( "mshared-217-parent" );
         parentProject.setVersion( "1.0-SNAPSHOT" );
-        parentProject.setBasedir( null ); // get it from repo
         parentProject.setName( "MSHARED-217 Parent" );
-        String siteDirectory = "src/site";
         
         SiteToolMavenProjectStub childProject = new SiteToolMavenProjectStub( "interpolation-child-test" );
         childProject.setParent( parentProject );
         childProject.setGroupId( "org.apache.maven.shared.its" );
         childProject.setArtifactId( "mshared-217-child" );
         childProject.setVersion( "1.0-SNAPSHOT" );
-        childProject.setBasedir( null ); // get it from repo
         childProject.setName( "MSHARED-217 Child" );
 
         List<MavenProject> reactorProjects = Collections.<MavenProject>singletonList( parentProject );
 
-        DecorationModel model =
-            tool.getDecorationModel( new File( siteDirectory ), Locale.getDefault(), childProject, reactorProjects,
-                                     getLocalRepo(), childProject.getRemoteArtifactRepositories() );
+        DecorationModel model = tool.getDecorationModel( new File( childProject.getBasedir(), "src/site" ),
+                                                         Locale.getDefault(), childProject, reactorProjects,
+                                                         getLocalRepo(), childProject.getRemoteArtifactRepositories() );
         assertNotNull( model );
         assertEquals( "MSHARED-217 Child", model.getName() );
+        assertEquals( "project.artifactId = mshared-217-child", model.getBannerLeft().getName() );
     }
 }

Added: maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml
URL: http://svn.apache.org/viewvc/maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml?rev=1725148&view=auto
==============================================================================
--- maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml (added)
+++ maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml Sun Jan 17 23:39:57 2016
@@ -0,0 +1,34 @@
+<?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/DECORATION/1.6.0"
+    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/DECORATION/1.6.0 http://maven.apache.org/xsd/decoration-1.6.0.xsd">
+  <bannerLeft>
+    <name>project.artifactId = ${project.artifactId}</name>
+  </bannerLeft>
+
+  <body>
+    <breadcrumbs>
+      <item name="Maven"  href="http://maven.apache.org/index.html" />
+    </breadcrumbs>
+  </body>
+</project>

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Propchange: maven/doxia/doxia-sitetools/trunk/doxia-integration-tools/src/test/resources/unit/interpolation-parent-test/src/site/site.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain