Coverage Report - org.apache.maven.shared.jar.identification.exposers.TextFileExposer
 
Classes in this File Line Coverage Branch Coverage Complexity
TextFileExposer
0%
0/29
0%
0/10
4
 
 1  
 package org.apache.maven.shared.jar.identification.exposers;
 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.shared.jar.JarAnalyzer;
 23  
 import org.apache.maven.shared.jar.identification.JarIdentification;
 24  
 import org.apache.maven.shared.jar.identification.JarIdentificationExposer;
 25  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 26  
 import org.codehaus.plexus.util.IOUtil;
 27  
 import org.codehaus.plexus.util.StringUtils;
 28  
 
 29  
 import java.io.BufferedReader;
 30  
 import java.io.IOException;
 31  
 import java.io.InputStream;
 32  
 import java.io.InputStreamReader;
 33  
 import java.util.ArrayList;
 34  
 import java.util.Iterator;
 35  
 import java.util.List;
 36  
 import java.util.jar.JarEntry;
 37  
 
 38  
 
 39  
 /**
 40  
  * Exposer that examines a a JAR for files that contain the text <code>version</code> (case-insensitive) and
 41  
  * adds the contents as potential version(s).
 42  
  *
 43  
  * @plexus.component role="org.apache.maven.shared.jar.identification.JarIdentificationExposer" role-hint="textFile"
 44  
  */
 45  0
 public class TextFileExposer
 46  
     extends AbstractLogEnabled
 47  
     implements JarIdentificationExposer
 48  
 {
 49  
     public void expose( JarIdentification identification, JarAnalyzer jarAnalyzer )
 50  
     {
 51  0
         List textFiles = findTextFileVersions( jarAnalyzer );
 52  0
         if ( !textFiles.isEmpty() )
 53  
         {
 54  0
             Iterator ithits = textFiles.iterator();
 55  0
             while ( ithits.hasNext() )
 56  
             {
 57  0
                 String ver = (String) ithits.next();
 58  0
                 identification.addVersion( ver );
 59  0
             }
 60  
         }
 61  0
     }
 62  
 
 63  
     private List findTextFileVersions( JarAnalyzer jarAnalyzer )
 64  
     {
 65  0
         List textVersions = new ArrayList();
 66  0
         List hits = jarAnalyzer.getVersionEntries();
 67  
 
 68  0
         Iterator it = hits.iterator();
 69  0
         while ( it.hasNext() )
 70  
         {
 71  0
             JarEntry entry = (JarEntry) it.next();
 72  
 
 73  
             // skip this entry if it's a class file.
 74  0
             if ( !entry.getName().endsWith( ".class" ) ) //$NON-NLS-1$
 75  
             {
 76  0
                 getLogger().debug( "Version Hit: " + entry.getName() );
 77  0
                 InputStream is = null;
 78  
                 try
 79  
                 {
 80  0
                     is = jarAnalyzer.getEntryInputStream( entry );
 81  0
                     BufferedReader br = new BufferedReader( new InputStreamReader( is ) );
 82  
 
 83  0
                     String line = br.readLine();
 84  
                     // TODO: check for key=value pair.
 85  
                     // TODO: maybe even for groupId entries.
 86  
 
 87  0
                     getLogger().debug( line );
 88  0
                     if ( StringUtils.isNotEmpty( line ) )
 89  
                     {
 90  0
                         textVersions.add( line );
 91  
                     }
 92  
                 }
 93  0
                 catch ( IOException e )
 94  
                 {
 95  0
                     getLogger().warn( "Unable to read line from " + entry.getName(), e );
 96  
                 }
 97  
                 finally
 98  
                 {
 99  0
                     IOUtil.close( is );
 100  0
                 }
 101  
             }
 102  0
         }
 103  0
         return textVersions;
 104  
     }
 105  
 }