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 * 32 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> 33 * @version $Id$ 34 * @since 1.5 (was since 1.1 in o.a.m.d.sink.render) 35 */ 36 public class RenderingContext 37 { 38 private final File basedir; 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 /** 53 * <p> 54 * Constructor for RenderingContext when document is not rendered from a Doxia source. 55 * </p> 56 * 57 * @param basedir the pseudo-source base directory. 58 * @param document the pseudo-source document name: will be used to compute output name (same name with extension 59 * replaced with <code>.html</code>). 60 */ 61 public RenderingContext( File basedir, String document ) 62 { 63 this( basedir, document, null, null ); 64 } 65 66 /** 67 * <p> 68 * Constructor for RenderingContext. 69 * </p> 70 * 71 * @param basedir the source base directory. 72 * @param document the source document name. 73 * @param parserId the Doxia module parser id associated to this document, may be null if document not rendered from 74 * a Doxia source. 75 * @param extension the source document filename extension. 76 */ 77 public RenderingContext( File basedir, String document, String parserId, String extension ) 78 { 79 this.basedir = basedir; 80 this.extension = extension; 81 this.inputName = document; 82 this.parserId = parserId; 83 this.attributes = new HashMap<String, String>(); 84 85 if ( StringUtils.isNotEmpty( extension ) ) 86 { 87 // here we now the parserId we can play with this 88 // index.xml -> index.html 89 // index.xml.vm -> index.html 90 // download.apt.vm --> download.html 91 if ( DefaultSiteRenderer.endsWithIgnoreCase( document, ".vm" ) ) 92 { 93 document = document.substring( 0, document.length() - 3 ); 94 } 95 String fileNameWithoutExt = document.substring( 0, document.length() - extension.length() - 1 ); 96 this.outputName = fileNameWithoutExt + ".html"; 97 } 98 else 99 { 100 this.outputName = document.substring( 0, document.lastIndexOf( '.' ) ).replace( '\\', '/' ) + ".html"; 101 } 102 this.relativePath = PathTool.getRelativePath( basedir.getPath(), new File( basedir, inputName ).getPath() ); 103 } 104 105 /** 106 * <p>Getter for the field <code>basedir</code>.</p> 107 * 108 * @return a {@link java.io.File} object. 109 */ 110 public File getBasedir() 111 { 112 return basedir; 113 } 114 115 /** 116 * <p>Getter for the field <code>inputName</code>.</p> 117 * 118 * @return a {@link java.lang.String} object. 119 */ 120 public String getInputName() 121 { 122 return inputName; 123 } 124 125 /** 126 * <p>Getter for the field <code>outputName</code>.</p> 127 * 128 * @return a {@link java.lang.String} object. 129 */ 130 public String getOutputName() 131 { 132 return outputName; 133 } 134 135 /** 136 * <p>Getter for the field <code>parserId</code>.</p> 137 * 138 * @return a {@link java.lang.String} object. 139 */ 140 public String getParserId() 141 { 142 return parserId; 143 } 144 145 /** 146 * <p>Getter for the field <code>relativePath</code>.</p> 147 * 148 * @return a {@link java.lang.String} object. 149 */ 150 public String getRelativePath() 151 { 152 return relativePath; 153 } 154 155 /** 156 * <p>setAttribute.</p> 157 * 158 * @param key a {@link java.lang.String} object. 159 * @param value a {@link java.lang.String} object. 160 */ 161 public void setAttribute( String key, String value ) 162 { 163 attributes.put( key, value ); 164 } 165 166 /** 167 * <p>getAttribute.</p> 168 * 169 * @param key a {@link java.lang.String} object. 170 * @return a {@link java.lang.String} object. 171 */ 172 public String getAttribute( String key ) 173 { 174 return attributes.get( key ); 175 } 176 177 /** 178 * <p>Getter for the field <code>extension</code>.</p> 179 * 180 * @return a {@link java.lang.String} object. 181 */ 182 public String getExtension() 183 { 184 return extension; 185 } 186 }