KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > plugin > PluginElement


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.plugin;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.Enumeration JavaDoc;
16 import java.util.Hashtable JavaDoc;
17 import java.util.Iterator JavaDoc;
18
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.pde.core.plugin.IPluginAttribute;
21 import org.eclipse.pde.core.plugin.IPluginElement;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginObject;
24 import org.eclipse.pde.internal.core.ischema.ISchema;
25 import org.eclipse.pde.internal.core.ischema.ISchemaElement;
26 import org.w3c.dom.Attr JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.NamedNodeMap JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 public class PluginElement extends PluginParent implements IPluginElement {
33     private static final long serialVersionUID = 1L;
34
35     static final String JavaDoc ATTRIBUTE_SHIFT = " "; //$NON-NLS-1$
36

37     static final String JavaDoc ELEMENT_SHIFT = " "; //$NON-NLS-1$
38

39     private transient ISchemaElement fElementInfo;
40
41     private String JavaDoc fText;
42
43     private Hashtable JavaDoc fAttributes = new Hashtable JavaDoc();
44
45     public PluginElement() {
46     }
47
48     PluginElement(PluginElement element) {
49         setModel(element.getModel());
50         setParent(element.getParent());
51         fName = element.getName();
52         IPluginAttribute[] atts = element.getAttributes();
53         for (int i = 0; i < atts.length; i++) {
54             PluginAttribute att = (PluginAttribute) atts[i];
55             fAttributes.put(att.getName(), att.clone());
56         }
57         fText = element.getText();
58         fElementInfo = (ISchemaElement) element.getElementInfo();
59     }
60
61     public boolean equals(Object JavaDoc obj) {
62         if (obj == this)
63             return true;
64         if (obj == null)
65             return false;
66         if (obj instanceof IPluginElement) {
67             IPluginElement target = (IPluginElement) obj;
68             if (target.getModel().equals(getModel()))
69                 return false;
70             if (target.getAttributeCount() != getAttributeCount())
71                 return false;
72             IPluginAttribute tatts[] = target.getAttributes();
73             for (int i = 0; i < tatts.length; i++) {
74                 IPluginAttribute tatt = tatts[i];
75                 if (tatt.equals(fAttributes.get(tatt.getName())) == false)
76                     return false;
77             }
78             return super.equals(obj);
79         }
80         return false;
81     }
82
83     public IPluginElement createCopy() {
84         return new PluginElement(this);
85     }
86
87     public IPluginAttribute getAttribute(String JavaDoc name) {
88         return (IPluginAttribute) fAttributes.get(name);
89     }
90
91     public IPluginAttribute[] getAttributes() {
92         Collection JavaDoc values = fAttributes.values();
93         IPluginAttribute[] result = new IPluginAttribute[values.size()];
94         return (IPluginAttribute[]) values.toArray(result);
95     }
96
97     public int getAttributeCount() {
98         return fAttributes.size();
99     }
100
101     public Object JavaDoc getElementInfo() {
102         if (fElementInfo != null) {
103             ISchema schema = fElementInfo.getSchema();
104             if (schema.isDisposed()) {
105                 fElementInfo = null;
106             }
107         }
108         if (fElementInfo == null) {
109             IPluginObject parent = getParent();
110             while (parent != null && !(parent instanceof IPluginExtension)) {
111                 parent = parent.getParent();
112             }
113             if (parent != null) {
114                 PluginExtension extension = (PluginExtension) parent;
115                 ISchema schema = (ISchema) extension.getSchema();
116                 if (schema != null) {
117                     fElementInfo = schema.findElement(getName());
118                 }
119             }
120         }
121         return fElementInfo;
122     }
123
124     public String JavaDoc getText() {
125         return fText;
126     }
127
128     void load(Element element) {
129         fName = element.getTagName();
130         NamedNodeMap JavaDoc attributes = element.getAttributes();
131         for (int i = 0; i < attributes.getLength(); i++) {
132             PluginAttribute att = (PluginAttribute) getModel().getFactory()
133                     .createAttribute(this);
134             Attr JavaDoc attr = (Attr JavaDoc)attributes.item(i);
135             att.fName = attr.getName();
136             att.fValue = attr.getValue();
137             fAttributes.put(att.getName(), att);
138         }
139         NodeList JavaDoc children = element.getChildNodes();
140         for (int i = 0; i < children.getLength(); i++) {
141             Node JavaDoc child = children.item(i);
142             if (child.getNodeType() == Node.ELEMENT_NODE) {
143                 PluginElement childElement = new PluginElement();
144                 childElement.setModel(getModel());
145                 childElement.setInTheModel(true);
146                 childElement.setParent(this);
147                 this.fChildren.add(childElement);
148                 childElement.load((Element)child);
149             }
150         }
151     }
152
153     void load(Node JavaDoc node) {
154         fName = node.getNodeName();
155         NamedNodeMap JavaDoc attributes = node.getAttributes();
156         for (int i = 0; i < attributes.getLength(); i++) {
157             Node JavaDoc attribute = attributes.item(i);
158             IPluginAttribute att = getModel().getFactory()
159                     .createAttribute(this);
160             ((PluginAttribute) att).load(attribute);
161             ((PluginAttribute) att).setInTheModel(true);
162             this.fAttributes.put(attribute.getNodeName(), att);
163         }
164         NodeList JavaDoc children = node.getChildNodes();
165         for (int i = 0; i < children.getLength(); i++) {
166             Node JavaDoc child = children.item(i);
167             if (child.getNodeType() == Node.ELEMENT_NODE) {
168                 PluginElement childElement = new PluginElement();
169                 childElement.setModel(getModel());
170                 childElement.setInTheModel(true);
171                 this.fChildren.add(childElement);
172                 childElement.setParent(this);
173                 childElement.load(child);
174             } else if (child.getNodeType() == Node.TEXT_NODE
175                     && child.getNodeValue() != null) {
176                 String JavaDoc text = child.getNodeValue();
177                 text = text.trim();
178                 if (isNotEmpty(text))
179                     this.fText = text;
180             }
181         }
182     }
183
184     public void removeAttribute(String JavaDoc name) throws CoreException {
185         ensureModelEditable();
186         PluginAttribute att = (PluginAttribute) fAttributes.remove(name);
187         String JavaDoc oldValue = att.getValue();
188         if (att != null) {
189             att.setInTheModel(false);
190         }
191         firePropertyChanged(P_ATTRIBUTE, oldValue, null);
192     }
193
194     public void setAttribute(String JavaDoc name, String JavaDoc value) throws CoreException {
195         ensureModelEditable();
196         if (value == null) {
197             removeAttribute(name);
198             return;
199         }
200         IPluginAttribute attribute = getAttribute(name);
201         if (attribute == null) {
202             attribute = getModel().getFactory().createAttribute(this);
203             attribute.setName(name);
204             fAttributes.put(name, attribute);
205             ((PluginAttribute) attribute).setInTheModel(true);
206         }
207         attribute.setValue(value);
208     }
209
210     public void setElementInfo(ISchemaElement newElementInfo) {
211         fElementInfo = newElementInfo;
212         if (fElementInfo == null) {
213             for (Enumeration JavaDoc atts = fAttributes.elements(); atts
214                     .hasMoreElements();) {
215                 PluginAttribute att = (PluginAttribute) atts.nextElement();
216                 att.setAttributeInfo(null);
217             }
218         }
219     }
220
221     public void setText(String JavaDoc newText) throws CoreException {
222         ensureModelEditable();
223         String JavaDoc oldValue = fText;
224         fText = newText;
225         firePropertyChanged(P_TEXT, oldValue, fText);
226
227     }
228
229     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
230         writer.print(indent);
231         writer.print("<" + getName()); //$NON-NLS-1$
232
String JavaDoc newIndent = indent + ATTRIBUTE_SHIFT;
233         if (fAttributes.isEmpty() == false) {
234             writer.println();
235             for (Iterator JavaDoc iter = fAttributes.values().iterator(); iter
236                     .hasNext();) {
237                 IPluginAttribute attribute = (IPluginAttribute) iter.next();
238                 attribute.write(newIndent, writer);
239                 if (iter.hasNext())
240                     writer.println();
241             }
242         }
243         writer.println(">"); //$NON-NLS-1$
244
newIndent = indent + ELEMENT_SHIFT;
245         IPluginObject[] children = getChildren();
246         for (int i = 0; i < children.length; i++) {
247             IPluginElement element = (IPluginElement) children[i];
248             element.write(newIndent, writer);
249         }
250         if (getText() != null) {
251             writer.println(newIndent + getWritableString(getText()));
252         }
253         writer.println(indent + "</" + getName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
254
}
255 }
256
Popular Tags