1 package org.apache.maven.doxia.util;
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 /*
23 * Originally from org.apache.doxia.module.apt.AptReaderSource. It was modified
24 * to get unget support
25 */
26
27 import java.io.IOException;
28 import java.io.LineNumberReader;
29 import java.io.Reader;
30
31 import org.apache.maven.doxia.parser.ParseException;
32 import org.codehaus.plexus.util.IOUtil;
33
34 /**
35 * {@link ByLineSource} default implementation
36 *
37 * @version $Id: ByLineReaderSource.java 775115 2009-05-15 12:54:18Z ltheussl $
38 */
39 public class ByLineReaderSource implements ByLineSource
40 {
41 /**
42 * reader
43 */
44 private LineNumberReader reader;
45
46 /**
47 * current line number
48 */
49 private int lineNumber;
50
51 /**
52 * holds the last line returned by getNextLine()
53 */
54 private String lastLine;
55
56 /**
57 * <code>true</code> if ungetLine() was called and no getNextLine() was
58 * called
59 */
60 private boolean ungetted = false;
61
62 /**
63 * Creates the ByLineReaderSource.
64 *
65 * @param in real source :)
66 */
67 public ByLineReaderSource( final Reader in )
68 {
69 reader = new LineNumberReader( in );
70
71 lineNumber = -1;
72 }
73
74 /** {@inheritDoc} */
75 public final String getNextLine() throws ParseException
76 {
77 if ( reader == null )
78 {
79 return null;
80 }
81
82 if ( ungetted )
83 {
84 ungetted = false;
85 return lastLine;
86 }
87
88 String line;
89
90 try
91 {
92 line = reader.readLine();
93 if ( line == null )
94 {
95 reader.close();
96 reader = null;
97 }
98 else
99 {
100 lineNumber = reader.getLineNumber();
101 }
102 }
103 catch ( IOException e )
104 {
105 throw new ParseException( e, lineNumber, 0 );
106 }
107
108 lastLine = line;
109
110 return line;
111 }
112
113 /** {@inheritDoc} */
114 public final String getName()
115 {
116 return "";
117 }
118
119 /** {@inheritDoc} */
120 public final int getLineNumber()
121 {
122 return lineNumber;
123 }
124
125 /** {@inheritDoc} */
126 public final void close()
127 {
128 IOUtil.close( reader );
129 reader = null;
130 }
131
132 /** {@inheritDoc} */
133 public final void ungetLine()
134 {
135 if ( ungetted )
136 {
137 throw new IllegalStateException( "we support only one level of ungetLine()" );
138 }
139 ungetted = true;
140 }
141
142 /** {@inheritDoc} */
143 public final void unget( final String s )
144 {
145 if ( s == null )
146 {
147 throw new IllegalArgumentException( "argument can't be null" );
148 }
149 if ( s.length() != 0 )
150 {
151 ungetLine();
152 lastLine = s;
153 }
154 }
155 }