RegistryParser { public RegistryKey parseFile(File registryFile, String rootName) { try (BufferedReader bufferReader = new BufferedReader(new FileReader(registryFile))) { final RegistryKey root = new RegistryKey(rootName); RegistryKey lastNode = null; int lineNumber = 1; for (String currentLine = bufferReader.readLine(); currentLine != null; currentLine = bufferReader .readLine()) { while (currentLine.trim().endsWith("\\")) { currentLine = StringUtils.substring(currentLine.trim(), 0, -1) + bufferReader.readLine().trim(); } if (currentLine.startsWith(";") || currentLine.startsWith("#") || StringUtils.isBlank(currentLine) || currentLine.startsWith("@")) { lineNumber++; continue; } if (currentLine.startsWith("[")) { lastNode = this.parseDirectoryLine(root, currentLine); } else if (lineNumber == 1) { lineNumber++; continue; } else if (lastNode == null) { throw new ParseException(String.format(PARSE_ERROR_MESSAGE, lineNumber), 0); } else { this.parseValueLine(lastNode, currentLine, lineNumber); } lineNumber++; } return root; } catch (IOException | ParseException e) { throw new IllegalArgumentException("Error while parsing the registry", e); } }  RegistryKey parseFile(File registryFile, String rootName);  }
@Test public void testRegistryParserParseSimpleFileTestKeyTree() throws IOException { File temporaryFile = File.createTempFile("registry", "test"); temporaryFile.deleteOnExit(); try (FileOutputStream outputStream = new FileOutputStream(temporaryFile)) { byte[] bytes = ("[A\\\\B\\\\C] 1430602912\n" + "\"C\"=\"1\"\n" + "\"D\"=\"2\"\n" + "\"E\"=\"3\"") .getBytes(); outputStream.write(bytes); outputStream.flush(); } RegistryParser registryParser = new RegistryParser(); RegistryKey node = registryParser.parseFile(temporaryFile, "Temporary"); RegistryKey levelOneNode = (RegistryKey) node.getChild(0); RegistryKey levelTwoNode = (RegistryKey) levelOneNode.getChild(0); RegistryKey levelThreeNode = (RegistryKey) levelTwoNode.getChild(0); assertEquals("Temporary", node.getName()); assertEquals("A", levelOneNode.getName()); assertEquals(1, node.getChildren().size()); assertEquals("B", levelTwoNode.getName()); assertEquals(1, levelOneNode.getChildren().size()); assertEquals("C", levelThreeNode.getName()); assertEquals(1, levelTwoNode.getChildren().size()); }
@Test public void testParseRealRegFileTestObjectPopulated() throws URISyntaxException { File registryFile = new File(this.getClass().getResource("user.reg").toURI()); RegistryParser registryParser = new RegistryParser(); RegistryKey parsedFile = registryParser.parseFile(registryFile, "User"); assertEquals(1542, parsedFile.toString().split("\n").length); }
@Test public void testRegistryParserWineBug37575ValueIsCorrectlyParsed() throws IOException { File temporaryFile = File.createTempFile("registry", "test"); RegistryParser registryParser = new RegistryParser(); temporaryFile.deleteOnExit(); try (FileOutputStream outputStream = new FileOutputStream(temporaryFile)) { byte[] bytes = ("[Software\\\\Wine\\\\DllOverrides] 1431283548\n" + "\"*d3dx9_24\"=\"native, builtin\\0\"\n" + "\"*d3dx9_25\"=\"native, builtin\\0\"\n" + "\"*d3dx9_26\"=\"native, builtin\\0\"").getBytes(); outputStream.write(bytes); outputStream.flush(); } AbstractRegistryNode registryNode = registryParser.parseFile(temporaryFile, "Temporary").getChild("Software", "Wine", "DllOverrides", "*d3dx9_24"); RegistryValue<?> registryValue = null; if (registryNode instanceof RegistryValue) { registryValue = (RegistryValue<?>) registryNode; } assertEquals("*d3dx9_24", registryValue.getName()); assertEquals("native, builtin", registryValue.getText()); }