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 | |
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 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 6 | public class DocumentDescriptorReader |
54 | |
{ |
55 | |
|
56 | |
private final MavenProject project; |
57 | |
|
58 | |
|
59 | |
private final Log log; |
60 | |
|
61 | |
private final Locale locale; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
public DocumentDescriptorReader() |
67 | |
{ |
68 | 1 | this( null, null, null ); |
69 | 1 | } |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public DocumentDescriptorReader( final MavenProject project ) |
77 | |
{ |
78 | 1 | this( project, null, null ); |
79 | 1 | } |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
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 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
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 | |
|
122 | 2 | final Properties filterProperties = System.getProperties(); |
123 | |
|
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 | |
|
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 | |
|
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 | |
} |