001: /* ResponseHandler.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: May 31, 2007 12:08:03 PM, Created by henrichen
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zkmob;
020:
021: import java.util.Stack;
022:
023: import org.xml.sax.Attributes;
024: import org.xml.sax.SAXException;
025: import org.xml.sax.helpers.DefaultHandler;
026: import org.zkoss.zkmob.ui.CTag;
027: import org.zkoss.zkmob.ui.DTag;
028: import org.zkoss.zkmob.ui.RTag;
029: import org.zkoss.zkmob.ui.RsTag;
030: import org.zkoss.zkmob.ui.SidTag;
031: import org.zkoss.zkmob.ui.ZkDesktop;
032:
033: /**
034: * MIL update response handler.
035: * @author henrichen
036: *
037: */
038: public class UpdateHandler extends DefaultHandler {
039: private Stack _stack;
040: private ZkDesktop _zk;
041:
042: public UpdateHandler(ZkDesktop zk) {
043: _zk = zk;
044: }
045:
046: //super//
047: public void startDocument() throws SAXException {
048: _stack = new Stack();
049: }
050:
051: public void endDocument() throws SAXException {
052: _stack = null;
053: }
054:
055: public void startElement(String namespaceURI, String sName,
056: String qName, Attributes attrs) throws SAXException {
057: String eName = sName; // element name
058: if ("".equals(eName))
059: eName = qName; // namespaceAware = false
060:
061: ResponseTag tag = null;
062: if ("d".equals(eName)) {
063: tag = new DTag();
064: } else if ("c".equals(eName)) {
065: tag = new CTag();
066: } else if ("r".equals(eName)) {
067: tag = new RTag();
068: } else if ("rs".equals(eName)) {
069: tag = new RsTag();
070: } else if ("sid".equals(eName)) {
071: tag = new SidTag();
072: } else {
073: throw new IllegalArgumentException(
074: "Unsupported response Tag: " + eName);
075: }
076:
077: _stack.push(tag);
078:
079: emit("<" + eName);
080: if (attrs != null) {
081: for (int i = 0; i < attrs.getLength(); i++) {
082: String aName = attrs.getLocalName(i); // Attr name
083: if ("".equals(aName))
084: aName = attrs.getQName(i);
085: emit(" ");
086: emit(aName + "=\"" + attrs.getValue(i) + "\"");
087: }
088: }
089: emit(">");
090: }
091:
092: public void endElement(String namespaceURI, String sName,
093: String qName) throws SAXException {
094: final ResponseTag tag = (ResponseTag) _stack.peek();
095: _stack.pop();
096: final ResponseTag parent = (ResponseTag) (_stack.empty() ? null
097: : _stack.peek());
098:
099: if (tag instanceof RTag) {
100: _zk.executeResponse((RTag) tag);
101: } else if (parent instanceof RTag) {
102: final RTag rtag = (RTag) parent;
103: rtag.addKid(tag);
104: }
105:
106: String eName = sName; // element name
107: if ("".equals(eName))
108: eName = qName; // namespaceAware = false
109: emit("</" + eName + ">");
110: }
111:
112: public void characters(char buf[], int offset, int len)
113: throws SAXException {
114: ResponseTag tag = (ResponseTag) _stack.peek();
115: tag.setValue(new String(buf, offset, len));
116:
117: //do nothing, should never called here
118: String s = new String(buf, offset, len);
119: emit(s);
120: }
121:
122: private void emit(String s) {
123: System.out.print(s);
124: }
125: }
|