diff --git a/.gitignore b/.gitignore index 288436f5c22977bf0acbde4e6d3455f50a0ca91c..1babe91b82a5dca2e3808d5950acf6ea1b8e37b2 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ .classpath .DS_Store +/*.xml +/*.json diff --git a/pom.xml b/pom.xml index 2b9412c35866a10aa29d27c6cbf60fa2171344f9..1b4fe3f6d6a218021839c3c3206b3939fcbda83f 100644 --- a/pom.xml +++ b/pom.xml @@ -33,5 +33,63 @@ <version>5.5.2</version> <scope>test</scope> </dependency> + + + <dependency> + <groupId>javax.xml.bind</groupId> + <artifactId>jaxb-api</artifactId> + <version>2.3.1</version> + </dependency> + <dependency> + <groupId>com.sun.xml.bind</groupId> + <artifactId>jaxb-core</artifactId> + <version>4.0.1</version> + </dependency> + <dependency> + <groupId>com.sun.xml.bind</groupId> + <artifactId>jaxb-impl</artifactId> + <version>4.0.1</version> + </dependency> + <!-- https://mvnrepository.com/artifact/javax.activation/activation --> + <dependency> + <groupId>javax.activation</groupId> + <artifactId>activation</artifactId> + <version>1.1.1</version> + </dependency> + + <dependency> + <groupId>jakarta.xml.bind</groupId> + <artifactId>jakarta.xml.bind-api</artifactId> + <version>4.0.0</version> + </dependency> + + <!-- https://mvnrepository.com/artifact/org.eclipse.persistence/org.eclipse.persistence.moxy --> + <dependency> + <groupId>org.eclipse.persistence</groupId> + <artifactId>org.eclipse.persistence.moxy</artifactId> + <version>4.0.0</version> + </dependency> + + <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-xjc --> + <dependency> + <groupId>com.sun.xml.bind</groupId> + <artifactId>jaxb-xjc</artifactId> + <version>4.0.1</version> + </dependency> + + <!-- https://mvnrepository.com/artifact/jakarta.json/jakarta.json-api --> + <dependency> + <groupId>jakarta.json</groupId> + <artifactId>jakarta.json-api</artifactId> + <version>2.1.1</version> + </dependency> + + <!-- https://mvnrepository.com/artifact/org.eclipse.parsson/parsson --> + <dependency> + <groupId>org.eclipse.parsson</groupId> + <artifactId>parsson</artifactId> + <version>1.1.1</version> + </dependency> + </dependencies> </project> diff --git a/src/main/java/cz/jezek/lab11/Match.java b/src/main/java/cz/jezek/lab11/Match.java index 00f2851654a1d6d954cc57bb12c10c81111d72bd..cb63d541fe048d8f9a6987d647bbdeefc203b427 100644 --- a/src/main/java/cz/jezek/lab11/Match.java +++ b/src/main/java/cz/jezek/lab11/Match.java @@ -2,12 +2,23 @@ package cz.jezek.lab11; import java.util.Objects; +import jakarta.xml.bind.annotation.XmlAttribute; +import jakarta.xml.bind.annotation.XmlElement; + public class Match { + @XmlElement private Oponents oponents; + + @XmlAttribute private int player1Scorel; + + @XmlAttribute private int player2Scorel; + public Match() { + } + public Match(Player player1, int player1Scorel, Player player2, int player2Scorel) { this.oponents = new Oponents(player1, player2); this.player1Scorel = player1Scorel; diff --git a/src/main/java/cz/jezek/lab11/Oponents.java b/src/main/java/cz/jezek/lab11/Oponents.java index 1de0f68f0823e939f4bde09bcc3b2f4ee84697cc..a703cfe6c96255793ef0157a43bcf7b7edef924e 100644 --- a/src/main/java/cz/jezek/lab11/Oponents.java +++ b/src/main/java/cz/jezek/lab11/Oponents.java @@ -2,13 +2,23 @@ package cz.jezek.lab11; import java.util.Objects; +import jakarta.xml.bind.annotation.XmlAttribute; +import jakarta.xml.bind.annotation.XmlIDREF; + public class Oponents { + @XmlAttribute + @XmlIDREF private Player player1; + + @XmlAttribute + @XmlIDREF private Player player2; + public Oponents() { + } + public Oponents(Player player1, Player player2) { - super(); this.player1 = player1; this.player2 = player2; } diff --git a/src/main/java/cz/jezek/lab11/Player.java b/src/main/java/cz/jezek/lab11/Player.java index 2a27b1b969e9d93d8144da9ab23f330eb8d3c13b..e152e5ab7d2cef0fe38baed7b0941f0efe16c01f 100644 --- a/src/main/java/cz/jezek/lab11/Player.java +++ b/src/main/java/cz/jezek/lab11/Player.java @@ -1,5 +1,8 @@ package cz.jezek.lab11; +import jakarta.xml.bind.annotation.XmlAttribute; +import jakarta.xml.bind.annotation.XmlID; + public class Player { private String firstName; @@ -9,9 +12,11 @@ public class Player { return new Player(RandomGenerator.selectRandom(RandomGenerator.NAMES), RandomGenerator.selectRandom(RandomGenerator.SURNAMES)); } + + public Player() { + } public Player(String firstName, String lastName) { - super(); this.firstName = firstName; this.lastName = lastName; } @@ -31,6 +36,12 @@ public class Player { public void setLastName(String lastName) { this.lastName = lastName; } + + @XmlID + @XmlAttribute + public String getFullName() { + return toString(); + } @Override public int hashCode() { diff --git a/src/main/java/cz/jezek/lab11/StreamMain.java b/src/main/java/cz/jezek/lab11/StreamMain.java index ea4babca284042d2a29e402e622abee1e01f3158..fe1c10f6d267c9b228329895ad380b6c66afa249 100644 --- a/src/main/java/cz/jezek/lab11/StreamMain.java +++ b/src/main/java/cz/jezek/lab11/StreamMain.java @@ -1,44 +1,129 @@ package cz.jezek.lab11; +import static java.util.Arrays.asList; + +import java.io.File; + +import org.eclipse.persistence.jaxb.MarshallerProperties; + +import jakarta.xml.bind.JAXBContext; +import jakarta.xml.bind.JAXBException; +import jakarta.xml.bind.Marshaller; +import jakarta.xml.bind.Unmarshaller; + public class StreamMain { - public static void main(String[] args) { - Tournament tournament = Tournament.generate(); - - printStatAboutTournament(tournament); - - saveTournament(tournament); - loadTournament(); - } - - public static void printStatAboutTournament(Tournament tournament) { - System.out.println(tournament); - Player player = tournament.getRandomPlayer(); - - System.out.println(player); - System.out.print("Number of matches: "); - //TODO 3.a Show the number of matches played by a given player (using Streams) - - System.out.print("Goals scored: "); - //TODO 3.b Show the number of goals scored by a given player (help Streams) - - Oponents oponents = new Oponents(tournament.getRandomPlayer(), - tournament.getRandomPlayer()); - System.out.println("Mutual statistics of oponents:" + oponents); - - System.out.print("Matches: "); - //TODO 4.a List the matches played between the given players (using the stream) - System.out.print("Number of mathes: "); - //TODO 4.b Print the number of matches played between the given players (using the stream) - System.out.print("Match with the largest total number:"); - //TODO 4.c Show match with the largest total number (sum of both players) scored goals between relevant players (help Streams) - } - - public static void loadTournament() { - //TODO 5.b Load a serialized tournament from a file - } - - public static void saveTournament(Tournament tournament) { - //TODO 5.a Save the tournament to a file using object serialization. - } + public static void main(String[] args) { + + System.setProperty("jakarta.xml.bind.JAXBContextFactory", "org.eclipse.persistence.jaxb.JAXBContextFactory"); + Player player1 = new Player("John", "Doe"); + Player player2 = new Player("Marry", "Doe"); + Match match = new Match(player1, 5, player2, 3); + Tournament tournament = new Tournament(asList(player1, player2), asList(match)); + + saveTournamentsAsXML(tournament); + + System.out.println("tournamen.player1 == tournamen.match.player1: " + + (tournament.getPlayers().get(0) == tournament.getMatches().get(0).getOponents().getPlayer1())); + + Tournament tournament2 = loadTournamentFromXML(); + + System.out.println(tournament2); + + System.out.println("tournament2.player1 == tournament2.match.player1: " + + (tournament2.getPlayers().get(0) == tournament2.getMatches().get(0).getOponents().getPlayer1())); + + + saveTournamentsAsJSON(tournament); + tournament2 = loadTournamentFromJSON(); + System.out.println(tournament2); + System.out.println("tournament2.player1 == tournament2.match.player1: " + + (tournament2.getPlayers().get(0) == tournament2.getMatches().get(0).getOponents().getPlayer1())); + + } + + private static Tournament loadTournamentFromXML() { + try { + JAXBContext context = JAXBContext.newInstance(Tournament.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (Tournament) unmarshaller.unmarshal(new File("tournament.xml")); + } catch (JAXBException e) { + e.printStackTrace(); + } + + return null; + } + + private static Tournament loadTournamentFromJSON() { + try { + JAXBContext context = JAXBContext.newInstance(Tournament.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + unmarshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); + return (Tournament) unmarshaller.unmarshal(new File("tournament.json")); + } catch (JAXBException e) { + e.printStackTrace(); + } + + return null; + } + + private static void saveTournamentsAsXML(Tournament tournament) { + + try { + JAXBContext context = JAXBContext.newInstance(Tournament.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.marshal(tournament, System.out); + marshaller.marshal(tournament, new File("tournament.xml")); + } catch (JAXBException e) { + e.printStackTrace(); + } + } + + private static void saveTournamentsAsJSON(Tournament tournament) { + + try { + JAXBContext context = JAXBContext.newInstance(Tournament.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true); + marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json"); + marshaller.marshal(tournament, System.out); + marshaller.marshal(tournament, new File("tournament.json")); + } catch (JAXBException e) { + e.printStackTrace(); + } + } + + public static void printStatAboutTournament(Tournament tournament) { + System.out.println(tournament); + Player player = tournament.getRandomPlayer(); + + System.out.println(player); + System.out.print("Number of matches: "); + // TODO 3.a Show the number of matches played by a given player (using Streams) + + System.out.print("Goals scored: "); + // TODO 3.b Show the number of goals scored by a given player (help Streams) + + Oponents oponents = new Oponents(tournament.getRandomPlayer(), tournament.getRandomPlayer()); + System.out.println("Mutual statistics of oponents:" + oponents); + + System.out.print("Matches: "); + // TODO 4.a List the matches played between the given players (using the stream) + System.out.print("Number of mathes: "); + // TODO 4.b Print the number of matches played between the given players (using + // the stream) + System.out.print("Match with the largest total number:"); + // TODO 4.c Show match with the largest total number (sum of both players) + // scored goals between relevant players (help Streams) + } + + public static void loadTournament() { + // TODO 5.b Load a serialized tournament from a file + } + + public static void saveTournament(Tournament tournament) { + // TODO 5.a Save the tournament to a file using object serialization. + } } diff --git a/src/main/java/cz/jezek/lab11/Tournament.java b/src/main/java/cz/jezek/lab11/Tournament.java index 8442be38135239116933fae58bab7e982522eac9..3077ed024602a9c89a7c769426e22df4b4020ec5 100644 --- a/src/main/java/cz/jezek/lab11/Tournament.java +++ b/src/main/java/cz/jezek/lab11/Tournament.java @@ -3,11 +3,24 @@ package cz.jezek.lab11; import java.util.Collections; import java.util.List; +import jakarta.xml.bind.annotation.XmlElement; +import jakarta.xml.bind.annotation.XmlElementWrapper; +import jakarta.xml.bind.annotation.XmlRootElement; + +@XmlRootElement public class Tournament { + @XmlElement(name = "player") + @XmlElementWrapper(name = "players") private List<Player> players; + + @XmlElement(name = "match") + @XmlElementWrapper(name = "matches") private List<Match> matches; + public Tournament() { + } + public static Tournament generate() { // TODO 1.a Generate a list of 10 random players using the stream and then filter so that it does not contain players with the same name List<Player> players = Collections.emptyList(); @@ -39,17 +52,9 @@ public class Tournament { @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Player count ").append(players.size()) - .append(" Match count: ").append(matches.size()); - builder.append("\n"); - - builder.append("\n\nPlayers:\n"); - // TODO 2.a Use the stream to add a listing of all players to the builder. Individual players are separated by a "\ n" character - builder.append(/* list of all players */""); + builder.append("players = ").append(players); - builder.append("\n\nMatches:\n"); - // TODO 2.b Use the stream to add a listing of all entries to the builder. Individual matches are separated by a "\ n" character - builder.append(/* list of all matches */""); + builder.append(", mathes = ").append(matches); return builder.toString(); } diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 35757404f5d12da2642d679d3eb1b4ce681e75da..b1bb065a5024172ecbc8080cd0d0e83dddd94047 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,4 +1,8 @@ module cz.jezek.lab10 { - opens cz.jezek.lab11 to javafx.fxml; + opens cz.jezek.lab11 to jakarta.xml.bind; exports cz.jezek.lab11; + requires jakarta.xml.bind; + requires org.eclipse.persistence.moxy; + requires jakarta.json; + requires com.sun.tools.xjc; } \ No newline at end of file