001: package org.enhydra.util.chiba;
002:
003: import org.chiba.xml.util.DOMUtil;
004: import org.chiba.xml.xforms.XFormsConstants;
005: import org.chiba.xml.xforms.events.XFormsEvent;
006: import org.chiba.xml.xforms.events.XFormsEventFactory;
007: import org.w3c.dom.Document;
008: import org.w3c.dom.Element;
009:
010: import java.util.Arrays;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: /**
015: * EventLog logs all events happening in XForms processor and build a DOM
016: * document which represent those events.
017: *
018: * @author Joern Turner, Slobodan Vujasinovic
019: * @version $Id: EventLog.java,v 1.1 2007-01-24 16:59:09 sinisa Exp $
020: */
021: public class EventLog {
022: private static List HELPER_ELEMENTS = Arrays.asList(new String[] {
023: XFormsConstants.LABEL, XFormsConstants.HELP,
024: XFormsConstants.HINT, XFormsConstants.ALERT,
025: XFormsConstants.VALUE });
026: private static List SELECTOR_ELEMENTS = Arrays.asList(new String[] {
027: XFormsConstants.SELECT1, XFormsConstants.SELECT });
028:
029: private Document doc;
030: private Element root;
031: private Element selector;
032:
033: public EventLog() {
034: this .doc = DOMUtil.newDocument(false, false);
035: this .root = this .doc.createElement("eventlog");
036: this .root.setAttribute("id", "eventlog");
037: this .doc.appendChild(this .root);
038: }
039:
040: public Element getLog() {
041: return (Element) this .root.cloneNode(true);
042: }
043:
044: public void add(XFormsEvent event) {
045: // get target properties
046: String type = event.getType();
047: Element target = (Element) event.getTarget();
048: String targetId = target.getAttributeNS(null, "id");
049: String targetName = target.getLocalName();
050:
051: // create event element
052: Element element;
053:
054: if (XFormsEventFactory.CHIBA_STATE_CHANGED.equals(type)
055: && SELECTOR_ELEMENTS.contains(targetName)) {
056: // selector events are always appended to the end of the log
057: // to ensure their items' labels and values are updated before
058: element = insert(null, type, targetId, targetName);
059: if (this .selector == null) {
060: this .selector = element;
061: }
062: } else {
063: // all other events are inserted before any selector events
064: element = insert(this .selector, type, targetId, targetName);
065: }
066:
067: if (XFormsEventFactory.CHIBA_STATE_CHANGED.equals(type)
068: && HELPER_ELEMENTS.contains(targetName)) {
069: // parent id is needed for updating all helper elements cause they
070: // are identified by '<parentId>-label' etc. rather than their own id
071: String parentId = ((Element) target.getParentNode())
072: .getAttributeNS(null, "id");
073: addProperty(element, "parentId", parentId);
074: }
075:
076: // add event params
077: Iterator iterator = event.getPropertyNames().iterator();
078: String name;
079: while (iterator.hasNext()) {
080: name = (String) iterator.next();
081: addProperty(element, name, event.getContextInfo(name)
082: .toString());
083: }
084: }
085:
086: public Element add(String type, String targetId, String targetName) {
087: return insert(this .selector, type, targetId, targetName);
088: }
089:
090: public Element addProperty(Element element, String name,
091: String value) {
092: Element property = this .doc.createElement("property");
093: property.setAttribute("name", name);
094: property.appendChild(this .doc.createTextNode(value));
095: element.appendChild(property);
096:
097: return element;
098: }
099:
100: private Element insert(Element ref, String type, String targetId,
101: String targetName) {
102: // create event element
103: Element element = this .doc.createElement("event");
104: this .root.insertBefore(element, ref);
105:
106: // add target properties
107: element.setAttribute("type", type);
108: element.setAttribute("targetId", targetId);
109: element.setAttribute("targetName", targetName);
110: return element;
111: }
112:
113: // clean the log
114: public void flush() {
115: DOMUtil.removeAllChildren(this.root);
116: this.selector = null;
117: }
118: }
|