1 11 package org.eclipse.pde.internal.core.text.plugin; 12 13 import java.io.PrintWriter ; 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 36 public IPluginElement createCopy() { 37 return null; 38 } 39 42 public IPluginAttribute getAttribute(String name) { 43 return (IPluginAttribute)fAttributes.get(name); 44 } 45 48 public IPluginAttribute[] getAttributes() { 49 return (IPluginAttribute[])fAttributes.values().toArray(new IPluginAttribute[fAttributes.size()]); 50 } 51 54 public int getAttributeCount() { 55 return fAttributes.size(); 56 } 57 60 public String getText() { 61 IDocumentTextNode node = getTextNode(); 62 return node == null ? "" : node.getText(); } 64 67 public void setAttribute(String name, String value) throws CoreException { 68 setXMLAttribute(name, value); 69 } 70 73 public void setText(String text) throws CoreException { 74 IDocumentTextNode node = getTextNode(); 75 String 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 88 public String write(boolean indent) { 89 String sep = getLineDelimiter(); 90 StringBuffer buffer = new StringBuffer (); 91 if (indent) 92 buffer.append(getIndent()); 93 94 IDocumentNode[] children = getChildNodes(); 95 String 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(" "); 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() + ">"); return buffer.toString(); 117 } 118 119 122 public String writeShallow(boolean terminate) { 123 String sep = getLineDelimiter(); 124 StringBuffer buffer = new StringBuffer ("<" + getXMLTagName()); 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()); } 131 if (terminate) 132 buffer.append("/"); buffer.append(">"); return buffer.toString(); 135 } 136 137 140 public String getName() { 141 return getXMLTagName(); 142 } 143 144 147 public void setName(String name) throws CoreException { 148 setXMLTagName(name); 149 } 150 151 154 public Object 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 176 public void reconnect(ISharedPluginModel model, ISchema schema, IDocumentNode parent) { 177 super.reconnect(model, schema, parent); 178 elementInfo = null; 182 if (schema != null) { 183 elementInfo = schema.findElement(getXMLTagName()); 184 } 185 } 186 187 190 public void write(String indent, PrintWriter writer) { 191 writer.write(write(true)); 193 } 194 195 } 196 | Popular Tags |