buildframework/helium/sf/java/quality/src/com/nokia/helium/quality/ant/taskdefs/CASummaryTask.java
changeset 628 7c4a911dc066
child 645 b8d81fa19e7d
equal deleted inserted replaced
588:c7c26511138f 628:7c4a911dc066
       
     1 /*
       
     2  * Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description:  
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.helium.quality.ant.taskdefs;
       
    19 
       
    20 import java.io.BufferedReader;
       
    21 import java.io.BufferedWriter;
       
    22 import java.io.FileNotFoundException;
       
    23 import java.io.FileOutputStream;
       
    24 import java.io.IOException;
       
    25 import java.io.OutputStreamWriter;
       
    26 import java.io.UnsupportedEncodingException;
       
    27 import java.util.Hashtable;
       
    28 import java.util.Iterator;
       
    29 import java.util.SortedSet;
       
    30 import java.util.TreeSet;
       
    31 
       
    32 import org.apache.tools.ant.Project;
       
    33 import org.apache.tools.ant.Task;
       
    34 import java.io.FileReader;
       
    35 import javax.xml.xpath.XPath;
       
    36 import org.w3c.dom.NodeList;
       
    37 import org.w3c.dom.Document;
       
    38 import javax.xml.parsers.DocumentBuilder;
       
    39 import javax.xml.parsers.DocumentBuilderFactory;
       
    40 import javax.xml.parsers.ParserConfigurationException;
       
    41 import javax.xml.xpath.XPathFactory;
       
    42 import javax.xml.xpath.XPathExpression;
       
    43 import javax.xml.xpath.XPathConstants;
       
    44 import javax.xml.xpath.XPathExpressionException;
       
    45 import org.xml.sax.SAXException;
       
    46 
       
    47 
       
    48 /**
       
    49  * This Task searches the bc_header.xml and bc_library.xml files created by Binary Comparison and 
       
    50  * looks for the typeIds and counts them up. It then prints them in order to write the number of each 
       
    51  * typeID that was found to the diamonds output file for display by diamonds.
       
    52  * 
       
    53  * <pre>
       
    54  * &lt;hlm:casummarytask   
       
    55  *             header="true"
       
    56  *             diamondsHeaderFileName="C:\diamonds_header.xml" 
       
    57  *             diamondsFooterFileName="C:\diamonds_footer.xml"
       
    58  *             outptuFile="Z:\output\diamonds/ca_summary_header.xml" 
       
    59  *             inputFile="Z:\output\logs/headers_report_minibuild_ido_0.0.03_.xml/&gt;
       
    60  * </pre>
       
    61  * 
       
    62  * @ant.task name="casummarytask" category="Quality"
       
    63  */
       
    64 
       
    65 public class CASummaryTask extends Task {
       
    66 
       
    67     /** String used to look for the tag values in the header xml file **/
       
    68     private static String headerExpression = "//issuelist/headerfile/issue/typeid/text()";
       
    69 
       
    70     /** String used to look for the tag values in the library xml file **/
       
    71     private static String libraryExpression = "//issuelist/library/issue/typeid/text()";
       
    72 
       
    73     /** The file containing the CA summary data */
       
    74     private String inputFile;
       
    75     /** Name of file to write the output to */
       
    76     private String outputFile;
       
    77     /** Whether we are dealng with a header or library */
       
    78     private boolean header;
       
    79 
       
    80     /** Each line of the input file is read into this */
       
    81     private String line;
       
    82 
       
    83     /** File descriptor for the input file */
       
    84     private BufferedReader inputFileReader;
       
    85 
       
    86     /** File handler used to write the summary numbers to the output file **/
       
    87     private BufferedWriter output; // default init = null
       
    88 
       
    89     /**
       
    90      * File name of the default diamonds XML file header part (1st few lines) used so not writing
       
    91      * the XML text here
       
    92      */
       
    93     private String diamondsHeaderFileName;
       
    94     /**
       
    95      * File name of the default diamonds XML file footer part (last line) used so not writing the
       
    96      * XML text here
       
    97      */
       
    98     private String diamondsFooterFileName;
       
    99 
       
   100     /**
       
   101      * @param outputFile set the output file name
       
   102      * @ant.required
       
   103      */
       
   104     public void setoutputFile(String outputFile) {
       
   105         this.outputFile = outputFile;
       
   106     }
       
   107 
       
   108     /**
       
   109      * @return the outputFile the output file name
       
   110      */
       
   111     public String getoutputFile() {
       
   112         return outputFile;
       
   113     }
       
   114 
       
   115     /**
       
   116      * @return the inputFile
       
   117      */
       
   118     public String getinputFile() {
       
   119         return inputFile;
       
   120     }
       
   121 
       
   122     /**
       
   123      * @param inputFile the name of file to scan and extract data from
       
   124      * @ant.required
       
   125      */
       
   126     public void setinputFile(String inputFile) {
       
   127         this.inputFile = inputFile;
       
   128     }
       
   129 
       
   130     /**
       
   131      * @param diamondsFooterFileName set the diamonds footer file name
       
   132      * @ant.required
       
   133      */
       
   134     public void setdiamondsFooterFileName(String diamondsFooterFileName) {
       
   135         this.diamondsFooterFileName = diamondsFooterFileName;
       
   136     }
       
   137 
       
   138     /**
       
   139      * @return the diamondsFooterFileName the diamonds footer file name
       
   140      */
       
   141     public String getdiamondsFooterFileName() {
       
   142         return diamondsFooterFileName;
       
   143     }
       
   144 
       
   145     /**
       
   146      * @param diamondsHeaderFileName set the diamonds header file name
       
   147      * @ant.required
       
   148      */
       
   149     public void setdiamondsHeaderFileName(String diamondsHeaderFileName) {
       
   150         this.diamondsHeaderFileName = diamondsHeaderFileName;
       
   151     }
       
   152 
       
   153     /**
       
   154      * @return the diamondsFooterFileName the diamonds footer file name
       
   155      */
       
   156     public String getdiamondsHeaderFileName() {
       
   157         return diamondsHeaderFileName;
       
   158     }
       
   159     
       
   160     /**
       
   161      * @param header set whether we are dealing with a headers or libraries
       
   162      * @ant.required
       
   163      */
       
   164     public void setheader(boolean header) {
       
   165         this.header = header;
       
   166     }
       
   167 
       
   168     /**
       
   169      * @return the fileType whether we are dealing with headers or libraries
       
   170      */
       
   171     public boolean getheader() {
       
   172         return header;
       
   173     }
       
   174 
       
   175     /** the main part of the code - the method that is called */
       
   176     public void execute() {
       
   177         log("CASummaryTask execute method with input file : " + inputFile, Project.MSG_ERR);
       
   178         boolean inputFileFound = true;
       
   179         BufferedReader diamondsHeaderFile;
       
   180         BufferedReader diamondsFooterFile;
       
   181 
       
   182         log("output File is " + getoutputFile(), Project.MSG_ERR);
       
   183 
       
   184         try {
       
   185             // open the file with the CA results init
       
   186             inputFileReader = new BufferedReader(new FileReader(inputFile));
       
   187         }
       
   188         catch (FileNotFoundException exc) {
       
   189             log("FileNotFoundException while getting the input file.  : " + inputFile + "  "
       
   190                 + exc.getMessage(), Project.MSG_ERR);
       
   191             inputFileFound = false; // stops an empty output file being created.
       
   192         }
       
   193         if (inputFileFound) {
       
   194             try {
       
   195                 // write the title stuff for the XML diamonds schema
       
   196                 output = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getoutputFile()), "UTF8"));
       
   197             }
       
   198             catch (FileNotFoundException exc) {
       
   199                 log("FileNotFoundException while getting the output file.  : " + getoutputFile()
       
   200                     + "   " + exc.getMessage(), Project.MSG_ERR);
       
   201             }
       
   202             catch (UnsupportedEncodingException exc) {
       
   203                 // We are Ignoring the errors as no need to fail the build.
       
   204                 log("UnsupportedEncodingException while creating the output file : "
       
   205                     + getoutputFile() + "   " + exc.getMessage(), Project.MSG_ERR);
       
   206             }
       
   207             catch (SecurityException exc) {
       
   208                 // We are Ignoring the errors as no need to fail the build.
       
   209                 log("SecurityException while creating the output file : " + getoutputFile() + "   "
       
   210                     + exc.getMessage(), Project.MSG_ERR);
       
   211             }
       
   212 
       
   213             if (output != null) {
       
   214                 // managed to open the output file
       
   215                 try {
       
   216                     // write the initial XML text to the file
       
   217                     String tempLine;
       
   218                     diamondsHeaderFile = null;
       
   219                     diamondsHeaderFile = new BufferedReader(new FileReader(getdiamondsHeaderFileName()));
       
   220                     while ((tempLine = diamondsHeaderFile.readLine()) != null) {
       
   221                         output.write(tempLine);
       
   222                         output.newLine();
       
   223                     }
       
   224                     diamondsHeaderFile.close();
       
   225                     boolean tempheader = getheader();
       
   226                     if (tempheader) {
       
   227                         output.write("    <quality aspect=\"compatibility-headers\"> \r\n");
       
   228                         // process each line
       
   229                         findTextAndOutput(headerExpression); // read input file and write the output
       
   230                     } else {
       
   231                         output.write("    <quality aspect=\"compatibility-libs\"> \r\n");
       
   232                         // process each line
       
   233                         findTextAndOutput(libraryExpression); // read input file and write the output
       
   234                     }
       
   235 
       
   236                     // write the end of file text
       
   237                     output.write("    </quality>");
       
   238                     output.newLine();
       
   239 
       
   240                     diamondsFooterFile = null;
       
   241                     diamondsFooterFile = new BufferedReader(new FileReader(getdiamondsFooterFileName()));
       
   242                     while ((tempLine = diamondsFooterFile.readLine()) != null) {
       
   243                         output.write(tempLine);
       
   244                         output.newLine();
       
   245                     }
       
   246                     output.close(); // close the output file
       
   247                     diamondsFooterFile.close();
       
   248                 }
       
   249                 catch (FileNotFoundException exc) {
       
   250                     log("FileNotFoundException while getting the diamonds header file : "
       
   251                         + getdiamondsHeaderFileName() + "   " + exc.getMessage(), Project.MSG_ERR);
       
   252                 }
       
   253                 catch (IOException exc) {
       
   254                     // We are Ignoring the errors as no need to fail the build.
       
   255                     log("IOException : " + getdiamondsHeaderFileName() + " output file =  "
       
   256                         + getoutputFile() + "  " + exc.getMessage(), Project.MSG_ERR);
       
   257                 }
       
   258                 catch (IllegalArgumentException exc) {
       
   259                     // We are Ignoring the errors as no need to fail the build.
       
   260                     log("IllegalArgumentException : " + getdiamondsHeaderFileName()
       
   261                         + " output file =  " + getoutputFile() + "  " + exc.getMessage(), Project.MSG_ERR);
       
   262                 }
       
   263             }
       
   264             else {
       
   265                 log("Error: no output File available ", Project.MSG_ERR);
       
   266             }
       
   267         }
       
   268         else {
       
   269             log("Error: no input File available ", Project.MSG_ERR);
       
   270         }
       
   271     }
       
   272 
       
   273     /**
       
   274      * This is the function that performs the actual file searches and writes the number of occurances
       
   275      * of each typeID to the output xml file
       
   276      */
       
   277     private void findTextAndOutput(String expression) {
       
   278         String value;
       
   279         Integer count;
       
   280         /** place to store the number of typeids found */
       
   281         Hashtable<Integer, Integer> typeIds = new Hashtable<Integer, Integer>();
       
   282         int tempKey;
       
   283         int tempVal;
       
   284         
       
   285         try {
       
   286             DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
       
   287             DocumentBuilder builder = docFactory.newDocumentBuilder();
       
   288             Document doc = builder.parse(getinputFile());
       
   289     
       
   290             //creating an XPathFactory:
       
   291             XPathFactory factory = XPathFactory.newInstance();
       
   292             //using this factory to create an XPath object: 
       
   293             XPath xpath = factory.newXPath();
       
   294             //XPath object created compiles the XPath expression: 
       
   295             XPathExpression expr = xpath.compile(expression);
       
   296     
       
   297             //expression is evaluated with respect to a certain context node which is doc.
       
   298             Object result = expr.evaluate(doc, XPathConstants.NODESET);
       
   299             NodeList nodeList = (NodeList) result;
       
   300             for (int i = 0; i < nodeList.getLength(); i++) {
       
   301                 value = nodeList.item(i).getNodeValue();    //get the value as a string from the xml file
       
   302                 tempKey = Integer.parseInt(value);          //convert it to an integer so they can be sorted
       
   303                 if (!typeIds.containsKey(tempKey)) {        //see if the typeID is already present in the hashtable
       
   304                     typeIds.put(tempKey, 0);                //it's not so create it (stops null pointer exceptions)
       
   305                 }
       
   306                 count = typeIds.get(tempKey);               //get the current count of this typeID
       
   307                 count++;                                    //inc the count
       
   308                 typeIds.put(tempKey, count);                //write it back to the hashtable
       
   309             }
       
   310             
       
   311             //now sort and write to xml file
       
   312             SortedSet<Integer> sortedset = new TreeSet<Integer>(typeIds.keySet());
       
   313             Iterator<Integer> sorted = sortedset.iterator();
       
   314             while (sorted.hasNext()) {      //go through each one on the file and write to xml output file
       
   315                 tempVal = sorted.next();
       
   316                 output.write("        <summary message=\"type ID " + tempVal + " occurs \" value=\"" + typeIds.get(tempVal) + "\"/> \r\n");
       
   317             }
       
   318         } catch (ParserConfigurationException err) {
       
   319             log("Error: ParserConfigurationException: trying to parse xml file ", Project.MSG_ERR);
       
   320         } catch (SAXException err) {
       
   321             log("Error: SAXException: trying to parse xml file ", Project.MSG_ERR);
       
   322         } catch (XPathExpressionException err) {
       
   323             log("Error: XPathExpressionException: trying to parse xml file ", Project.MSG_ERR);
       
   324         } catch (IOException err) {
       
   325             log("Error: IOException: trying to parse xml file ", Project.MSG_ERR);
       
   326         }
       
   327     }
       
   328 
       
   329 }