View Javadoc
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.basedirRelativePath = basedirRelativePath;
107         this.inputName = document;
108         this.parserId = parserId;
109         this.extension = extension;
110         this.generator = generator;
111         this.attributes = new HashMap<String, String>();
112 
113         if (StringUtils.isNotEmpty(extension)) {
114             // document comes from a Doxia source: see DoxiaDocumentRenderer
115             this.editable = editable;
116 
117             // here we know the parserId and extension, we can play with this to get output name from document:
118             // - index.xml -> index.html
119             // - index.xml.vm -> index.html
120             // - download.apt.vm --> download.html
121             if (DefaultSiteRenderer.endsWithIgnoreCase(document, ".vm")) {
122                 document = document.substring(0, document.length() - 3);
123             }
124             String fileNameWithoutExt = document.substring(0, document.length() - extension.length() - 1);
125             this.outputName = fileNameWithoutExt + ".html";
126         } else {
127             // document does not come from a Doxia source but direct Sink API
128             this.editable = false;
129             // make sure output name ends in .html
130             this.outputName = document.substring(0, document.lastIndexOf('.')).replace('\\', '/') + ".html";
131         }
132 
133         this.relativePath = PathTool.getRelativePath(basedir.getPath(), new File(basedir, inputName).getPath());
134     }
135 
136     /**
137      * <p>Getter for the field <code>basedir</code>.</p>
138      *
139      * @return a {@link java.io.File} object.
140      */
141     public File getBasedir() {
142         return basedir;
143     }
144 
145     /**
146      * <p>Getter for the field <code>inputName</code>.</p>
147      *
148      * @return a {@link java.lang.String} object.
149      */
150     public String getInputName() {
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         return outputName;
162     }
163 
164     /**
165      * Get the parserId when document comes from a Doxia source.
166      *
167      * @return parser id, or <code>null</code> if not froma DOxia source.
168      */
169     public String getParserId() {
170         return parserId;
171     }
172 
173     /**
174      * Get the relative path to site root.
175      *
176      * @return the relative path to site root
177      */
178     public String getRelativePath() {
179         return relativePath;
180     }
181 
182     /**
183      * <p>setAttribute.</p>
184      *
185      * @param key a {@link java.lang.String} object.
186      * @param value a {@link java.lang.String} object.
187      */
188     public void setAttribute(String key, String value) {
189         attributes.put(key, value);
190     }
191 
192     /**
193      * <p>getAttribute.</p>
194      *
195      * @param key a {@link java.lang.String} object.
196      * @return a {@link java.lang.String} object.
197      */
198     public String getAttribute(String key) {
199         return attributes.get(key);
200     }
201 
202     /**
203      * Get the source document filename extension (when a Doxia source)
204      *
205      * @return the source document filename extension when a Doxia source, or <code>null</code> if not a Doxia source
206      */
207     public String getExtension() {
208         return extension;
209     }
210 
211     /**
212      * Is the source document editable?
213      *
214      * @return <code>true</code> if comes from an editable Doxia source (not generated one).
215      * @since 1.8
216      */
217     public boolean isEditable() {
218         return editable;
219     }
220 
221     /**
222      * Is the document rendered from a Doxia source?
223      *
224      * @return <code>true</code> if comes from a Doxia source.
225      * @since 1.8
226      */
227     public boolean isDoxiaSource() {
228         return StringUtils.isNotEmpty(extension);
229     }
230 
231     /**
232      * What is the generator (if any)?
233      *
234      * @return <code>null</code> if no known generator
235      * @since 1.8
236      */
237     public String getGenerator() {
238         return generator;
239     }
240 
241     /**
242      * Get the relative path of basedir (when a Doxia source)
243      *
244      * @return the relative path of basedir when a Doxia source, or <code>null</code> if not a Doxia source
245      * @since 1.8
246      */
247     public String getBasedirRelativePath() {
248         return basedirRelativePath;
249     }
250 
251     /**
252      * Get the relative path to Doxia source from build root.
253      *
254      * @return the relative path to Doxia source from build root, or <code>null</code> if not a Doxia source
255      * @since 1.8
256      */
257     public String getDoxiaSourcePath() {
258         return isDoxiaSource() ? (basedirRelativePath + '/' + inputName) : null;
259     }
260 
261     /**
262      * Get url of the Doxia source calculate from given base url.
263      *
264      * @param base the base url to use
265      * @return the resulting url
266      * @since 1.8
267      */
268     public String getDoxiaSourcePath(String base) {
269         return PathTool.calculateLink(getDoxiaSourcePath(), base);
270     }
271 }