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 org.eclipse.core.runtime.CoreException;
013: import org.eclipse.pde.core.plugin.IPluginAttribute;
014: import org.eclipse.pde.core.plugin.IPluginElement;
015: import org.eclipse.pde.core.plugin.IPluginExtension;
016: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
017: import org.eclipse.pde.core.plugin.IPluginImport;
018: import org.eclipse.pde.core.plugin.IPluginLibrary;
019: import org.eclipse.pde.core.plugin.IPluginModelFactory;
020: import org.eclipse.pde.core.plugin.IPluginObject;
021: import org.eclipse.pde.internal.core.text.DocumentTextNode;
022: import org.eclipse.pde.internal.core.text.IDocumentAttributeNode;
023: import org.eclipse.pde.internal.core.text.IDocumentNodeFactory;
024: import org.eclipse.pde.internal.core.text.IDocumentElementNode;
025: import org.eclipse.pde.internal.core.text.IDocumentTextNode;
026:
027: public class PluginDocumentNodeFactory implements IPluginModelFactory,
028: IDocumentNodeFactory {
029:
030: private PluginModelBase fModel;
031:
032: public PluginDocumentNodeFactory(PluginModelBase model) {
033: fModel = model;
034: }
035:
036: public IDocumentElementNode createDocumentNode(String name,
037: IDocumentElementNode parent) {
038: if (parent == null)
039: return createPluginBase(name);
040:
041: if (parent instanceof PluginBaseNode) {
042: if ("extension".equals(name)) //$NON-NLS-1$
043: return (IDocumentElementNode) createExtension();
044: if ("extension-point".equals(name)) //$NON-NLS-1$
045: return (IDocumentElementNode) createExtensionPoint();
046: } else {
047: if (name.equals("import") && parent instanceof PluginElementNode) { //$NON-NLS-1$
048: if (((PluginElementNode) parent).getName().equals(
049: "requires")) { //$NON-NLS-1$
050: IDocumentElementNode ancestor = parent
051: .getParentNode();
052: if (ancestor != null
053: && ancestor instanceof PluginBaseNode) {
054: return (IDocumentElementNode) createImport();
055: }
056: }
057: } else if (name.equals("library") && parent instanceof PluginElementNode) { //$NON-NLS-1$
058: if (((PluginElementNode) parent).getName().equals(
059: "runtime")) { //$NON-NLS-1$
060: IDocumentElementNode ancestor = parent
061: .getParentNode();
062: if (ancestor != null
063: && ancestor instanceof PluginBaseNode) {
064: return (IDocumentElementNode) createLibrary();
065: }
066: }
067: }
068: }
069: IDocumentElementNode node = (IDocumentElementNode) createElement((IPluginObject) parent);
070: node.setXMLTagName(name);
071: return node;
072: }
073:
074: public IDocumentAttributeNode createAttribute(String name,
075: String value, IDocumentElementNode enclosingElement) {
076: PluginAttribute attribute = new PluginAttribute();
077: try {
078: attribute.setName(name);
079: attribute.setValue(value);
080: } catch (CoreException e) {
081: }
082: attribute.setEnclosingElement(enclosingElement);
083: attribute.setModel(fModel);
084: attribute.setInTheModel(true);
085: return attribute;
086: }
087:
088: private PluginBaseNode createPluginBase(String name) {
089: return (PluginBaseNode) fModel.createPluginBase(name
090: .equals("fragment")); //$NON-NLS-1$
091:
092: }
093:
094: /* (non-Javadoc)
095: * @see org.eclipse.pde.core.plugin.IPluginModelFactory#createImport()
096: */
097: public IPluginImport createImport() {
098: PluginImportNode node = new PluginImportNode();
099: node.setModel(fModel);
100: node.setXMLTagName("import"); //$NON-NLS-1$
101: return node;
102: }
103:
104: public IPluginImport createImport(String pluginId) {
105: PluginImportNode node = new PluginImportNode(pluginId);
106: node.setModel(fModel);
107: node.setXMLTagName("import"); //$NON-NLS-1$
108: return node;
109: }
110:
111: /* (non-Javadoc)
112: * @see org.eclipse.pde.core.plugin.IPluginModelFactory#createLibrary()
113: */
114: public IPluginLibrary createLibrary() {
115: PluginLibraryNode node = new PluginLibraryNode();
116: node.setModel(fModel);
117: node.setXMLTagName("library"); //$NON-NLS-1$
118: return node;
119: }
120:
121: /* (non-Javadoc)
122: * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createAttribute(org.eclipse.pde.core.plugin.IPluginElement)
123: */
124: public IPluginAttribute createAttribute(IPluginElement element) {
125: return null;
126: }
127:
128: /* (non-Javadoc)
129: * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createElement(org.eclipse.pde.core.plugin.IPluginObject)
130: */
131: public IPluginElement createElement(IPluginObject parent) {
132: PluginElementNode node = new PluginElementNode();
133: node.setModel(fModel);
134: return node;
135: }
136:
137: /* (non-Javadoc)
138: * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createExtension()
139: */
140: public IPluginExtension createExtension() {
141: PluginExtensionNode node = new PluginExtensionNode();
142: node.setModel(fModel);
143: node.setXMLTagName("extension"); //$NON-NLS-1$
144: return node;
145: }
146:
147: /* (non-Javadoc)
148: * @see org.eclipse.pde.core.plugin.IExtensionsModelFactory#createExtensionPoint()
149: */
150: public IPluginExtensionPoint createExtensionPoint() {
151: PluginExtensionPointNode node = new PluginExtensionPointNode();
152: node.setModel(fModel);
153: node.setXMLTagName("extension-point"); //$NON-NLS-1$
154: return node;
155: }
156:
157: /* (non-Javadoc)
158: * @see org.eclipse.pde.internal.core.text.IDocumentNodeFactory#createDocumentTextNode(java.lang.String, org.eclipse.pde.internal.core.text.IDocumentElementNode)
159: */
160: public IDocumentTextNode createDocumentTextNode(String content,
161: IDocumentElementNode parent) {
162: DocumentTextNode textNode = new DocumentTextNode();
163: textNode.setEnclosingElement(parent);
164: parent.addTextNode(textNode);
165: textNode.setText(content.trim());
166: return textNode;
167: }
168: }
|