Coverage Report - org.apache.maven.scm.provider.bazaar.command.BazaarConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
BazaarConsumer
69 %
38/55
50 %
13/26
3,667
 
 1  
 package org.apache.maven.scm.provider.bazaar.command;
 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.scm.ScmFileStatus;
 23  
 import org.apache.maven.scm.log.ScmLogger;
 24  
 import org.apache.maven.scm.util.AbstractConsumer;
 25  
 
 26  
 import java.util.ArrayList;
 27  
 import java.util.HashMap;
 28  
 import java.util.Iterator;
 29  
 import java.util.List;
 30  
 import java.util.Map;
 31  
 
 32  
 /**
 33  
  * Base consumer to do common parsing for all bazaar commands.
 34  
  * <p/>
 35  
  * More specific: log line each line if debug is enabled, get file status
 36  
  * and detect warnings from bazaar
 37  
  *
 38  
  * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
 39  
  * @version $Id: BazaarConsumer.java 1212880 2011-12-10 21:22:16Z olamy $
 40  
  */
 41  
 public class BazaarConsumer
 42  
     extends AbstractConsumer
 43  
 {
 44  
 
 45  
     /**
 46  
      * A list of known keywords from bazaar
 47  
      */
 48  1
     private static final Map<String,ScmFileStatus> IDENTIFIERS = new HashMap<String,ScmFileStatus>();
 49  
 
 50  
     /**
 51  
      * A list of known message prefixes from bazaar
 52  
      */
 53  1
     private static final Map<String,String> MESSAGES = new HashMap<String,String>();
 54  
 
 55  
     /**
 56  
      * Number of lines to keep from Std.Err
 57  
      * This size is set to ensure that we capture enough info
 58  
      * but still keeps a low memory footprint.
 59  
      */
 60  
     private static final int MAX_STDERR_SIZE = 10;
 61  
 
 62  
     /**
 63  
      * A list of the MAX_STDERR_SIZE last errors or warnings.
 64  
      */
 65  1
     private final List<String> stderr = new ArrayList<String>();
 66  
 
 67  
     static
 68  
     {
 69  1
         IDENTIFIERS.put( "added", ScmFileStatus.ADDED );
 70  1
         IDENTIFIERS.put( "adding", ScmFileStatus.ADDED );
 71  1
         IDENTIFIERS.put( "unknown", ScmFileStatus.UNKNOWN );
 72  1
         IDENTIFIERS.put( "modified", ScmFileStatus.MODIFIED );
 73  1
         IDENTIFIERS.put( "removed", ScmFileStatus.DELETED );
 74  1
         IDENTIFIERS.put( "renamed", ScmFileStatus.RENAMED );
 75  1
         MESSAGES.put( "bzr: WARNING:", "WARNING" );
 76  1
         MESSAGES.put( "bzr: ERROR:", "ERROR" );
 77  1
         MESSAGES.put( "'bzr' ", "ERROR" ); // bzr isn't found in windows path
 78  1
     }
 79  
 
 80  
     public BazaarConsumer( ScmLogger logger )
 81  
     {
 82  1
         super( logger );
 83  1
     }
 84  
 
 85  
     public void doConsume( ScmFileStatus status, String trimmedLine )
 86  
     {
 87  
         //override this
 88  0
     }
 89  
 
 90  
     /** {@inheritDoc} */
 91  
     public void consumeLine( String line )
 92  
     {
 93  52
         if ( getLogger().isDebugEnabled() )
 94  
         {
 95  0
             getLogger().debug( line );
 96  
         }
 97  52
         String trimmedLine = line.trim();
 98  
 
 99  52
         String statusStr = processInputForKnownIdentifiers( trimmedLine );
 100  
 
 101  
         //If its not a status report - then maybe its a message?
 102  52
         if ( statusStr == null )
 103  
         {
 104  47
             boolean isMessage = processInputForKnownMessages( trimmedLine );
 105  
             //If it is then its already processed and we can ignore futher processing
 106  47
             if ( isMessage )
 107  
             {
 108  0
                 return;
 109  
             }
 110  47
         }
 111  
         else
 112  
         {
 113  
             //Strip away identifier
 114  5
             trimmedLine = trimmedLine.substring( statusStr.length() );
 115  5
             trimmedLine = trimmedLine.trim(); //one or more spaces
 116  
         }
 117  
 
 118  52
         ScmFileStatus status = statusStr != null ? ( (ScmFileStatus) IDENTIFIERS.get( statusStr.intern() ) ) : null;
 119  52
         doConsume( status, trimmedLine );
 120  52
     }
 121  
 
 122  
     /**
 123  
      * Warnings and errors is usually printed out in Std.Err, thus for derived consumers
 124  
      * operating on Std.Out this would typically return an empty string.
 125  
      *
 126  
      * @return Return the last lines interpreted as an warning or an error
 127  
      */
 128  
     public String getStdErr()
 129  
     {
 130  0
         StringBuilder str = new StringBuilder();
 131  0
         for ( Iterator<String> it = stderr.iterator(); it.hasNext(); )
 132  
         {
 133  0
             str.append( it.next() );
 134  
         }
 135  0
         return str.toString();
 136  
     }
 137  
 
 138  
     private static String processInputForKnownIdentifiers( String line )
 139  
     {
 140  52
         for ( Iterator<String> it = IDENTIFIERS.keySet().iterator(); it.hasNext(); )
 141  
         {
 142  303
             String id = it.next();
 143  303
             if ( line.startsWith( id ) )
 144  
             {
 145  5
                 return id;
 146  
             }
 147  298
         }
 148  47
         return null;
 149  
     }
 150  
 
 151  
     private boolean processInputForKnownMessages( String line )
 152  
     {
 153  47
         for ( Iterator<String> it = MESSAGES.keySet().iterator(); it.hasNext(); )
 154  
         {
 155  141
             String prefix = it.next();
 156  141
             if ( line.startsWith( prefix ) )
 157  
             {
 158  0
                 stderr.add( line ); //Add line
 159  0
                 if ( stderr.size() > MAX_STDERR_SIZE )
 160  
                 {
 161  0
                     stderr.remove( 0 ); //Rotate list
 162  
                 }
 163  0
                 String message = line.substring( prefix.length() );
 164  0
                 if ( MESSAGES.get( prefix ).equals( "WARNING" ) )
 165  
                 {
 166  0
                     if ( getLogger().isWarnEnabled() )
 167  
                     {
 168  0
                         getLogger().warn( message );
 169  
                     }
 170  
                 }
 171  
                 else
 172  
                 {
 173  0
                     if ( getLogger().isErrorEnabled() )
 174  
                     {
 175  0
                         getLogger().error( message );
 176  
                     }
 177  
                 }
 178  0
                 return true;
 179  
             }
 180  141
         }
 181  47
         return false;
 182  
     }
 183  
 }