KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > xam > dom > SyncUnit


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /**
20  *
21  * @author Nam Nguyen
22  */

23
24 package org.netbeans.modules.xml.xam.dom;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32 import org.w3c.dom.Attr JavaDoc;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.Text JavaDoc;
36
37 public class SyncUnit {
38     private final DocumentComponent target;
39     private List JavaDoc<ChangeInfo> changes = new ArrayList JavaDoc<ChangeInfo>();
40     private List JavaDoc<DocumentComponent> toRemove = new ArrayList JavaDoc<DocumentComponent>();
41     private List JavaDoc<DocumentComponent> toAdd = new ArrayList JavaDoc<DocumentComponent>();
42     private Map JavaDoc<String JavaDoc, Attr JavaDoc> removedAttributes = new HashMap JavaDoc<String JavaDoc, Attr JavaDoc>();
43     private Map JavaDoc<String JavaDoc, Attr JavaDoc> addedAttributes = new HashMap JavaDoc<String JavaDoc, Attr JavaDoc>();
44     private boolean componentChanged;
45     private boolean hasTextContentChanges = false;
46     
47     public SyncUnit(DocumentComponent syncTarget) {
48         if (syncTarget == null) {
49             throw new IllegalArgumentException JavaDoc("Null syncTarget");
50         }
51         target = syncTarget;
52     }
53     
54     public void addChange(ChangeInfo change) {
55         if (! target.referencesSameNode(change.getParent())) {
56             throw new IllegalArgumentException JavaDoc("ChangeInfo does not match target id");
57         }
58         changes.add(change);
59         if (change.getChangedNode() instanceof Attr JavaDoc) {
60             Attr JavaDoc attr = (Attr JavaDoc) change.getChangedNode();
61             if (change.isAdded()) {
62                 addToAddedAttributes(attr);
63             } else {
64                 addToRemovedAttributes(attr);
65             }
66         } else {
67             Node JavaDoc actualChanged = change.getActualChangedNode();
68             if (! (actualChanged instanceof Attribute || actualChanged instanceof Element JavaDoc)) {
69                 // should be text, cdata, comment...
70
if (actualChanged.getNodeType() != Node.TEXT_NODE ||
71                     ((Text JavaDoc)actualChanged).getNodeValue().trim().length() != 0) {
72                     setHasTextContentChanges(true);
73                 }
74             }
75         }
76     }
77     
78     public List JavaDoc<ChangeInfo> getChanges() { return changes; }
79     public DocumentComponent getTarget() { return target; }
80     public List JavaDoc<DocumentComponent> getToRemoveList() { return toRemove; }
81     public void addToRemoveList(DocumentComponent c) {
82         if (c == null) {
83             throw new IllegalArgumentException JavaDoc("Null component");
84         }
85         toRemove.add(c);
86     }
87     public List JavaDoc<DocumentComponent> getToAddList() { return toAdd; }
88     public void addToAddList(DocumentComponent c) {
89         if (c == null) {
90             throw new IllegalArgumentException JavaDoc("Null component");
91         }
92         toAdd.add(c);
93     }
94     public void setComponentChanged(boolean v) { componentChanged = v; }
95     public boolean isComponentChanged() { return componentChanged; }
96     public void addToAddedAttributes(Attr JavaDoc attr) {
97         addedAttributes.put(attr.getName(), attr);
98     }
99     public Map JavaDoc<String JavaDoc,Attr JavaDoc> getAddedAttributes() {
100         return addedAttributes;
101     }
102     
103     public Map JavaDoc<String JavaDoc,Attr JavaDoc> getRemovedAttributes() {
104         return removedAttributes;
105     }
106     
107     public void addToRemovedAttributes(Attr JavaDoc attr) {
108         removedAttributes.put(attr.getName(), attr);
109     }
110
111     public void merge(SyncUnit su) {
112         if (target != su.getTarget()) {
113             throw new IllegalArgumentException JavaDoc("Invalid sync unit for merge");
114         }
115         changes.addAll(su.getChanges());
116         for (String JavaDoc name : su.getRemovedAttributes().keySet()) {
117             addToRemovedAttributes(su.getRemovedAttributes().get(name));
118         }
119         for (String JavaDoc name : su.getAddedAttributes().keySet()) {
120             addToAddedAttributes(su.getAddedAttributes().get(name));
121         }
122         
123         if (! su.getToAddList().isEmpty()) {
124             HashSet JavaDoc<Element JavaDoc> addSet = new HashSet JavaDoc<Element JavaDoc>();
125             for (DocumentComponent component : toAdd) {
126                 addSet.add(component.getPeer());
127             }
128             for (DocumentComponent component : su.getToAddList()) {
129                 if (! addSet.contains(component.getPeer())) {
130                     toAdd.add(component);
131                 }
132             }
133         }
134
135         for (DocumentComponent component : su.getToRemoveList()) {
136             if (! toRemove.contains(component)) {
137                 toRemove.add(component);
138             }
139         }
140     }
141     
142     public void updateTargetReference() {
143         AbstractDocumentComponent component = (AbstractDocumentComponent) target;
144         if (component != null) {
145             component.updateReference(getParentToRootPath());
146         }
147     }
148     
149     public ChangeInfo getLastChange() {
150         if (changes.size() > 0) {
151             return changes.get(changes.size()-1);
152         } else {
153             return null;
154         }
155     }
156     
157     public List JavaDoc<Element JavaDoc> getParentToRootPath() {
158         if (getLastChange() == null) {
159             return Collections.emptyList();
160         } else {
161             return getLastChange().getParentToRootPath();
162         }
163     }
164     
165     public boolean hasTextContentChanges() {
166         return hasTextContentChanges;
167     }
168
169     public void setHasTextContentChanges(boolean val) {
170         hasTextContentChanges = val;
171     }
172     
173     public boolean hasWhitespaceChangeOnly() {
174         for (ChangeInfo ci : getChanges()) {
175             if (ci.isDomainElement()) {
176                 continue;
177             }
178             Node JavaDoc n = ci.getActualChangedNode();
179             if (n.getNodeType() == Node.TEXT_NODE) {
180                 String JavaDoc text = ((Text JavaDoc)n).getNodeValue();
181                 if (text != null && text.trim().length() > 0) {
182                     return false;
183                 }
184             } else if (n.getNodeType() == Node.ATTRIBUTE_NODE) {
185                 String JavaDoc name = ((Attr JavaDoc) n).getName();
186                 Attr JavaDoc removed = getRemovedAttributes().get(name);
187                 if (removed == null) {
188                     return false;
189                 }
190                 Attr JavaDoc added = getAddedAttributes().get(name);
191                 if (added == null) {
192                     return false;
193                 }
194                 if (removed.getValue() == null ||
195                     ! removed.getValue().equals(added.getValue())) {
196                     return false;
197                 }
198             } else {
199                 // node type must be either element or comment or cdata...
200
return false;
201             }
202         }
203         return true;
204     }
205 }
206
Popular Tags