Java tutorial
/* * Copyright 2014 the original author or authors. * * 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. * See the License for the specific language governing permissions and * limitations under the License. */ package io.github.carlomicieli.footballdb.starter.parsers; import io.github.carlomicieli.footballdb.starter.App; import io.github.carlomicieli.footballdb.starter.documents.DocumentDownloader; import io.github.carlomicieli.footballdb.starter.documents.PathBuilder; import io.github.carlomicieli.footballdb.starter.domain.TeamRoster; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; /** * @author Carlo Micieli */ @Component("teamRosterParser") public class TeamRosterParser extends Parser<TeamRoster> { @Autowired public TeamRosterParser(DocumentDownloader docs) { super(docs); } @Override protected PathBuilder pathBuilder() { return PathBuilder.nflDotCom(); } @Override protected TeamRoster parseDocument(Document doc) { Stream<Element> rows = rosterTable(doc).flatMap(TeamRosterParser::rosterTableBody) .map(TeamRosterParser::rosterTableRows).orElseThrow(RuntimeException::new); List<TeamRoster.RosterEntry> players = rows.map(TeamRosterParser::rosterEntryValues) .map(TeamRoster.RosterEntry::of).collect(Collectors.toList()); App.log().info("{} player(s) found.", players.size()); return TeamRoster.builder().players(players).build(); } private static Optional<Element> rosterTable(Document doc) { Element table = doc.select("#result").first(); return Optional.ofNullable(table); } private static Optional<Element> rosterTableBody(Element table) { return table.getElementsByTag("tbody").stream().skip(1).limit(1).findFirst(); } private static Stream<Element> rosterTableRows(Element tableBody) { return tableBody.getElementsByTag("tr").stream(); } private static Map<String, String> rosterEntryValues(Element e) { Map<String, String> values = new HashMap<>(); values.put("number", childValueAsString(e, 0).orElse(null)); values.put("profile", e.children().get(1).getElementsByTag("a").attr("href")); values.put("name", e.children().get(1).getElementsByTag("a").text()); values.put("position", childValueAsString(e, 2).orElse(null)); values.put("status", childValueAsString(e, 3).orElse(null)); values.put("height", childValueAsString(e, 4).orElse(null)); values.put("weight", childValueAsString(e, 5).orElse(null)); values.put("birth_date", childValueAsString(e, 6).orElse(null)); values.put("exp", childValueAsString(e, 7).orElse(null)); values.put("college", childValueAsString(e, 8).orElse(null)); return Collections.unmodifiableMap(values); } }