1 11 package org.eclipse.pde.internal.core.plugin; 12 13 import java.io.PrintWriter ; 14 import java.util.Collection ; 15 import java.util.Enumeration ; 16 import java.util.Hashtable ; 17 import java.util.Iterator ; 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 ; 27 import org.w3c.dom.Element ; 28 import org.w3c.dom.NamedNodeMap ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 32 public class PluginElement extends PluginParent implements IPluginElement { 33 private static final long serialVersionUID = 1L; 34 35 static final String ATTRIBUTE_SHIFT = " "; 37 static final String ELEMENT_SHIFT = " "; 39 private transient ISchemaElement fElementInfo; 40 41 private String fText; 42 43 private Hashtable fAttributes = new Hashtable (); 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 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 name) { 88 return (IPluginAttribute) fAttributes.get(name); 89 } 90 91 public IPluginAttribute[] getAttributes() { 92 Collection 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 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 getText() { 125 return fText; 126 } 127 128 void load(Element element) { 129 fName = element.getTagName(); 130 NamedNodeMap attributes = element.getAttributes(); 131 for (int i = 0; i < attributes.getLength(); i++) { 132 PluginAttribute att = (PluginAttribute) getModel().getFactory() 133 .createAttribute(this); 134 Attr attr = (Attr )attributes.item(i); 135 att.fName = attr.getName(); 136 att.fValue = attr.getValue(); 137 fAttributes.put(att.getName(), att); 138 } 139 NodeList children = element.getChildNodes(); 140 for (int i = 0; i < children.getLength(); i++) { 141 Node 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 node) { 154 fName = node.getNodeName(); 155 NamedNodeMap attributes = node.getAttributes(); 156 for (int i = 0; i < attributes.getLength(); i++) { 157 Node 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 children = node.getChildNodes(); 165 for (int i = 0; i < children.getLength(); i++) { 166 Node 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 text = child.getNodeValue(); 177 text = text.trim(); 178 if (isNotEmpty(text)) 179 this.fText = text; 180 } 181 } 182 } 183 184 public void removeAttribute(String name) throws CoreException { 185 ensureModelEditable(); 186 PluginAttribute att = (PluginAttribute) fAttributes.remove(name); 187 String 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 name, String 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 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 newText) throws CoreException { 222 ensureModelEditable(); 223 String oldValue = fText; 224 fText = newText; 225 firePropertyChanged(P_TEXT, oldValue, fText); 226 227 } 228 229 public void write(String indent, PrintWriter writer) { 230 writer.print(indent); 231 writer.print("<" + getName()); String newIndent = indent + ATTRIBUTE_SHIFT; 233 if (fAttributes.isEmpty() == false) { 234 writer.println(); 235 for (Iterator 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(">"); 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() + ">"); } 255 } 256 | Popular Tags |