From e54778efea45c0cf3e479fd5dbffde69de806ca8 Mon Sep 17 00:00:00 2001 From: Zimin Chen Date: Fri, 4 Jan 2019 11:29:58 +0100 Subject: [PATCH 1/2] All path are now handle by java.nio.file.Path --- data7/src/main/java/data7/Exporter.java | 12 +++---- data7/src/main/java/data7/ResourcesPath.java | 34 ++++++++++--------- .../java/data7/importer/Data7Importer.java | 24 +++++++------ .../java/data7/importer/cwe/CWEImporter.java | 8 ++--- 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/data7/src/main/java/data7/Exporter.java b/data7/src/main/java/data7/Exporter.java index e85c40e..577c054 100644 --- a/data7/src/main/java/data7/Exporter.java +++ b/data7/src/main/java/data7/Exporter.java @@ -60,8 +60,8 @@ public Exporter(ResourcesPath path) { * @throws IOException */ public void saveDataset(Data7 data7) throws IOException { - Utils.checkFolderDestination(path.getBinaryPath()); - FileOutputStream fos = new FileOutputStream(path.getBinaryPath() + data7.getProject().getName() + "-data7.obj", false); + Utils.checkFolderDestination(path.getBinaryPath().toString()); + FileOutputStream fos = new FileOutputStream(path.getBinaryPath().resolve(data7.getProject().getName() + "-data7.obj").toString(), false); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(data7); oos.close(); @@ -77,7 +77,7 @@ public void saveDataset(Data7 data7) throws IOException { */ public void exportDatasetToXML(Data7 data7) { VulnerabilitySet dataset = data7.getVulnerabilitySet(); - Utils.checkFolderDestination(path.getXmlPath()); + Utils.checkFolderDestination(path.getXmlPath().toString()); try { DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); @@ -182,7 +182,7 @@ public void exportDatasetToXML(Data7 data7) { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); - StreamResult result = new StreamResult(path.getXmlPath() + data7.getProject().getName() + "-data7.xml"); + StreamResult result = new StreamResult(path.getXmlPath().resolve(data7.getProject().getName() + "-data7.xml").toString()); transformer.setOutputProperty(OutputKeys.VERSION, "1.0"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); @@ -210,7 +210,7 @@ public void exportDatasetToXML(Data7 data7) { * @throws IOException */ public void saveCWEList(List cweList) throws IOException { - FileOutputStream fos = new FileOutputStream(path.getBinaryPath() + CWE_OBJ, false); + FileOutputStream fos = new FileOutputStream(path.getBinaryPath().resolve(CWE_OBJ).toString(), false); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(cweList); oos.close(); @@ -225,7 +225,7 @@ public void saveCWEList(List cweList) throws IOException { * @throws ClassNotFoundException */ public List loadCWEMist() throws IOException, ClassNotFoundException { - File file = new File(path.getBinaryPath() + CWE_OBJ); + File file = path.getBinaryPath().resolve(CWE_OBJ).toFile(); if (file.exists()) { FileInputStream fileIn = new FileInputStream(file); ObjectInputStream read = new ObjectInputStream(fileIn); diff --git a/data7/src/main/java/data7/ResourcesPath.java b/data7/src/main/java/data7/ResourcesPath.java index ea52a07..190cb91 100644 --- a/data7/src/main/java/data7/ResourcesPath.java +++ b/data7/src/main/java/data7/ResourcesPath.java @@ -1,43 +1,45 @@ package data7; import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; public class ResourcesPath { - private final String savingPath; - private final String binaryPath; - private final String gitPath; - private final String xmlPath; - private final String cvePath; + private final Path savingPath; + private final Path binaryPath; + private final Path gitPath; + private final Path xmlPath; + private final Path cvePath; public ResourcesPath(String path) { - File f = new File(path); + savingPath = Paths.get(path); + File f = savingPath.toFile(); if (f.exists() && f.isDirectory()) { - savingPath = path; - binaryPath = savingPath + "binary/"; - gitPath = savingPath + "git/"; - xmlPath = savingPath + "xml/"; - cvePath = savingPath + "cve/"; + binaryPath = savingPath.resolve("binary"); + gitPath = savingPath.resolve("git"); + xmlPath = savingPath.resolve("xml"); + cvePath = savingPath.resolve("cve"); } else throw new RuntimeException("Path is incorrect or inexisting"); } - public String getSavingPath() { + public Path getSavingPath() { return savingPath; } - public String getBinaryPath() { + public Path getBinaryPath() { return binaryPath; } - public String getGitPath() { + public Path getGitPath() { return gitPath; } - public String getXmlPath() { + public Path getXmlPath() { return xmlPath; } - public String getCvePath() { + public Path getCvePath() { return cvePath; } } diff --git a/data7/src/main/java/data7/importer/Data7Importer.java b/data7/src/main/java/data7/importer/Data7Importer.java index 511b1f1..c76027c 100644 --- a/data7/src/main/java/data7/importer/Data7Importer.java +++ b/data7/src/main/java/data7/importer/Data7Importer.java @@ -14,6 +14,7 @@ import java.io.*; import java.nio.file.Files; +import java.nio.file.Path; import java.text.ParseException; import java.time.Year; import java.util.*; @@ -108,7 +109,7 @@ public Data7 updateOrCreateDataset() throws ParseException, IOException, ClassNo //clone the git Map git = new HashMap<>(); data7.getProject().getSubProjects().forEach((component, metainf) -> { - git.put(component, new GitActions(metainf.getOnlineRepository(), path.getGitPath() + component)); + git.put(component, new GitActions(metainf.getOnlineRepository(), path.getGitPath().resolve(component).toString())); }); //Source To proceed with @@ -185,8 +186,8 @@ private List dowloadRecentXML(long lastUpdated, long currentTime) throws * @throws ParseException */ private boolean checkMetaIfNewerThan(String year, long lastUpdate) throws IOException, ParseException { - Misc.downloadFromURL(CVE_URL + year + META, path.getCvePath()); - File meta = new File(path.getCvePath() + FILE_START + year + META); + Misc.downloadFromURL(CVE_URL + year + META, path.getCvePath().toString()); + File meta = path.getCvePath().resolve(FILE_START + year + META).toFile(); BufferedReader brTest = new BufferedReader(new FileReader(meta)); String date = brTest.readLine(); date = date.replace("lastModifiedDate:", ""); @@ -202,13 +203,13 @@ private boolean checkMetaIfNewerThan(String year, long lastUpdate) throws IOExce * @throws IOException */ private String downloadCVEXML(String year) throws IOException { - checkFolderDestination(path.getCvePath()); - Misc.downloadFromURL(CVE_URL + year + XML, path.getCvePath()); - String fpath = path.getCvePath() + FILE_START + year + XML; - File zip = new File(fpath); - Misc.unzipping(fpath, path.getCvePath()); - Files.delete(zip.toPath()); - return fpath.replace(".zip", ""); + checkFolderDestination(path.getCvePath().toString()); + Misc.downloadFromURL(CVE_URL + year + XML, path.getCvePath().toString()); + Path fpath = path.getCvePath().resolve(FILE_START + year + XML); + File zip = fpath.toFile(); + Misc.unzipping(fpath.toString(), path.getCvePath().toString()); + Files.delete(fpath); + return fpath.toString().replace(".zip", ""); } /** @@ -259,7 +260,8 @@ private List listOfNewCVE(List pathOfXMLFiles) { * @throws ClassNotFoundException */ public Data7 loadDataset() throws IOException, ClassNotFoundException { - File file = new File(path.getBinaryPath() + project + "-data7.obj"); + Path file_path = path.getBinaryPath().resolve(project + "-data7.obj"); + File file = file_path.toFile(); if (file.exists()) { FileInputStream fileIn = new FileInputStream(file); ObjectInputStream read = new ObjectInputStream(fileIn); diff --git a/data7/src/main/java/data7/importer/cwe/CWEImporter.java b/data7/src/main/java/data7/importer/cwe/CWEImporter.java index 13dfe78..e537437 100644 --- a/data7/src/main/java/data7/importer/cwe/CWEImporter.java +++ b/data7/src/main/java/data7/importer/cwe/CWEImporter.java @@ -9,9 +9,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -43,8 +43,8 @@ public CWEImporter(ResourcesPath path){ public List retrieveCWEOnline() throws IOException { - Misc.downloadFromURL(Resources.CWE_URL, path.getCvePath()); - Misc.unzipping(path.getCvePath() + CWE_XML_FILE + ".zip", path.getCvePath()); + Misc.downloadFromURL(Resources.CWE_URL, path.getCvePath().toString()); + Misc.unzipping(path.getCvePath().resolve(CWE_XML_FILE + ".zip").toString(), path.getCvePath().toString()); List cweList = CWEParser.parse(path); new Exporter(path).saveCWEList(cweList); return cweList; From 49a3231dd1ca8448fe2a7cdd4cdc6f3c3a92db6c Mon Sep 17 00:00:00 2001 From: Zimin Chen Date: Fri, 4 Jan 2019 11:33:51 +0100 Subject: [PATCH 2/2] Small fix to remove unnecessary variable --- data7/src/main/java/data7/importer/Data7Importer.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/data7/src/main/java/data7/importer/Data7Importer.java b/data7/src/main/java/data7/importer/Data7Importer.java index c76027c..76776ed 100644 --- a/data7/src/main/java/data7/importer/Data7Importer.java +++ b/data7/src/main/java/data7/importer/Data7Importer.java @@ -260,8 +260,7 @@ private List listOfNewCVE(List pathOfXMLFiles) { * @throws ClassNotFoundException */ public Data7 loadDataset() throws IOException, ClassNotFoundException { - Path file_path = path.getBinaryPath().resolve(project + "-data7.obj"); - File file = file_path.toFile(); + File file = path.getBinaryPath().resolve(project + "-data7.obj").toFile(); if (file.exists()) { FileInputStream fileIn = new FileInputStream(file); ObjectInputStream read = new ObjectInputStream(fileIn);