1 19 20 package org.netbeans.modules.xml.xam.dom; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.util.Collection ; 26 import java.util.List ; 27 import java.util.Map ; 28 import javax.swing.text.BadLocationException ; 29 import javax.xml.namespace.QName ; 30 import org.netbeans.modules.xml.xam.ModelAccess; 31 import org.w3c.dom.CDATASection ; 32 import org.w3c.dom.Comment ; 33 import org.w3c.dom.Document ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.ProcessingInstruction ; 37 38 43 44 public abstract class DocumentModelAccess extends ModelAccess { 45 46 public interface NodeUpdater { 47 void updateReference(Element node); 48 <T extends Node > void updateReference(List <T> pathToRoot); 49 } 50 51 public abstract Document getDocumentRoot(); 52 53 57 public AbstractDocumentModel getModel() { 58 return null; 59 } 60 61 public abstract boolean areSameNodes(Node n1, Node n2); 62 63 66 public abstract int getElementIndexOf(Node parent, Element child); 67 68 public abstract void setAttribute(Element element, String name, String value, NodeUpdater updater); 69 70 public abstract void removeAttribute(Element element, String name, NodeUpdater updater); 71 72 public abstract void appendChild(Node node, Node newChild, NodeUpdater updater); 73 74 public abstract void insertBefore(Node node, Node newChild, Node refChild, NodeUpdater updater); 75 76 public abstract void removeChild(Node node, Node child, NodeUpdater updater); 77 78 public void removeChildren(Node node, Collection <Node > children, NodeUpdater updater) { 79 throw new UnsupportedOperationException (); 80 } 81 82 public abstract void replaceChild(Node node, Node child, Node newChild, NodeUpdater updater); 83 84 public abstract void setText(Element element, String val, NodeUpdater updater); 85 86 public abstract void setPrefix(org.w3c.dom.Element node, String prefix); 87 88 public abstract int findPosition(org.w3c.dom.Node node); 89 90 public abstract Element getContainingElement(int position); 91 92 public abstract Element duplicate(Element element); 93 94 public String getXmlFragmentInclusive(Element element) { 95 if (getModel() == null) { 96 throw new UnsupportedOperationException ("Unavailable because access does not support getModel()"); 97 } 98 DocumentComponent component = getModel().findComponent(element); 99 if (component == null) { 100 throw new IllegalArgumentException ("Know nothing about '"+element.getTagName()+"'"); 101 } 102 Node 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 next = element.getNextSibling(); 109 try { 110 javax.swing.text.Document doc = getModel().getBaseDocument(); 111 StringBuilder sb = new StringBuilder (doc.getText(0, doc.getLength()-1)); 112 if (parent instanceof Document ) { 113 assert ((Document )parent).getDocumentElement() == element; 114 end = sb.lastIndexOf(">") + 1; 115 } else if (next == null) { end = sb.indexOf(parent.getNodeName(), start)-2; 117 } else if (next instanceof Element ) { 118 end = findPosition(next); 119 } else { 120 while (next != null && 121 ! (next instanceof Element ) && 122 ! (next instanceof CDATASection ) && 123 ! (next instanceof ProcessingInstruction ) && 124 ! (next instanceof Comment )) 125 { 126 next = next.getNextSibling(); 127 } 128 if (next instanceof Element ) { 129 end = findPosition(next); 130 } else if (next instanceof CDATASection || next instanceof Comment || 131 next instanceof ProcessingInstruction ) 132 { 133 end = sb.indexOf(next.getNodeValue(), start); 134 } else { 135 end = sb.indexOf("</"+parent.getNodeName(), start); 136 } 137 } 138 139 String result = sb.substring(start, end); 140 end = result.lastIndexOf("</"+element.getNodeName()); 141 if (end < 0) { end = result.indexOf(">") + 1; 143 } else { 144 end = result.indexOf(">", end) + 1; 145 } 146 return result.substring(0, end); 147 } catch(BadLocationException ble) { 148 assert false : "start="+start+" end="+end; 149 return ""; 150 } 151 } 152 153 156 public abstract String getXmlFragment(Element element); 157 158 166 public abstract void setXmlFragment(Element element, String text, NodeUpdater updater) throws IOException ; 167 168 171 public abstract Map <QName ,String > getAttributeMap(Element element); 172 173 176 public abstract List <Element > getPathFromRoot(Document root, Element node); 177 178 181 public abstract String getXPath(Document root, Element node); 182 183 190 public String normalizeUndefinedAttributeValue(String value) { 191 return value; 192 } 193 194 197 public abstract Node findNode(Document root, String xpath); 198 199 202 public abstract List <Node > findNodes(Document root, String xpath); 203 204 207 public abstract ElementIdentity getElementIdentity(); 208 209 212 public abstract void addMergeEventHandler(PropertyChangeListener l); 213 214 public abstract void removeMergeEventHandler(PropertyChangeListener l); 215 216 public abstract Node getOldEventNode(PropertyChangeEvent evt); 217 218 public abstract Node getOldEventParentNode(PropertyChangeEvent evt); 219 220 public abstract Node getNewEventNode(PropertyChangeEvent evt); 221 222 public abstract Node getNewEventParentNode(PropertyChangeEvent evt); 223 224 public String lookupNamespaceURI(Node node, List <Node > pathToRoot) { 225 String prefix = node.getPrefix(); 226 if (prefix == null) prefix = ""; String namespace = node.lookupNamespaceURI(prefix); 228 if (namespace == null) { 229 for (Node 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 |