Home | History | Annotate | Download | only in output
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.motorolamobility.preflighting.output;
     17 
     18 import java.io.File;
     19 import java.io.OutputStream;
     20 import java.io.PrintStream;
     21 import java.util.List;
     22 
     23 import com.motorolamobility.preflighting.core.exception.PreflightingToolException;
     24 import com.motorolamobility.preflighting.core.validation.ApplicationValidationResult;
     25 import com.motorolamobility.preflighting.core.validation.Parameter;
     26 import com.motorolamobility.preflighting.core.validation.ValidationManager.InputParameter;
     27 
     28 /**
     29  * Abstract class for printing results
     30  */
     31 public abstract class AbstractOutputter
     32 {
     33     private File applicationFile;
     34 
     35     private int limit = -1;
     36 
     37     /**
     38      * Returns the path for the application being validated.
     39      * <br><br>
     40      * Note: call initializeParams before calling this method.
     41      * @return the path for the application being validated.
     42      */
     43     public File getApplicationFile()
     44     {
     45         return applicationFile;
     46     }
     47 
     48     /**
     49      * Sets the path for the application being validated.
     50      * @param applicationFile the file path.
     51      */
     52     public void setApplicationFile(File applicationFile)
     53     {
     54         this.applicationFile = applicationFile;
     55     }
     56 
     57     /**
     58      * The maximum number of results that will be displayed.
     59      * <br><br>
     60      * Note: call initializeParams before calling this method.
     61      * @return an integer representing the limit.
     62      */
     63     public int getLimit()
     64     {
     65         return limit;
     66     }
     67 
     68     /**
     69      * Sets the maximum number of results that will be displayed.
     70      * @param limit the integer representing the limit.
     71      */
     72     public void setLimit(int limit)
     73     {
     74         this.limit = limit;
     75     }
     76 
     77     /**
     78      * Initialize limit and applicationFile variables
     79      * @param parameters the parameters to the outputter
     80      */
     81     public void initializeParams(List<Parameter> parameters)
     82     {
     83         for (Parameter inputParameter : parameters)
     84         {
     85             if (InputParameter.APPLICATION_PATH.getAlias()
     86                     .equals(inputParameter.getParameterType()))
     87             {
     88                 String applicationPath = inputParameter.getValue();
     89                 applicationFile = new File(applicationPath);
     90             }
     91             else if (InputParameter.LIMIT.getAlias().equals(inputParameter.getParameterType()))
     92             {
     93                 try
     94                 {
     95                     limit = Integer.parseInt(inputParameter.getValue());
     96                 }
     97                 catch (NumberFormatException nfe)
     98                 {
     99                     //do nothing
    100                 }
    101             }
    102         }
    103     }
    104 
    105     /**
    106      * Print errors to a stream.
    107      *
    108      * @param exceptionThrown
    109      * @param out the stream used to print errors.
    110      */
    111     public abstract void printError(Exception exceptionThrown, PrintStream errorStream);
    112 
    113     /**
    114      * Prints the results to a stream.
    115      * <br>
    116      * Note: If you need parameter information inside this method, call initializeParams.
    117      *
    118      * @param result the result set to be printed.
    119      * @param stream the stream used to print validation results.
    120      * @param parameters the parameters from the command line
    121      * @throws PreflightingToolException
    122      */
    123     public abstract void print(ApplicationValidationResult results, OutputStream stream,
    124             List<Parameter> parameters) throws PreflightingToolException;
    125 
    126     /**
    127      * Compute the relative path of project and APK resources
    128      * @param resouce the file representing the resource whose path is desired
    129      * @return the resource path
    130      */
    131     protected String computeResourcePath(File resource)
    132     {
    133         // Append file location (only relative path)
    134         String fileLocation = resource.getAbsolutePath();
    135         // project passed
    136         if (fileLocation.startsWith(applicationFile.getAbsolutePath()))
    137         {
    138             fileLocation = fileLocation.substring(applicationFile.getAbsolutePath().length() + 1);
    139         }
    140         // probably apk passed, check
    141         else
    142         {
    143             String apkName = applicationFile.getName();
    144             int apkNameIndex = fileLocation.indexOf(apkName);
    145             // if this test fails, the file with problem is possibly somewhere
    146             // unknown and the relative path cannot be guessed and it will be used as is
    147             if (apkNameIndex != -1)
    148             {
    149                 int relativePathStartIndex = fileLocation.indexOf(File.separator, apkNameIndex);
    150                 fileLocation = fileLocation.substring(relativePathStartIndex + 1);
    151             }
    152             else
    153             {
    154                 fileLocation = resource.getPath();
    155             }
    156 
    157         }
    158         return fileLocation;
    159     }
    160 }
    161