KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > cheatsheet > comp > CompCSObject


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.pde.internal.core.cheatsheet.comp;
13
14 import java.io.IOException JavaDoc;
15 import java.io.PrintWriter JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.List JavaDoc;
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 JavaDoc;
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31 import org.w3c.dom.Text JavaDoc;
32
33 /**
34  * CompCSObject
35  *
36  */

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 JavaDoc DEFAULT_TAG_EXCEPTIONS = new HashSet JavaDoc(12);
45     
46     protected static final HashMap JavaDoc DEFAULT_SUBSTITUTE_CHARS = new HashMap JavaDoc(5);
47     
48     static {
49         DEFAULT_TAG_EXCEPTIONS.add("b"); //$NON-NLS-1$
50
DEFAULT_TAG_EXCEPTIONS.add("/b"); //$NON-NLS-1$
51
DEFAULT_TAG_EXCEPTIONS.add("br/"); //$NON-NLS-1$
52
DEFAULT_TAG_EXCEPTIONS.add("p"); //$NON-NLS-1$
53
DEFAULT_TAG_EXCEPTIONS.add("/p"); //$NON-NLS-1$
54
DEFAULT_TAG_EXCEPTIONS.add("li"); //$NON-NLS-1$
55
DEFAULT_TAG_EXCEPTIONS.add("/li"); //$NON-NLS-1$
56
DEFAULT_TAG_EXCEPTIONS.add("a"); //$NON-NLS-1$
57
DEFAULT_TAG_EXCEPTIONS.add("/a"); //$NON-NLS-1$
58
DEFAULT_TAG_EXCEPTIONS.add("span"); //$NON-NLS-1$
59
DEFAULT_TAG_EXCEPTIONS.add("/span"); //$NON-NLS-1$
60
DEFAULT_TAG_EXCEPTIONS.add("img"); //$NON-NLS-1$
61

62         DEFAULT_SUBSTITUTE_CHARS.put(new Character JavaDoc('&'), "&"); //$NON-NLS-1$
63
DEFAULT_SUBSTITUTE_CHARS.put(new Character JavaDoc('<'), "&lt;"); //$NON-NLS-1$
64
DEFAULT_SUBSTITUTE_CHARS.put(new Character JavaDoc('>'), "&gt;"); //$NON-NLS-1$
65
DEFAULT_SUBSTITUTE_CHARS.put(new Character JavaDoc('\''), "&apos;"); //$NON-NLS-1$
66
DEFAULT_SUBSTITUTE_CHARS.put(new Character JavaDoc('\"'), "&quot;"); //$NON-NLS-1$
67
}
68     
69     /**
70      * @param model
71      * @param parent
72      */

73     public CompCSObject(ICompCSModel model, ICompCSObject parent) {
74         fModel = model;
75         fParent = parent;
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getChildren()
80      */

81     public abstract List JavaDoc getChildren();
82
83     /* (non-Javadoc)
84      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getCompCS()
85      */

86     public ICompCS getCompCS() {
87         return fModel.getCompCS();
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getModel()
92      */

93     public ICompCSModel getModel() {
94         return fModel;
95     }
96
97     /* (non-Javadoc)
98      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getName()
99      */

100     public abstract String JavaDoc 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 JavaDoc 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 JavaDoc indent, PrintWriter JavaDoc writer) {
140
141         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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, buffer.toString(),
149                     indent, false);
150             // Print elements
151
writeElements(indent, writer);
152             // Print end element
153
XMLPrintHandler.printEndElement(writer, getElement(), indent);
154         } catch (IOException JavaDoc e) {
155             // Suppress
156
//e.printStackTrace();
157
}
158     }
159
160     /**
161      * @param property
162      * @param oldValue
163      * @param newValue
164      */

165     protected void firePropertyChanged(String JavaDoc property, Object JavaDoc oldValue,
166             Object JavaDoc newValue) {
167         firePropertyChanged(this, property, oldValue, newValue);
168     }
169         
170     /**
171      * @param object
172      * @param property
173      * @param oldValue
174      * @param newValue
175      */

176     private void firePropertyChanged(ICompCSObject object, String JavaDoc property,
177         Object JavaDoc oldValue, Object JavaDoc newValue) {
178         if (fModel.isEditable()) {
179             IModelChangeProvider provider = fModel;
180             provider.fireModelObjectChanged(object, property, oldValue, newValue);
181         }
182     }
183         
184     /**
185      * @param child
186      * @param changeType
187      */

188     protected void fireStructureChanged(ICompCSObject child, int changeType) {
189         fireStructureChanged(new ICompCSObject[] { child }, changeType);
190     }
191     
192     /**
193      * @param newValue
194      * @param oldValue
195      * @param changeType
196      */

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     /**
213      * @param children
214      * @param changeType
215      */

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     /**
226      * @return
227      */

228     protected boolean isEditable() {
229         return getModel().isEditable();
230     }
231     
232     /**
233      * @param element
234      */

235     protected abstract void parseAttributes(Element JavaDoc element);
236     
237     /**
238      * @param element
239      */

240     protected void parseContent(Element JavaDoc element) {
241         // Process children
242
NodeList JavaDoc children = element.getChildNodes();
243         for (int i = 0; i < children.getLength(); i++) {
244             Node JavaDoc child = children.item(i);
245             if (child.getNodeType() == Node.ELEMENT_NODE) {
246                 parseElement((Element JavaDoc)child);
247             } else if (child.getNodeType() == Node.TEXT_NODE) {
248                 parseText((Text JavaDoc)child);
249             }
250         }
251     }
252     
253     /**
254      * @param element
255      */

256     protected abstract void parseElement(Element JavaDoc element);
257
258     /**
259      * @param element
260      */

261     protected abstract void parseText(Text JavaDoc text);
262     
263     /**
264      * @param buffer
265      */

266     protected abstract void writeAttributes(StringBuffer JavaDoc buffer);
267     
268     /**
269      * Writes child elements or child content
270      * @param indent
271      * @param writer
272      */

273     protected abstract void writeElements(String JavaDoc indent, PrintWriter JavaDoc writer);
274     
275     /* (non-Javadoc)
276      * @see org.eclipse.pde.internal.core.icheatsheet.comp.ICompCSObject#getElement()
277      */

278     public abstract String JavaDoc getElement();
279 }
280
Popular Tags