Coverage Report - net.sf.statsvn.util.SvnPropgetUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
SvnPropgetUtils
38%
45/118
20%
16/80
3.444
 
 1  
 package net.sf.statsvn.util;
 2  
 
 3  
 import java.io.FileInputStream;
 4  
 import java.io.IOException;
 5  
 import java.io.InputStream;
 6  
 import java.io.InputStreamReader;
 7  
 import java.util.ArrayList;
 8  
 import java.util.List;
 9  
 
 10  
 import net.sf.statcvs.util.LookaheadReader;
 11  
 import net.sf.statsvn.output.SvnConfigurationOptions;
 12  
 
 13  
 /**
 14  
  * Utilities class that manages calls to svn propget. Used to find binary files.
 15  
  * 
 16  
  * @author Jason Kealey <jkealey@shade.ca>
 17  
  * 
 18  
  * @version $Id: SvnPropgetUtils.java 394 2009-08-10 20:08:46Z jkealey $
 19  
  */
 20  
 public  class SvnPropgetUtils implements ISvnPropgetProcessor {
 21  
 
 22  
         protected List binaryFiles;
 23  
 
 24  
     protected ISvnProcessor processor;
 25  
 
 26  0
     /**
 27  0
      * Invokes propget via the svn propget via the command line.  
 28  
      */
 29  1
     public SvnPropgetUtils(ISvnProcessor processor) {
 30  1
         this.processor = processor;
 31  1
     }
 32  
 
 33  
     protected ISvnProcessor getProcessor() {
 34  0
         return processor;
 35  0
     }
 36  
 
 37  
         /**
 38  
          * Get the svn:mime-types for all files, latest revision.
 39  
          * 
 40  
          * @return the inputstream from which to read the information.
 41  
          */
 42  
         protected synchronized ProcessUtils getFileMimeTypes() {
 43  0
                 return getFileMimeTypes(null, null);
 44  
         }
 45  
 
 46  
         /**
 47  
          * Get the svn:mime-type for a certain file (leave null for all files).
 48  0
          * 
 49  0
          * @param revision
 50  0
          *            revision for which to query;
 51  
          * @param filename
 52  
          *            the filename (or null for all files)
 53  0
          * @return the inputstream from which to read the information.
 54  0
          */
 55  
         protected synchronized ProcessUtils getFileMimeTypes(final String revision, final String filename) {
 56  0
                 String svnPropgetCommand = "svn propget svn:mime-type";
 57  0
                 if (revision != null && revision.length() > 0) {
 58  0
                         svnPropgetCommand += " -r " + revision;
 59  
                 }
 60  0
 
 61  0
                 if (filename != null && filename.length() > 0) {
 62  0
                         svnPropgetCommand += " " + StringUtils.replace(" ", "%20", getProcessor().getInfoProcessor().relativePathToUrl(filename));
 63  0
 
 64  0
                         if (revision != null && revision.length() > 0) {
 65  0
                                 svnPropgetCommand += "@" + revision;
 66  0
                         }
 67  0
                 } else {
 68  0
                         svnPropgetCommand += " -R ";
 69  0
                 }
 70  
 
 71  0
                 svnPropgetCommand += SvnCommandHelper.getAuthString();
 72  
 
 73  
                 try {
 74  0
                         return ProcessUtils.call(svnPropgetCommand);
 75  0
                 } catch (final Exception e) {
 76  0
                         SvnConfigurationOptions.getTaskLogger().info(e.toString());
 77  0
                         return null;
 78  
                 }
 79  157122
         }
 80  0
 
 81  
         /* (non-Javadoc)
 82  0
      * @see net.sf.statsvn.util.ISvnPropgetProcessor#getBinaryFiles()
 83  0
      */
 84  0
         public List getBinaryFiles() {
 85  5418
                 if (binaryFiles == null) {
 86  0
                         ProcessUtils pUtils = null;
 87  0
                         try {
 88  0
                                 pUtils = getFileMimeTypes();
 89  0
                                 loadBinaryFiles(pUtils);
 90  0
                         } finally {
 91  0
                                 if (pUtils != null) {
 92  0
                                         try {
 93  0
                                                 pUtils.close();
 94  0
                                         } catch (final IOException e) {
 95  157122
                                                 SvnConfigurationOptions.getTaskLogger().info(e.toString());
 96  0
                                         }
 97  
                                 }
 98  
                         }
 99  
                 }
 100  
 
 101  5418
                 return binaryFiles;
 102  
         }
 103  
         
 104  
         
 105  
     /**
 106  
      * Loads the list of binary files from the input stream equivalent to an svn
 107  29
      * propget command.
 108  29
      * 
 109  
      * @param path
 110  4263
      *            a file on disk which contains the results of an svn propget
 111  4234
      */        
 112  4234
         public void loadBinaryFiles(final String path) throws IOException
 113  4234
         {
 114  4235
        final InputStream stream = new FileInputStream(path);
 115  1
         final ProcessUtils pUtils = new ProcessUtils();
 116  3796
         try {
 117  30
             pUtils.setInputStream(stream);
 118  1
             loadBinaryFiles(pUtils);
 119  
         } finally {
 120  1
             pUtils.close();
 121  1
         }
 122  30
         }
 123  29
         /**
 124  
          * Loads the list of binary files from the input stream equivalent to an svn
 125  
          * propget command.
 126  
          * 
 127  
          * @param stream
 128  
          *            stream equivalent to an svn propget command
 129  
          */
 130  
         protected void loadBinaryFiles(final ProcessUtils pUtils) {
 131  1
                 binaryFiles = new ArrayList();
 132  1
                 final LookaheadReader mimeReader = new LookaheadReader(new InputStreamReader(pUtils.getInputStream()));
 133  
                 try {
 134  147
                         while (mimeReader.hasNextLine()) {
 135  146
                                 mimeReader.nextLine();
 136  146
                                 final String file = getBinaryFilename(mimeReader.getCurrentLine(), false);
 137  146
                                 if (file != null) {
 138  146
                                         binaryFiles.add(file);
 139  0
                                 }
 140  146
                         }
 141  1
                         if (pUtils.hasErrorOccured()) {
 142  0
                                 throw new IOException(pUtils.getErrorMessage());
 143  0
                         }
 144  0
                 } catch (final IOException e) {
 145  0
                         SvnConfigurationOptions.getTaskLogger().info(e.getMessage());
 146  1
                 }
 147  1
         }
 148  0
 
 149  0
         /* (non-Javadoc)
 150  0
      * @see net.sf.statsvn.util.ISvnPropgetProcessor#isBinaryFile(java.lang.String, java.lang.String)
 151  0
      */
 152  
         public boolean isBinaryFile(final String revision, final String filename) {
 153  0
                 ProcessUtils pUtils = null;
 154  0
                 try {
 155  0
                         pUtils = getFileMimeTypes(revision, filename);
 156  0
                         final LookaheadReader mimeReader = new LookaheadReader(new InputStreamReader(pUtils.getInputStream()));
 157  0
                         while (mimeReader.hasNextLine()) {
 158  0
                                 mimeReader.nextLine();
 159  0
                                 final String file = getBinaryFilename(mimeReader.getCurrentLine(), true);
 160  0
                                 if (file != null && file.equals(filename)) {
 161  0
                                         return true;
 162  
                                 }
 163  0
                         }
 164  0
                 } catch (final IOException e) {
 165  0
                         SvnConfigurationOptions.getTaskLogger().info(e.toString());
 166  
                 } finally {
 167  0
                         if (pUtils != null) {
 168  
                                 try {
 169  0
                                         pUtils.close();
 170  0
                                 } catch (final IOException e) {
 171  0
                                         SvnConfigurationOptions.getTaskLogger().info(e.toString());
 172  0
                                 }
 173  
                         }
 174  
                 }
 175  
 
 176  0
                 return false;
 177  
         }
 178  
 
 179  4234
         /**
 180  
          * Given a string such as: "lib\junit.jar - application/octet-stream" or
 181  
          * "svn:\\host\repo\lib\junit.jar - application/octet-stream" will return
 182  
          * the filename if the mime type is binary (doesn't end with text/*)
 183  4234
          * 
 184  
          * Will return the filename with / was a directory seperator.
 185  4234
          * 
 186  
          * @param currentLine
 187  4234
          *            the line obtained from svn propget svn:mime-type
 188  4234
          * @param removeRoot
 189  0
          *            if true, will remove any repository prefix
 190  
          * @return should return lib\junit.jar in both cases, given that
 191  4234
          *         removeRoot==true in the second case.
 192  
          */
 193  
         protected String getBinaryFilename(final String currentLine, final boolean removeRoot) {
 194  0
                 // want to make sure we only have / in end result.
 195  146
                 String line = removeRoot ? currentLine : currentLine.replace('\\', '/');
 196  
 
 197  
                 // HACK: See bug 18. if removeRoot==true, no - will be found because we
 198  
                 // are calling for one specific file.
 199  146
                 final String octetStream = " - application/octet-stream";
 200  
                 // if is common binary file or identified as something other than text
 201  146
                 if (line.endsWith(octetStream) || line.lastIndexOf(" - text/") < 0 && line.lastIndexOf(" - text/") == line.lastIndexOf(" - ")
 202  
                         && line.lastIndexOf(" - ") >= 0) {
 203  146
                         line = line.substring(0, line.lastIndexOf(" - "));
 204  146
                         if (removeRoot) {
 205  0
                                 line = getProcessor().getInfoProcessor().urlToRelativePath(line);
 206  
                         }
 207  146
                         return line;
 208  
                 }
 209  
 
 210  0
                 return null;
 211  
         }
 212  
 
 213  
 }