Coverage Report - org.apache.maven.plugins.pdf.DocumentDescriptorReader
 
Classes in this File Line Coverage Branch Coverage Complexity
DocumentDescriptorReader
73 %
28/38
41 %
5/12
3,2
DocumentDescriptorReader$1
40 %
2/5
N/A
3,2
 
 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  6
 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  1
         this( null, null, null );
 69  1
     }
 70  
 
 71  
     /**
 72  
      * Constructor.
 73  
      *
 74  
      * @param project may be null.
 75  
      */
 76  
     public DocumentDescriptorReader( final MavenProject project )
 77  
     {
 78  1
         this( project, null, null );
 79  1
     }
 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  2
     {
 89  2
         this.project = project;
 90  2
         this.log = log;
 91  2
         this.locale = locale;
 92  2
     }
 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  2
         if ( locale != null )
 106  
         {
 107  0
             String descriptorFilename = docDescriptor.getName();
 108  0
             String localized = FileUtils.removeExtension( descriptorFilename ) + '_' + locale.getLanguage() + '.'
 109  
                 + FileUtils.getExtension( descriptorFilename );
 110  0
             File localizedDocDescriptor = new File( docDescriptor.getParentFile(), localized );
 111  
 
 112  0
             if ( localizedDocDescriptor.exists() )
 113  
             {
 114  0
                 docDescriptor = localizedDocDescriptor;
 115  
             }
 116  
         }
 117  
 
 118  2
         Reader reader = null;
 119  
         try
 120  
         {
 121  
             // System properties
 122  2
             final Properties filterProperties = System.getProperties();
 123  
             // Project properties
 124  2
             if ( project != null && project.getProperties() != null )
 125  
             {
 126  1
                 filterProperties.putAll( project.getProperties() );
 127  
             }
 128  
 
 129  2
             final Interpolator interpolator = new RegexBasedInterpolator();
 130  2
             interpolator.addValueSource( new MapBasedValueSource( filterProperties ) );
 131  2
             interpolator.addValueSource( new EnvarBasedValueSource() );
 132  2
             interpolator.addValueSource( new ObjectBasedValueSource( project )
 133  2
             {
 134  
                 /** {@inheritDoc} */
 135  
                 public Object getValue( final String expression )
 136  
                 {
 137  
                     try
 138  
                     {
 139  6
                         return ReflectionValueExtractor.evaluate( expression, project );
 140  
                     }
 141  0
                     catch ( Exception e )
 142  
                     {
 143  0
                         addFeedback( "Failed to extract \'" + expression + "\' from: " + project, e );
 144  
                     }
 145  
 
 146  0
                     return null;
 147  
                 }
 148  
             } );
 149  
 
 150  2
             final DateBean bean = new DateBean();
 151  2
             interpolator.addValueSource( new ObjectBasedValueSource( bean ) );
 152  
 
 153  2
             reader = ReaderFactory.newXmlReader( docDescriptor );
 154  
 
 155  2
             final String interpolatedDoc = interpolator.interpolate( IOUtil.toString( reader ) );
 156  
 
 157  2
             reader.close();
 158  2
             reader = null;
 159  
 
 160  2
             if ( log != null && log.isDebugEnabled() )
 161  
             {
 162  0
                 log.debug( "Interpolated document descriptor ("
 163  
                                + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc );
 164  
             }
 165  
 
 166  
             // No Strict
 167  2
             return new DocumentXpp3Reader().read( new StringReader( interpolatedDoc ), false );
 168  
         }
 169  0
         catch ( InterpolationException e )
 170  
         {
 171  0
             final IOException io = new IOException( "Error interpolating document descriptor" );
 172  0
             io.initCause( e );
 173  0
             throw io;
 174  
         }
 175  
         finally
 176  
         {
 177  2
             IOUtil.close( reader );
 178  
         }
 179  
     }
 180  
 }