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