KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > text > plugin > PluginElementNode


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.text.plugin;
12
13 import java.io.PrintWriter JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.pde.core.plugin.IPluginAttribute;
17 import org.eclipse.pde.core.plugin.IPluginElement;
18 import org.eclipse.pde.core.plugin.IPluginExtension;
19 import org.eclipse.pde.core.plugin.ISharedPluginModel;
20 import org.eclipse.pde.internal.core.ischema.ISchema;
21 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
22 import org.eclipse.pde.internal.core.text.DocumentTextNode;
23 import org.eclipse.pde.internal.core.text.IDocumentAttribute;
24 import org.eclipse.pde.internal.core.text.IDocumentNode;
25 import org.eclipse.pde.internal.core.text.IDocumentTextNode;
26
27 public class PluginElementNode extends PluginParentNode implements
28         IPluginElement, IDocumentElement {
29
30     private static final long serialVersionUID = 1L;
31
32     private transient ISchemaElement elementInfo;
33     /* (non-Javadoc)
34      * @see org.eclipse.pde.core.plugin.IPluginElement#createCopy()
35      */

36     public IPluginElement createCopy() {
37         return null;
38     }
39     /* (non-Javadoc)
40      * @see org.eclipse.pde.core.plugin.IPluginElement#getAttribute(java.lang.String)
41      */

42     public IPluginAttribute getAttribute(String JavaDoc name) {
43         return (IPluginAttribute)fAttributes.get(name);
44     }
45     /* (non-Javadoc)
46      * @see org.eclipse.pde.core.plugin.IPluginElement#getAttributes()
47      */

48     public IPluginAttribute[] getAttributes() {
49         return (IPluginAttribute[])fAttributes.values().toArray(new IPluginAttribute[fAttributes.size()]);
50     }
51     /* (non-Javadoc)
52      * @see org.eclipse.pde.core.plugin.IPluginElement#getAttributeCount()
53      */

54     public int getAttributeCount() {
55         return fAttributes.size();
56     }
57     /* (non-Javadoc)
58      * @see org.eclipse.pde.core.plugin.IPluginElement#getText()
59      */

60     public String JavaDoc getText() {
61         IDocumentTextNode node = getTextNode();
62         return node == null ? "" : node.getText(); //$NON-NLS-1$
63
}
64     /* (non-Javadoc)
65      * @see org.eclipse.pde.core.plugin.IPluginElement#setAttribute(java.lang.String, java.lang.String)
66      */

67     public void setAttribute(String JavaDoc name, String JavaDoc value) throws CoreException {
68         setXMLAttribute(name, value);
69     }
70     /* (non-Javadoc)
71      * @see org.eclipse.pde.core.plugin.IPluginElement#setText(java.lang.String)
72      */

73     public void setText(String JavaDoc text) throws CoreException {
74         IDocumentTextNode node = getTextNode();
75         String JavaDoc oldText = node == null ? null : node.getText();
76         if (node == null) {
77             node = new DocumentTextNode();
78             node.setEnclosingElement(this);
79             addTextNode(node);
80         }
81         node.setText(text.trim());
82         firePropertyChanged(node, P_TEXT, oldText, text);
83     }
84     
85     /* (non-Javadoc)
86      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#write()
87      */

88     public String JavaDoc write(boolean indent) {
89         String JavaDoc sep = getLineDelimiter();
90         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
91         if (indent)
92             buffer.append(getIndent());
93         
94         IDocumentNode[] children = getChildNodes();
95         String JavaDoc text = getText();
96         buffer.append(writeShallow(false));
97         if (getAttributeCount() > 0 || children.length > 0 || text.length() > 0)
98             buffer.append(sep);
99         if (children.length > 0 || text.length() > 0) {
100             if (text.length() > 0) {
101                 buffer.append(getIndent());
102                 buffer.append(" "); //$NON-NLS-1$
103
buffer.append(text);
104                 buffer.append(sep);
105             }
106             for (int i = 0; i < children.length; i++) {
107                 children[i].setLineIndent(getLineIndent() + 3);
108                 buffer.append(children[i].write(true));
109                 buffer.append(sep);
110             }
111         }
112         if (getAttributeCount() > 0 || children.length > 0 || text.length() > 0)
113             buffer.append(getIndent());
114
115         buffer.append("</" + getXMLTagName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
116
return buffer.toString();
117     }
118     
119     /* (non-Javadoc)
120      * @see org.eclipse.pde.internal.ui.model.plugin.PluginObjectNode#writeShallow(boolean)
121      */

122     public String JavaDoc writeShallow(boolean terminate) {
123         String JavaDoc sep = getLineDelimiter();
124         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc("<" + getXMLTagName()); //$NON-NLS-1$
125

126         IDocumentAttribute[] attrs = getNodeAttributes();
127         for (int i = 0; i < attrs.length; i++) {
128             if (attrs[i].getAttributeValue().length() > 0)
129                 buffer.append(sep + getIndent() + " " + attrs[i].write()); //$NON-NLS-1$
130
}
131         if (terminate)
132             buffer.append("/"); //$NON-NLS-1$
133
buffer.append(">"); //$NON-NLS-1$
134
return buffer.toString();
135     }
136     
137     /* (non-Javadoc)
138      * @see org.eclipse.pde.core.plugin.IPluginObject#getName()
139      */

140     public String JavaDoc getName() {
141         return getXMLTagName();
142     }
143     
144     /* (non-Javadoc)
145      * @see org.eclipse.pde.core.plugin.IPluginObject#setName(java.lang.String)
146      */

147     public void setName(String JavaDoc name) throws CoreException {
148         setXMLTagName(name);
149     }
150     
151     /* (non-Javadoc)
152      * @see org.eclipse.pde.core.plugin.IPluginElement#getElementInfo()
153      */

154     public Object JavaDoc getElementInfo() {
155         if (elementInfo == null) {
156             IDocumentNode node = getParentNode();
157             for (;;) {
158                 if (node == null || node instanceof IPluginExtension)
159                     break;
160                 node = node.getParentNode();
161             }
162             if (node != null) {
163                 IPluginExtension extension = (IPluginExtension) node;
164                 ISchema schema = (ISchema)extension.getSchema();
165                 if (schema != null) {
166                     elementInfo = schema.findElement(getName());
167                 }
168             }
169         }
170         return elementInfo;
171     }
172     
173     /* (non-Javadoc)
174      * @see org.eclipse.pde.internal.core.text.plugin.PluginObjectNode#reconnect(org.eclipse.pde.core.plugin.ISharedPluginModel, org.eclipse.pde.internal.core.ischema.ISchema, org.eclipse.pde.internal.core.text.IDocumentNode)
175      */

176     public void reconnect(ISharedPluginModel model, ISchema schema, IDocumentNode parent) {
177         super.reconnect(model, schema, parent);
178         // Transient Field: Element Info
179
// This may not be necessary. getElementInfo will retrieve the schema
180
// on demand if it is null
181
elementInfo = null;
182         if (schema != null) {
183             elementInfo = schema.findElement(getXMLTagName());
184         }
185     }
186     
187     /* (non-Javadoc)
188      * @see org.eclipse.pde.internal.core.text.plugin.PluginObjectNode#write(java.lang.String, java.io.PrintWriter)
189      */

190     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
191         // Used for text transfers for copy, cut, paste operations
192
writer.write(write(true));
193     }
194     
195 }
196
Popular Tags