1 package org.apache.maven.doxia.siterenderer;
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.util.HashMap;
24 import java.util.Map;
25
26 import org.codehaus.plexus.util.PathTool;
27 import org.codehaus.plexus.util.StringUtils;
28
29 /**
30 * The rendering context of a document.
31 * If not rendered from a Doxia markup source, parserId and extension will be null.
32 *
33 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
34 * @since 1.5 (was since 1.1 in o.a.m.d.sink.render)
35 */
36 public class RenderingContext // TODO rename to DocumentRenderingContext
37 {
38 private final File basedir;
39
40 private final String basedirRelativePath;
41
42 private final String inputName;
43
44 private final String outputName;
45
46 private final String parserId;
47
48 private final String relativePath;
49
50 private final String extension;
51
52 private Map<String, String> attributes;
53
54 private final boolean editable;
55
56 private final String generator;
57
58 @Deprecated
59 public RenderingContext( File basedir, String document )
60 {
61 this( basedir, null, document, null, null, false, null );
62 }
63
64 /**
65 * <p>
66 * Constructor for RenderingContext when document is not rendered from a Doxia markup source.
67 * </p>
68 *
69 * @param basedir the pseudo-source base directory.
70 * @param document the pseudo-source document name: will be used to compute output name (same name with extension
71 * replaced with <code>.html</code>).
72 * @param generator the generator (in general a reporting goal: <code>groupId:artifactId:version:goal</code>)
73 * @since 1.8
74 */
75 public RenderingContext( File basedir, String document, String generator )
76 {
77 this( basedir, null, document, null, null, false, generator );
78 }
79
80 @Deprecated
81 public RenderingContext( File basedir, String document, String parserId, String extension )
82 {
83 this( basedir, null, document, parserId, extension, false, null );
84 }
85
86 public RenderingContext( File basedir, String basedirRelativePath, String document, String parserId,
87 String extension, boolean editable )
88 {
89 this( basedir, basedirRelativePath, document, parserId, extension, editable, null );
90 }
91
92 /**
93 * <p>
94 * Constructor for document RenderingContext.
95 * </p>
96 *
97 * @param basedir the source base directory (not null, pseudo value when not a Doxia source).
98 * @param basedirRelativePath the relative path from root (null if not Doxia source)
99 * @param document the source document name.
100 * @param parserId the Doxia module parser id associated to this document, may be null if document not rendered from
101 * a Doxia source.
102 * @param extension the source document filename extension, may be null if document not rendered from
103 * a Doxia source.
104 * @param editable is the document editable as source, i.e. not generated?
105 * @param generator the generator (in general a reporting goal: <code>groupId:artifactId:version:goal</code>)
106 * @since 1.8
107 */
108 public RenderingContext( File basedir, String basedirRelativePath, String document, String parserId,
109 String extension, boolean editable, String generator )
110 {
111 this.basedir = basedir;
112 this.basedirRelativePath = basedirRelativePath;
113 this.inputName = document;
114 this.parserId = parserId;
115 this.extension = extension;
116 this.generator = generator;
117 this.attributes = new HashMap<String, String>();
118
119 if ( StringUtils.isNotEmpty( extension ) )
120 {
121 // document comes from a Doxia source: see DoxiaDocumentRenderer
122 this.editable = editable;
123
124 // here we know the parserId and extension, we can play with this to get output name from document:
125 // - index.xml -> index.html
126 // - index.xml.vm -> index.html
127 // - download.apt.vm --> download.html
128 if ( DefaultSiteRenderer.endsWithIgnoreCase( document, ".vm" ) )
129 {
130 document = document.substring( 0, document.length() - 3 );
131 }
132 String fileNameWithoutExt = document.substring( 0, document.length() - extension.length() - 1 );
133 this.outputName = fileNameWithoutExt + ".html";
134 }
135 else
136 {
137 // document does not come from a Doxia source but direct Sink API
138 this.editable = false;
139 // make sure output name ends in .html
140 this.outputName = document.substring( 0, document.lastIndexOf( '.' ) ).replace( '\\', '/' ) + ".html";
141 }
142
143 this.relativePath = PathTool.getRelativePath( basedir.getPath(), new File( basedir, inputName ).getPath() );
144 }
145
146 /**
147 * <p>Getter for the field <code>basedir</code>.</p>
148 *
149 * @return a {@link java.io.File} object.
150 */
151 public File getBasedir()
152 {
153 return basedir;
154 }
155
156 /**
157 * <p>Getter for the field <code>inputName</code>.</p>
158 *
159 * @return a {@link java.lang.String} object.
160 */
161 public String getInputName()
162 {
163 return inputName;
164 }
165
166 /**
167 * Get html output name, relative to site root.
168 *
169 * @return html output name
170 * @see PathTool#getRelativePath(String)
171 */
172 public String getOutputName()
173 {
174 return outputName;
175 }
176
177 /**
178 * Get the parserId when document comes from a Doxia source.
179 *
180 * @return parser id, or <code>null</code> if not froma DOxia source.
181 */
182 public String getParserId()
183 {
184 return parserId;
185 }
186
187 /**
188 * Get the relative path to site root.
189 *
190 * @return the relative path to site root
191 */
192 public String getRelativePath()
193 {
194 return relativePath;
195 }
196
197 /**
198 * <p>setAttribute.</p>
199 *
200 * @param key a {@link java.lang.String} object.
201 * @param value a {@link java.lang.String} object.
202 */
203 public void setAttribute( String key, String value )
204 {
205 attributes.put( key, value );
206 }
207
208 /**
209 * <p>getAttribute.</p>
210 *
211 * @param key a {@link java.lang.String} object.
212 * @return a {@link java.lang.String} object.
213 */
214 public String getAttribute( String key )
215 {
216 return attributes.get( key );
217 }
218
219 /**
220 * Get the source document filename extension (when a Doxia source)
221 *
222 * @return the source document filename extension when a Doxia source, or <code>null</code> if not a Doxia source
223 */
224 public String getExtension()
225 {
226 return extension;
227 }
228
229 /**
230 * Is the source document editable?
231 *
232 * @return <code>true</code> if comes from an editable Doxia source (not generated one).
233 * @since 1.8
234 */
235 public boolean isEditable()
236 {
237 return editable;
238 }
239
240 /**
241 * Is the document rendered from a Doxia source?
242 *
243 * @return <code>true</code> if comes from a Doxia source.
244 * @since 1.8
245 */
246 public boolean isDoxiaSource()
247 {
248 return StringUtils.isNotEmpty( extension );
249 }
250
251 /**
252 * What is the generator (if any)?
253 *
254 * @return <code>null</code> if no known generator
255 * @since 1.8
256 */
257 public String getGenerator()
258 {
259 return generator;
260 }
261
262 /**
263 * Get the relative path of basedir (when a Doxia source)
264 *
265 * @return the relative path of basedir when a Doxia source, or <code>null</code> if not a Doxia source
266 * @since 1.8
267 */
268 public String getBasedirRelativePath()
269 {
270 return basedirRelativePath;
271 }
272
273 /**
274 * Get the relative path to Doxia source from build root.
275 *
276 * @return the relative path to Doxia source from build root, or <code>null</code> if not a Doxia source
277 * @since 1.8
278 */
279 public String getDoxiaSourcePath()
280 {
281 return isDoxiaSource() ? ( basedirRelativePath + '/' + inputName ) : null;
282 }
283
284 /**
285 * Get url of the Doxia source calculate from given base url.
286 *
287 * @param base the base url to use
288 * @return the resulting url
289 * @since 1.8
290 */
291 public String getDoxiaSourcePath( String base )
292 {
293 return PathTool.calculateLink( getDoxiaSourcePath(), base );
294 }
295 }