Datasheet

75: public void characters(char[] ch, int start, int length)
76: throws SAXException {
77: currentText.append(ch, start, length);
78: }
79:
The remainder of BookHandler implements the three public methods of the ErrorHandler callback inter-
face, which controls how errors are reported by the application. In this case, you’re just printing an
extended error message to System.out. The warning, error, and fatalError methods use a shared private
method getLocationString to process the contents of a SAXParseException, which is where they obtain
position information about the location of the error:
80: public void warning(SAXParseException ex) throws SAXException {
81: System.err.println(
82: "[Warning] " + getLocationString(ex) + ": " +
83: ex.getMessage());
84: }
85:
86: public void error(SAXParseException ex) throws SAXException {
87: System.err.println(
88: "[Error] " + getLocationString(ex) + ": " +
89: ex.getMessage());
90: }
91:
92: public void fatalError(SAXParseException ex)
93: throws SAXException {
94: System.err.println(
95: "[Fatal Error] " + getLocationString(ex) + ": " +
96: ex.getMessage());
97: throw ex;
98: }
99:
100: /** Returns a string of the location. */
101: private String getLocationString(SAXParseException ex) {
102: StringBuffer str = new StringBuffer();
103:
104: String systemId = ex.getSystemId();
105: if (systemId != null) {
106: int index = systemId.lastIndexOf('/');
107: if (index != -1)
108: systemId = systemId.substring(index + 1);
109: str.append(systemId);
110: }
111: str.append(':');
112: str.append(ex.getLineNumber());
113: str.append(':');
114: str.append(ex.getColumnNumber());
115:
116: return str.toString();
117:
118: }
119:
120: }
12
Chapter 1
01 543555 Ch01.qxd 11/5/03 9:40 AM Page 12