Coverage Report - org.owasp.dependencycheck.data.cwe.CweDB
 
Classes in this File Line Coverage Branch Coverage Complexity
CweDB
56%
13/23
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  
      * The Logger.
 35  
      */
 36  1
     private static final Logger LOGGER = Logger.getLogger(CweDB.class.getName());
 37  
 
 38  
     /**
 39  
      * Empty private constructor as this is a utility class.
 40  
      */
 41  
     private CweDB() {
 42  
         //empty
 43  
     }
 44  
     /**
 45  
      * A HashMap of the CWE data.
 46  
      */
 47  1
     private static final HashMap<String, String> CWE = loadData();
 48  
 
 49  
     /**
 50  
      * Loads a HashMap containing the CWE data from a resource found in the jar.
 51  
      *
 52  
      * @return a HashMap of CWE data
 53  
      */
 54  
     private static HashMap<String, String> loadData() {
 55  1
         ObjectInputStream oin = null;
 56  
         try {
 57  1
             final String filePath = "data/cwe.hashmap.serialized";
 58  1
             final InputStream input = CweDB.class.getClassLoader().getResourceAsStream(filePath);
 59  1
             oin = new ObjectInputStream(input);
 60  
             @SuppressWarnings("unchecked")
 61  1
             final HashMap<String, String> ret = (HashMap<String, String>) oin.readObject();
 62  1
             return ret;
 63  0
         } catch (ClassNotFoundException ex) {
 64  0
             LOGGER.log(Level.WARNING, "Unable to load CWE data. This should not be an issue.");
 65  0
             LOGGER.log(Level.FINE, null, ex);
 66  0
         } catch (IOException ex) {
 67  0
             LOGGER.log(Level.WARNING, "Unable to load CWE data due to an IO Error. This should not be an issue.");
 68  0
             LOGGER.log(Level.FINE, null, ex);
 69  
         } finally {
 70  1
             if (oin != null) {
 71  
                 try {
 72  1
                     oin.close();
 73  0
                 } catch (IOException ex) {
 74  0
                     LOGGER.log(Level.FINEST, null, ex);
 75  2
                 }
 76  
             }
 77  
         }
 78  0
         return null;
 79  
     }
 80  
 
 81  
     /**
 82  
      * <p>
 83  
      * Returns the full CWE name from the CWE ID.</p>
 84  
      *
 85  
      * @param cweId the CWE ID
 86  
      * @return the full name of the CWE
 87  
      */
 88  
     public static String getCweName(String cweId) {
 89  9
         if (cweId != null) {
 90  9
             return CWE.get(cweId);
 91  
         }
 92  0
         return null;
 93  
     }
 94  
 }