View Javadoc
1   /*******************************************************************************
2    * jhunters: Pool League
3    * Copyright 2015 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ********************************************************************************/
17  package net.sourceforge.jhunters.pool.data;
18  
19  import java.time.LocalDate;
20  import java.util.Iterator;
21  
22  import javafx.beans.property.IntegerProperty;
23  import javafx.beans.property.ObjectProperty;
24  
25  import org.w3c.dom.Document;
26  import org.w3c.dom.Element;
27  
28  /**
29   * XML document builder.
30   */
31  public class XmlBuilder {
32      /**
33       * The current document.
34       */
35      private final Document theDocument;
36  
37      /**
38       * Constructor.
39       * @param pDocument the document
40       */
41      public XmlBuilder(final Document pDocument) {
42          theDocument = pDocument;
43      }
44  
45      /**
46       * Build the competition.
47       * @param pData the dataSet
48       */
49      public void buildDataSet(final DataSet pData) {
50          /* Create a master element for the item */
51          Element myElement = theDocument.createElement(DataSet.OBJECT_NAME);
52          theDocument.appendChild(myElement);
53  
54          /* Build the participant list */
55          buildPlayers(myElement, pData.playerIterator());
56  
57          /* Build the competition list */
58          buildCompetitions(myElement, pData.competitionIterator());
59      }
60  
61      /**
62       * Build the player list.
63       * @param pContainer the containing element
64       * @param pPlayers the player iterator
65       */
66      private void buildPlayers(final Element pContainer,
67                                final Iterator<Player> pPlayers) {
68          /* Create a master element for the item */
69          Element myElement = theDocument.createElement(PlayerList.LIST_NAME);
70          pContainer.appendChild(myElement);
71  
72          /* Iterate through the list */
73          while (pPlayers.hasNext()) {
74              Player myPlayer = pPlayers.next();
75  
76              /* build the player */
77              buildPlayer(myElement, myPlayer);
78          }
79      }
80  
81      /**
82       * Build the player.
83       * @param pContainer the containing element
84       * @param pPlayer the player
85       */
86      private void buildPlayer(final Element pContainer,
87                               final Player pPlayer) {
88          /* Create a new element for the player */
89          Element myElement = theDocument.createElement(Player.OBJECT_NAME);
90          pContainer.appendChild(myElement);
91  
92          /* Add the properties */
93          addIntegerProperty(myElement, pPlayer.idProperty());
94          addStringProperty(myElement, pPlayer.nameProperty());
95          addStringProperty(myElement, pPlayer.contactProperty());
96      }
97  
98      /**
99       * Build the competition list.
100      * @param pContainer the containing element
101      * @param pCompetitions the competition iterator
102      */
103     private void buildCompetitions(final Element pContainer,
104                                    final Iterator<Competition> pCompetitions) {
105         /* Create a master element for the item */
106         Element myElement = theDocument.createElement(CompetitionList.OBJECT_NAME);
107         pContainer.appendChild(myElement);
108 
109         /* Iterate through the list */
110         while (pCompetitions.hasNext()) {
111             Competition myComp = pCompetitions.next();
112 
113             /* build the player */
114             buildCompetition(myElement, myComp);
115         }
116     }
117 
118     /**
119      * Build the competition.
120      * @param pContainer the containing element
121      * @param pCompetition the competition
122      */
123     private void buildCompetition(final Element pContainer,
124                                   final Competition pCompetition) {
125         /* Create a master element for the item */
126         Element myElement = theDocument.createElement(Competition.OBJECT_NAME);
127         pContainer.appendChild(myElement);
128 
129         /* Add the properties */
130         addStringProperty(myElement, pCompetition.nameProperty());
131         addDateProperty(myElement, pCompetition.startDateProperty());
132 
133         /* Build the participant list */
134         buildParticipants(myElement, pCompetition.participantIterator());
135 
136         /* Build the fixture list */
137         buildFixtures(myElement, pCompetition.fixtureIterator());
138     }
139 
140     /**
141      * Build the participant list.
142      * @param pContainer the containing element
143      * @param pParticipants the participant iterator
144      */
145     private void buildParticipants(final Element pContainer,
146                                    final Iterator<Player> pParticipants) {
147         /* Create a master element for the item */
148         Element myElement = theDocument.createElement(Competition.PARTICIPANT_LIST);
149         pContainer.appendChild(myElement);
150 
151         /* Iterate through the list */
152         while (pParticipants.hasNext()) {
153             Player myPlayer = pParticipants.next();
154 
155             /* build the participant */
156             buildParticipant(myElement, myPlayer);
157         }
158     }
159 
160     /**
161      * Build the participant.
162      * @param pContainer the containing element
163      * @param pPlayer the player
164      */
165     private void buildParticipant(final Element pContainer,
166                                   final Player pPlayer) {
167         /* Create a new element for the player */
168         Element myElement = theDocument.createElement(Competition.PARTICIPANT_NAME);
169         pContainer.appendChild(myElement);
170 
171         /* Add the properties */
172         addStringProperty(myElement, pPlayer.nameProperty());
173     }
174 
175     /**
176      * Build the fixtures.
177      * @param pContainer the containing element
178      * @param pFixtureSets the fixtureSet
179      */
180     private void buildFixtures(final Element pContainer,
181                                final Iterator<FixtureSet> pFixtureSets) {
182         /* Create a master element for the list */
183         Element myList = theDocument.createElement(FixtureList.OBJECT_NAME);
184         pContainer.appendChild(myList);
185 
186         /* Iterate through the fixtureSets */
187         while (pFixtureSets.hasNext()) {
188             FixtureSet mySet = pFixtureSets.next();
189 
190             /* build the fixture set */
191             buildFixtureSet(myList, mySet);
192         }
193     }
194 
195     /**
196      * Build the fixtureSet.
197      * @param pContainer the containing element
198      * @param pSet the fixtureSet
199      */
200     private void buildFixtureSet(final Element pContainer,
201                                  final FixtureSet pSet) {
202         /* Create a master element for the set */
203         Element mySet = theDocument.createElement(FixtureSet.OBJECT_NAME);
204         pContainer.appendChild(mySet);
205 
206         /* Add the week number */
207         addIntegerProperty(mySet, pSet.weekNoProperty());
208 
209         /* Iterate through the fixtures */
210         Iterator<Fixture> myIterator = pSet.fixtureIterator();
211         while (myIterator.hasNext()) {
212             Fixture myFixture = myIterator.next();
213 
214             /* build the fixture */
215             buildFixture(mySet, myFixture);
216         }
217     }
218 
219     /**
220      * Build the fixture.
221      * @param pContainer the containing element
222      * @param pFixture the fixture
223      */
224     private void buildFixture(final Element pContainer,
225                               final Fixture pFixture) {
226         /* Create a new element for the fixture */
227         Element myElement = theDocument.createElement(Fixture.OBJECT_NAME);
228         pContainer.appendChild(myElement);
229 
230         /* Add the properties */
231         addDateProperty(myElement, pFixture.dateProperty());
232         addDateProperty(myElement, pFixture.newDateProperty());
233         addPlayerProperty(myElement, pFixture.homePlayerProperty());
234         addPlayerProperty(myElement, pFixture.awayPlayerProperty());
235         addResultProperty(myElement, pFixture.resultProperty());
236     }
237 
238     /**
239      * Add a string property to the element.
240      * @param pElement the element to hold the property.
241      * @param pProperty the property to add
242      */
243     private void addStringProperty(final Element pElement,
244                                    final ObjectProperty<String> pProperty) {
245         /* Access the value */
246         String myValue = pProperty.getValue();
247         if (myValue != null) {
248             /* Create a new element for the string */
249             Element myElement = theDocument.createElement(pProperty.getName());
250             myElement.setTextContent(myValue);
251             pElement.appendChild(myElement);
252         }
253     }
254 
255     /**
256      * Add an integer property to the element.
257      * @param pElement the element to hold the property.
258      * @param pProperty the property to add
259      */
260     private void addIntegerProperty(final Element pElement,
261                                     final IntegerProperty pProperty) {
262         /* Access the value */
263         Integer myValue = pProperty.getValue();
264         if (myValue != null) {
265             /* Create a new element for the string */
266             Element myElement = theDocument.createElement(pProperty.getName());
267             myElement.setTextContent(myValue.toString());
268             pElement.appendChild(myElement);
269         }
270     }
271 
272     /**
273      * Add a player property to the element.
274      * @param pElement the element to hold the property.
275      * @param pProperty the property to add
276      */
277     private void addPlayerProperty(final Element pElement,
278                                    final ObjectProperty<Player> pProperty) {
279         /* Access the value */
280         Player myValue = pProperty.getValue();
281         if (myValue != null) {
282             /* Create a new element for the player */
283             Element myElement = theDocument.createElement(pProperty.getName());
284             myElement.setTextContent(myValue.getName());
285             pElement.appendChild(myElement);
286         }
287     }
288 
289     /**
290      * Add a result property to the element.
291      * @param pElement the element to hold the property.
292      * @param pProperty the property to add
293      */
294     private void addResultProperty(final Element pElement,
295                                    final ObjectProperty<Result> pProperty) {
296         /* Access the value */
297         Result myValue = pProperty.getValue();
298         if (myValue != null) {
299             /* Create a new element for the result */
300             Element myElement = theDocument.createElement(pProperty.getName());
301             myElement.setTextContent(myValue.toString());
302             pElement.appendChild(myElement);
303         }
304     }
305 
306     /**
307      * Add a date property to the element.
308      * @param pElement the element to hold the property.
309      * @param pProperty the property to add
310      */
311     private void addDateProperty(final Element pElement,
312                                  final ObjectProperty<LocalDate> pProperty) {
313         /* Access the value */
314         LocalDate myValue = pProperty.getValue();
315         if (myValue != null) {
316             /* Create a new element for the result */
317             Element myElement = theDocument.createElement(pProperty.getName());
318             myElement.setTextContent(DataParser.formatDate(myValue));
319             pElement.appendChild(myElement);
320         }
321     }
322 }