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