1 package com.icesoft.faces.context; 2 3 import com.icesoft.faces.util.DOMUtils; 4 import com.icesoft.faces.webapp.command.CommandQueue; 5 import com.icesoft.faces.webapp.command.UpdateElements; 6 import org.w3c.dom.Document ; 7 import org.w3c.dom.Element ; 8 import org.w3c.dom.Node ; 9 10 import java.io.IOException ; 11 import java.util.ArrayList ; 12 13 public class PushModeSerializer implements DOMSerializer { 14 private Document oldDocument; 15 private CommandQueue commandQueue; 16 17 public PushModeSerializer(Document currentDocument, CommandQueue commandQueue) { 18 this.oldDocument = currentDocument; 19 this.commandQueue = commandQueue; 20 } 21 22 public void serialize(Document document) throws IOException { 23 Node [] changed = DOMUtils.domDiff(oldDocument, document); 24 for (int i = 0; i < changed.length; i++) { 25 Element changeRoot = 26 DOMUtils.ascendToNodeWithID(changed[i]); 27 for (int j = 0; j < i; j++) { 28 if (changed[j] == null) 29 continue; 30 if (changeRoot == changed[j]) { 32 changeRoot = null; 33 break; 34 } 35 else if (isAncestor(changeRoot, changed[j])) { 37 changed[j] = null; 38 } 39 else if (isAncestor(changed[j], changeRoot)) { 41 changeRoot = null; 42 break; 43 } 44 } 45 changed[i] = changeRoot; 46 } 47 48 ArrayList elementList = new ArrayList (changed.length); 49 for (int i = 0; i < changed.length; i++) { 50 Element element = (Element ) changed[i]; 51 if (null != element) { 52 elementList.add(element); 53 } 54 } 55 if (!elementList.isEmpty()) { 56 Element [] elements = (Element []) elementList.toArray(new Element [elementList.size()]); 57 commandQueue.put(new UpdateElements(elements)); 58 } 59 60 oldDocument = document; 61 } 62 63 private boolean isAncestor(Node test, Node child) { 64 if (test == null || child == null) 65 return false; 66 if (!test.hasChildNodes()) { 67 return false; 68 } 69 Node parent = child; 70 while ((parent = parent.getParentNode()) != null) { 71 if (test.equals(parent)) { 72 return true; 73 } 74 } 75 return false; 76 } 77 } 78 | Popular Tags |