Coverage Report - org.owasp.dependencycheck.data.cwe.CweDB
 
Classes in this File Line Coverage Branch Coverage Complexity
CweDB
47%
11/23
50%
2/4
3.333
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.data.cwe;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.ObjectInputStream;
 24  
 import java.util.HashMap;
 25  
 import java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 
 28  
 /**
 29  
  *
 30  
  * @author Jeremy Long <jeremy.long@owasp.org>
 31  
  */
 32  
 public final class CweDB {
 33  
 
 34  
     /**
 35  
      * Empty private constructor as this is a utility class.
 36  
      */
 37  0
     private CweDB() {
 38  
         //empty
 39  0
     }
 40  
     /**
 41  
      * A HashMap of the CWE data.
 42  
      */
 43  1
     private static final HashMap<String, String> CWE = loadData();
 44  
 
 45  
     /**
 46  
      * Loads a HashMap containing the CWE data from a resource found in the jar.
 47  
      *
 48  
      * @return a HashMap of CWE data
 49  
      */
 50  
     private static HashMap<String, String> loadData() {
 51  1
         ObjectInputStream oin = null;
 52  
         try {
 53  1
             final String filePath = "data/cwe.hashmap.serialized";
 54  1
             final InputStream input = CweDB.class.getClassLoader().getResourceAsStream(filePath);
 55  1
             oin = new ObjectInputStream(input);
 56  1
             return (HashMap<String, String>) oin.readObject();
 57  0
         } catch (ClassNotFoundException ex) {
 58  0
             Logger.getLogger(CweDB.class.getName()).log(Level.WARNING, "Unable to load CWE data. This should not be an issue.");
 59  0
             Logger.getLogger(CweDB.class.getName()).log(Level.FINE, null, ex);
 60  0
         } catch (IOException ex) {
 61  0
             Logger.getLogger(CweDB.class.getName()).log(Level.WARNING, "Unable to load CWE data due to an IO Error. This should not be an issue.");
 62  0
             Logger.getLogger(CweDB.class.getName()).log(Level.FINE, null, ex);
 63  
         } finally {
 64  1
             if (oin != null) {
 65  
                 try {
 66  1
                     oin.close();
 67  0
                 } catch (IOException ex) {
 68  0
                     Logger.getLogger(CweDB.class.getName()).log(Level.FINEST, null, ex);
 69  2
                 }
 70  
             }
 71  
         }
 72  0
         return null;
 73  
     }
 74  
 
 75  
     /**
 76  
      * <p>Returns the full CWE name from the CWE ID.</p>
 77  
      *
 78  
      * @param cweId the CWE ID
 79  
      * @return the full name of the CWE
 80  
      */
 81  
     public static String getCweName(String cweId) {
 82  51
         if (cweId != null) {
 83  51
             return CWE.get(cweId);
 84  
         }
 85  0
         return null;
 86  
     }
 87  
 }