1 | |
package org.apache.maven.plugins.pdf; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | 12 | public class DocumentDescriptorReader |
53 | |
{ |
54 | |
|
55 | |
private final MavenProject project; |
56 | |
|
57 | |
|
58 | |
private final Log log; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public DocumentDescriptorReader() |
64 | |
{ |
65 | 1 | this( null, null ); |
66 | 1 | } |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public DocumentDescriptorReader( final MavenProject project ) |
74 | |
{ |
75 | 1 | this( project, null ); |
76 | 1 | } |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
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 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public DocumentModel readAndFilterDocumentDescriptor( final File docDescriptor ) |
99 | |
throws XmlPullParserException, IOException |
100 | |
{ |
101 | 5 | Reader reader = null; |
102 | |
try |
103 | |
{ |
104 | |
|
105 | 5 | final Properties filterProperties = System.getProperties(); |
106 | |
|
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 | |
|
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 | |
|
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 | |
} |