View Javadoc
1   package org.apache.maven.doxia.module.confluence.parser;
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 org.apache.maven.doxia.parser.ParseException;
23  import org.apache.maven.doxia.util.ByLineSource;
24  
25  /**
26   * <p>FigureBlockParser class.</p>
27   *
28   * @since 1.1
29   */
30  public class FigureBlockParser
31      implements BlockParser
32  {
33      /** {@inheritDoc} */
34      public boolean accept( String line, ByLineSource source )
35      {
36          return line.startsWith( "!" ) && line.lastIndexOf( "!" ) > 1;
37      }
38  
39      /** {@inheritDoc} */
40      public Block visit( String line, ByLineSource source )
41          throws ParseException
42      {
43          String image = line.substring( 1, line.lastIndexOf( "!" ) );
44          if ( image.contains( "|" ) )
45          {
46              // DOXIA-303: handle figure attributes
47              image = image.substring( 0, image.indexOf( "|" ) );
48          }
49  
50          line = line.substring( line.lastIndexOf( "!" ) + 1 ).trim();
51  
52          if ( line.startsWith( "\\\\" ) )
53          {
54              // ignore linebreak at start of caption
55              line = line.substring( 2 );
56          }
57  
58          String caption = line + appendUntilEmptyLine( source );
59  
60          if ( caption.trim().length() > 0 )
61          {
62              return new FigureBlock( image, caption );
63          }
64  
65          return new FigureBlock( image );
66      }
67  
68      /**
69       * Slurp lines from the source starting with the given line appending them together into a StringBuilder until an
70       * empty line is reached, and while the source contains more lines.
71       *
72       * @param source the source to read new lines from
73       * @return a StringBuilder appended with lines
74       * @throws ParseException
75       */
76      private String appendUntilEmptyLine( ByLineSource source )
77          throws ParseException
78      {
79          StringBuilder text = new StringBuilder();
80  
81          String line;
82  
83          while ( ( line = source.getNextLine() ) != null )
84          {
85  
86              if ( line.trim().length() == 0 )
87              {
88                  break;
89              }
90  
91              if ( text.length() == 0 )
92              {
93                  text.append( line.trim() );
94              }
95              else
96              {
97                  text.append( " " ).append( line.trim() );
98              }
99  
100         }
101 
102         return text.toString();
103     }
104 }