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 inputName; 40 41 private final String outputName; 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 name: will be used to compute output name (same name 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 name. 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.inputName = 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 fileNameWithoutExt = document.substring(0, document.length() - extension.length() - 1); 126 this.outputName = fileNameWithoutExt + ".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.outputName = document + ".html"; 132 } 133 134 this.relativePath = PathTool.getRelativePath(basedir.getPath(), new File(basedir, inputName).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>inputName</code>.</p> 149 * 150 * @return a {@link java.lang.String} object. 151 */ 152 public String getInputName() { 153 return inputName; 154 } 155 156 /** 157 * Get html output name, relative to site root. 158 * 159 * @return html output name 160 * @see PathTool#getRelativePath(String) 161 */ 162 public String getOutputName() { 163 return outputName; 164 } 165 166 /** 167 * Get the parserId when document comes from a Doxia source. 168 * 169 * @return parser id, or <code>null</code> if not froma DOxia source. 170 */ 171 public String getParserId() { 172 return parserId; 173 } 174 175 /** 176 * Get the relative path to site root. 177 * 178 * @return the relative path to site root 179 */ 180 public String getRelativePath() { 181 return relativePath; 182 } 183 184 /** 185 * <p>setAttribute.</p> 186 * 187 * @param key a {@link java.lang.String} object. 188 * @param value a {@link java.lang.String} object. 189 */ 190 public void setAttribute(String key, String value) { 191 attributes.put(key, value); 192 } 193 194 /** 195 * <p>getAttribute.</p> 196 * 197 * @param key a {@link java.lang.String} object. 198 * @return a {@link java.lang.String} object. 199 */ 200 public String getAttribute(String key) { 201 return attributes.get(key); 202 } 203 204 /** 205 * Get the source document filename extension (when a Doxia source) 206 * 207 * @return the source document filename extension when a Doxia source, or <code>null</code> if not a Doxia source 208 */ 209 public String getExtension() { 210 return extension; 211 } 212 213 /** 214 * Is the source document editable? 215 * 216 * @return <code>true</code> if comes from an editable Doxia source (not generated one). 217 * @since 1.8 218 */ 219 public boolean isEditable() { 220 return editable; 221 } 222 223 /** 224 * Is the document rendered from a Doxia source? 225 * 226 * @return <code>true</code> if comes from a Doxia source. 227 * @since 1.8 228 */ 229 public boolean isDoxiaSource() { 230 return extension != null && !extension.isEmpty(); 231 } 232 233 /** 234 * What is the generator (if any)? 235 * 236 * @return <code>null</code> if no known generator 237 * @since 1.8 238 */ 239 public String getGenerator() { 240 return generator; 241 } 242 243 /** 244 * Get the relative path of basedir (when a Doxia source) 245 * 246 * @return the relative path of basedir when a Doxia source, or <code>null</code> if not a Doxia source 247 * @since 1.8 248 */ 249 public String getBasedirRelativePath() { 250 return basedirRelativePath; 251 } 252 253 /** 254 * Get the relative path to Doxia source from build root. 255 * 256 * @return the relative path to Doxia source from build root, or <code>null</code> if not a Doxia source 257 * @since 1.8 258 */ 259 public String getDoxiaSourcePath() { 260 return isDoxiaSource() ? (basedirRelativePath + '/' + inputName) : null; 261 } 262 263 /** 264 * Get url of the Doxia source calculate from given base url. 265 * 266 * @param base the base url to use 267 * @return the resulting url 268 * @since 1.8 269 */ 270 public String getDoxiaSourcePath(String base) { 271 return PathTool.calculateLink(getDoxiaSourcePath(), base); 272 } 273 }