001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.plugin;
011:
012: import java.io.PrintWriter;
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.Enumeration;
016: import java.util.Hashtable;
017: import java.util.Iterator;
018:
019: import org.eclipse.core.runtime.CoreException;
020: import org.eclipse.core.runtime.IConfigurationElement;
021: import org.eclipse.pde.core.plugin.IPluginAttribute;
022: import org.eclipse.pde.core.plugin.IPluginElement;
023: import org.eclipse.pde.core.plugin.IPluginExtension;
024: import org.eclipse.pde.core.plugin.IPluginObject;
025: import org.eclipse.pde.internal.core.ischema.ISchema;
026: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
027:
028: public class PluginElement extends PluginParent implements
029: IPluginElement {
030: private static final long serialVersionUID = 1L;
031:
032: static final String ATTRIBUTE_SHIFT = " "; //$NON-NLS-1$
033:
034: static final String ELEMENT_SHIFT = " "; //$NON-NLS-1$
035:
036: private transient ISchemaElement fElementInfo;
037:
038: protected String fText;
039:
040: protected Hashtable fAttributes;
041:
042: private IConfigurationElement fElement = null;
043:
044: public PluginElement() {
045: }
046:
047: public PluginElement(IConfigurationElement element) {
048: fElement = element;
049: }
050:
051: PluginElement(PluginElement element) {
052: setModel(element.getModel());
053: setParent(element.getParent());
054: fName = element.getName();
055: IPluginAttribute[] atts = element.getAttributes();
056: for (int i = 0; i < atts.length; i++) {
057: PluginAttribute att = (PluginAttribute) atts[i];
058: getAttributeMap().put(att.getName(), att.clone());
059: }
060: fText = element.getText();
061: fElementInfo = (ISchemaElement) element.getElementInfo();
062: fElement = element.fElement;
063: }
064:
065: public boolean equals(Object obj) {
066: if (obj == this )
067: return true;
068: if (obj == null)
069: return false;
070: if (obj instanceof IPluginElement) {
071: IPluginElement target = (IPluginElement) obj;
072: if (target.getModel().equals(getModel()))
073: return false;
074: if (target.getAttributeCount() != getAttributeCount())
075: return false;
076: IPluginAttribute tatts[] = target.getAttributes();
077: for (int i = 0; i < tatts.length; i++) {
078: IPluginAttribute tatt = tatts[i];
079: IPluginAttribute att = (IPluginAttribute) getAttributeMap()
080: .get(tatt.getName());
081: if (att == null || att.equals(tatt) == false)
082: return false;
083: }
084: return super .equals(obj);
085: }
086: return false;
087: }
088:
089: public IPluginElement createCopy() {
090: return new PluginElement(this );
091: }
092:
093: public IPluginAttribute getAttribute(String name) {
094: return (IPluginAttribute) getAttributeMap().get(name);
095: }
096:
097: public IPluginAttribute[] getAttributes() {
098: Collection values = getAttributeMap().values();
099: IPluginAttribute[] result = new IPluginAttribute[values.size()];
100: return (IPluginAttribute[]) values.toArray(result);
101: }
102:
103: public int getAttributeCount() {
104: // if attributes are initialized, don't load the entire map to find the # of elements
105: if (fAttributes == null && fElement != null)
106: return fElement.getAttributeNames().length;
107: return getAttributeMap().size();
108: }
109:
110: public Object getElementInfo() {
111: if (fElementInfo != null) {
112: ISchema schema = fElementInfo.getSchema();
113: if (schema.isDisposed()) {
114: fElementInfo = null;
115: }
116: }
117: if (fElementInfo == null) {
118: IPluginObject parent = getParent();
119: while (parent != null
120: && !(parent instanceof IPluginExtension)) {
121: parent = parent.getParent();
122: }
123: if (parent != null) {
124: PluginExtension extension = (PluginExtension) parent;
125: ISchema schema = (ISchema) extension.getSchema();
126: if (schema != null) {
127: fElementInfo = schema.findElement(getName());
128: }
129: }
130: }
131: return fElementInfo;
132: }
133:
134: public String getText() {
135: if (fText == null && fElement != null)
136: fText = fElement.getValue();
137: return fText;
138: }
139:
140: public void removeAttribute(String name) throws CoreException {
141: ensureModelEditable();
142: PluginAttribute att = (PluginAttribute) getAttributeMap()
143: .remove(name);
144: String oldValue = att.getValue();
145: if (att != null) {
146: att.setInTheModel(false);
147: }
148: firePropertyChanged(P_ATTRIBUTE, oldValue, null);
149: }
150:
151: public void setAttribute(String name, String value)
152: throws CoreException {
153: ensureModelEditable();
154: if (value == null) {
155: removeAttribute(name);
156: return;
157: }
158: IPluginAttribute attribute = getAttribute(name);
159: if (attribute == null) {
160: attribute = getModel().getFactory().createAttribute(this );
161: attribute.setName(name);
162: getAttributeMap().put(name, attribute);
163: ((PluginAttribute) attribute).setInTheModel(true);
164: }
165: attribute.setValue(value);
166: }
167:
168: public void setElementInfo(ISchemaElement newElementInfo) {
169: fElementInfo = newElementInfo;
170: if (fElementInfo == null) {
171: for (Enumeration atts = getAttributeMap().elements(); atts
172: .hasMoreElements();) {
173: PluginAttribute att = (PluginAttribute) atts
174: .nextElement();
175: att.setAttributeInfo(null);
176: }
177: }
178: }
179:
180: public void setText(String newText) throws CoreException {
181: ensureModelEditable();
182: String oldValue = fText;
183: fText = newText;
184: firePropertyChanged(P_TEXT, oldValue, fText);
185:
186: }
187:
188: public void write(String indent, PrintWriter writer) {
189: writer.print(indent);
190: writer.print("<" + getName()); //$NON-NLS-1$
191: String newIndent = indent + ATTRIBUTE_SHIFT;
192: if (getAttributeMap().isEmpty() == false) {
193: writer.println();
194: for (Iterator iter = getAttributeMap().values().iterator(); iter
195: .hasNext();) {
196: IPluginAttribute attribute = (IPluginAttribute) iter
197: .next();
198: attribute.write(newIndent, writer);
199: if (iter.hasNext())
200: writer.println();
201: }
202: }
203: writer.println(">"); //$NON-NLS-1$
204: newIndent = indent + ELEMENT_SHIFT;
205: IPluginObject[] children = getChildren();
206: for (int i = 0; i < children.length; i++) {
207: IPluginElement element = (IPluginElement) children[i];
208: element.write(newIndent, writer);
209: }
210: if (getText() != null) {
211: writer.println(newIndent + getWritableString(getText()));
212: }
213: writer.println(indent + "</" + getName() + ">"); //$NON-NLS-1$ //$NON-NLS-2$
214: }
215:
216: protected Hashtable getAttributeMap() {
217: if (fAttributes == null) {
218: fAttributes = new Hashtable();
219: if (fElement != null) {
220: String[] names = fElement.getAttributeNames();
221: for (int i = 0; i < names.length; i++) {
222: IPluginAttribute attr = createAttribute(names[i],
223: fElement.getAttribute(names[i]));
224: if (attr != null)
225: fAttributes.put(names[i], attr);
226: }
227: }
228: }
229: return fAttributes;
230: }
231:
232: private IPluginAttribute createAttribute(String name, String value) {
233: if (name == null || value == null)
234: return null;
235: try {
236: IPluginAttribute attr = getPluginModel().getFactory()
237: .createAttribute(this );
238: if (attr instanceof PluginAttribute)
239: ((PluginAttribute) attr).load(name, value);
240: else {
241: attr.setName(name);
242: attr.setValue(value);
243: }
244: return attr;
245: } catch (CoreException e) {
246: }
247: return null;
248: }
249:
250: protected ArrayList getChildrenList() {
251: if (fChildren == null) {
252: fChildren = new ArrayList();
253: if (fElement != null) {
254: IConfigurationElement[] elements = fElement
255: .getChildren();
256: for (int i = 0; i < elements.length; i++) {
257: PluginElement element = new PluginElement(
258: elements[i]);
259: element.setModel(getModel());
260: element.setParent(this );
261: fChildren.add(element);
262: }
263: }
264: }
265: return fChildren;
266: }
267:
268: public String getName() {
269: if (fName == null && fElement != null) {
270: fName = fElement.getName();
271: }
272: return fName;
273: }
274: }
|