KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > faces > context > PushModeSerializer


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 JavaDoc;
7 import org.w3c.dom.Element JavaDoc;
8 import org.w3c.dom.Node JavaDoc;
9
10 import java.io.IOException JavaDoc;
11 import java.util.ArrayList JavaDoc;
12
13 public class PushModeSerializer implements DOMSerializer {
14     private Document JavaDoc oldDocument;
15     private CommandQueue commandQueue;
16
17     public PushModeSerializer(Document JavaDoc currentDocument, CommandQueue commandQueue) {
18         this.oldDocument = currentDocument;
19         this.commandQueue = commandQueue;
20     }
21
22     public void serialize(Document JavaDoc document) throws IOException JavaDoc {
23         Node JavaDoc[] changed = DOMUtils.domDiff(oldDocument, document);
24         for (int i = 0; i < changed.length; i++) {
25             Element JavaDoc changeRoot =
26                     DOMUtils.ascendToNodeWithID(changed[i]);
27             for (int j = 0; j < i; j++) {
28                 if (changed[j] == null)
29                     continue;
30                 // If new is already in, then discard new
31
if (changeRoot == changed[j]) {
32                     changeRoot = null;
33                     break;
34                 }
35                 // If new is parent of old, then replace old with new
36
else if (isAncestor(changeRoot, changed[j])) {
37                     changed[j] = null;
38                 }
39                 // If new is a child of old, then discard new
40
else if (isAncestor(changed[j], changeRoot)) {
41                     changeRoot = null;
42                     break;
43                 }
44             }
45             changed[i] = changeRoot;
46         }
47
48         ArrayList JavaDoc elementList = new ArrayList JavaDoc(changed.length);
49         for (int i = 0; i < changed.length; i++) {
50             Element JavaDoc element = (Element JavaDoc) changed[i];
51             if (null != element) {
52                 elementList.add(element);
53             }
54         }
55         if (!elementList.isEmpty()) {
56             Element JavaDoc[] elements = (Element JavaDoc[]) elementList.toArray(new Element JavaDoc[elementList.size()]);
57             commandQueue.put(new UpdateElements(elements));
58         }
59
60         oldDocument = document;
61     }
62
63     private boolean isAncestor(Node JavaDoc test, Node JavaDoc child) {
64         if (test == null || child == null)
65             return false;
66         if (!test.hasChildNodes()) {
67             return false;
68         }
69         Node JavaDoc 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