|
9 | 9 | package de.rub.nds.scanner.core.guideline; |
10 | 10 |
|
11 | 11 | import de.rub.nds.scanner.core.probe.AnalyzedProperty; |
| 12 | +import de.rub.nds.scanner.core.report.ScanReport; |
12 | 13 | import de.rub.nds.scanner.core.util.JaxbSerializer; |
13 | 14 | import jakarta.xml.bind.JAXBContext; |
14 | 15 | import jakarta.xml.bind.JAXBException; |
15 | | -import java.util.HashSet; |
16 | | -import java.util.Set; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.net.JarURLConnection; |
| 19 | +import java.net.URISyntaxException; |
| 20 | +import java.net.URL; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.nio.file.Path; |
| 23 | +import java.nio.file.Paths; |
| 24 | +import java.util.*; |
| 25 | +import java.util.jar.JarEntry; |
| 26 | +import java.util.jar.JarFile; |
| 27 | +import java.util.stream.Stream; |
| 28 | +import javax.xml.stream.XMLStreamException; |
17 | 29 | import org.apache.logging.log4j.LogManager; |
18 | 30 | import org.apache.logging.log4j.Logger; |
19 | 31 | import org.reflections.Reflections; |
20 | 32 | import org.reflections.util.ClasspathHelper; |
21 | 33 | import org.reflections.util.ConfigurationBuilder; |
22 | 34 | import org.reflections.util.FilterBuilder; |
23 | 35 |
|
24 | | -public final class GuidelineIO extends JaxbSerializer<Guideline<?>> { |
| 36 | +public final class GuidelineIO<ReportT extends ScanReport> |
| 37 | + extends JaxbSerializer<Guideline<ReportT>> { |
25 | 38 |
|
26 | 39 | private Logger LOGGER = LogManager.getLogger(); |
27 | 40 |
|
@@ -58,4 +71,70 @@ private JAXBContext getJAXBContext() throws JAXBException { |
58 | 71 |
|
59 | 72 | return context; |
60 | 73 | } |
| 74 | + |
| 75 | + private static List<String> listXmlFiles(ClassLoader classLoader, String folder) |
| 76 | + throws IOException, URISyntaxException { |
| 77 | + List<String> xmlFilePaths = new ArrayList<>(); |
| 78 | + URL url = classLoader.getResource(folder); |
| 79 | + |
| 80 | + if (url == null) { |
| 81 | + throw new IOException("Folder not found: " + folder); |
| 82 | + } |
| 83 | + |
| 84 | + String protocol = url.getProtocol(); |
| 85 | + |
| 86 | + if ("file".equals(protocol)) { |
| 87 | + // Development mode - reading directly from filesystem |
| 88 | + Path path = Paths.get(url.toURI()); |
| 89 | + try (Stream<Path> paths = Files.list(path)) { |
| 90 | + paths.filter(p -> p.toString().endsWith(".xml")) |
| 91 | + .forEach(p -> xmlFilePaths.add(folder + "/" + p.getFileName().toString())); |
| 92 | + } |
| 93 | + } else if ("jar".equals(protocol)) { |
| 94 | + // Running from a jar |
| 95 | + JarURLConnection jarConnection = (JarURLConnection) url.openConnection(); |
| 96 | + try (JarFile jarFile = jarConnection.getJarFile()) { |
| 97 | + Enumeration<JarEntry> entries = jarFile.entries(); |
| 98 | + while (entries.hasMoreElements()) { |
| 99 | + JarEntry entry = entries.nextElement(); |
| 100 | + String name = entry.getName(); |
| 101 | + if (name.startsWith(folder + "/") |
| 102 | + && name.endsWith(".xml") |
| 103 | + && !entry.isDirectory()) { |
| 104 | + xmlFilePaths.add(name); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } else { |
| 109 | + throw new IOException("Unsupported protocol: " + protocol); |
| 110 | + } |
| 111 | + |
| 112 | + return xmlFilePaths; |
| 113 | + } |
| 114 | + |
| 115 | + public List<Guideline<ReportT>> readGuidelines(ClassLoader classLoader, String subFolder) { |
| 116 | + |
| 117 | + LOGGER.debug("Loading guidelines from files..."); |
| 118 | + |
| 119 | + List<Guideline<ReportT>> guidelines = new ArrayList<>(); |
| 120 | + |
| 121 | + try { |
| 122 | + // Get all files in guideline folder |
| 123 | + List<String> xmlFilePaths = listXmlFiles(classLoader, subFolder); |
| 124 | + |
| 125 | + for (String path : xmlFilePaths) { |
| 126 | + try (InputStream input = classLoader.getResourceAsStream(path)) { |
| 127 | + if (input != null) { |
| 128 | + guidelines.add(read(input)); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + } catch (XMLStreamException | IOException | JAXBException | URISyntaxException e) { |
| 134 | + LOGGER.error("Error reading guideline reports", e); |
| 135 | + return new ArrayList<>(); |
| 136 | + } |
| 137 | + |
| 138 | + return guidelines; |
| 139 | + } |
61 | 140 | } |
0 commit comments