dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/HintAnalyzer.java
/**
     * Loads the hint rules file.
     *
     * @throws HintParseException thrown if the XML cannot be parsed.
     */
Loads the hint rules file.
@throws HintParseException thrown if the XML cannot be parsed.
Loads the hint rules file . @throws HintParseException thrown if the XML cannot be parsed .
private void loadHintRules() throws HintParseException {
        final HintParser parser = new HintParser();
        File file = null;
        try {
            hints = parser.parseHints(FileUtils.getResourceAsStream(HINT_RULE_FILE_NAME));
        } catch (HintParseException | SAXException ex) {
            LOGGER.error("Unable to parse the base hint data file");
            LOGGER.debug("Unable to parse the base hint data file", ex);
        }
        final String filePath = Settings.getString(Settings.KEYS.HINTS_FILE);
        if (filePath == null) {
            return;
        }
        boolean deleteTempFile = false;
        try {
            final Pattern uriRx = Pattern.compile("^(https?|file)\\:.*", Pattern.CASE_INSENSITIVE);
            if (uriRx.matcher(filePath).matches()) {
                deleteTempFile = true;
                file = FileUtils.getTempFile("hint", "xml");
                final URL url = new URL(filePath);
                try {
                    Downloader.fetchFile(url, file, false);
                } catch (DownloadFailedException ex) {
                    Downloader.fetchFile(url, file, true);
                }
            } else {
                file = new File(filePath);
                if (!file.exists()) {
                    try (InputStream fromClasspath = FileUtils.getResourceAsStream(filePath)) {
                        if (fromClasspath != null) {
                            deleteTempFile = true;
                            file = FileUtils.getTempFile("hint", "xml");
                            try {
                                org.apache.commons.io.FileUtils.copyInputStreamToFile(fromClasspath, file);
                            } catch (IOException ex) {
                                throw new HintParseException("Unable to locate hints file in classpath", ex);
                            }
                        }
                    }
                }
            }
            if (file != null) {
                try {
                    final Hints newHints = parser.parseHints(file);
                    hints.getHintRules().addAll(newHints.getHintRules());
                    hints.getVendorDuplicatingHintRules().addAll(newHints.getVendorDuplicatingHintRules());
                    LOGGER.debug("{} hint rules were loaded.", hints.getHintRules().size());
                    LOGGER.debug("{} duplicating hint rules were loaded.", hints.getVendorDuplicatingHintRules().size());
                } catch (HintParseException ex) {
                    LOGGER.warn("Unable to parse hint rule xml file '{}'", file.getPath());
                    LOGGER.warn(ex.getMessage());
                    LOGGER.debug("", ex);
                    throw ex;
                }
            }
        } catch (DownloadFailedException ex) {
            throw new HintParseException("Unable to fetch the configured hint file", ex);
        } catch (MalformedURLException ex) {
            throw new HintParseException("Configured hint file has an invalid URL", ex);
        } catch (IOException ex) {
            throw new HintParseException("Unable to create temp file for hints", ex);
        } finally {
            if (deleteTempFile && file != null) {
                FileUtils.delete(file);
            }
        }
    }
private void loadHintRules ( ) throws HintParseException { final HintParser parser = new HintParser ( ) ; File file = null ; try { hints = parser . parseHints ( FileUtils . getResourceAsStream ( HINT_RULE_FILE_NAME ) ) ; } catch ( HintParseException | SAXException ex ) { LOGGER . error ( "Unable to parse the base hint data file" ) ; LOGGER . debug ( "Unable to parse the base hint data file" , ex ) ; } final String filePath = Settings . getString ( Settings . KEYS . HINTS_FILE ) ; if ( filePath == null ) { return ; } boolean deleteTempFile = false ; try { final Pattern uriRx = Pattern . compile ( "^(https?|file)\\:.*" , Pattern . CASE_INSENSITIVE ) ; if ( uriRx . matcher ( filePath ) . matches ( ) ) { deleteTempFile = true ; file = FileUtils . getTempFile ( "hint" , "xml" ) ; final URL url = new URL ( filePath ) ; try { Downloader . fetchFile ( url , file , false ) ; } catch ( DownloadFailedException ex ) { Downloader . fetchFile ( url , file , true ) ; } } else { file = new File ( filePath ) ; if ( ! file . exists ( ) ) { try ( InputStream fromClasspath = FileUtils . getResourceAsStream ( filePath ) ) { if ( fromClasspath != null ) { deleteTempFile = true ; file = FileUtils . getTempFile ( "hint" , "xml" ) ; try { org . apache . commons . io . FileUtils . copyInputStreamToFile ( fromClasspath , file ) ; } catch ( IOException ex ) { throw new HintParseException ( "Unable to locate hints file in classpath" , ex ) ; } } } } } if ( file != null ) { try { final Hints newHints = parser . parseHints ( file ) ; hints . getHintRules ( ) . addAll ( newHints . getHintRules ( ) ) ; hints . getVendorDuplicatingHintRules ( ) . addAll ( newHints . getVendorDuplicatingHintRules ( ) ) ; LOGGER . debug ( "{} hint rules were loaded." , hints . getHintRules ( ) . size ( ) ) ; LOGGER . debug ( "{} duplicating hint rules were loaded." , hints . getVendorDuplicatingHintRules ( ) . size ( ) ) ; } catch ( HintParseException ex ) { LOGGER . warn ( "Unable to parse hint rule xml file '{}'" , file . getPath ( ) ) ; LOGGER . warn ( ex . getMessage ( ) ) ; LOGGER . debug ( "" , ex ) ; throw ex ; } } } catch ( DownloadFailedException ex ) { throw new HintParseException ( "Unable to fetch the configured hint file" , ex ) ; } catch ( MalformedURLException ex ) { throw new HintParseException ( "Configured hint file has an invalid URL" , ex ) ; } catch ( IOException ex ) { throw new HintParseException ( "Unable to create temp file for hints" , ex ) ; } finally { if ( deleteTempFile && file != null ) { FileUtils . delete ( file ) ; } } }
Loads the hint rules file.
Loads the hint rules file .