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