Coverage Report - org.apache.maven.doxia.util.ByLineReaderSource
 
Classes in this File Line Coverage Branch Coverage Complexity
ByLineReaderSource
65%
23/35
58%
7/12
2,714
 
 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  2
     private boolean ungetted = false;
 61  
 
 62  
     /**
 63  
      * Creates the ByLineReaderSource.
 64  
      *
 65  
      * @param in real source :)
 66  
      */
 67  
     public ByLineReaderSource( final Reader in )
 68  2
     {
 69  2
         reader = new LineNumberReader( in );
 70  
 
 71  2
         lineNumber = -1;
 72  2
     }
 73  
 
 74  
     /** {@inheritDoc} */
 75  
     public final String getNextLine() throws ParseException
 76  
     {
 77  12
         if ( reader == null )
 78  
         {
 79  0
             return null;
 80  
         }
 81  
 
 82  12
         if ( ungetted )
 83  
         {
 84  4
             ungetted = false;
 85  4
             return lastLine;
 86  
         }
 87  
 
 88  
         String line;
 89  
 
 90  
         try
 91  
         {
 92  8
             line = reader.readLine();
 93  8
             if ( line == null )
 94  
             {
 95  2
                 reader.close();
 96  2
                 reader = null;
 97  
             }
 98  
             else
 99  
             {
 100  6
                 lineNumber = reader.getLineNumber();
 101  
             }
 102  
         }
 103  0
         catch ( IOException e )
 104  
         {
 105  0
             throw new ParseException( e, lineNumber, 0 );
 106  8
         }
 107  
 
 108  8
         lastLine = line;
 109  
 
 110  8
         return line;
 111  
     }
 112  
 
 113  
     /** {@inheritDoc} */
 114  
     public final String getName()
 115  
     {
 116  2
         return "";
 117  
     }
 118  
 
 119  
     /** {@inheritDoc} */
 120  
     public final int getLineNumber()
 121  
     {
 122  2
         return lineNumber;
 123  
     }
 124  
 
 125  
     /** {@inheritDoc} */
 126  
     public final void close()
 127  
     {
 128  0
         IOUtil.close( reader );
 129  0
         reader = null;
 130  0
     }
 131  
 
 132  
     /** {@inheritDoc} */
 133  
     public final void ungetLine()
 134  
     {
 135  6
         if ( ungetted )
 136  
         {
 137  2
             throw new IllegalStateException( "we support only one level of ungetLine()" );
 138  
         }
 139  4
         ungetted = true;
 140  4
     }
 141  
 
 142  
     /** {@inheritDoc} */
 143  
     public final void unget( final String s )
 144  
     {
 145  0
         if ( s == null )
 146  
         {
 147  0
             throw new IllegalArgumentException( "argument can't be null" );
 148  
         }
 149  0
         if ( s.length() != 0 )
 150  
         {
 151  0
             ungetLine();
 152  0
             lastLine = s;
 153  
         }
 154  0
     }
 155  
 }