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