import java.io.*; import org.w3c.dom.*; import com.javaworld.JavaBeans.XMLBeans.*; public class Statistics implements Serializable { protected int _year; protected int _atbats; protected int _runs; protected int _hits; protected int _homeruns; protected int _runsbattedin; protected int _strikeouts; public Statistics() { } public int getAtBats() { return _atbats; } /** * This method "encrypts" the strikeouts property and returns an * element that contains the encrypted string. */ public DocumentFragment getEncryptedStrikeouts(Document d) { StringBuffer sbStrikeouts = new StringBuffer(String.valueOf(_strikeouts)); // "Encrypt" the strikeout count by adding 'A' to every character. for (int i = 0; i < sbStrikeouts.length(); i++) { sbStrikeouts.setCharAt(i, (char) (sbStrikeouts.charAt(i) + (int) 'A')); } // The result is a document fragment // containing a text node DocumentFragment dfResult = d.createDocumentFragment(); Text textSo = d.createTextNode(new String(sbStrikeouts)); dfResult.appendChild(textSo); return dfResult; } public int getHits() { return _hits; } // Given an element that contains the number of // hits for a player, return that number. We store // hits in base 16, just to be obtuse. public DocumentFragment getHitsAsDOM(Document d) { String sValue = Integer.toHexString(_hits); Text textResult = d.createTextNode("0x" + sValue); Comment comment = d.createComment("This node created by Statistics.getHitsAsDOM()"); DocumentFragment df = d.createDocumentFragment(); df.appendChild(comment); df.appendChild(textResult); return df; } public int getHomeRuns() { return _homeruns; } public int getRuns() { return _runs; } public int getRunsBattedIn() { return _runsbattedin; } public int getYear() { return _year; } public void print() { System.out.print("Year(" + _year + "), " + "AB(" + _atbats +"), " + "H(" + _hits +"), " + "R(" + _runs +"), " + "HR(" + _homeruns +"), " + "RBI(" + _runsbattedin + "), " + "SO(" + _strikeouts + ")"); } public void setAtBats(int atbats_) { _atbats = atbats_; } /** * This method "decrypts" the grade point average from a "Strikeouts" element, * and sets the result in the bean. */ public void setEncryptedStrikeouts(Element eStrikeouts) { // Use the first nonempty text node in the Element's children Node firstKid = Util.getFirstInterestingChild(eStrikeouts); String sText = null; if (firstKid instanceof Text) { sText = ((Text)firstKid).getData(); } // If there were no nonempty text nodes, ignore this element if (sText == null) return; StringBuffer sbStrikeouts = new StringBuffer(sText); // "Decrypt" the Strikeouts by subtracting one from every character. for (int i = 0; i < sbStrikeouts.length(); i++) { sbStrikeouts.setCharAt(i, (char)(sbStrikeouts.charAt(i) - (int)'A')); } sText = new String(sbStrikeouts); try { Integer iStrikeouts = new Integer(sText); _strikeouts = iStrikeouts.intValue(); } catch (NumberFormatException nfe) { ; // Ignore this exception - this is a demo! } } public void setHits(int hits_) { _hits = hits_; } public void setHitsAsDOM(Element e) throws InvalidPropertyException { // Find a kid of this element that's a text node. // It should contain the number of hits in hexadecimal. // If it fails, throw an exception Node n = Util.getFirstInterestingChild(e); boolean set = false; if (n instanceof Text) { try { String sNum = ((Text) n).getData(); _hits = Integer.decode(sNum).intValue(); set = true; } catch (NumberFormatException nfe) { throw new InvalidPropertyException("Invalid number format for Statistics.hits"); } } if (!set) { throw new InvalidPropertyException("Invalid DOM structure setting Statistics.hits"); } } public void setHomeRuns(int homeruns_) { _homeruns = homeruns_; } public void setRuns(int runs_) { _runs = runs_; } public void setRunsBattedIn(int runsbattedin_) { _runsbattedin = runsbattedin_; } public void setYear(int year_) { _year = year_; } }