Coverage Report - org.owasp.dependencycheck.data.cwe.CweDB
 
Classes in this File Line Coverage Branch Coverage Complexity
CweDB
52%
11/21
20%
2/10
3.333
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 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  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.cwe;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.io.InputStream;
 22  
 import java.io.ObjectInputStream;
 23  
 import java.util.HashMap;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 
 27  
 /**
 28  
  *
 29  
  * @author Jeremy Long <jeremy.long@owasp.org>
 30  
  */
 31  
 public final class CweDB {
 32  
 
 33  
     /**
 34  
      * Empty private constructor as this is a utility class.
 35  
      */
 36  
     private CweDB() {
 37  
         //empty
 38  
     }
 39  
     /**
 40  
      * A HashMap of the CWE data.
 41  
      */
 42  1
     private static final HashMap<String, String> CWE = loadData();
 43  
 
 44  
     /**
 45  
      * Loads a HashMap containing the CWE data from a resource found in the jar.
 46  
      *
 47  
      * @return a HashMap of CWE data
 48  
      */
 49  
     private static HashMap<String, String> loadData() {
 50  1
         ObjectInputStream oin = null;
 51  
         try {
 52  1
             final String filePath = "data/cwe.hashmap.serialized";
 53  1
             final InputStream input = CweDB.class.getClassLoader().getResourceAsStream(filePath);
 54  1
             oin = new ObjectInputStream(input);
 55  1
             return (HashMap<String, String>) oin.readObject();
 56  0
         } catch (ClassNotFoundException ex) {
 57  0
             Logger.getLogger(CweDB.class.getName()).log(Level.WARNING, "Unable to load CWE data. This should not be an issue.");
 58  0
             Logger.getLogger(CweDB.class.getName()).log(Level.FINE, null, ex);
 59  0
         } catch (IOException ex) {
 60  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.");
 61  0
             Logger.getLogger(CweDB.class.getName()).log(Level.FINE, null, ex);
 62  
         } finally {
 63  1
             if (oin != null) {
 64  
                 try {
 65  1
                     oin.close();
 66  0
                 } catch (IOException ex) {
 67  0
                     Logger.getLogger(CweDB.class.getName()).log(Level.FINEST, null, ex);
 68  2
                 }
 69  
             }
 70  
         }
 71  0
         return null;
 72  
     }
 73  
 
 74  
     /**
 75  
      * <p>
 76  
      * 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  
 }