1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
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
101
102
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
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
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
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
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
173 Element handRanks = DOMUtil.getFirstElement(ruleSet, "handRanks");
174
175 NodeList handRanksRoot = handRanks.getElementsByTagName("rank");
176
177
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
185
186 for (int i = 0; i < handRanksRoot.getLength(); i++) {
187 Element rankNode = (Element) handRanksRoot.item(i);
188
189
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
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
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 }