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