Coverage Report - org.apache.maven.archiva.xml.LatinEntityResolutionReader
 
Classes in this File Line Coverage Branch Coverage Complexity
LatinEntityResolutionReader
0%
0/56
0%
0/24
3.6
 
 1  
 package org.apache.maven.archiva.xml;
 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.io.BufferedReader;
 23  
 import java.io.IOException;
 24  
 import java.io.Reader;
 25  
 import java.util.regex.Matcher;
 26  
 import java.util.regex.Pattern;
 27  
 
 28  
 /**
 29  
  * LatinEntityResolutionReader - Read a Character Stream.
 30  
  *
 31  
  * @version $Id: LatinEntityResolutionReader.java 718864 2008-11-19 06:33:35Z brett $
 32  
  */
 33  
 public class LatinEntityResolutionReader
 34  
     extends Reader
 35  
 {
 36  
     private BufferedReader originalReader;
 37  
 
 38  
     private char leftover[];
 39  
 
 40  
     private Pattern entityPattern;
 41  
 
 42  
     public LatinEntityResolutionReader( Reader reader )
 43  0
     {
 44  0
         this.originalReader = new BufferedReader( reader );
 45  0
         this.entityPattern = Pattern.compile( "\\&[a-zA-Z]+\\;" );
 46  0
     }
 47  
 
 48  
     /**
 49  
      * Read characters into a portion of an array. This method will block until some input is available, 
 50  
      * an I/O error occurs, or the end of the stream is reached.
 51  
      * 
 52  
      * @param destbuf Destination buffer
 53  
      * @param offset Offset (in destination buffer) at which to start storing characters
 54  
      * @param length Maximum number of characters to read
 55  
      * @return The number of characters read, or -1 if the end of the stream has been reached
 56  
      * @throws IOException if an I/O error occurs.
 57  
      */
 58  
     public int read( char[] destbuf, int offset, int length )
 59  
         throws IOException
 60  
     {
 61  
         int tmp_length;
 62  0
         int current_requested_offset = offset;
 63  0
         int current_requested_length = length;
 64  
 
 65  
         // Drain leftover from last read request.
 66  0
         if ( leftover != null )
 67  
         {
 68  0
             if ( leftover.length > length )
 69  
             {
 70  
                 // Copy partial leftover.
 71  0
                 System.arraycopy( leftover, 0, destbuf, current_requested_offset, length );
 72  
 
 73  
                 // Create new leftover of remaining.
 74  0
                 char tmp[] = new char[length];
 75  0
                 System.arraycopy( leftover, length, tmp, 0, length );
 76  0
                 leftover = new char[tmp.length];
 77  0
                 System.arraycopy( tmp, 0, leftover, 0, length );
 78  
 
 79  
                 // Return len
 80  0
                 return length;
 81  
             }
 82  
             else
 83  
             {
 84  0
                 tmp_length = leftover.length;
 85  
 
 86  
                 // Copy full leftover
 87  0
                 System.arraycopy( leftover, 0, destbuf, current_requested_offset, tmp_length );
 88  
 
 89  
                 // Empty out leftover (as there is now none left)
 90  0
                 leftover = null;
 91  
 
 92  
                 // Adjust offset and lengths.
 93  0
                 current_requested_offset += tmp_length;
 94  0
                 current_requested_length -= tmp_length;
 95  
             }
 96  
         }
 97  
 
 98  0
         StringBuffer sbuf = getExpandedBuffer( current_requested_length );
 99  
 
 100  
         // Have we reached the end of the buffer?
 101  0
         if ( sbuf == null )
 102  
         {
 103  
             // Do we have content?
 104  0
             if ( current_requested_offset > offset )
 105  
             {
 106  
                 // Signal that we do, by calculating length.
 107  0
                 return ( current_requested_offset - offset );
 108  
             }
 109  
 
 110  
             // No content. signal end of buffer.
 111  0
             return -1;
 112  
         }
 113  
 
 114  
         // Copy from expanded buf whatever length we can accomodate.
 115  0
         tmp_length = Math.min( sbuf.length(), current_requested_length );
 116  0
         sbuf.getChars( 0, tmp_length, destbuf, current_requested_offset );
 117  
 
 118  
         // Create the leftover (if any)
 119  0
         if ( tmp_length < sbuf.length() )
 120  
         {
 121  0
             leftover = new char[sbuf.length() - tmp_length];
 122  0
             sbuf.getChars( tmp_length, tmp_length + leftover.length, leftover, 0 );
 123  
         }
 124  
 
 125  
         // Calculate Actual Length and return.
 126  0
         return ( current_requested_offset - offset ) + tmp_length;
 127  
     }
 128  
 
 129  
     private StringBuffer getExpandedBuffer( int minimum_length )
 130  
         throws IOException
 131  
     {
 132  0
         StringBuffer buf = null;
 133  0
         String line = this.originalReader.readLine();
 134  0
         boolean done = ( line == null );
 135  
 
 136  0
         while ( !done )
 137  
         {
 138  0
             if ( buf == null )
 139  
             {
 140  0
                 buf = new StringBuffer();
 141  
             }
 142  
 
 143  0
             buf.append( expandLine( line ) );
 144  
 
 145  
             // Add newline only if there is more data.
 146  0
             if ( this.originalReader.ready() )
 147  
             {
 148  0
                 buf.append( "\n" );
 149  
             }
 150  
 
 151  0
             if ( buf.length() > minimum_length )
 152  
             {
 153  0
                 done = true;
 154  
             }
 155  
             else
 156  
             {
 157  0
                 line = this.originalReader.readLine();
 158  0
                 done = ( line == null );
 159  
             }
 160  
         }
 161  
 
 162  0
         return buf;
 163  
     }
 164  
 
 165  
     private String expandLine( String line )
 166  
     {
 167  0
         StringBuffer ret = new StringBuffer();
 168  
 
 169  0
         int offset = 0;
 170  
         String entity;
 171  0
         Matcher mat = this.entityPattern.matcher( line );
 172  0
         while ( mat.find( offset ) )
 173  
         {
 174  0
             ret.append( line.substring( offset, mat.start() ) );
 175  0
             entity = mat.group();
 176  0
             ret.append( LatinEntities.resolveEntity( entity ) );
 177  0
             offset = mat.start() + entity.length();
 178  
         }
 179  0
         ret.append( line.substring( offset ) );
 180  
 
 181  0
         return ret.toString();
 182  
     }
 183  
 
 184  
     public void close()
 185  
         throws IOException
 186  
     {
 187  0
         this.originalReader.close();
 188  0
     }
 189  
 }