1 11 package org.eclipse.pde.internal.core.text.plugin; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.Map ; 16 import java.util.TreeMap ; 17 18 import org.eclipse.pde.core.plugin.ISharedPluginModel; 19 import org.eclipse.pde.internal.core.ischema.ISchema; 20 import org.eclipse.pde.internal.core.text.IDocumentAttribute; 21 import org.eclipse.pde.internal.core.text.IDocumentNode; 22 import org.eclipse.pde.internal.core.text.IDocumentTextNode; 23 24 public abstract class PluginDocumentNode implements IDocumentNode { 25 26 private transient IDocumentNode fParent; 27 private transient boolean fIsErrorNode; 28 private transient int fLength = -1; 29 private transient int fOffset = -1; 30 private transient IDocumentNode fPreviousSibling; 31 private transient int fIndent = 0; 32 33 private ArrayList fChildren = new ArrayList (); 34 protected Map fAttributes = new TreeMap (); 35 private String fTag; 36 protected IDocumentTextNode fTextNode; 37 38 41 public IDocumentNode[] getChildNodes() { 42 return (IDocumentNode[]) fChildren.toArray(new IDocumentNode[fChildren.size()]); 43 } 44 45 48 public int indexOf(IDocumentNode child) { 49 return fChildren.indexOf(child); 50 } 51 52 55 public IDocumentNode getChildAt(int index) { 56 if (index < fChildren.size()) 57 return (IDocumentNode)fChildren.get(index); 58 return null; 59 } 60 63 public IDocumentNode getParentNode() { 64 return fParent; 65 } 66 69 public void setParentNode(IDocumentNode node) { 70 fParent = node; 71 } 72 75 public void addChildNode(IDocumentNode child) { 76 addChildNode(child, fChildren.size()); 77 } 78 79 82 public void addChildNode(IDocumentNode child, int position) { 83 fChildren.add(position, child); 84 if (position > 0 && fChildren.size() > 1) 85 child.setPreviousSibling((IDocumentNode)fChildren.get(position - 1)); 86 if (fChildren.size() > 1 && position < fChildren.size() - 1) 87 ((IDocumentNode)fChildren.get(position + 1)).setPreviousSibling(child); 88 child.setParentNode(this); 89 } 90 91 94 public IDocumentNode removeChildNode(IDocumentNode child) { 95 int index = fChildren.indexOf(child); 96 if (index != -1) { 97 fChildren.remove(child); 98 if (index < fChildren.size()) { 99 IDocumentNode prevSibling = index == 0 ? null : (IDocumentNode)fChildren.get(index - 1); 100 ((IDocumentNode)fChildren.get(index)).setPreviousSibling(prevSibling); 101 return child; 102 } 103 } 104 return null; 105 } 106 109 public boolean isErrorNode() { 110 return fIsErrorNode; 111 } 112 115 public void setIsErrorNode(boolean isErrorNode) { 116 fIsErrorNode = isErrorNode; 117 } 118 121 public void setOffset(int offset) { 122 fOffset = offset; 123 } 124 127 public void setLength(int length) { 128 fLength = length; 129 } 130 133 public int getOffset() { 134 return fOffset; 135 } 136 139 public int getLength() { 140 return fLength; 141 } 142 145 public void setXMLAttribute(IDocumentAttribute attribute) { 146 fAttributes.put(attribute.getAttributeName(), attribute); 147 } 148 151 public String getXMLAttributeValue(String name) { 152 PluginAttribute attr = (PluginAttribute)fAttributes.get(name); 153 return attr == null ? null : attr.getValue(); 154 } 155 156 159 public void setXMLTagName(String tag) { 160 fTag = tag; 161 } 162 163 166 public String getXMLTagName() { 167 return fTag; 168 } 169 170 173 public IDocumentAttribute getDocumentAttribute(String name) { 174 return (IDocumentAttribute)fAttributes.get(name); 175 } 176 177 180 public int getLineIndent() { 181 return fIndent; 182 } 183 184 187 public void setLineIndent(int indent) { 188 fIndent = indent; 189 } 190 191 194 public IDocumentAttribute[] getNodeAttributes() { 195 ArrayList list = new ArrayList (); 196 Iterator iter = fAttributes.values().iterator(); 197 while (iter.hasNext()) 198 list.add(iter.next()); 199 return (IDocumentAttribute[])list.toArray(new IDocumentAttribute[list.size()]); 200 } 201 202 205 public IDocumentNode getPreviousSibling() { 206 return fPreviousSibling; 207 } 208 209 212 public void setPreviousSibling(IDocumentNode sibling) { 213 fPreviousSibling = sibling; 214 } 215 216 protected String getIndent() { 217 StringBuffer buffer = new StringBuffer (); 218 for (int i = 0; i < fIndent; i++) { 219 buffer.append(" "); } 221 return buffer.toString(); 222 } 223 224 227 public void swap(IDocumentNode child1, IDocumentNode child2) { 228 int index1 = fChildren.indexOf(child1); 229 int index2 = fChildren.indexOf(child2); 230 231 fChildren.set(index1, child2); 232 fChildren.set(index2, child1); 233 234 child1.setPreviousSibling(index2 == 0 ? null : (IDocumentNode)fChildren.get(index2 - 1)); 235 child2.setPreviousSibling(index1 == 0 ? null : (IDocumentNode)fChildren.get(index1 - 1)); 236 237 if (index1 < fChildren.size() - 1) 238 ((IDocumentNode)fChildren.get(index1 + 1)).setPreviousSibling(child2); 239 240 if (index2 < fChildren.size() - 1) 241 ((IDocumentNode)fChildren.get(index2 + 1)).setPreviousSibling(child1); 242 } 243 244 247 public void addTextNode(IDocumentTextNode textNode) { 248 fTextNode = textNode; 249 } 250 251 254 public IDocumentTextNode getTextNode() { 255 return fTextNode; 256 } 257 260 public void removeTextNode() { 261 fTextNode = null; 262 } 263 264 267 public void removeDocumentAttribute(IDocumentAttribute attr) { 268 fAttributes.remove(attr.getAttributeName()); 269 } 270 271 274 public void reconnect(ISharedPluginModel model, ISchema schema, IDocumentNode parent) { 275 reconnectDocument(); 278 reconnectParent(parent); 282 reconnectPreviousSibling(parent); 286 reconnectText(); 288 reconnectAttributes(model, schema); 290 reconnectChildren(model, schema); 292 } 293 294 298 private void reconnectAttributes(ISharedPluginModel model, ISchema schema) { 299 Iterator keys = fAttributes.keySet().iterator(); 301 while (keys.hasNext()) { 303 String key = (String )keys.next(); 304 IDocumentAttribute attribute = (IDocumentAttribute)fAttributes.get(key); 305 attribute.reconnect(model, schema, this); 306 } 307 } 308 309 313 private void reconnectChildren(ISharedPluginModel model, ISchema schema) { 314 for (int i = 0; i < fChildren.size(); i++) { 316 IDocumentNode child = (IDocumentNode)fChildren.get(i); 317 child.reconnect(model, schema, this); 319 } 320 } 321 322 325 private void reconnectDocument() { 326 fIndent = 0; 328 fIsErrorNode = false; 330 fLength = -1; 332 fOffset = -1; 334 } 335 336 339 private void reconnectParent(IDocumentNode parent) { 340 fParent = parent; 342 } 343 344 347 private void reconnectPreviousSibling(IDocumentNode parent) { 348 int childCount = parent.getChildCount(); 350 if (childCount < 1) { 351 fPreviousSibling = null; 352 } else { 353 fPreviousSibling = (IDocumentNode)parent.getChildAt(childCount - 1); 356 } 357 } 358 359 362 private void reconnectText() { 363 if (fTextNode != null) { 365 fTextNode.reconnect(this); 366 } 367 } 368 369 372 public int getChildCount() { 373 return fChildren.size(); 374 } 375 376 } 377 | Popular Tags |