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.sink;
20  
21  import java.io.StringWriter;
22  import java.io.Writer;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.maven.doxia.markup.HtmlMarkup;
27  import org.apache.maven.doxia.module.xhtml5.Xhtml5Sink;
28  import org.apache.maven.doxia.siterenderer.DocumentContent;
29  import org.apache.maven.doxia.siterenderer.DocumentRenderingContext;
30  import org.codehaus.plexus.util.StringUtils;
31  
32  /**
33   * Sink for site rendering of a document, to allow later merge document's output with a template.
34   * During raw Doxia rendering, content is stored in multiple fields for later use when incorporating
35   * into skin or template: title, date, authors, head, body
36   *
37   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
38   */
39  @SuppressWarnings("checkstyle:methodname")
40  public class SiteRendererSink extends Xhtml5Sink implements DocumentContent {
41      private String date;
42  
43      private String title;
44  
45      private List<String> authors = new ArrayList<>();
46  
47      private final StringWriter headWriter;
48  
49      private final Writer writer;
50  
51      private DocumentRenderingContext docRenderingContext;
52  
53      /**
54       * Construct a new SiteRendererSink for a document.
55       *
56       * @param docRenderingContext the document's rendering context.
57       */
58      public SiteRendererSink(DocumentRenderingContext docRenderingContext) {
59          this(new StringWriter(), docRenderingContext);
60      }
61  
62      /**
63       * Construct a new SiteRendererSink for a document.
64       *
65       * @param writer the writer for the sink.
66       * @param docRenderingContext the document's rendering context.
67       */
68      private SiteRendererSink(StringWriter writer, DocumentRenderingContext docRenderingContext) {
69          super(writer);
70  
71          this.writer = writer;
72          this.headWriter = new StringWriter();
73          this.docRenderingContext = docRenderingContext;
74  
75          /* the template is expected to have used the main tag, which can be used only once */
76          super.contentStack.push(HtmlMarkup.MAIN);
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public void title_() {
82          if (getTextBuffer().length() > 0) {
83              title = getTextBuffer().toString();
84          }
85  
86          resetTextBuffer();
87      }
88  
89      /**
90       * {@inheritDoc}
91       *
92       * Reset text buffer, since text content before title must not be in title.
93       * @see org.apache.maven.doxia.module.xhtml5.Xhtml5Sink#title()
94       */
95      @Override
96      public void title() {
97          resetTextBuffer();
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public void author() {
103         resetTextBuffer();
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public void author_() {
109         if (getTextBuffer().length() > 0) {
110             String text = getTextBuffer().toString().trim();
111             authors.add(text);
112         }
113 
114         resetTextBuffer();
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public void date() {
120         resetTextBuffer();
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public void date_() {
126         if (getTextBuffer().length() > 0) {
127             date = getTextBuffer().toString().trim();
128         }
129 
130         resetTextBuffer();
131     }
132 
133     /**
134      * {@inheritDoc}
135      *
136      * Do nothing.
137      * @see org.apache.maven.doxia.module.xhtml5.Xhtml5Sink#body_()
138      */
139     @Override
140     public void body_() {
141         // nop
142     }
143 
144     /**
145      * {@inheritDoc}
146      *
147      * Do nothing.
148      * @see org.apache.maven.doxia.module.xhtml5.Xhtml5Sink#body()
149      */
150     @Override
151     public void body() {
152         // nop
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public void head_() {
158         setHeadFlag(false);
159     }
160 
161     /** {@inheritDoc} */
162     @Override
163     public void head() {
164         setHeadFlag(true);
165     }
166 
167     /** {@inheritDoc} */
168     @Override
169     protected void write(String text) {
170         String txt = text;
171 
172         if (isHeadFlag()) {
173             headWriter.write(unifyEOLs(txt));
174 
175             return;
176         }
177 
178         if (docRenderingContext != null) {
179             String relativePathToBasedir = docRenderingContext.getRelativePath();
180 
181             if (relativePathToBasedir == null) {
182                 txt = StringUtils.replace(txt, "$relativePath", ".");
183             } else {
184                 txt = StringUtils.replace(txt, "$relativePath", relativePathToBasedir);
185             }
186         }
187 
188         super.write(txt);
189     }
190 
191     // DocumentContent interface
192 
193     /** {@inheritDoc} */
194     public String getTitle() {
195         return title;
196     }
197 
198     /** {@inheritDoc} */
199     public List<String> getAuthors() {
200         return authors;
201     }
202 
203     /** {@inheritDoc} */
204     public String getDate() {
205         return date;
206     }
207 
208     /** {@inheritDoc} */
209     public String getBody() {
210         String body = writer.toString();
211 
212         return body.length() > 0 ? body : null;
213     }
214 
215     /** {@inheritDoc} */
216     public String getHead() {
217         String head = headWriter.toString();
218 
219         return head.length() > 0 ? head : null;
220     }
221 
222     /** {@inheritDoc} */
223     public DocumentRenderingContext getRenderingContext() {
224         return docRenderingContext;
225     }
226 }