1 11 12 package org.eclipse.pde.internal.core.cheatsheet.comp; 13 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 20 import org.eclipse.core.runtime.PlatformObject; 21 import org.eclipse.pde.core.IModelChangeProvider; 22 import org.eclipse.pde.core.IModelChangedEvent; 23 import org.eclipse.pde.core.ModelChangedEvent; 24 import org.eclipse.pde.internal.core.XMLPrintHandler; 25 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS; 26 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel; 27 import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.Node ; 30 import org.w3c.dom.NodeList ; 31 import org.w3c.dom.Text ; 32 33 37 public abstract class CompCSObject extends PlatformObject implements 38 ICompCSObject { 39 40 private transient ICompCSModel fModel; 41 42 private transient ICompCSObject fParent; 43 44 protected static final HashSet DEFAULT_TAG_EXCEPTIONS = new HashSet (12); 45 46 protected static final HashMap DEFAULT_SUBSTITUTE_CHARS = new HashMap (5); 47 48 static { 49 DEFAULT_TAG_EXCEPTIONS.add("b"); DEFAULT_TAG_EXCEPTIONS.add("/b"); DEFAULT_TAG_EXCEPTIONS.add("br/"); DEFAULT_TAG_EXCEPTIONS.add("p"); DEFAULT_TAG_EXCEPTIONS.add("/p"); DEFAULT_TAG_EXCEPTIONS.add("li"); DEFAULT_TAG_EXCEPTIONS.add("/li"); DEFAULT_TAG_EXCEPTIONS.add("a"); DEFAULT_TAG_EXCEPTIONS.add("/a"); DEFAULT_TAG_EXCEPTIONS.add("span"); DEFAULT_TAG_EXCEPTIONS.add("/span"); DEFAULT_TAG_EXCEPTIONS.add("img"); 62 DEFAULT_SUBSTITUTE_CHARS.put(new Character ('&'), "&"); DEFAULT_SUBSTITUTE_CHARS.put(new Character ('<'), "<"); DEFAULT_SUBSTITUTE_CHARS.put(new Character ('>'), ">"); DEFAULT_SUBSTITUTE_CHARS.put(new Character ('\''), "'"); DEFAULT_SUBSTITUTE_CHARS.put(new Character ('\"'), """); } 68 69 73 public CompCSObject(ICompCSModel model, ICompCSObject parent) { 74 fModel = model; 75 fParent = parent; 76 } 77 78 81 public abstract List getChildren(); 82 83 86 public ICompCS getCompCS() { 87 return fModel.getCompCS(); 88 } 89 90 93 public ICompCSModel getModel() { 94 return fModel; 95 } 96 97 100 public abstract String getName(); 101 102 105 public ICompCSObject getParent() { 106 return fParent; 107 } 108 109 112 public abstract int getType(); 113 114 117 public void parse(Element element) { 118 if (element.getNodeName().equals(getElement())) { 119 parseAttributes(element); 120 parseContent(element); 121 } 122 } 123 124 127 public abstract void reset(); 128 129 132 public void setModel(ICompCSModel model) { 133 fModel = model; 134 } 135 136 139 public void write(String indent, PrintWriter writer) { 140 141 StringBuffer buffer = new StringBuffer (); 142 try { 143 buffer.append(getElement()); 145 writeAttributes(buffer); 147 XMLPrintHandler.printBeginElement(writer, buffer.toString(), 149 indent, false); 150 writeElements(indent, writer); 152 XMLPrintHandler.printEndElement(writer, getElement(), indent); 154 } catch (IOException e) { 155 } 158 } 159 160 165 protected void firePropertyChanged(String property, Object oldValue, 166 Object newValue) { 167 firePropertyChanged(this, property, oldValue, newValue); 168 } 169 170 176 private void firePropertyChanged(ICompCSObject object, String property, 177 Object oldValue, Object newValue) { 178 if (fModel.isEditable()) { 179 IModelChangeProvider provider = fModel; 180 provider.fireModelObjectChanged(object, property, oldValue, newValue); 181 } 182 } 183 184 188 protected void fireStructureChanged(ICompCSObject child, int changeType) { 189 fireStructureChanged(new ICompCSObject[] { child }, changeType); 190 } 191 192 197 protected void fireStructureChanged(ICompCSObject newValue, 198 ICompCSObject oldValue) { 199 200 int changeType = -1; 201 ICompCSObject object = null; 202 if (newValue == null) { 203 changeType = IModelChangedEvent.REMOVE; 204 object = oldValue; 205 } else { 206 changeType = IModelChangedEvent.INSERT; 207 object = newValue; 208 } 209 fireStructureChanged(object, changeType); 210 } 211 212 216 private void fireStructureChanged(ICompCSObject[] children, 217 int changeType) { 218 if (fModel.isEditable()) { 219 IModelChangeProvider provider = fModel; 220 provider.fireModelChanged(new ModelChangedEvent(provider, 221 changeType, children, null)); 222 } 223 } 224 225 228 protected boolean isEditable() { 229 return getModel().isEditable(); 230 } 231 232 235 protected abstract void parseAttributes(Element element); 236 237 240 protected void parseContent(Element element) { 241 NodeList children = element.getChildNodes(); 243 for (int i = 0; i < children.getLength(); i++) { 244 Node child = children.item(i); 245 if (child.getNodeType() == Node.ELEMENT_NODE) { 246 parseElement((Element )child); 247 } else if (child.getNodeType() == Node.TEXT_NODE) { 248 parseText((Text )child); 249 } 250 } 251 } 252 253 256 protected abstract void parseElement(Element element); 257 258 261 protected abstract void parseText(Text text); 262 263 266 protected abstract void writeAttributes(StringBuffer buffer); 267 268 273 protected abstract void writeElements(String indent, PrintWriter writer); 274 275 278 public abstract String getElement(); 279 } 280 | Popular Tags |