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 net.sourceforge.jhunters.pool.PoolException;
20  
21  import org.w3c.dom.Document;
22  import org.w3c.dom.Element;
23  import org.w3c.dom.Node;
24  
25  /**
26   * XML parser.
27   */
28  public class XmlParser {
29      /**
30       * The current document.
31       */
32      private Document theDocument;
33  
34      /**
35       * Constructor.
36       * @param pDocument the document
37       */
38      public XmlParser(final Document pDocument) {
39          theDocument = pDocument;
40      }
41  
42      /**
43       * Parse the dataSet.
44       * @return the DataSet
45       * @throws PoolException on error
46       */
47      public DataSet parseDataSet() throws PoolException {
48          /* Build the dataSet */
49          DataSet myDataSet = new DataSet();
50  
51          /* Access base element */
52          Element myBase = theDocument.getDocumentElement();
53  
54          /* Check for correct node name */
55          if (!DataSet.OBJECT_NAME.equals(myBase.getNodeName())) {
56              throw new PoolException("Wrong document element: " + myBase.getNodeName());
57          }
58  
59          /* Iterate through the children */
60          for (Node myNode = myBase.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
61              String myName = myNode.getNodeName();
62  
63              /* If we have a players node */
64              if (myName.equals(PlayerList.LIST_NAME)) {
65                  /* create a new PlayerList */
66                  PlayerList myList = parsePlayers(myNode);
67  
68                  /* Add the player to the list */
69                  myDataSet.setPlayers(myList);
70  
71                  /* If we have a competition node */
72              } else if (myName.equals(CompetitionList.OBJECT_NAME)) {
73                  /* create a new CompetitionList */
74                  CompetitionList myList = parseCompetitionList(myDataSet.getPlayers(), myNode);
75  
76                  /* Set the competitions */
77                  myDataSet.setCompetitions(myList);
78              }
79          }
80  
81          /* validate the dataSet */
82          myDataSet.validate();
83  
84          /* Check for valid document */
85          if (!myDataSet.getStatus().isValid()) {
86              throw new PoolException("Corrupt document");
87          }
88  
89          /* Return the dataSet */
90          return myDataSet;
91      }
92  
93      /**
94       * Parse the player list.
95       * @param pElement the element to parse
96       * @return the player list
97       * @throws PoolException on error
98       */
99      private PlayerList parsePlayers(final Node pElement) throws PoolException {
100         /* Build the player list */
101         PlayerList myList = new PlayerList();
102 
103         /* Iterate through the children */
104         for (Node myNode = pElement.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
105             String myName = myNode.getNodeName();
106 
107             /* If we have a player node */
108             if (myName.equals(Player.OBJECT_NAME)) {
109                 /* create a new Player */
110                 Player myPlayer = new Player();
111 
112                 /* Loop through the child nodes */
113                 for (Node myChild = myNode.getFirstChild(); myChild != null; myChild = myChild.getNextSibling()) {
114                     /* Set data */
115                     myPlayer.setNamedProperty(myChild.getNodeName(), myChild.getTextContent());
116                 }
117 
118                 /* Add the player to the list */
119                 myList.addPlayer(myPlayer);
120             }
121         }
122 
123         /* Return the list */
124         return myList;
125     }
126 
127     /**
128      * Parse the competition list.
129      * @param pPlayers the player list
130      * @param pElement the element to parse
131      * @return the participant list
132      */
133     private CompetitionList parseCompetitionList(final PlayerList pPlayers,
134                                                  final Node pElement) {
135         /* Build the competition list */
136         CompetitionList myList = new CompetitionList();
137 
138         /* Iterate through the children */
139         for (Node myNode = pElement.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
140             String myName = myNode.getNodeName();
141 
142             /* If we have a participant list */
143             if (myName.equals(Competition.OBJECT_NAME)) {
144                 /* create a new Competition */
145                 Competition myCompetition = parseCompetition(pPlayers, myNode);
146 
147                 /* Add the competition to the list */
148                 myList.addCompetition(myCompetition);
149             }
150         }
151 
152         /* Return the competition list */
153         return myList;
154     }
155 
156     /**
157      * Parse the competition.
158      * @param pPlayers the player list
159      * @param pElement the element to parse
160      * @return the participant list
161      */
162     private Competition parseCompetition(final PlayerList pPlayers,
163                                          final Node pElement) {
164         /* Build the competition */
165         Competition myComp = new Competition();
166 
167         /* Iterate through the children */
168         for (Node myNode = pElement.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
169             String myName = myNode.getNodeName();
170 
171             /* If we have a participant list */
172             if (myName.equals(Competition.PARTICIPANT_LIST)) {
173                 /* create a new Participant list */
174                 PlayerList myParticipants = parseParticipantList(pPlayers, myNode);
175 
176                 /* Set the competition participants */
177                 myComp.setParticipants(myParticipants.playerIterator());
178 
179                 /* If we have a fixture list */
180             } else if (myName.equals(FixtureList.OBJECT_NAME)) {
181                 /* create a new fixture list */
182                 FixtureList myFixtures = parseFixtureList(pPlayers, myNode);
183 
184                 /* Set the competition fixtures */
185                 myComp.setFixtures(myFixtures.fixtureIterator());
186 
187             } else {
188                 /* Set data */
189                 myComp.setNamedProperty(myName, myNode.getTextContent());
190             }
191         }
192 
193         /* Return the competition */
194         return myComp;
195     }
196 
197     /**
198      * Parse the participant list.
199      * @param pPlayers the player list
200      * @param pElement the element to parse
201      * @return the participant list
202      */
203     private PlayerList parseParticipantList(final PlayerList pPlayers,
204                                             final Node pElement) {
205         /* Build the player list */
206         PlayerList myList = new PlayerList();
207 
208         /* Iterate through the children */
209         for (Node myNode = pElement.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
210             String myName = myNode.getNodeName();
211 
212             /* If we have a player node */
213             if (myName.equals(Competition.PARTICIPANT_NAME)) {
214                 /* obtain the name of the player */
215                 String myPlayerName = myNode.getTextContent();
216                 Player myPlayer = pPlayers.getNamedPlayer(myPlayerName);
217 
218                 /* Add the player to the list */
219                 myList.addPlayer(myPlayer);
220             }
221         }
222 
223         /* validate the list */
224         myList.validate();
225 
226         /* Return the list */
227         return myList;
228     }
229 
230     /**
231      * Parse the fixture list.
232      * @param pPlayers the player list
233      * @param pElement the element to parse
234      * @return the fixtureSet
235      */
236     private FixtureList parseFixtureList(final PlayerList pPlayers,
237                                          final Node pElement) {
238         /* Build the fixtureList */
239         FixtureList myList = new FixtureList();
240 
241         /* Iterate through the children */
242         for (Node myNode = pElement.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
243             String myName = myNode.getNodeName();
244 
245             /* If we have a fixture node */
246             if (myName.equals(FixtureSet.OBJECT_NAME)) {
247                 /* create a new FixtureSet */
248                 FixtureSet mySet = parseFixtureSet(pPlayers, myNode);
249 
250                 /* Add the fixture to the set */
251                 myList.addFixtureSet(mySet);
252             }
253         }
254 
255         /* Return the list */
256         return myList;
257     }
258 
259     /**
260      * Parse the fixture set.
261      * @param pPlayers the player list
262      * @param pElement the element to parse
263      * @return the fixtureSet
264      */
265     private FixtureSet parseFixtureSet(final PlayerList pPlayers,
266                                        final Node pElement) {
267         /* Build the fixtureSet */
268         FixtureSet mySet = new FixtureSet();
269 
270         /* Iterate through the children */
271         for (Node myNode = pElement.getFirstChild(); myNode != null; myNode = myNode.getNextSibling()) {
272             String myName = myNode.getNodeName();
273 
274             /* If we have a fixture node */
275             if (myName.equals(Fixture.OBJECT_NAME)) {
276                 /* create a new Fixture */
277                 Fixture myFixture = new Fixture();
278 
279                 /* Loop through the child nodes */
280                 for (Node myChild = myNode.getFirstChild(); myChild != null; myChild = myChild.getNextSibling()) {
281                     /* Set data */
282                     myFixture.setNamedProperty(pPlayers, myChild.getNodeName(), myChild.getTextContent());
283                 }
284 
285                 /* Add the fixture to the set */
286                 mySet.addFixture(myFixture);
287 
288             } else {
289                 /* Set data */
290                 mySet.setNamedProperty(myName, myNode.getTextContent());
291             }
292         }
293 
294         /* Return the set */
295         return mySet;
296     }
297 }