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