View Javadoc
1   package org.apache.maven.plugins.pdf;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.Reader;
25  import java.io.StringReader;
26  
27  import java.util.Properties;
28  
29  import org.apache.maven.doxia.document.DocumentModel;
30  import org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader;
31  
32  import org.apache.maven.plugin.logging.Log;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.shared.utils.io.FileUtils;
35  import org.codehaus.plexus.interpolation.EnvarBasedValueSource;
36  import org.codehaus.plexus.interpolation.InterpolationException;
37  import org.codehaus.plexus.interpolation.Interpolator;
38  import org.codehaus.plexus.interpolation.MapBasedValueSource;
39  import org.codehaus.plexus.interpolation.ObjectBasedValueSource;
40  import org.codehaus.plexus.interpolation.RegexBasedInterpolator;
41  import org.codehaus.plexus.util.IOUtil;
42  import org.codehaus.plexus.util.ReaderFactory;
43  import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
44  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
45  import java.util.Locale;
46  
47  /**
48   * Read and filter a DocumentModel from a document descriptor file.
49   *
50   * @author ltheussl
51   * @version $Id$
52   */
53  public class DocumentDescriptorReader
54  {
55      /** A MavenProject to extract additional info. */
56      private final MavenProject project;
57  
58      /** Used to log the interpolated document descriptor. */
59      private final Log log;
60  
61      private final Locale locale;
62  
63      /**
64       * Constructor.
65       */
66      public DocumentDescriptorReader()
67      {
68          this( null, null, null );
69      }
70  
71      /**
72       * Constructor.
73       *
74       * @param project may be null.
75       */
76      public DocumentDescriptorReader( final MavenProject project )
77      {
78          this( project, null, null );
79      }
80  
81      /**
82       * Constructor.
83       *
84       * @param project may be null.
85       * @param log may be null.
86       */
87      public DocumentDescriptorReader( final MavenProject project, final Log log, final Locale locale )
88      {
89          this.project = project;
90          this.log = log;
91          this.locale = locale;
92      }
93  
94      /**
95       * Read and filter the <code>docDescriptor</code> file.
96       *
97       * @param docDescriptor not null, corresponding to non-localized descriptor file.
98       * @return a DocumentModel instance.
99       * @throws XmlPullParserException if an error occurs during parsing.
100      * @throws IOException if an error occurs during reading.
101      */
102     public DocumentModel readAndFilterDocumentDescriptor( File docDescriptor )
103         throws XmlPullParserException, IOException
104     {
105         if ( locale != null )
106         {
107             String descriptorFilename = docDescriptor.getName();
108             String localized = FileUtils.removeExtension( descriptorFilename ) + '_' + locale.getLanguage() + '.'
109                 + FileUtils.getExtension( descriptorFilename );
110             File localizedDocDescriptor = new File( docDescriptor.getParentFile(), localized );
111 
112             if ( localizedDocDescriptor.exists() )
113             {
114                 docDescriptor = localizedDocDescriptor;
115             }
116         }
117 
118         Reader reader = null;
119         try
120         {
121             // System properties
122             final Properties filterProperties = System.getProperties();
123             // Project properties
124             if ( project != null && project.getProperties() != null )
125             {
126                 filterProperties.putAll( project.getProperties() );
127             }
128 
129             final Interpolator interpolator = new RegexBasedInterpolator();
130             interpolator.addValueSource( new MapBasedValueSource( filterProperties ) );
131             interpolator.addValueSource( new EnvarBasedValueSource() );
132             interpolator.addValueSource( new ObjectBasedValueSource( project )
133             {
134                 /** {@inheritDoc} */
135                 public Object getValue( final String expression )
136                 {
137                     try
138                     {
139                         return ReflectionValueExtractor.evaluate( expression, project );
140                     }
141                     catch ( Exception e )
142                     {
143                         addFeedback( "Failed to extract \'" + expression + "\' from: " + project, e );
144                     }
145 
146                     return null;
147                 }
148             } );
149 
150             final DateBean bean = new DateBean();
151             interpolator.addValueSource( new ObjectBasedValueSource( bean ) );
152 
153             reader = ReaderFactory.newXmlReader( docDescriptor );
154 
155             final String interpolatedDoc = interpolator.interpolate( IOUtil.toString( reader ) );
156 
157             reader.close();
158             reader = null;
159 
160             if ( log != null && log.isDebugEnabled() )
161             {
162                 log.debug( "Interpolated document descriptor ("
163                                + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc );
164             }
165 
166             // No Strict
167             return new DocumentXpp3Reader().read( new StringReader( interpolatedDoc ), false );
168         }
169         catch ( InterpolationException e )
170         {
171             final IOException io = new IOException( "Error interpolating document descriptor" );
172             io.initCause( e );
173             throw io;
174         }
175         finally
176         {
177             IOUtil.close( reader );
178         }
179     }
180 }