01: package com.jeta.forms.store.xml.parser;
02:
03: import org.xml.sax.Attributes;
04: import org.xml.sax.SAXException;
05: import org.xml.sax.helpers.DefaultHandler;
06:
07: public class MainHandler extends DefaultHandler {
08:
09: private XMLNodeContext m_ctx = new XMLNodeContext();
10:
11: private XMLHandler m_tophandler;
12:
13: public void characters(char[] ch, int start, int length)
14: throws SAXException {
15: // System.out.println( "MainHandler.characters: " + new String( ch,
16: // start, length ) );
17: m_ctx.getCurrentHandler().characters(ch, start, length);
18: }
19:
20: public void startElement(String uri, String localName,
21: String qName, Attributes attribs) throws SAXException {
22: try {
23: m_ctx.set(uri, qName, attribs);
24: if (m_tophandler == null) {
25: if ("object".equals(m_ctx.getQualifiedName())) {
26: m_tophandler = XMLHandlerFactory.getInstance()
27: .getHandler(attribs.getValue("classname"));
28: assert (m_tophandler != null);
29: m_tophandler.startElement(m_ctx);
30: } else {
31: throw new SAXException(
32: "Invalid tag. Expecting <object classname=\"...\">. Got instead: "
33: + m_ctx.getQualifiedName());
34: }
35: } else {
36: m_ctx.getCurrentHandler().startElement(m_ctx);
37: }
38: } catch (Exception e) {
39: e.printStackTrace();
40: if (e instanceof SAXException) {
41: throw (SAXException) e;
42: } else {
43: throw new SAXException(e.getMessage(), e);
44: }
45: }
46: }
47:
48: public void endElement(String uri, String localName, String qName)
49: throws SAXException {
50: m_ctx.set(uri, qName, null);
51: m_ctx.getCurrentHandler().endElement(m_ctx);
52: if (m_ctx.getCurrentHandler() == null) {
53: // we are done
54: if ("object".equalsIgnoreCase(qName)) {
55:
56: } else {
57: throw new SAXException(
58: "Invalid tag. Expecting <object classname=\"...\">. Got instead: "
59: + qName);
60: }
61: }
62: }
63:
64: public Object getObject() {
65: return ((ObjectHandler) m_tophandler).getObject();
66: }
67: }
|