KickJava   Java API By Example, From Geeks To Geeks.

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


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-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.xam.dom;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import javax.swing.text.BadLocationException JavaDoc;
29 import javax.xml.namespace.QName JavaDoc;
30 import org.netbeans.modules.xml.xam.ModelAccess;
31 import org.w3c.dom.CDATASection JavaDoc;
32 import org.w3c.dom.Comment JavaDoc;
33 import org.w3c.dom.Document JavaDoc;
34 import org.w3c.dom.Element JavaDoc;
35 import org.w3c.dom.Node JavaDoc;
36 import org.w3c.dom.ProcessingInstruction JavaDoc;
37
38 /**
39  * Access to the underlying structure of the model.
40  *
41  * @author Nam Nguyen
42  */

43
44 public abstract class DocumentModelAccess extends ModelAccess {
45     
46     public interface NodeUpdater {
47         void updateReference(Element JavaDoc node);
48         <T extends Node JavaDoc> void updateReference(List JavaDoc<T> pathToRoot);
49     }
50     
51     public abstract Document JavaDoc getDocumentRoot();
52     
53     /**
54      * Returns the associated document model.
55      * Subclass should override.
56      */

57     public AbstractDocumentModel getModel() {
58         return null;
59     }
60     
61     public abstract boolean areSameNodes(Node JavaDoc n1, Node JavaDoc n2);
62     
63     /**
64      * @return child element index in the children list of given parent.
65      */

66     public abstract int getElementIndexOf(Node JavaDoc parent, Element JavaDoc child);
67     
68     public abstract void setAttribute(Element JavaDoc element, String JavaDoc name, String JavaDoc value, NodeUpdater updater);
69     
70     public abstract void removeAttribute(Element JavaDoc element, String JavaDoc name, NodeUpdater updater);
71     
72     public abstract void appendChild(Node JavaDoc node, Node JavaDoc newChild, NodeUpdater updater);
73     
74     public abstract void insertBefore(Node JavaDoc node, Node JavaDoc newChild, Node JavaDoc refChild, NodeUpdater updater);
75     
76     public abstract void removeChild(Node JavaDoc node, Node JavaDoc child, NodeUpdater updater);
77     
78     public void removeChildren(Node JavaDoc node, Collection JavaDoc<Node JavaDoc> children, NodeUpdater updater) {
79         throw new UnsupportedOperationException JavaDoc();
80     }
81
82     public abstract void replaceChild(Node JavaDoc node, Node JavaDoc child, Node JavaDoc newChild, NodeUpdater updater);
83     
84     public abstract void setText(Element JavaDoc element, String JavaDoc val, NodeUpdater updater);
85     
86     public abstract void setPrefix(org.w3c.dom.Element JavaDoc node, String JavaDoc prefix);
87
88     public abstract int findPosition(org.w3c.dom.Node JavaDoc node);
89     
90     public abstract Element JavaDoc getContainingElement(int position);
91     
92     public abstract Element JavaDoc duplicate(Element JavaDoc element);
93
94     public String JavaDoc getXmlFragmentInclusive(Element JavaDoc element) {
95         if (getModel() == null) {
96             throw new UnsupportedOperationException JavaDoc("Unavailable because access does not support getModel()");
97         }
98         DocumentComponent component = getModel().findComponent(element);
99         if (component == null) {
100             throw new IllegalArgumentException JavaDoc("Know nothing about '"+element.getTagName()+"'");
101         }
102         Node JavaDoc parent = component.getParent() == null ?
103             getModel().getDocument() : ((DocumentComponent)component.getParent()).getPeer();
104         
105         int end = -1;
106         int start = findPosition(element);
107         assert start > -1 : "Negative start position";
108         Node JavaDoc next = element.getNextSibling();
109         try {
110             javax.swing.text.Document JavaDoc doc = getModel().getBaseDocument();
111             StringBuilder JavaDoc sb = new StringBuilder JavaDoc(doc.getText(0, doc.getLength()-1));
112             if (parent instanceof Document JavaDoc) {
113                 assert ((Document JavaDoc)parent).getDocumentElement() == element;
114                 end = sb.lastIndexOf(">") + 1;
115             } else if (next == null) { // use parent end tag
116
end = sb.indexOf(parent.getNodeName(), start)-2;
117             } else if (next instanceof Element JavaDoc) {
118                 end = findPosition(next);
119             } else {
120                 while (next != null &&
121                         ! (next instanceof Element JavaDoc) &&
122                         ! (next instanceof CDATASection JavaDoc) &&
123                         ! (next instanceof ProcessingInstruction JavaDoc) &&
124                         ! (next instanceof Comment JavaDoc))
125                 {
126                     next = next.getNextSibling();
127                 }
128                 if (next instanceof Element JavaDoc) {
129                     end = findPosition(next);
130                 } else if (next instanceof CDATASection JavaDoc || next instanceof Comment JavaDoc ||
131                            next instanceof ProcessingInstruction JavaDoc)
132                 {
133                     end = sb.indexOf(next.getNodeValue(), start);
134                 } else {
135                     end = sb.indexOf("</"+parent.getNodeName(), start);
136                 }
137             }
138
139             String JavaDoc result = sb.substring(start, end);
140             end = result.lastIndexOf("</"+element.getNodeName());
141             if (end < 0) { // self-closing
142
end = result.indexOf(">") + 1;
143             } else {
144                 end = result.indexOf(">", end) + 1;
145             }
146             return result.substring(0, end);
147         } catch(BadLocationException JavaDoc ble) {
148             assert false : "start="+start+" end="+end;
149             return "";
150         }
151     }
152     
153     /**
154      * @return XML fragment text of the given element content.
155      */

156     public abstract String JavaDoc getXmlFragment(Element JavaDoc element);
157     
158     /**
159      * Sets the XML fragment text for given element content.
160      * The XML fragment will be parsed and the resulting nodes will
161      * replace the current children of this documentation element.
162      * @param element element to set content to.
163      * @param text XML fragment text.
164      * @exception IOException if the fragment text is not well-form.
165      */

166     public abstract void setXmlFragment(Element JavaDoc element, String JavaDoc text, NodeUpdater updater) throws IOException JavaDoc;
167     
168     /**
169      * Returns map of attribute names and string values.
170      */

171     public abstract Map JavaDoc<QName JavaDoc,String JavaDoc> getAttributeMap(Element JavaDoc element);
172
173     /**
174      * Returns path from given element to given root; or null if the node is not in tree.
175      */

176     public abstract List JavaDoc<Element JavaDoc> getPathFromRoot(Document JavaDoc root, Element JavaDoc node);
177     
178     /**
179      * Returns xpath expression of given element.
180      */

181     public abstract String JavaDoc getXPath(Document JavaDoc root, Element JavaDoc node);
182     
183     /**
184      * Provide a uniform return value for undefined attribute values.
185      * XDM supports full fidelty so this deviates slightly from the DOM
186      * specification in that the return value for an undefined attribute
187      * is null instead of "". This method normalizes the return value
188      * for an undefined element to null.
189      */

190     public String JavaDoc normalizeUndefinedAttributeValue(String JavaDoc value) {
191     return value;
192     }
193
194     /**
195      * Returns node from given xpath expression
196      */

197     public abstract Node JavaDoc findNode(Document JavaDoc root, String JavaDoc xpath);
198     
199     /**
200      * Returns nodes from given xpath expression
201      */

202     public abstract List JavaDoc<Node JavaDoc> findNodes(Document JavaDoc root, String JavaDoc xpath);
203     
204     /**
205      * Returns element identity helper.
206      */

207     public abstract ElementIdentity getElementIdentity();
208     
209     /**
210      * Add/remove merge property change listener.
211      */

212     public abstract void addMergeEventHandler(PropertyChangeListener JavaDoc l);
213
214     public abstract void removeMergeEventHandler(PropertyChangeListener JavaDoc l);
215     
216     public abstract Node JavaDoc getOldEventNode(PropertyChangeEvent JavaDoc evt);
217
218     public abstract Node JavaDoc getOldEventParentNode(PropertyChangeEvent JavaDoc evt);
219     
220     public abstract Node JavaDoc getNewEventNode(PropertyChangeEvent JavaDoc evt);
221
222     public abstract Node JavaDoc getNewEventParentNode(PropertyChangeEvent JavaDoc evt);
223
224     public String JavaDoc lookupNamespaceURI(Node JavaDoc node, List JavaDoc<Node JavaDoc> pathToRoot) {
225         String JavaDoc prefix = node.getPrefix();
226         if (prefix == null) prefix = ""; //NOI18N
227
String JavaDoc namespace = node.lookupNamespaceURI(prefix);
228         if (namespace == null) {
229             for (Node JavaDoc n : pathToRoot) {
230                 namespace = n.lookupNamespaceURI(prefix);
231                 if (namespace != null) {
232                     break;
233                 }
234             }
235         }
236         return namespace;
237     }
238     
239     private long dirtyTimeMillis = 0;
240     public long dirtyIntervalMillis() {
241         if (dirtyTimeMillis == 0) return 0;
242         return System.currentTimeMillis() - dirtyTimeMillis;
243     }
244     
245     public void setDirty() {
246         dirtyTimeMillis = System.currentTimeMillis();
247     }
248     
249     public void unsetDirty() {
250         dirtyTimeMillis = 0;
251     }
252 }
253
Popular Tags