View Javadoc

1   //  Open Hold em is a java-based, server and client environment for hosting 
2   //  your own texas hold poker tournaments, and regular tables. It is full 
3   //  customizable and deployable in a number of different java-based hosting 
4   //  environments.
5   //  Copyright (C) 2005 Chris A. Mattmann <chris@baron.pagemewhen.com>
6   //
7   //  This program is free software; you can redistribute it and/or modify
8   //  it under the terms of the GNU General Public License as published by
9   //  the Free Software Foundation; either version 2 of the License, or
10  //  (at your option) any later version.
11  //
12  //  This program is distributed in the hope that it will be useful,
13  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  //  GNU General Public License for more details.
16  //
17  //  You should have received a copy of the GNU General Public License
18  //  along with this program; if not, write to the Free Software
19  //  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  //
21  //  $Id: RuleSetReader.java,v 1.2 2005/08/07 02:48:36 mattmann Exp $
22  
23  package com.openholdem.util;
24  
25  import com.openholdem.structs.RuleSet;
26  import com.openholdem.structs.exceptions.PokerException;
27  
28  import org.w3c.dom.Document;
29  import org.w3c.dom.Element;
30  import org.w3c.dom.NodeList;
31  
32  import org.xml.sax.InputSource;
33  
34  import javax.xml.parsers.DocumentBuilderFactory;
35  import javax.xml.parsers.DocumentBuilder;
36  
37  import java.net.URL;
38  
39  import java.io.IOException;
40  
41  /***
42   * @author mattmann
43   * @version $Revision: 1.2 $
44   * 
45   * <p>
46   * A Rule Set reader reads an XML file and creates a {@link RuleSet} object.
47   * </p>
48   * 
49   */
50  public class RuleSetReader {
51  
52      /* the rule set xml file that contains the poker game rule set */
53      private URL ruleSetUrl = null;
54  
55      /***
56       * <p>
57       * Constructs a new RuleSetReader that will read the given url file
58       * </p>
59       * 
60       * @param ruleUrl
61       */
62      public RuleSetReader(URL ruleUrl) {
63          ruleSetUrl = ruleUrl;
64      }
65  
66      public RuleSet readRuleSet() throws PokerException, Exception {
67          RuleSet r = new RuleSet();
68  
69          DocumentBuilderFactory factory = null;
70          DocumentBuilder parser = null;
71          Document document = null;
72          InputSource inputSource = null;
73  
74          try {
75              inputSource = new InputSource(ruleSetUrl.openStream());
76          } catch (IOException e) {
77              e.printStackTrace();
78              throw new PokerException("Unable to read rule set from url "
79                      + ruleSetUrl);
80          }
81  
82          try {
83              factory = DocumentBuilderFactory.newInstance();
84              parser = factory.newDocumentBuilder();
85              document = parser.parse(inputSource);
86          } catch (Exception parseException) {
87              parseException.printStackTrace();
88              return null;
89          }
90  
91          Element ruleSet = document.getDocumentElement();
92  
93          Element handTag = DOMUtil.getFirstElement(ruleSet, "hand");
94  
95          if (handTag == null) {
96              throw new PokerException("poker rules file " + ruleSetUrl
97                      + " must define a hand element!");
98          }
99  
100         // pick up the attributes
101         // numPlayerCards="2" minHandCardsUsed="0" maxHandCardsUsed="2"
102         // community="2"
103 
104         String numPlayerCards = handTag.getAttribute("numPlayerCards");
105 
106         if (numPlayerCards == null) {
107             throw new PokerException("poker rules file " + ruleSetUrl
108                     + " must define the hand attribute numPlayerCards!");
109         } else {
110             // try and parse it as an int
111             try {
112                 r.setFNumPlayerCards(Integer.parseInt(numPlayerCards));
113             } catch (NumberFormatException e) {
114                 e.printStackTrace();
115                 throw new PokerException(
116                         "can't parse num player cards attribute "
117                                 + numPlayerCards + " ! " + e.getMessage());
118             }
119         }
120 
121         String minHandCardsUsed = handTag.getAttribute("minHandCardsUsed");
122 
123         if (minHandCardsUsed == null) {
124             throw new PokerException("poker rules file " + ruleSetUrl
125                     + " must define the hand attribute minHandCardsUsed!");
126         } else {
127             // try and parse it as an int
128             try {
129                 r.setFMinHandCardsUsed(Integer.parseInt(minHandCardsUsed));
130             } catch (NumberFormatException e) {
131                 e.printStackTrace();
132                 throw new PokerException(
133                         "can't parse min hand cards used attribute "
134                                 + minHandCardsUsed + " ! " + e.getMessage());
135             }
136         }
137 
138         String maxHandCardsUsed = handTag.getAttribute("maxHandCardsUsed");
139 
140         if (maxHandCardsUsed == null) {
141             throw new PokerException("poker rules file " + ruleSetUrl
142                     + " must define the hand attribute maxHandCardsUsed!");
143         } else {
144             // try and parse it as an int
145             try {
146                 r.setFMaxHandCardsUsed(Integer.parseInt(maxHandCardsUsed));
147             } catch (NumberFormatException e) {
148                 e.printStackTrace();
149                 throw new PokerException(
150                         "can't parse max hand cards used attribute "
151                                 + maxHandCardsUsed + " ! " + e.getMessage());
152             }
153         }
154 
155         String community = handTag.getAttribute("community");
156 
157         if (community == null) {
158             throw new PokerException("poker rules file " + ruleSetUrl
159                     + " must define the hand attribute community!");
160         } else {
161             // try and parse it as an int
162             try {
163                 r.setFNumCommunityCards(Integer.parseInt(community));
164             } catch (NumberFormatException e) {
165                 e.printStackTrace();
166                 throw new PokerException(
167                         "can't parse community cards attribute " + community
168                                 + " ! " + e.getMessage());
169             }
170         }
171 
172         // now iterate through the set of handRanks
173         Element handRanks = DOMUtil.getFirstElement(ruleSet, "handRanks");
174 
175         NodeList handRanksRoot = handRanks.getElementsByTagName("rank");
176 
177         // should be at least two hand ranks
178         if (handRanksRoot.getLength() < 2) {
179             throw new PokerException(
180                     "Must be at least two hand ranks in poker rule set file "
181                             + ruleSetUrl);
182         }
183 
184         // now get the
185 
186         for (int i = 0; i < handRanksRoot.getLength(); i++) {
187             Element rankNode = (Element) handRanksRoot.item(i);
188 
189             // two attributes num (rank) and hand (name)
190             String rankStr = rankNode.getAttribute("num");
191 
192             if (rankStr == null) {
193                 throw new PokerException("rank node from poker file "
194                         + ruleSetUrl + " must define the attribute num!");
195             }
196 
197             // get the name now
198 
199             String handName = rankNode.getAttribute("hand");
200 
201             if (handName == null) {
202                 throw new PokerException("rank node from poker file "
203                         + ruleSetUrl + " must define the attribute hand!");
204             }
205 
206             // try and parse the hand name as an int
207             try {
208                 r.getFHandRanks().add(
209                         new RuleSet().new HandRank(Integer.parseInt(rankStr),
210                                 handName));
211             } catch (NumberFormatException e) {
212                 e.printStackTrace();
213                 throw new PokerException(
214                         "Unable to parse the rank of the hand name " + handName
215                                 + " in poker file " + ruleSetUrl + " !");
216             }
217         }
218 
219         return r;
220 
221     }
222 
223 }