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