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