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