You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by vs...@apache.org on 2009/05/06 16:02:45 UTC

svn commit: r772268 - in /maven/plugins/trunk/maven-pdf-plugin/src/main: java/org/apache/maven/plugins/pdf/PdfMojo.java resources/pdf-plugin.properties resources/pdf-plugin_en.properties resources/pdf-plugin_fr.properties

Author: vsiveton
Date: Wed May  6 14:02:42 2009
New Revision: 772268

URL: http://svn.apache.org/viewvc?rev=772268&view=rev
Log:
MPDF-9: Use site.xml for PDF document structure

o read the default site.xml and generate a default pdf.xml from it

Added:
    maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties   (with props)
    maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties   (with props)
    maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties   (with props)
Modified:
    maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java

Modified: maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java?rev=772268&r1=772267&r2=772268&view=diff
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java (original)
+++ maven/plugins/trunk/maven-pdf-plugin/src/main/java/org/apache/maven/plugins/pdf/PdfMojo.java Wed May  6 14:02:42 2009
@@ -22,23 +22,34 @@
 import java.io.File;
 import java.io.IOException;
 import java.io.Reader;
+import java.io.StringReader;
 import java.io.Writer;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.Properties;
 
 import org.apache.maven.doxia.docrenderer.DocumentRenderer;
 import org.apache.maven.doxia.docrenderer.DocumentRendererException;
 import org.apache.maven.doxia.document.DocumentMeta;
 import org.apache.maven.doxia.document.DocumentModel;
+import org.apache.maven.doxia.document.DocumentTOC;
+import org.apache.maven.doxia.document.DocumentTOCItem;
 import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader;
 import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Writer;
+import org.apache.maven.doxia.site.decoration.DecorationModel;
+import org.apache.maven.doxia.site.decoration.Menu;
+import org.apache.maven.doxia.site.decoration.MenuItem;
+import org.apache.maven.doxia.site.decoration.io.xpp3.DecorationXpp3Reader;
+import org.apache.maven.doxia.tools.SiteToolException;
 
 import org.apache.maven.plugin.MojoExecutionException;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.logging.Log;
 import org.apache.maven.project.MavenProject;
+import org.apache.maven.wagon.PathUtils;
 import org.codehaus.plexus.util.FileUtils;
 import org.codehaus.plexus.util.IOUtil;
 import org.codehaus.plexus.util.InterpolationFilterReader;
@@ -46,6 +57,7 @@
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.WriterFactory;
 import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
+import org.codehaus.plexus.util.xml.XmlStreamReader;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
 /**
@@ -126,6 +138,16 @@
      */
     private DocumentRenderer docRenderer;
 
+    /**
+     * Default locale
+     */
+    private Locale defaultLocale;
+
+    /**
+     * Default decoration model
+     */
+    private DecorationModel defaultDecorationModel;
+
     // ----------------------------------------------------------------------
     // Public methods
     // ----------------------------------------------------------------------
@@ -193,10 +215,11 @@
      * @return DocumentModel.
      * @throws DocumentRendererException if any.
      * @throws IOException if any.
+     * @throws MojoExecutionException if any
      * @see #readAndFilterDocumentDescriptor(MavenProject, File, Log)
      */
     private DocumentModel getDocumentModel()
