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>ParagraphBlockParser class.</p>
27   *
28   * @version $Id: ParagraphBlockParser.java 775115 2009-05-15 12:54:18Z ltheussl $
29   */
30  public class ParagraphBlockParser
31      implements BlockParser
32  {
33      private BlockParser[] parsers;
34  
35      /**
36       * <p>Constructor for ParagraphBlockParser.</p>
37       *
38       * @param parsers the parsers.
39       */
40      public ParagraphBlockParser( BlockParser[] parsers )
41      {
42          super();
43          this.parsers = parsers;
44      }
45  
46      /** {@inheritDoc} */
47      public boolean accept( String line, ByLineSource source )
48      {
49          return true;
50      }
51  
52      /**
53       * Visit the Block.
54       *
55       * @param line the line to visit.
56       * @param source the source.
57       * @param generateParagraphTags whether to generate a paragraph.
58       * @return the visited Block.
59       *
60       * @throws org.apache.maven.doxia.parser.ParseException if any
61       */
62      public Block visit( String line, ByLineSource source, boolean generateParagraphTags )
63              throws ParseException
64      {
65          if ( generateParagraphTags )
66          {
67              return this.visit( line, source );
68          }
69          else
70          {
71              ChildBlocksBuilder builder = new ChildBlocksBuilder( line );
72              return new ParagraphBlock( builder.getBlocks(), generateParagraphTags );
73          }
74      }
75  
76      /** {@inheritDoc} */
77      public Block visit( String line, ByLineSource source )
78          throws ParseException
79      {
80  
81          ChildBlocksBuilder builder = new ChildBlocksBuilder( appendUntilEmptyLine( line, source ) );
82          return new ParagraphBlock( builder.getBlocks() );
83      }
84  
85      /**
86       * Slurp lines from the source starting with the given line appending them together into a StringBuffer until an
87       * empty line is reached, and while the source contains more lines. The result can be passed to the
88       * {@link #getBlocks(String)} method.
89       *
90       * @param line the first line
91       * @param source the source to read new lines from
92       * @return a StringBuffer appended with lines
93       * @throws ParseException
94       */
95      private String appendUntilEmptyLine( String line, ByLineSource source )
96          throws ParseException
97      {
98          StringBuffer text = new StringBuffer();
99  
100         do
101         {
102 
103             if ( line.trim().length() == 0 )
104             {
105                 break;
106             }
107 
108             boolean accepted = false;
109             for ( int i = 0; i < parsers.length; i++ )
110             {
111                 BlockParser parser = parsers[i];
112                 if ( parser.accept( line, source ) )
113                 {
114                     accepted = true;
115                     break;
116                 }
117             }
118             if ( accepted )
119             {
120                 // Slightly fragile - if any of the parsers need to do this in order to decide whether to accept a line,
121                 // then it will barf because of the contract of ByLineSource
122                 source.ungetLine();
123                 break;
124             }
125 
126             if ( text.length() == 0 )
127             {
128                 text.append( line.trim() );
129             }
130             else
131             {
132                 text.append( " " + line.trim() );
133             }
134 
135         }
136         while ( ( line = source.getNextLine() ) != null );
137 
138         return text.toString();
139     }
140 
141 }