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