1 package org.apache.maven.doxia.module.twiki.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 java.util.ArrayList;
23 import java.util.List;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import org.apache.maven.doxia.parser.ParseException;
28 import org.apache.maven.doxia.util.ByLineSource;
29
30 /**
31 * Parse verbatim blocks
32 *
33 * @author Christian Nardi
34 * @version $Id: VerbatimBlockParser.java 1090706 2011-04-09 23:15:28Z hboutemy $
35 * @since 1.1
36 */
37 public class VerbatimBlockParser
38 implements BlockParser
39 {
40 /**
41 * pattern to detect verbatim start tags
42 */
43 private static final Pattern VERBATIM_START_PATTERN = Pattern.compile( "\\s*<verbatim>" );
44
45 private static final Pattern VERBATIM_END_PATTERN = Pattern.compile( "</verbatim>" );
46
47 /** {@inheritDoc} */
48 public final boolean accept( final String line )
49 {
50 return VERBATIM_START_PATTERN.matcher( line ).lookingAt();
51 }
52
53 /**
54 * {@inheritDoc}
55 */
56 public final Block visit( final String line, final ByLineSource source )
57 throws ParseException
58 {
59 if ( !accept( line ) )
60 {
61 throw new IllegalAccessError( "call accept before this ;)" );
62 }
63
64 final List<Block> lines = new ArrayList<Block>();
65 Matcher matcher = VERBATIM_START_PATTERN.matcher( line );
66 matcher.find();
67 String l = line.substring( matcher.end() );
68
69 while ( l != null )
70 {
71 matcher = VERBATIM_END_PATTERN.matcher( l );
72 if ( matcher.find() )
73 {
74 lines.add( new TextBlock( l.substring( 0, matcher.start() ) + "\n" ) );
75 break;
76 }
77 lines.add( new TextBlock( l + "\n" ) );
78 l = source.getNextLine();
79 }
80
81 return new VerbatimBlock( lines.toArray( new Block[] {} ) );
82 }
83 }