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