Coverage Report - org.apache.maven.plugins.pdf.DocumentDescriptorReader
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentDescriptorReader
83%
24/29
75%
6/8
2.4
DocumentDescriptorReader$1
40%
2/5
N/A
2.4
 
 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  12
 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  1
         this( null, null );
 66  1
     }
 67  
 
 68  
     /**
 69  
      * Constructor.
 70  
      *
 71  
      * @param project may be null.
 72  
      */
 73  
     public DocumentDescriptorReader( final MavenProject project )
 74  
     {
 75  1
         this( project, null );
 76  1
     }
 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  5
     {
 86  5
         this.project = project;
 87  5
         this.log = log;
 88  5
     }
 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  5
         Reader reader = null;
 102  
         try
 103  
         {
 104  
             // System properties
 105  5
             final Properties filterProperties = System.getProperties();
 106  
             // Project properties
 107  5
             if ( project != null && project.getProperties() != null )
 108  
             {
 109  4
                 filterProperties.putAll( project.getProperties() );
 110  
             }
 111  
 
 112  5
             final Interpolator interpolator = new RegexBasedInterpolator();
 113  5
             interpolator.addValueSource( new MapBasedValueSource( filterProperties ) );
 114  5
             interpolator.addValueSource( new EnvarBasedValueSource() );
 115  5
             interpolator.addValueSource( new ObjectBasedValueSource( project )
 116  
             {
 117  
                 /** {@inheritDoc} */
 118  5
                 public Object getValue( final String expression )
 119  
                 {
 120  
                     try
 121  
                     {
 122  12
                         return ReflectionValueExtractor.evaluate( expression, project );
 123  
                     }
 124  0
                     catch ( Exception e )
 125  
                     {
 126  0
                         addFeedback( "Failed to extract \'" + expression + "\' from: " + project, e );
 127  
                     }
 128  
 
 129  0
                     return null;
 130  
                 }
 131  
             } );
 132  
 
 133  5
             final DateBean bean = new DateBean();
 134  5
             interpolator.addValueSource( new ObjectBasedValueSource( bean ) );
 135  
 
 136  5
             reader = ReaderFactory.newXmlReader( docDescriptor );
 137  
 
 138  5
             final String interpolatedDoc = interpolator.interpolate( IOUtil.toString( reader ) );
 139  
 
 140  5
             if ( log != null && log.isDebugEnabled() )
 141  
             {
 142  0
                 log.debug( "Interpolated document descriptor ("
 143  
                         + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc );
 144  
             }
 145  
 
 146  
             // No Strict
 147  5
             return new DocumentXpp3Reader().read( new StringReader( interpolatedDoc ), false );
 148  
         }
 149  0
         catch ( InterpolationException e )
 150  
         {
 151  0
             final IOException io = new IOException( "Error interpolating document descriptor" );
 152  0
             io.initCause( e );
 153  0
             throw io;
 154  
         }
 155  
         finally
 156  
         {
 157  5
             IOUtil.close( reader );
 158  
         }
 159  
     }
 160  
 }