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>DefinitionListBlockParser class.</p>
27   *
28   * @author Dave Syer
29   * @since 1.1
30   */
31  public class DefinitionListBlockParser
32      implements BlockParser
33  {
34      static final String LS = System.getProperty( "line.separator" );
35  
36      /** {@inheritDoc} */
37      public boolean accept( String line, ByLineSource source )
38      {
39          return ( line.startsWith( "{note" ) || line.startsWith( "{tip" )
40              || line.startsWith( "{info" ) || line.startsWith( "{quote" ) );
41      }
42  
43      /** {@inheritDoc} */
44      public Block visit( String line, ByLineSource source )
45          throws ParseException
46      {
47          StringBuilder text = new StringBuilder();
48          StringBuilder title = new StringBuilder();
49  
50          int index = line.indexOf( "title=" );
51  
52          if ( index >= 0 )
53          {
54              line = line.substring( index + 6 );
55  
56              while ( !( line.contains( "}" ) ) && line != null )
57              {
58                  append( title, line );
59                  line = source.getNextLine();
60              }
61  
62              if ( line != null )
63              {
64                  append( title, line.substring( 0, line.indexOf( "}" ) ) );
65              }
66          }
67  
68          while ( ( line = source.getNextLine() ) != null )
69          {
70              if ( line.startsWith( "{note" ) || line.startsWith( "{tip" )
71                  || line.startsWith( "{info" ) || line.startsWith( "{quote" ) )
72              {
73                  break;
74              }
75  
76              append( text, line );
77          }
78  
79          return new DefinitionListBlock( title.toString(), text.toString() );
80      }
81  
82      private void append( StringBuilder title, String line )
83      {
84          if ( title.length() > 0 )
85          {
86              title.append( " " );
87          }
88  
89          title.append( line.trim() );
90      }
91  }