001: /* PageCreator.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Tue May 5 21:15:13 2007, 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: import java.util.Vector;
023:
024: import javax.microedition.lcdui.Displayable;
025:
026: import org.xml.sax.Attributes;
027: import org.xml.sax.SAXException;
028: import org.xml.sax.helpers.DefaultHandler;
029: import org.zkoss.zkmob.ui.ZkDesktop;
030:
031: /** The Page handler that deserialize Java Object from RMIL(Raw Mobile Interactive Language)
032: * XML Pages.
033: *
034: * @author henrichen
035: *
036: */
037: public class PageHandler extends DefaultHandler {
038: private Stack _stack = new Stack();
039: private String _hostURL;
040: private String _pathURL;
041: private Vector _roots = new Vector(8);
042: private ZkComponent _parent;
043: private ZkDesktop _zk;
044:
045: /*package*/PageHandler(String hostURL, String pathURL) {
046: _hostURL = hostURL;
047: _pathURL = pathURL;
048: }
049:
050: /*package*/PageHandler(ZkComponent parent, String hostURL,
051: String pathURL) {
052: _hostURL = hostURL;
053: _pathURL = pathURL;
054: _parent = parent;
055: _zk = _parent == null ? null : _parent.getZkDesktop();
056: _stack.push(parent);
057: }
058:
059: public Vector getRoots() {
060: return _roots;
061: }
062:
063: public ZkDesktop getZk() {
064: return _zk;
065: }
066:
067: //super//
068: public void startDocument() throws SAXException {
069: }
070:
071: public void endDocument() throws SAXException {
072: }
073:
074: public void startElement(String namespaceURI, String sName,
075: String qName, Attributes attrs) throws SAXException {
076: String eName = sName; // element name
077: if ("".equals(eName))
078: eName = qName; // namespaceAware = false
079:
080: ZkComponent parent = (ZkComponent) (_stack.empty() ? null
081: : _stack.peek());
082: ZkComponent comp = UiManager.create(parent, eName, attrs,
083: _hostURL, _pathURL);
084: _stack.push(comp);
085:
086: if (parent instanceof ZkDesktop) {
087: _roots.addElement(comp);
088: }
089:
090: if (_zk == null) {
091: if (comp instanceof ZkDesktop) {
092: _zk = (ZkDesktop) comp;
093: } else {
094: throw new IllegalArgumentException(
095: "RMIL page must be in <zk> root:" + eName);
096: }
097: }
098:
099: if (comp instanceof Displayable) {
100: final String cr = attrs.getValue("cr");
101: if ("t".equalsIgnoreCase(cr)) {
102: _zk.setCurrent((Displayable) comp);
103: }
104: }
105: _zk.registerUi(comp.getId(), comp);
106:
107: emit("<" + eName);
108: if (attrs != null) {
109: for (int i = 0; i < attrs.getLength(); i++) {
110: String aName = attrs.getLocalName(i); // Attr name
111: if ("".equals(aName))
112: aName = attrs.getQName(i);
113: emit(" ");
114: emit(aName + "=\"" + attrs.getValue(i) + "\"");
115: }
116: }
117: emit(">");
118: }
119:
120: public void endElement(String namespaceURI, String sName,
121: String qName) throws SAXException {
122: _stack.pop();
123:
124: String eName = sName; // element name
125: if ("".equals(eName))
126: eName = qName; // namespaceAware = false
127:
128: emit("</" + eName + ">");
129: }
130:
131: public void characters(char buf[], int offset, int len)
132: throws SAXException {
133: //do nothing, should never called here
134: String s = new String(buf, offset, len);
135: emit(s);
136: }
137:
138: private void emit(String s) {
139: System.out.print(s);
140: }
141: }
|