001: /*******************************************************************************
002: * Copyright (c) 2006, 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.cheatsheet.comp;
011:
012: import java.io.IOException;
013: import java.io.PrintWriter;
014: import java.util.HashMap;
015: import java.util.HashSet;
016: import java.util.List;
017:
018: import org.eclipse.core.runtime.PlatformObject;
019: import org.eclipse.pde.core.IModelChangeProvider;
020: import org.eclipse.pde.core.IModelChangedEvent;
021: import org.eclipse.pde.core.ModelChangedEvent;
022: import org.eclipse.pde.internal.core.XMLPrintHandler;
023: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCS;
024: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel;
025: import org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.Node;
028: import org.w3c.dom.NodeList;
029: import org.w3c.dom.Text;
030:
031: /**
032: * CompCSObject
033: *
034: */
035: public abstract class CompCSObject extends PlatformObject implements
036: ICompCSObject {
037:
038: private transient ICompCSModel fModel;
039:
040: private transient ICompCSObject fParent;
041:
042: protected static final HashSet DEFAULT_TAG_EXCEPTIONS = new HashSet(
043: 12);
044:
045: protected static final HashMap DEFAULT_SUBSTITUTE_CHARS = new HashMap(
046: 5);
047:
048: static {
049: DEFAULT_TAG_EXCEPTIONS.add("b"); //$NON-NLS-1$
050: DEFAULT_TAG_EXCEPTIONS.add("/b"); //$NON-NLS-1$
051: DEFAULT_TAG_EXCEPTIONS.add("br/"); //$NON-NLS-1$
052: DEFAULT_TAG_EXCEPTIONS.add("p"); //$NON-NLS-1$
053: DEFAULT_TAG_EXCEPTIONS.add("/p"); //$NON-NLS-1$
054: DEFAULT_TAG_EXCEPTIONS.add("li"); //$NON-NLS-1$
055: DEFAULT_TAG_EXCEPTIONS.add("/li"); //$NON-NLS-1$
056: DEFAULT_TAG_EXCEPTIONS.add("a"); //$NON-NLS-1$
057: DEFAULT_TAG_EXCEPTIONS.add("/a"); //$NON-NLS-1$
058: DEFAULT_TAG_EXCEPTIONS.add("span"); //$NON-NLS-1$
059: DEFAULT_TAG_EXCEPTIONS.add("/span"); //$NON-NLS-1$
060: DEFAULT_TAG_EXCEPTIONS.add("img"); //$NON-NLS-1$
061:
062: DEFAULT_SUBSTITUTE_CHARS.put(new Character('&'), "&"); //$NON-NLS-1$
063: DEFAULT_SUBSTITUTE_CHARS.put(new Character('<'), "<"); //$NON-NLS-1$
064: DEFAULT_SUBSTITUTE_CHARS.put(new Character('>'), ">"); //$NON-NLS-1$
065: DEFAULT_SUBSTITUTE_CHARS.put(new Character('\''), "'"); //$NON-NLS-1$
066: DEFAULT_SUBSTITUTE_CHARS.put(new Character('\"'), """); //$NON-NLS-1$
067: }
068:
069: /**
070: * @param model
071: * @param parent
072: */
073: public CompCSObject(ICompCSModel model, ICompCSObject parent) {
074: fModel = model;
075: fParent = parent;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getChildren()
080: */
081: public abstract List getChildren();
082:
083: /* (non-Javadoc)
084: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getCompCS()
085: */
086: public ICompCS getCompCS() {
087: return fModel.getCompCS();
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getModel()
092: */
093: public ICompCSModel getModel() {
094: return fModel;
095: }
096:
097: /* (non-Javadoc)
098: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getName()
099: */
100: public abstract String getName();
101:
102: /* (non-Javadoc)
103: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getParent()
104: */
105: public ICompCSObject getParent() {
106: return fParent;
107: }
108:
109: /* (non-Javadoc)
110: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getType()
111: */
112: public abstract int getType();
113:
114: /* (non-Javadoc)
115: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#parse(org.w3c.dom.Element)
116: */
117: public void parse(Element element) {
118: if (element.getNodeName().equals(getElement())) {
119: parseAttributes(element);
120: parseContent(element);
121: }
122: }
123:
124: /* (non-Javadoc)
125: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#reset()
126: */
127: public abstract void reset();
128:
129: /* (non-Javadoc)
130: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#setModel(org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSModel)
131: */
132: public void setModel(ICompCSModel model) {
133: fModel = model;
134: }
135:
136: /* (non-Javadoc)
137: * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
138: */
139: public void write(String indent, PrintWriter writer) {
140:
141: StringBuffer buffer = new StringBuffer();
142: try {
143: // Assemble start element
144: buffer.append(getElement());
145: // Assemble attributes
146: writeAttributes(buffer);
147: // Print start element and attributes
148: XMLPrintHandler.printBeginElement(writer,
149: buffer.toString(), indent, false);
150: // Print elements
151: writeElements(indent, writer);
152: // Print end element
153: XMLPrintHandler.printEndElement(writer, getElement(),
154: indent);
155: } catch (IOException e) {
156: // Suppress
157: //e.printStackTrace();
158: }
159: }
160:
161: /**
162: * @param property
163: * @param oldValue
164: * @param newValue
165: */
166: protected void firePropertyChanged(String property,
167: Object oldValue, Object newValue) {
168: firePropertyChanged(this , property, oldValue, newValue);
169: }
170:
171: /**
172: * @param object
173: * @param property
174: * @param oldValue
175: * @param newValue
176: */
177: private void firePropertyChanged(ICompCSObject object,
178: String property, Object oldValue, Object newValue) {
179: if (fModel.isEditable()) {
180: IModelChangeProvider provider = fModel;
181: provider.fireModelObjectChanged(object, property, oldValue,
182: newValue);
183: }
184: }
185:
186: /**
187: * @param child
188: * @param changeType
189: */
190: protected void fireStructureChanged(ICompCSObject child,
191: int changeType) {
192: fireStructureChanged(new ICompCSObject[] { child }, changeType);
193: }
194:
195: /**
196: * @param newValue
197: * @param oldValue
198: * @param changeType
199: */
200: protected void fireStructureChanged(ICompCSObject newValue,
201: ICompCSObject oldValue) {
202:
203: int changeType = -1;
204: ICompCSObject object = null;
205: if (newValue == null) {
206: changeType = IModelChangedEvent.REMOVE;
207: object = oldValue;
208: } else {
209: changeType = IModelChangedEvent.INSERT;
210: object = newValue;
211: }
212: fireStructureChanged(object, changeType);
213: }
214:
215: /**
216: * @param children
217: * @param changeType
218: */
219: private void fireStructureChanged(ICompCSObject[] children,
220: int changeType) {
221: if (fModel.isEditable()) {
222: IModelChangeProvider provider = fModel;
223: provider.fireModelChanged(new ModelChangedEvent(provider,
224: changeType, children, null));
225: }
226: }
227:
228: /**
229: * @return
230: */
231: protected boolean isEditable() {
232: return getModel().isEditable();
233: }
234:
235: /**
236: * @param element
237: */
238: protected abstract void parseAttributes(Element element);
239:
240: /**
241: * @param element
242: */
243: protected void parseContent(Element element) {
244: // Process children
245: NodeList children = element.getChildNodes();
246: for (int i = 0; i < children.getLength(); i++) {
247: Node child = children.item(i);
248: if (child.getNodeType() == Node.ELEMENT_NODE) {
249: parseElement((Element) child);
250: } else if (child.getNodeType() == Node.TEXT_NODE) {
251: parseText((Text) child);
252: }
253: }
254: }
255:
256: /**
257: * @param element
258: */
259: protected abstract void parseElement(Element element);
260:
261: /**
262: * @param element
263: */
264: protected abstract void parseText(Text text);
265:
266: /**
267: * @param buffer
268: */
269: protected abstract void writeAttributes(StringBuffer buffer);
270:
271: /**
272: * Writes child elements or child content
273: * @param indent
274: * @param writer
275: */
276: protected abstract void writeElements(String indent,
277: PrintWriter writer);
278:
279: /* (non-Javadoc)
280: * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getElement()
281: */
282: public abstract String getElement();
283: }
|