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  
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  
46  /**
47   * Read and filter a DocumentModel from a document descriptor file.
48   *
49   * @author ltheussl
50   * @version $Id: DocumentDescriptorReader.java 787005 2009-06-21 12:54:41Z ltheussl $
51   */
52  public class DocumentDescriptorReader
53  {
54      /** A MavenProject to extract additional info. */
55      private final MavenProject project;
56  
57      /** Used to log the interpolated document descriptor. */
58      private final Log log;
59  
60      /**
61       * Constructor.
62       */
63      public DocumentDescriptorReader()
64      {
65          this( null, null );
66      }
67  
68      /**
69       * Constructor.
70       *
71       * @param project may be null.
72       */
73      public DocumentDescriptorReader( final MavenProject project )
74      {
75          this( project, null );
76      }
77  
78      /**
79       * Constructor.
80       *
81       * @param project may be null.
82       * @param log may be null.
83       */
84      public DocumentDescriptorReader( final MavenProject project, final Log log )
85      {
86          this.project = project;
87          this.log = log;
88      }
89  
90      /**
91       * Read and filter the <code>docDescriptor</code> file.
92       *
93       * @param docDescriptor not null.
94       * @return a DocumentModel instance.
95       * @throws XmlPullParserException if an error occurs during parsing.
96       * @throws IOException if an error occurs during reading.
97       */
98      public DocumentModel readAndFilterDocumentDescriptor( final File docDescriptor )
99          throws XmlPullParserException, IOException
100     {
101         Reader reader = null;
102         try
103         {
104             // System properties
105             final Properties filterProperties = System.getProperties();
106             // Project properties
107             if ( project != null && project.getProperties() != null )
108             {
109                 filterProperties.putAll( project.getProperties() );
110             }
111 
112             final Interpolator interpolator = new RegexBasedInterpolator();
113             interpolator.addValueSource( new MapBasedValueSource( filterProperties ) );
114             interpolator.addValueSource( new EnvarBasedValueSource() );
115             interpolator.addValueSource( new ObjectBasedValueSource( project )
116             {
117                 /** {@inheritDoc} */
118                 public Object getValue( final String expression )
119                 {
120                     try
121                     {
122                         return ReflectionValueExtractor.evaluate( expression, project );
123                     }
124                     catch ( Exception e )
125                     {
126                         addFeedback( "Failed to extract \'" + expression + "\' from: " + project, e );
127                     }
128 
129                     return null;
130                 }
131             } );
132 
133             final DateBean bean = new DateBean();
134             interpolator.addValueSource( new ObjectBasedValueSource( bean ) );
135 
136             reader = ReaderFactory.newXmlReader( docDescriptor );
137 
138             final String interpolatedDoc = interpolator.interpolate( IOUtil.toString( reader ) );
139 
140             if ( log != null && log.isDebugEnabled() )
141             {
142                 log.debug( "Interpolated document descriptor ("
143                         + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc );
144             }
145 
146             // No Strict
147             return new DocumentXpp3Reader().read( new StringReader( interpolatedDoc ), false );
148         }
149         catch ( InterpolationException e )
150         {
151             final IOException io = new IOException( "Error interpolating document descriptor" );
152             io.initCause( e );
153             throw io;
154         }
155         finally
156         {
157             IOUtil.close( reader );
158         }
159     }
160 }