001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.core.text.plugin;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.CoreException;
015: import org.eclipse.pde.core.IModelChangedEvent;
016: import org.eclipse.pde.core.plugin.IPluginBase;
017: import org.eclipse.pde.core.plugin.IPluginElement;
018: import org.eclipse.pde.core.plugin.IPluginExtension;
019: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
020: import org.eclipse.pde.core.plugin.IPluginImport;
021: import org.eclipse.pde.core.plugin.IPluginLibrary;
022: import org.eclipse.pde.core.plugin.IPluginObject;
023: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
024:
025: public abstract class PluginBaseNode extends PluginObjectNode implements
026: IPluginBase {
027:
028: private String fSchemaVersion;
029:
030: /* (non-Javadoc)
031: * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginLibrary)
032: */
033: public void add(IPluginLibrary library) throws CoreException {
034: IDocumentElementNode parent = getEnclosingElement(
035: "runtime", true); //$NON-NLS-1$
036: if (library instanceof PluginLibraryNode) {
037: PluginLibraryNode node = (PluginLibraryNode) library;
038: node.setModel(getModel());
039: parent.addChildNode(node);
040: fireStructureChanged(library, IModelChangedEvent.INSERT);
041: }
042: }
043:
044: /* (non-Javadoc)
045: * @see org.eclipse.pde.core.plugin.IPluginBase#add(org.eclipse.pde.core.plugin.IPluginImport)
046: */
047: public void add(IPluginImport pluginImport) throws CoreException {
048: IDocumentElementNode parent = getEnclosingElement(
049: "requires", true); //$NON-NLS-1$
050: if (pluginImport instanceof PluginImportNode) {
051: PluginImportNode node = (PluginImportNode) pluginImport;
052: parent.addChildNode(node);
053: fireStructureChanged(pluginImport,
054: IModelChangedEvent.INSERT);
055: }
056: }
057:
058: public void add(IPluginImport[] pluginImports) throws CoreException {
059: IDocumentElementNode parent = getEnclosingElement(
060: "requires", true); //$NON-NLS-1$
061: for (int i = 0; i < pluginImports.length; i++) {
062: if (pluginImports[i] != null
063: && pluginImports[i] instanceof PluginImportNode) {
064: PluginImportNode node = (PluginImportNode) pluginImports[i];
065: parent.addChildNode(node);
066: }
067: }
068: fireStructureChanged(pluginImports, IModelChangedEvent.INSERT);
069: }
070:
071: /* (non-Javadoc)
072: * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginImport)
073: */
074: public void remove(IPluginImport pluginImport) throws CoreException {
075: IDocumentElementNode parent = getEnclosingElement(
076: "requires", false); //$NON-NLS-1$
077: if (parent != null) {
078: parent.removeChildNode((IDocumentElementNode) pluginImport);
079: pluginImport.setInTheModel(false);
080: fireStructureChanged(pluginImport,
081: IModelChangedEvent.REMOVE);
082: }
083: }
084:
085: public void remove(IPluginImport[] pluginImports)
086: throws CoreException {
087: IDocumentElementNode parent = getEnclosingElement(
088: "requires", false); //$NON-NLS-1$
089: if (parent != null) {
090: for (int i = 0; i < pluginImports.length; i++) {
091: parent
092: .removeChildNode((IDocumentElementNode) pluginImports[i]);
093: pluginImports[i].setInTheModel(false);
094: }
095: fireStructureChanged(pluginImports,
096: IModelChangedEvent.REMOVE);
097: }
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.pde.core.plugin.IPluginBase#getLibraries()
102: */
103: public IPluginLibrary[] getLibraries() {
104: ArrayList result = new ArrayList();
105: IDocumentElementNode requiresNode = getEnclosingElement(
106: "runtime", false); //$NON-NLS-1$
107: if (requiresNode != null) {
108: IDocumentElementNode[] children = requiresNode
109: .getChildNodes();
110: for (int i = 0; i < children.length; i++) {
111: if (children[i] instanceof IPluginLibrary)
112: result.add(children[i]);
113: }
114: }
115:
116: return (IPluginLibrary[]) result
117: .toArray(new IPluginLibrary[result.size()]);
118: }
119:
120: private IDocumentElementNode getEnclosingElement(
121: String elementName, boolean create) {
122: PluginElementNode element = null;
123: IDocumentElementNode[] children = getChildNodes();
124: for (int i = 0; i < children.length; i++) {
125: if (children[i] instanceof IPluginElement) {
126: if (((PluginElementNode) children[i]).getXMLTagName()
127: .equals(elementName)) {
128: element = (PluginElementNode) children[i];
129: break;
130: }
131: }
132: }
133: if (element == null && create) {
134: element = new PluginElementNode();
135: element.setXMLTagName(elementName);
136: element.setParentNode(this );
137: element.setModel(getModel());
138: element.setInTheModel(true);
139: if (elementName.equals("runtime")) { //$NON-NLS-1$
140: addChildNode(element, 0);
141: } else if (elementName.equals("requires")) { //$NON-NLS-1$
142: if (children.length > 0
143: && children[0].getXMLTagName()
144: .equals("runtime")) { //$NON-NLS-1$
145: addChildNode(element, 1);
146: } else {
147: addChildNode(element, 0);
148: }
149: }
150: }
151: return element;
152: }
153:
154: /* (non-Javadoc)
155: * @see org.eclipse.pde.core.plugin.IPluginBase#getImports()
156: */
157: public IPluginImport[] getImports() {
158: ArrayList result = new ArrayList();
159: IDocumentElementNode requiresNode = getEnclosingElement(
160: "requires", false); //$NON-NLS-1$
161: if (requiresNode != null) {
162: IDocumentElementNode[] children = requiresNode
163: .getChildNodes();
164: for (int i = 0; i < children.length; i++) {
165: if (children[i] instanceof IPluginImport)
166: result.add(children[i]);
167: }
168: }
169:
170: return (IPluginImport[]) result
171: .toArray(new IPluginImport[result.size()]);
172: }
173:
174: /* (non-Javadoc)
175: * @see org.eclipse.pde.core.plugin.IPluginBase#getProviderName()
176: */
177: public String getProviderName() {
178: return getXMLAttributeValue(P_PROVIDER);
179: }
180:
181: /* (non-Javadoc)
182: * @see org.eclipse.pde.core.plugin.IPluginBase#getVersion()
183: */
184: public String getVersion() {
185: return getXMLAttributeValue(P_VERSION);
186: }
187:
188: /* (non-Javadoc)
189: * @see org.eclipse.pde.core.plugin.IPluginBase#remove(org.eclipse.pde.core.plugin.IPluginLibrary)
190: */
191: public void remove(IPluginLibrary library) throws CoreException {
192: IDocumentElementNode parent = getEnclosingElement(
193: "runtime", false); //$NON-NLS-1$
194: if (parent != null) {
195: parent.removeChildNode((IDocumentElementNode) library);
196: library.setInTheModel(false);
197: fireStructureChanged(library, IModelChangedEvent.REMOVE);
198: }
199: }
200:
201: /* (non-Javadoc)
202: * @see org.eclipse.pde.core.plugin.IPluginBase#setProviderName(java.lang.String)
203: */
204: public void setProviderName(String providerName)
205: throws CoreException {
206: setXMLAttribute(P_PROVIDER, providerName);
207: }
208:
209: /* (non-Javadoc)
210: * @see org.eclipse.pde.core.plugin.IPluginBase#setVersion(java.lang.String)
211: */
212: public void setVersion(String version) throws CoreException {
213: setXMLAttribute(P_VERSION, version);
214: }
215:
216: /* (non-Javadoc)
217: * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginLibrary, org.eclipse.pde.core.plugin.IPluginLibrary)
218: */
219: public void swap(IPluginLibrary l1, IPluginLibrary l2)
220: throws CoreException {
221: IDocumentElementNode node = getEnclosingElement(
222: "runtime", false); //$NON-NLS-1$
223: if (node != null) {
224: node.swap((IDocumentElementNode) l1,
225: (IDocumentElementNode) l2);
226: firePropertyChanged(node, P_LIBRARY_ORDER, l1, l2);
227: }
228: }
229:
230: /* (non-Javadoc)
231: * @see org.eclipse.pde.core.plugin.IPluginBase#getSchemaVersion()
232: */
233: public String getSchemaVersion() {
234: return fSchemaVersion;
235: }
236:
237: /* (non-Javadoc)
238: * @see org.eclipse.pde.core.plugin.IPluginBase#setSchemaVersion(java.lang.String)
239: */
240: public void setSchemaVersion(String schemaVersion)
241: throws CoreException {
242: fSchemaVersion = schemaVersion;
243: }
244:
245: /* (non-Javadoc)
246: * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtension)
247: */
248: public void add(IPluginExtension extension) throws CoreException {
249: if (extension instanceof PluginExtensionNode) {
250: PluginExtensionNode node = (PluginExtensionNode) extension;
251: node.setModel(getModel());
252: addChildNode(node);
253: fireStructureChanged(extension, IModelChangedEvent.INSERT);
254: }
255: }
256:
257: /**
258: * @param extension
259: * @param position
260: * @throws CoreException
261: */
262: public void add(IPluginExtension extension, int position)
263: throws CoreException {
264: // TODO: MP: DND: Make API?
265: if ((extension instanceof PluginExtensionNode) == false) {
266: return;
267: } else if ((position < 0) || (position > getChildCount())) {
268: return;
269: }
270: PluginExtensionNode node = (PluginExtensionNode) extension;
271: node.setModel(getModel());
272: addChildNode(node, position);
273: fireStructureChanged(extension, IModelChangedEvent.INSERT);
274: }
275:
276: /* (non-Javadoc)
277: * @see org.eclipse.pde.core.plugin.IExtensions#add(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
278: */
279: public void add(IPluginExtensionPoint extensionPoint)
280: throws CoreException {
281: if (extensionPoint instanceof PluginExtensionPointNode) {
282: PluginExtensionPointNode node = (PluginExtensionPointNode) extensionPoint;
283: node.setModel(getModel());
284: extensionPoint.setInTheModel(true);
285: node.setParentNode(this );
286: IPluginExtensionPoint[] extPoints = getExtensionPoints();
287: if (extPoints.length > 0)
288: addChildNode(
289: node,
290: indexOf((IDocumentElementNode) extPoints[extPoints.length - 1]) + 1);
291: else {
292: IDocumentElementNode requires = getEnclosingElement(
293: "requires", false); //$NON-NLS-1$
294: if (requires != null) {
295: addChildNode(node, indexOf(requires) + 1);
296: } else {
297: IDocumentElementNode runtime = getEnclosingElement(
298: "runtime", false); //$NON-NLS-1$
299: if (runtime != null)
300: addChildNode(node, indexOf(runtime) + 1);
301: else
302: addChildNode(node, 0);
303: }
304: }
305: fireStructureChanged(extensionPoint,
306: IModelChangedEvent.INSERT);
307: }
308: }
309:
310: /* (non-Javadoc)
311: * @see org.eclipse.pde.core.plugin.IExtensions#getExtensionPoints()
312: */
313: public IPluginExtensionPoint[] getExtensionPoints() {
314: ArrayList result = new ArrayList();
315: IDocumentElementNode[] children = getChildNodes();
316: for (int i = 0; i < children.length; i++) {
317: if (children[i] instanceof IPluginExtensionPoint)
318: result.add(children[i]);
319: }
320: return (IPluginExtensionPoint[]) result
321: .toArray(new IPluginExtensionPoint[result.size()]);
322: }
323:
324: /* (non-Javadoc)
325: * @see org.eclipse.pde.core.plugin.IExtensions#getExtensions()
326: */
327: public IPluginExtension[] getExtensions() {
328: ArrayList result = new ArrayList();
329: IDocumentElementNode[] children = getChildNodes();
330: for (int i = 0; i < children.length; i++) {
331: if (children[i] instanceof IPluginExtension)
332: result.add(children[i]);
333: }
334: return (IPluginExtension[]) result
335: .toArray(new IPluginExtension[result.size()]);
336: }
337:
338: public int getIndexOf(IPluginExtension e) {
339: IPluginExtension[] children = getExtensions();
340: for (int i = 0; i < children.length; i++) {
341: if (children[i].equals(e))
342: return i;
343: }
344: return -1;
345: }
346:
347: /* (non-Javadoc)
348: * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtension)
349: */
350: public void remove(IPluginExtension extension) throws CoreException {
351: if (extension instanceof IDocumentElementNode) {
352: removeChildNode((IDocumentElementNode) extension);
353: extension.setInTheModel(false);
354: fireStructureChanged(extension, IModelChangedEvent.REMOVE);
355: }
356: }
357:
358: /* (non-Javadoc)
359: * @see org.eclipse.pde.core.plugin.IExtensions#remove(org.eclipse.pde.core.plugin.IPluginExtensionPoint)
360: */
361: public void remove(IPluginExtensionPoint extensionPoint)
362: throws CoreException {
363: if (extensionPoint instanceof IDocumentElementNode) {
364: removeChildNode((IDocumentElementNode) extensionPoint);
365: extensionPoint.setInTheModel(false);
366: fireStructureChanged(extensionPoint,
367: IModelChangedEvent.REMOVE);
368: }
369: }
370:
371: public void remove(IPluginObject node) {
372: if (node instanceof IDocumentElementNode) {
373: removeChildNode((IDocumentElementNode) node);
374: node.setInTheModel(false);
375: fireStructureChanged(node, IModelChangedEvent.REMOVE);
376: }
377: }
378:
379: /* (non-Javadoc)
380: * @see org.eclipse.pde.core.plugin.IExtensions#swap(org.eclipse.pde.core.plugin.IPluginExtension, org.eclipse.pde.core.plugin.IPluginExtension)
381: */
382: public void swap(IPluginExtension e1, IPluginExtension e2)
383: throws CoreException {
384: swap((IDocumentElementNode) e1, (IDocumentElementNode) e2);
385: firePropertyChanged(this , P_EXTENSION_ORDER, e1, e2);
386: }
387:
388: /* (non-Javadoc)
389: * @see org.eclipse.pde.core.plugin.IPluginBase#swap(org.eclipse.pde.core.plugin.IPluginImport, org.eclipse.pde.core.plugin.IPluginImport)
390: */
391: public void swap(IPluginImport import1, IPluginImport import2)
392: throws CoreException {
393: IDocumentElementNode node = getEnclosingElement(
394: "requires", false); //$NON-NLS-1$
395: if (node != null) {
396: node.swap((IDocumentElementNode) import1,
397: (IDocumentElementNode) import2);
398: firePropertyChanged(node, P_IMPORT_ORDER, import1, import2);
399: }
400: }
401:
402: /* (non-Javadoc)
403: * @see org.eclipse.pde.core.IIdentifiable#getId()
404: */
405: public String getId() {
406: return getXMLAttributeValue(P_ID);
407: }
408:
409: /* (non-Javadoc)
410: * @see org.eclipse.pde.core.IIdentifiable#setId(java.lang.String)
411: */
412: public void setId(String id) throws CoreException {
413: setXMLAttribute(P_ID, id);
414: }
415:
416: /* (non-Javadoc)
417: * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
418: */
419: public String getName() {
420: return getXMLAttributeValue(P_NAME);
421: }
422:
423: /* (non-Javadoc)
424: * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
425: */
426: public void setName(String name) throws CoreException {
427: setXMLAttribute(P_NAME, name);
428: }
429:
430: /* (non-Javadoc)
431: * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#write()
432: */
433: public String write(boolean indent) {
434: String newLine = getLineDelimiter();
435:
436: StringBuffer buffer = new StringBuffer();
437: buffer
438: .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + newLine); //$NON-NLS-1$
439: buffer.append("<?eclipse version=\"3.0\"?>" + newLine); //$NON-NLS-1$
440:
441: buffer.append(writeShallow(false) + newLine);
442:
443: IDocumentElementNode runtime = getEnclosingElement(
444: "runtime", false); //$NON-NLS-1$
445: if (runtime != null) {
446: runtime.setLineIndent(getLineIndent() + 3);
447: buffer.append(runtime.write(true) + newLine);
448: }
449:
450: IDocumentElementNode requires = getEnclosingElement(
451: "requires", false); //$NON-NLS-1$
452: if (requires != null) {
453: requires.setLineIndent(getLineIndent() + 3);
454: buffer.append(requires.write(true) + newLine);
455: }
456:
457: IPluginExtensionPoint[] extPoints = getExtensionPoints();
458: for (int i = 0; i < extPoints.length; i++) {
459: IDocumentElementNode extPoint = (IDocumentElementNode) extPoints[i];
460: extPoint.setLineIndent(getLineIndent() + 3);
461: buffer.append(extPoint.write(true) + newLine);
462: }
463:
464: IPluginExtension[] extensions = getExtensions();
465: for (int i = 0; i < extensions.length; i++) {
466: IDocumentElementNode extension = (IDocumentElementNode) extensions[i];
467: extension.setLineIndent(getLineIndent() + 3);
468: buffer.append(extension.write(true) + newLine);
469: }
470:
471: buffer.append("</" + getXMLTagName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
472: return buffer.toString();
473: }
474:
475: /* (non-Javadoc)
476: * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#writeShallow()
477: */
478: public String writeShallow(boolean terminate) {
479: String newLine = System.getProperty("line.separator"); //$NON-NLS-1$
480: StringBuffer buffer = new StringBuffer();
481: buffer.append("<" + getXMLTagName()); //$NON-NLS-1$
482: buffer.append(newLine);
483:
484: String id = getId();
485: if (id != null && id.trim().length() > 0)
486: buffer
487: .append(" " + P_ID + "=\"" + getWritableString(id) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
488:
489: String name = getName();
490: if (name != null && name.trim().length() > 0)
491: buffer
492: .append(" " + P_NAME + "=\"" + getWritableString(name) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
493:
494: String version = getVersion();
495: if (version != null && version.trim().length() > 0)
496: buffer
497: .append(" " + P_VERSION + "=\"" + getWritableString(version) + "\"" + newLine); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
498:
499: String provider = getProviderName();
500: if (provider != null && provider.trim().length() > 0) {
501: buffer
502: .append(" " + P_PROVIDER + "=\"" + getWritableString(provider) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
503: }
504:
505: String[] specific = getSpecificAttributes();
506: for (int i = 0; i < specific.length; i++)
507: buffer.append(newLine + specific[i]);
508: if (terminate)
509: buffer.append("/"); //$NON-NLS-1$
510: buffer.append(">"); //$NON-NLS-1$
511:
512: return buffer.toString();
513: }
514:
515: protected abstract String[] getSpecificAttributes();
516:
517: public boolean isRoot() {
518: return true;
519: }
520: }
|