View Javadoc

    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      private CweDB() {
    38          //empty
    39      }
    40      /**
    41       * A HashMap of the CWE data.
    42       */
    43      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          ObjectInputStream oin = null;
    52          try {
    53              final String filePath = "data/cwe.hashmap.serialized";
    54              final InputStream input = CweDB.class.getClassLoader().getResourceAsStream(filePath);
    55              oin = new ObjectInputStream(input);
    56              return (HashMap<String, String>) oin.readObject();
    57          } catch (ClassNotFoundException ex) {
    58              Logger.getLogger(CweDB.class.getName()).log(Level.WARNING, "Unable to load CWE data. This should not be an issue.");
    59              Logger.getLogger(CweDB.class.getName()).log(Level.FINE, null, ex);
    60          } catch (IOException ex) {
    61              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              Logger.getLogger(CweDB.class.getName()).log(Level.FINE, null, ex);
    63          } finally {
    64              if (oin != null) {
    65                  try {
    66                      oin.close();
    67                  } catch (IOException ex) {
    68                      Logger.getLogger(CweDB.class.getName()).log(Level.FINEST, null, ex);
    69                  }
    70              }
    71          }
    72          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          if (cweId != null) {
    83              return CWE.get(cweId);
    84          }
    85          return null;
    86      }
    87  }