-        throws DocumentRendererException, IOException
+        throws DocumentRendererException, IOException, MojoExecutionException
     {
         if ( docDescriptor.exists() )
         {
@@ -227,9 +250,10 @@
     /**
      * @return Generate a default document descriptor from the Maven project
      * @throws IOException if any
+     * @throws MojoExecutionException if any
      */
     private DocumentModel generateDefaultDocDescriptor()
-        throws IOException
+        throws IOException, MojoExecutionException
     {
         File outputDir = new File( project.getBuild().getDirectory(), "pdf" );
 
@@ -253,6 +277,31 @@
         docModel.setOutputName( project.getArtifactId() );
         docModel.setMeta( meta );
 
+        // Populate docModel from defaultDecirationModel
+        DecorationModel decorationModel = getDefaultDecorationModel();
+        if ( decorationModel != null )
+        {
+            DocumentTOC toc = new DocumentTOC();
+
+            toc.setName( getI18n().getString( "pdf-plugin", getDefaultLocale(), "toc.title" ) );
+            for ( Iterator it = decorationModel.getMenus().iterator(); it.hasNext(); )
+            {
+                Menu menu = (Menu) it.next();
+
+                for ( Iterator it2 = menu.getItems().iterator(); it2.hasNext(); )
+                {
+                    MenuItem item = (MenuItem) it2.next();
+
+                    DocumentTOCItem documentTOCItem = new DocumentTOCItem();
+                    documentTOCItem.setName( item.getName() );
+                    documentTOCItem.setRef( item.getHref() );
+                    toc.addItem( documentTOCItem );
+                }
+            }
+
+            docModel.setToc( toc );
+        }
+
         if ( getLog().isDebugEnabled() )
         {
             File doc = FileUtils.createTempFile( "pdf", ".xml", outputDir );
@@ -298,6 +347,78 @@
     }
 
     /**
+     * @return the default locale from <code>siteTool</code>.
+     */
+    private Locale getDefaultLocale()
+    {
+        if ( this.defaultLocale == null )
+        {
+            List localesList = getSiteTool().getAvailableLocales( getLocales() );
+            this.defaultLocale = (Locale) localesList.get( 0 );
+        }
+
+        return this.defaultLocale;
+    }
+
+    /**
+     * @return the DecorationModel instance from <code>site.xml</code>
+     * @throws MojoExecutionException if any
+     */
+    private DecorationModel getDefaultDecorationModel()
+        throws MojoExecutionException
+    {
+        if ( this.defaultDecorationModel == null )
+        {
+            Locale locale = getDefaultLocale();
+
+            File descriptorFile =
+                getSiteTool()
+                             .getSiteDescriptorFromBasedir(
+                                                            PathUtils.toRelative( project.getBasedir(),
+                                                                                  siteDirectory.getAbsolutePath() ),
+                                                            project.getBasedir(), locale );
+            DecorationModel decoration = null;
+            if ( descriptorFile.exists() )
+            {
+                Map props = new HashMap();
+
+                XmlStreamReader reader = null;
+                try
+                {
+                    reader = ReaderFactory.newXmlReader( descriptorFile );
+                    String siteDescriptorContent = IOUtil.toString( reader );
+
+                    siteDescriptorContent =
+                        getSiteTool().getInterpolatedSiteDescriptorContent( props, project, siteDescriptorContent,
+                                                                            reader.getEncoding(), "UTF-8" );
+
+                    decoration = new DecorationXpp3Reader().read( new StringReader( siteDescriptorContent ) );
+                }
+                catch ( XmlPullParserException e )
+                {
+                    throw new MojoExecutionException( "Error parsing site descriptor", e );
+                }
+                catch ( IOException e )
+                {
+                    throw new MojoExecutionException( "Error reading site descriptor", e );
+                }
+                catch ( SiteToolException e )
+                {
+                    throw new MojoExecutionException( "Error when interpoling site descriptor", e );
+                }
+                finally
+                {
+                    IOUtil.close( reader );
+                }
+            }
+
+            this.defaultDecorationModel = decoration;
+        }
+
+        return this.defaultDecorationModel;
+    }
+
+    /**
      * Read and filter the <code>docDescriptor</code> file.
      *
      * @param project not null

Added: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties?rev=772268&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties (added)
+++ maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties Wed May  6 14:02:42 2009
@@ -0,0 +1,18 @@
+# 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.
+
+toc.title = Table Of Content

Propchange: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties?rev=772268&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties (added)
+++ maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties Wed May  6 14:02:42 2009
@@ -0,0 +1,23 @@
+# 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.
+
+# NOTE:
+# This bundle is intentionally empty because English strings are provided by the base bundle via the parent chain. It
+# must be provided nevertheless such that a request for locale "en" will not errorneously pick up the bundle for the
+# JVM's default locale (which need not be "en"). See the method javadoc about
+#   ResourceBundle.getBundle(String, Locale, ClassLoader)
+# for a full description of the lookup strategy.

Propchange: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_en.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties
URL: http://svn.apache.org/viewvc/maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties?rev=772268&view=auto
==============================================================================
--- maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties (added)
+++ maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties Wed May  6 14:02:42 2009
@@ -0,0 +1,18 @@
+# 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.
+
+toc.title = Table des matières

Propchange: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: maven/plugins/trunk/maven-pdf-plugin/src/main/resources/pdf-plugin_fr.properties
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision