@Test public void parse() throws Exception { Path file = Paths.get("src/test/resources/maven-settings.xml"); MavenSettings settings = MavenSettings.parse(file); assertThat(settings.getServers(), hasSize(4)); assertThat(settings.getServers().get(3).getId(), is("sonatype-nexus-staging")); assertThat(settings.getServers().get(3).getUsername(), is("dude")); assertThat(settings.getServers().get(3).getPassword(), is("1&3")); }
static public MavenSettings parse( Path file) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final MavenSettings settings = new MavenSettings(); try (InputStream input = Files.newInputStream(file)) { Document doc = builder.parse(input); final NodeList serversNodes = doc.getElementsByTagName("servers"); if (serversNodes == null || serversNodes.getLength() == 0) { return null; } else if (serversNodes.getLength() > 1) { throw new IOException("Invalid " + file + " file. Wanted 1 'servers' but got " + serversNodes.getLength()); } else { final NodeList serverNodes = serversNodes.item(0).getChildNodes(); for (int i = 0; i < serverNodes.getLength(); i++) { final Node serverNode = serverNodes.item(i); if (serverNode instanceof Element) { final Element serverElem = (Element)serverNode; final MavenServer server = new MavenServer(); ofNullable(getElementByTagName(serverElem, "id")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setId(v)); ofNullable(getElementByTagName(serverElem, "username")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setUsername(v)); ofNullable(getElementByTagName(serverElem, "password")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setPassword(v)); settings.addServer(server); } } } } return settings; }
MavenSettings { static public MavenSettings parse( Path file) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final MavenSettings settings = new MavenSettings(); try (InputStream input = Files.newInputStream(file)) { Document doc = builder.parse(input); final NodeList serversNodes = doc.getElementsByTagName("servers"); if (serversNodes == null || serversNodes.getLength() == 0) { return null; } else if (serversNodes.getLength() > 1) { throw new IOException("Invalid " + file + " file. Wanted 1 'servers' but got " + serversNodes.getLength()); } else { final NodeList serverNodes = serversNodes.item(0).getChildNodes(); for (int i = 0; i < serverNodes.getLength(); i++) { final Node serverNode = serverNodes.item(i); if (serverNode instanceof Element) { final Element serverElem = (Element)serverNode; final MavenServer server = new MavenServer(); ofNullable(getElementByTagName(serverElem, "id")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setId(v)); ofNullable(getElementByTagName(serverElem, "username")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setUsername(v)); ofNullable(getElementByTagName(serverElem, "password")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setPassword(v)); settings.addServer(server); } } } } return settings; } }
MavenSettings { static public MavenSettings parse( Path file) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final MavenSettings settings = new MavenSettings(); try (InputStream input = Files.newInputStream(file)) { Document doc = builder.parse(input); final NodeList serversNodes = doc.getElementsByTagName("servers"); if (serversNodes == null || serversNodes.getLength() == 0) { return null; } else if (serversNodes.getLength() > 1) { throw new IOException("Invalid " + file + " file. Wanted 1 'servers' but got " + serversNodes.getLength()); } else { final NodeList serverNodes = serversNodes.item(0).getChildNodes(); for (int i = 0; i < serverNodes.getLength(); i++) { final Node serverNode = serverNodes.item(i); if (serverNode instanceof Element) { final Element serverElem = (Element)serverNode; final MavenServer server = new MavenServer(); ofNullable(getElementByTagName(serverElem, "id")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setId(v)); ofNullable(getElementByTagName(serverElem, "username")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setUsername(v)); ofNullable(getElementByTagName(serverElem, "password")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setPassword(v)); settings.addServer(server); } } } } return settings; }  }
MavenSettings { static public MavenSettings parse( Path file) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final MavenSettings settings = new MavenSettings(); try (InputStream input = Files.newInputStream(file)) { Document doc = builder.parse(input); final NodeList serversNodes = doc.getElementsByTagName("servers"); if (serversNodes == null || serversNodes.getLength() == 0) { return null; } else if (serversNodes.getLength() > 1) { throw new IOException("Invalid " + file + " file. Wanted 1 'servers' but got " + serversNodes.getLength()); } else { final NodeList serverNodes = serversNodes.item(0).getChildNodes(); for (int i = 0; i < serverNodes.getLength(); i++) { final Node serverNode = serverNodes.item(i); if (serverNode instanceof Element) { final Element serverElem = (Element)serverNode; final MavenServer server = new MavenServer(); ofNullable(getElementByTagName(serverElem, "id")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setId(v)); ofNullable(getElementByTagName(serverElem, "username")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setUsername(v)); ofNullable(getElementByTagName(serverElem, "password")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setPassword(v)); settings.addServer(server); } } } } return settings; }  List<MavenServer> getServers(); void setServers(List<MavenServer> servers); void addServer(MavenServer server); MavenServer findServerById(String id); static MavenSettings parse(
            Path file); }
MavenSettings { static public MavenSettings parse( Path file) throws Exception { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); final DocumentBuilder builder = factory.newDocumentBuilder(); final MavenSettings settings = new MavenSettings(); try (InputStream input = Files.newInputStream(file)) { Document doc = builder.parse(input); final NodeList serversNodes = doc.getElementsByTagName("servers"); if (serversNodes == null || serversNodes.getLength() == 0) { return null; } else if (serversNodes.getLength() > 1) { throw new IOException("Invalid " + file + " file. Wanted 1 'servers' but got " + serversNodes.getLength()); } else { final NodeList serverNodes = serversNodes.item(0).getChildNodes(); for (int i = 0; i < serverNodes.getLength(); i++) { final Node serverNode = serverNodes.item(i); if (serverNode instanceof Element) { final Element serverElem = (Element)serverNode; final MavenServer server = new MavenServer(); ofNullable(getElementByTagName(serverElem, "id")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setId(v)); ofNullable(getElementByTagName(serverElem, "username")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setUsername(v)); ofNullable(getElementByTagName(serverElem, "password")) .map(v -> v.getTextContent()) .ifPresent(v -> server.setPassword(v)); settings.addServer(server); } } } } return settings; }  List<MavenServer> getServers(); void setServers(List<MavenServer> servers); void addServer(MavenServer server); MavenServer findServerById(String id); static MavenSettings parse(
            Path file);  }