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.HashSet;
26  import java.util.List;
27  import java.util.Set;
28  
29  import javax.swing.text.html.HTML.Attribute;
30  
31  import org.apache.maven.doxia.module.xhtml.XhtmlSink;
32  import org.apache.maven.doxia.sink.render.RenderingContext;
33  import org.apache.maven.doxia.sink.Sink;
34  import org.apache.maven.doxia.sink.SinkEventAttributes;
35  import org.apache.maven.doxia.util.HtmlTools;
36  
37  import org.codehaus.plexus.util.StringUtils;
38  
39  /**
40   * Sink for site rendering.
41   *
42   * @author <a href="mailto:evenisse@codehaus.org">Emmanuel Venisse</a>
43   * @version $Id: SiteRendererSink.java 1087124 2011-03-30 22:45:41Z hboutemy $
44   */
45  public class SiteRendererSink
46      extends XhtmlSink
47      implements Sink, org.codehaus.doxia.sink.Sink
48  {
49      private String date = "";
50  
51      private String title = "";
52  
53      private List<String> authors = new ArrayList<String>();
54  
55      private final StringWriter headWriter;
56  
57      private StringBuffer sectionTitleBuffer;
58  
59      private boolean sectionHasID;
60  
61      private boolean sectionTitle;
62  
63      private Set<String> anchorsInSectionTitle;
64  
65      private final Writer writer;
66  
67      private RenderingContext renderingContext;
68  
69      /**
70       * Construct a new SiteRendererSink.
71       *
72       * @param renderingContext the RenderingContext.
73       */
74      public SiteRendererSink( RenderingContext renderingContext )
75      {
76          this( new StringWriter(), renderingContext );
77      }
78  
79      /**
80       * Construct a new SiteRendererSink.
81       *
82       * @param writer the writer for the sink.
83       * @param renderingContext the RenderingContext.
84       */
85      private SiteRendererSink( StringWriter writer, RenderingContext renderingContext )
86      {
87          super( writer );
88  
89          this.writer = writer;
90          this.headWriter = new StringWriter();
91          this.renderingContext = renderingContext;
92      }
93  
94      /** {@inheritDoc} */
95      public void title_()
96      {
97          if ( getTextBuffer().length() > 0 )
98          {
99              title = getTextBuffer().toString();
100         }
101 
102         resetTextBuffer();
103     }
104 
105     /**
106      * {@inheritDoc}
107      *
108      * Do nothing.
109      * @see org.apache.maven.doxia.module.xhtml.XhtmlSink#title()
110      */
111     public void title()
112     {
113         // nop
114     }
115 
116     /**
117      * <p>Getter for the field <code>title</code>.</p>
118      *
119      * @return a {@link java.lang.String} object.
120      */
121     public String getTitle()
122     {
123         return title;
124     }
125 
126     /** {@inheritDoc} */
127     public void author_()
128     {
129         if ( getTextBuffer().length() > 0 )
130         {
131             String text = HtmlTools.escapeHTML( getTextBuffer().toString() );
132             text = StringUtils.replace( text, "&amp;#", "&#" );
133             authors.add( text.trim() );
134         }
135 
136         resetTextBuffer();
137     }
138 
139     /**
140      * <p>Getter for the field <code>authors</code>.</p>
141      *
142      * @return a {@link java.util.List} object.
143      */
144     public List<String> getAuthors()
145     {
146         return authors;
147     }
148 
149     /** {@inheritDoc} */
150     public void date_()
151     {
152         if ( getTextBuffer().length() > 0 )
153         {
154             date = getTextBuffer().toString().trim();
155         }
156 
157         resetTextBuffer();
158     }
159 
160     /**
161      * <p>Getter for the field <code>date</code>.</p>
162      *
163      * @return a {@link java.lang.String} object.
164      */
165     public String getDate()
166     {
167         return date;
168     }
169 
170     /**
171      * {@inheritDoc}
172      *
173      * Do nothing.
174      * @see org.apache.maven.doxia.module.xhtml.XhtmlSink#body_()
175      */
176     public void body_()
177     {
178         // nop
179     }
180 
181     /**
182      * {@inheritDoc}
183      *
184      * Do nothing.
185      * @see org.apache.maven.doxia.module.xhtml.XhtmlSink#body()
186      */
187     public void body()
188     {
189         // nop
190     }
191 
192     /**
193      * <p>getBody.</p>
194      *
195      * @return a {@link java.lang.String} object.
196      */
197     public String getBody()
198     {
199         return writer.toString();
200     }
201 
202     /**
203      * <p>getHead.</p>
204      *
205      * @return a {@link java.lang.String} object.
206      *
207      * @since 1.1.1
208      */
209     public String getHead()
210     {
211         return headWriter.toString();
212     }
213 
214     /** {@inheritDoc} */
215     public void head_()
216     {
217         setHeadFlag( false );
218     }
219 
220     /** {@inheritDoc} */
221     public void head()
222     {
223         setHeadFlag( true );
224     }
225 
226     /** {@inheritDoc} */
227     public void anchor( String name, SinkEventAttributes attributes )
228     {
229         super.anchor( name, attributes );
230         if ( sectionTitle )
231         {
232             if ( anchorsInSectionTitle == null )
233             {
234                 anchorsInSectionTitle = new HashSet<String>();
235             }
236             anchorsInSectionTitle.add( name );
237         }
238     }
239     
240     /** {@inheritDoc} */
241     protected void onSectionTitle( int depth, SinkEventAttributes attributes )
242     {
243         this.sectionTitleBuffer = new StringBuffer();
244         sectionHasID = ( attributes != null && attributes.isDefined ( Attribute.ID.toString() ) );
245         sectionTitle = true;
246 
247         super.onSectionTitle( depth, attributes );
248     }
249 
250     /** {@inheritDoc} */
251     protected void onSectionTitle_( int depth )
252     {
253         String sectionTitle = sectionTitleBuffer.toString();
254         this.sectionTitleBuffer = null;
255 
256         if ( !sectionHasID && !StringUtils.isEmpty( sectionTitle ) )
257         {
258             String id = HtmlTools.encodeId( sectionTitle );
259             if ( ( anchorsInSectionTitle == null ) || (! anchorsInSectionTitle.contains( id ) ) )
260             {
261                 anchor( id );
262                 anchor_();
263             }
264         }
265         else
266         {
267             sectionHasID = false;
268         }
269 
270         this.sectionTitle = false;
271         anchorsInSectionTitle = null;
272         super.onSectionTitle_( depth );
273     }
274 
275     /**
276      * <p>Getter for the field <code>renderingContext</code>.</p>
277      *
278      * @return the current rendering context
279      * @since 1.1
280      */
281     public RenderingContext getRenderingContext()
282     {
283         return renderingContext;
284     }
285 
286     /** {@inheritDoc} */
287     public void text( String text )
288     {
289         if ( sectionTitleBuffer != null )
290         {
291             // this implies we're inside a section title, collect text events for anchor generation
292             sectionTitleBuffer.append( text );
293         }
294 
295         super.text( text );
296     }
297 
298     /** {@inheritDoc} */
299     protected void write( String text )
300     {
301         if ( isHeadFlag() )
302         {
303             headWriter.write( unifyEOLs( text ) );
304 
305             return;
306         }
307 
308         if ( renderingContext != null )
309         {
310             String relativePathToBasedir = renderingContext.getRelativePath();
311 
312             if ( relativePathToBasedir == null )
313             {
314                 text = StringUtils.replace( text, "$relativePath", "." );
315             }
316             else
317             {
318                 text = StringUtils.replace( text, "$relativePath", relativePathToBasedir );
319             }
320         }
321 
322         super.write( text );
323     }
324 }