1 11 package org.eclipse.pde.internal.core.text.plugin; 12 13 import java.util.ArrayList ; 14 15 import org.eclipse.core.runtime.CoreException; 16 import org.eclipse.pde.core.IModelChangedEvent; 17 import org.eclipse.pde.core.plugin.IPluginBase; 18 import org.eclipse.pde.core.plugin.IPluginElement; 19 import org.eclipse.pde.core.plugin.IPluginExtension; 20 import org.eclipse.pde.core.plugin.IPluginExtensionPoint; 21 import org.eclipse.pde.core.plugin.IPluginImport; 22 import org.eclipse.pde.core.plugin.IPluginLibrary; 23 import org.eclipse.pde.core.plugin.IPluginObject; 24 import org.eclipse.pde.internal.core.text.IDocumentNode; 25 26 public abstract class PluginBaseNode extends PluginObjectNode implements IPluginBase { 27 28 private String fSchemaVersion; 29 30 33 public void add(IPluginLibrary library) throws CoreException { 34 IDocumentNode parent = getEnclosingElement("runtime", true); if (library instanceof PluginLibraryNode) { 36 PluginLibraryNode node = (PluginLibraryNode)library; 37 node.setModel(getModel()); 38 parent.addChildNode(node); 39 fireStructureChanged(library, IModelChangedEvent.INSERT); 40 } 41 } 42 45 public void add(IPluginImport pluginImport) throws CoreException { 46 IDocumentNode parent = getEnclosingElement("requires", true); if (pluginImport instanceof PluginImportNode) { 48 PluginImportNode node = (PluginImportNode)pluginImport; 49 parent.addChildNode(node); 50 fireStructureChanged(pluginImport, IModelChangedEvent.INSERT); 51 } 52 } 53 54 public void add(IPluginImport[] pluginImports) throws CoreException { 55 IDocumentNode parent = getEnclosingElement("requires", true); for (int i = 0; i < pluginImports.length; i++) { 57 if (pluginImports[i] != null && pluginImports[i] instanceof PluginImportNode) { 58 PluginImportNode node = (PluginImportNode)pluginImports[i]; 59 parent.addChildNode(node); 60 } 61 } 62 fireStructureChanged(pluginImports, IModelChangedEvent.INSERT); 63 } 64 67 public void remove(IPluginImport pluginImport) throws CoreException { 68 IDocumentNode parent = getEnclosingElement("requires", false); if (parent != null) { 70 parent.removeChildNode((IDocumentNode)pluginImport); 71 pluginImport.setInTheModel(false); 72 fireStructureChanged(pluginImport, IModelChangedEvent.REMOVE); 73 } 74 } 75 76 public void remove(IPluginImport[] pluginImports) throws CoreException { 77 IDocumentNode parent = getEnclosingElement("requires", false); if (parent != null) { 79 for (int i = 0; i < pluginImports.length; i++) { 80 parent.removeChildNode((IDocumentNode)pluginImports[i]); 81 pluginImports[i].setInTheModel(false); 82 } 83 fireStructureChanged(pluginImports, IModelChangedEvent.REMOVE); 84 } 85 } 86 87 90 public IPluginLibrary[] getLibraries() { 91 ArrayList result = new ArrayList (); 92 IDocumentNode requiresNode = getEnclosingElement("runtime", false); if (requiresNode != null) { 94 IDocumentNode[] children = requiresNode.getChildNodes(); 95 for (int i = 0; i < children.length; i++) { 96 if (children[i] instanceof IPluginLibrary) 97 result.add(children[i]); 98 } 99 } 100 101 return (IPluginLibrary[]) result.toArray(new IPluginLibrary[result.size()]); 102 } 103 104 private IDocumentNode getEnclosingElement(String elementName, boolean create) { 105 PluginElementNode element = null; 106 IDocumentNode[] children = getChildNodes(); 107 for (int i = 0; i < children.length; i++) { 108 if (children[i] instanceof IPluginElement) { 109 if (((PluginElementNode)children[i]).getXMLTagName().equals(elementName)) { 110 element = (PluginElementNode)children[i]; 111 break; 112 } 113 } 114 } 115 if (element == null && create) { 116 element = new PluginElementNode(); 117 element.setXMLTagName(elementName); 118 element.setParentNode(this); 119 element.setModel(getModel()); 120 element.setInTheModel(true); 121 if (elementName.equals("runtime")) { addChildNode(element, 0); 123 } else if (elementName.equals("requires")) { if (children.length > 0 && children[0].getXMLTagName().equals("runtime")) { addChildNode(element, 1); 126 } else { 127 addChildNode(element, 0); 128 } 129 } 130 } 131 return element; 132 } 133 134 137 public IPluginImport[] getImports() { 138 ArrayList result = new ArrayList (); 139 IDocumentNode requiresNode = getEnclosingElement("requires", false); if (requiresNode != null) { 141 IDocumentNode[] children = requiresNode.getChildNodes(); 142 for (int i = 0; i < children.length; i++) { 143 if (children[i] instanceof IPluginImport) 144 result.add(children[i]); 145 } 146 } 147 148 return (IPluginImport[]) result.toArray(new IPluginImport[result.size()]); 149 } 150 153 public String getProviderName() { 154 return getXMLAttributeValue(P_PROVIDER); 155 } 156 159 public String getVersion() { 160 return getXMLAttributeValue(P_VERSION); 161 } 162 165 public void remove(IPluginLibrary library) throws CoreException { 166 IDocumentNode parent = getEnclosingElement("runtime", false); if (parent != null) { 168 parent.removeChildNode((IDocumentNode)library); 169 library.setInTheModel(false); 170 fireStructureChanged(library, IModelChangedEvent.REMOVE); 171 } 172 } 173 176 public void setProviderName(String providerName) throws CoreException { 177 setXMLAttribute(P_PROVIDER, providerName); 178 } 179 182 public void setVersion(String version) throws CoreException { 183 setXMLAttribute(P_VERSION, version); 184 } 185 188 public void swap(IPluginLibrary l1, IPluginLibrary l2) throws CoreException { 189 IDocumentNode node = getEnclosingElement("runtime", false); if (node != null) { 191 node.swap((IDocumentNode)l1, (IDocumentNode)l2); 192 firePropertyChanged(node, P_LIBRARY_ORDER, l1, l2); 193 } 194 } 195 198 public String getSchemaVersion() { 199 return fSchemaVersion; 200 } 201 204 public void setSchemaVersion(String schemaVersion) throws CoreException { 205 fSchemaVersion = schemaVersion; 206 } 207 210 public void add(IPluginExtension extension) throws CoreException { 211 if (extension instanceof PluginExtensionNode) { 212 PluginExtensionNode node = (PluginExtensionNode)extension; 213 node.setModel(getModel()); 214 addChildNode(node); 215 fireStructureChanged(extension, IModelChangedEvent.INSERT); 216 } 217 } 218 221 public void add(IPluginExtensionPoint extensionPoint) throws CoreException { 222 if (extensionPoint instanceof PluginExtensionPointNode) { 223 PluginExtensionPointNode node = (PluginExtensionPointNode)extensionPoint; 224 node.setModel(getModel()); 225 extensionPoint.setInTheModel(true); 226 node.setParentNode(this); 227 IPluginExtensionPoint[] extPoints = getExtensionPoints(); 228 if (extPoints.length > 0) 229 addChildNode(node, indexOf((IDocumentNode)extPoints[extPoints.length - 1]) + 1); 230 else { 231 IDocumentNode requires = getEnclosingElement("requires", false); if (requires != null) { 233 addChildNode(node, indexOf(requires) + 1); 234 } else { 235 IDocumentNode runtime = getEnclosingElement("runtime", false); if (runtime != null) 237 addChildNode(node, indexOf(runtime) + 1); 238 else 239 addChildNode(node, 0); 240 } 241 } 242 fireStructureChanged(extensionPoint, IModelChangedEvent.INSERT); 243 } 244 } 245 248 public IPluginExtensionPoint[] getExtensionPoints() { 249 ArrayList result = new ArrayList (); 250 IDocumentNode[] children = getChildNodes(); 251 for (int i = 0; i < children.length; i++) { 252 if (children[i] instanceof IPluginExtensionPoint) 253 result.add(children[i]); 254 } 255 return (IPluginExtensionPoint[]) result.toArray(new IPluginExtensionPoint[result.size()]); 256 } 257 260 public IPluginExtension[] getExtensions() { 261 ArrayList result = new ArrayList (); 262 IDocumentNode[] children = getChildNodes(); 263 for (int i = 0; i < children.length; i++) { 264 if (children[i] instanceof IPluginExtension) 265 result.add(children[i]); 266 } 267 return (IPluginExtension[]) result.toArray(new IPluginExtension[result.size()]); 268 } 269 public int getIndexOf(IPluginExtension e) { 270 IPluginExtension [] children = getExtensions(); 271 for (int i=0; i<children.length; i++) { 272 if (children[i].equals(e)) 273 return i; 274 } 275 return -1; 276 } 277 280 public void remove(IPluginExtension extension) throws CoreException { 281 if (extension instanceof IDocumentNode) { 282 removeChildNode((IDocumentNode)extension); 283 extension.setInTheModel(false); 284 fireStructureChanged(extension, IModelChangedEvent.REMOVE); 285 } 286 } 287 290 public void remove(IPluginExtensionPoint extensionPoint) 291 throws CoreException { 292 if (extensionPoint instanceof IDocumentNode) { 293 removeChildNode((IDocumentNode)extensionPoint); 294 extensionPoint.setInTheModel(false); 295 fireStructureChanged(extensionPoint, IModelChangedEvent.REMOVE); 296 } 297 } 298 299 public void remove(IPluginObject node) { 300 if (node instanceof IDocumentNode) { 301 removeChildNode((IDocumentNode)node); 302 node.setInTheModel(false); 303 fireStructureChanged(node, IModelChangedEvent.REMOVE); 304 } 305 } 306 309 public void swap(IPluginExtension e1, IPluginExtension e2) 310 throws CoreException { 311 swap((IDocumentNode)e1, (IDocumentNode)e2); 312 firePropertyChanged(this, P_EXTENSION_ORDER, e1, e2); 313 } 314 315 318 public void swap(IPluginImport import1, IPluginImport import2) 319 throws CoreException { 320 IDocumentNode node = getEnclosingElement("requires", false); if (node != null) { 322 node.swap((IDocumentNode)import1, (IDocumentNode)import2); 323 firePropertyChanged(node, P_IMPORT_ORDER, import1, import2); 324 } 325 } 326 329 public String getId() { 330 return getXMLAttributeValue(P_ID); 331 } 332 335 public void setId(String id) throws CoreException { 336 setXMLAttribute(P_ID, id); 337 } 338 339 342 public String getName() { 343 return getXMLAttributeValue(P_NAME); 344 } 345 346 349 public void setName(String name) throws CoreException { 350 setXMLAttribute(P_NAME, name); 351 } 352 353 356 public String write(boolean indent) { 357 String newLine = getLineDelimiter(); 358 359 StringBuffer buffer = new StringBuffer (); 360 buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newLine); buffer.append("<?eclipse version=\"3.0\"?>" + newLine); 363 buffer.append(writeShallow(false) + newLine); 364 365 IDocumentNode runtime = getEnclosingElement("runtime", false); if (runtime != null) { 367 runtime.setLineIndent(getLineIndent() + 3); 368 buffer.append(runtime.write(true) + newLine); 369 } 370 371 IDocumentNode requires = getEnclosingElement("requires", false); if (requires != null) { 373 requires.setLineIndent(getLineIndent() + 3); 374 buffer.append(requires.write(true) + newLine); 375 } 376 377 IPluginExtensionPoint[] extPoints = getExtensionPoints(); 378 for (int i = 0; i < extPoints.length; i++) { 379 IDocumentNode extPoint = (IDocumentNode)extPoints[i]; 380 extPoint.setLineIndent(getLineIndent() + 3); 381 buffer.append(extPoint.write(true) + newLine); 382 } 383 384 IPluginExtension[] extensions = getExtensions(); 385 for (int i = 0; i < extensions.length; i++) { 386 IDocumentNode extension = (IDocumentNode)extensions[i]; 387 extension.setLineIndent(getLineIndent() + 3); 388 buffer.append(extension.write(true) + newLine); 389 } 390 391 buffer.append("</" + getXMLTagName() + ">"); return buffer.toString(); 393 } 394 395 398 public String writeShallow(boolean terminate) { 399 String newLine = System.getProperty("line.separator"); StringBuffer buffer = new StringBuffer (); 401 buffer.append("<" + getXMLTagName()); buffer.append(newLine); 403 404 String id = getId(); 405 if (id != null && id.trim().length() > 0) 406 buffer.append(" " + P_ID + "=\"" + getWritableString(id) + "\"" + newLine); 408 String name = getName(); 409 if (name != null && name.trim().length() > 0) 410 buffer.append(" " + P_NAME + "=\"" + getWritableString(name) + "\"" + newLine); 412 String version = getVersion(); 413 if (version != null && version.trim().length() > 0) 414 buffer.append(" " + P_VERSION + "=\"" + getWritableString(version) + "\"" + newLine); 416 String provider = getProviderName(); 417 if (provider != null && provider.trim().length() > 0) { 418 buffer.append(" " + P_PROVIDER + "=\"" + getWritableString(provider) + "\""); } 420 421 String [] specific = getSpecificAttributes(); 422 for (int i = 0; i < specific.length; i++) 423 buffer.append(newLine + specific[i]); 424 if (terminate) 425 buffer.append("/"); buffer.append(">"); 428 return buffer.toString(); 429 } 430 431 protected abstract String [] getSpecificAttributes(); 432 433 public boolean isRoot() { 434 return true; 435 } 436 } 437 | Popular Tags |