KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > dialogs > WelcomeParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.ui.internal.ide.dialogs;
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.ArrayList JavaDoc;
17
18 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
19 import javax.xml.parsers.ParserConfigurationException JavaDoc;
20 import javax.xml.parsers.SAXParser JavaDoc;
21 import javax.xml.parsers.SAXParserFactory JavaDoc;
22
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
26 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.ContentHandler JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.Locator JavaDoc;
31 import org.xml.sax.SAXException JavaDoc;
32 import org.xml.sax.helpers.DefaultHandler JavaDoc;
33
34 /**
35  * A parser for the the welcome page
36  */

37 public class WelcomeParser extends DefaultHandler JavaDoc {
38     private static final String JavaDoc TAG_WELCOME_PAGE = "welcomePage"; //$NON-NLS-1$
39

40     private static final String JavaDoc TAG_INTRO = "intro"; //$NON-NLS-1$
41

42     private static final String JavaDoc TAG_ITEM = "item"; //$NON-NLS-1$
43

44     private static final String JavaDoc TAG_BOLD = "b"; //$NON-NLS-1$
45

46     private static final String JavaDoc TAG_ACTION = "action"; //$NON-NLS-1$
47

48     private static final String JavaDoc TAG_PARAGRAPH = "p"; //$NON-NLS-1$
49

50     private static final String JavaDoc TAG_TOPIC = "topic"; //$NON-NLS-1$
51

52     private static final String JavaDoc ATT_TITLE = "title"; //$NON-NLS-1$
53

54     private static final String JavaDoc ATT_FORMAT = "format"; //$NON-NLS-1$
55

56     private static final String JavaDoc ATT_PLUGIN_ID = "pluginId"; //$NON-NLS-1$
57

58     private static final String JavaDoc ATT_CLASS = "class"; //$NON-NLS-1$
59

60     private static final String JavaDoc ATT_ID = "id"; //$NON-NLS-1$
61

62     private static final String JavaDoc ATT_HREF = "href"; //$NON-NLS-1$
63

64     private static final String JavaDoc FORMAT_WRAP = "wrap"; //$NON-NLS-1$
65

66     private static final char DELIMITER = '\n'; // sax parser replaces crlf with lf
67

68     private SAXParser JavaDoc parser;
69
70     private String JavaDoc title;
71
72     private WelcomeItem introItem;
73
74     private ArrayList JavaDoc items = new ArrayList JavaDoc();
75
76     private String JavaDoc format;
77
78     private class WelcomeContentHandler implements ContentHandler JavaDoc {
79         protected ContentHandler JavaDoc parent;
80
81         public void setParent(ContentHandler JavaDoc p) {
82             parent = p;
83         }
84
85         public void characters(char[] ch, int start, int length)
86                 throws SAXException JavaDoc {
87         }
88
89         public void endDocument() throws SAXException JavaDoc {
90         }
91
92         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
93                 String JavaDoc qName) throws SAXException JavaDoc {
94         }
95
96         public void endPrefixMapping(String JavaDoc prefix) throws SAXException JavaDoc {
97         }
98
99         public void ignorableWhitespace(char[] ch, int start, int length)
100                 throws SAXException JavaDoc {
101         }
102
103         public void processingInstruction(String JavaDoc target, String JavaDoc data)
104                 throws SAXException JavaDoc {
105         }
106
107         public void setDocumentLocator(Locator JavaDoc locator) {
108         }
109
110         public void skippedEntity(String JavaDoc name) throws SAXException JavaDoc {
111         }
112
113         public void startDocument() throws SAXException JavaDoc {
114         }
115
116         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
117                 String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
118         }
119
120         public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
121                 throws SAXException JavaDoc {
122         }
123     }
124
125     private class WelcomePageHandler extends WelcomeContentHandler {
126         public WelcomePageHandler(String JavaDoc newTitle) {
127             title = newTitle;
128         }
129
130         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
131                 String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
132             if (localName.equals(TAG_INTRO)) {
133                 ItemHandler h = new IntroItemHandler();
134                 h.setParent(WelcomePageHandler.this);
135                 parser.getXMLReader().setContentHandler(h);
136             } else if (localName.equals(TAG_ITEM)) {
137                 ItemHandler h = new ItemHandler();
138                 h.setParent(WelcomePageHandler.this);
139                 parser.getXMLReader().setContentHandler(h);
140             }
141         }
142     }
143
144     private class ItemHandler extends WelcomeContentHandler {
145         private ArrayList JavaDoc boldRanges = new ArrayList JavaDoc();
146
147         protected ArrayList JavaDoc wrapRanges = new ArrayList JavaDoc();
148
149         private ArrayList JavaDoc actionRanges = new ArrayList JavaDoc();
150
151         private ArrayList JavaDoc pluginIds = new ArrayList JavaDoc();
152
153         private ArrayList JavaDoc classes = new ArrayList JavaDoc();
154
155         private ArrayList JavaDoc helpRanges = new ArrayList JavaDoc();
156
157         private ArrayList JavaDoc helpIds = new ArrayList JavaDoc();
158
159         private ArrayList JavaDoc helpHrefs = new ArrayList JavaDoc();
160
161         private StringBuffer JavaDoc text = new StringBuffer JavaDoc();
162
163         protected int offset = 0;
164
165         protected int textStart;
166
167         protected int wrapStart;
168
169         private class BoldHandler extends WelcomeContentHandler {
170             public void characters(char[] ch, int start, int length)
171                     throws SAXException JavaDoc {
172                 ItemHandler.this.characters(ch, start, length);
173             }
174
175             public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
176                     String JavaDoc qName) throws SAXException JavaDoc {
177                 if (localName.equals(TAG_BOLD)) {
178                     boldRanges.add(new int[] { textStart, offset - textStart });
179                     parser.getXMLReader().setContentHandler(parent);
180                 }
181             }
182         }
183
184         private class ActionHandler extends WelcomeContentHandler {
185             public ActionHandler(String JavaDoc pluginId, String JavaDoc className) {
186                 pluginIds.add(pluginId);
187                 classes.add(className);
188             }
189
190             public void characters(char[] ch, int start, int length)
191                     throws SAXException JavaDoc {
192                 ItemHandler.this.characters(ch, start, length);
193             }
194
195             public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
196                     String JavaDoc qName) throws SAXException JavaDoc {
197                 if (localName.equals(TAG_ACTION)) {
198                     actionRanges
199                             .add(new int[] { textStart, offset - textStart });
200                     parser.getXMLReader().setContentHandler(parent);
201                 }
202             }
203         }
204
205         private class TopicHandler extends WelcomeContentHandler {
206             public TopicHandler(String JavaDoc helpId, String JavaDoc href) {
207                 helpIds.add(helpId);
208                 helpHrefs.add(href);
209             }
210
211             public void characters(char[] ch, int start, int length)
212                     throws SAXException JavaDoc {
213                 ItemHandler.this.characters(ch, start, length);
214             }
215
216             public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
217                     String JavaDoc qName) throws SAXException JavaDoc {
218                 if (localName.equals(TAG_TOPIC)) {
219                     helpRanges.add(new int[] { textStart, offset - textStart });
220                     parser.getXMLReader().setContentHandler(parent);
221                 }
222             }
223         }
224
225         protected WelcomeItem constructWelcomeItem() {
226             if (isFormatWrapped()) {
227                 // replace all line delimiters with a space
228
for (int i = 0; i < wrapRanges.size(); i++) {
229                     int[] range = (int[]) wrapRanges.get(i);
230                     int start = range[0];
231                     int length = range[1];
232                     for (int j = start; j < start + length; j++) {
233                         char ch = text.charAt(j);
234                         if (ch == DELIMITER) {
235                             text.replace(j, j + 1, " "); //$NON-NLS-1$
236
}
237                     }
238                 }
239             }
240             return new WelcomeItem(
241                     text.toString(),
242                     (int[][]) boldRanges.toArray(new int[boldRanges.size()][2]),
243                     (int[][]) actionRanges
244                             .toArray(new int[actionRanges.size()][2]),
245                     (String JavaDoc[]) pluginIds.toArray(new String JavaDoc[pluginIds.size()]),
246                     (String JavaDoc[]) classes.toArray(new String JavaDoc[classes.size()]),
247                     (int[][]) helpRanges.toArray(new int[helpRanges.size()][2]),
248                     (String JavaDoc[]) helpIds.toArray(new String JavaDoc[helpIds.size()]),
249                     (String JavaDoc[]) helpHrefs.toArray(new String JavaDoc[helpHrefs.size()]));
250         }
251
252         public void characters(char[] ch, int start, int length)
253                 throws SAXException JavaDoc {
254             for (int i = 0; i < length; i++) {
255                 text.append(ch[start + i]);
256             }
257             offset += length;
258         }
259
260         public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
261                 String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
262             textStart = offset;
263             if (localName.equals(TAG_BOLD)) {
264                 BoldHandler h = new BoldHandler();
265                 h.setParent(ItemHandler.this);
266                 parser.getXMLReader().setContentHandler(h);
267             } else if (localName.equals(TAG_ACTION)) {
268                 ActionHandler h = new ActionHandler(atts
269                         .getValue(ATT_PLUGIN_ID), atts.getValue(ATT_CLASS));
270                 h.setParent(ItemHandler.this);
271                 parser.getXMLReader().setContentHandler(h);
272             } else if (localName.equals(TAG_PARAGRAPH)) {
273                 wrapStart = textStart;
274             } else if (localName.equals(TAG_TOPIC)) {
275                 TopicHandler h = new TopicHandler(atts.getValue(ATT_ID), atts
276                         .getValue(ATT_HREF));
277                 h.setParent(ItemHandler.this);
278                 parser.getXMLReader().setContentHandler(h);
279             }
280         }
281
282         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
283                 String JavaDoc qName) throws SAXException JavaDoc {
284             if (localName.equals(TAG_ITEM)) {
285                 items.add(constructWelcomeItem());
286                 parser.getXMLReader().setContentHandler(parent);
287             } else if (localName.equals(TAG_PARAGRAPH)) {
288                 wrapRanges.add(new int[] { wrapStart, offset - wrapStart });
289             }
290         }
291     }
292
293     private class IntroItemHandler extends ItemHandler {
294         public void endElement(String JavaDoc namespaceURI, String JavaDoc localName,
295                 String JavaDoc qName) throws SAXException JavaDoc {
296             if (localName.equals(TAG_INTRO)) {
297                 introItem = constructWelcomeItem();
298                 parser.getXMLReader().setContentHandler(parent);
299             } else if (localName.equals(TAG_PARAGRAPH)) {
300                 wrapRanges.add(new int[] { wrapStart, offset - wrapStart });
301             }
302         }
303     }
304
305     /**
306      * Creates a new welcome parser.
307      */

308     public WelcomeParser() throws ParserConfigurationException JavaDoc, SAXException JavaDoc,
309             FactoryConfigurationError JavaDoc {
310         super();
311         SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
312         factory.setFeature("http://xml.org/sax/features/namespaces", true); //$NON-NLS-1$
313
parser = factory.newSAXParser();
314
315         parser.getXMLReader().setContentHandler(this);
316         parser.getXMLReader().setDTDHandler(this);
317         parser.getXMLReader().setEntityResolver(this);
318         parser.getXMLReader().setErrorHandler(this);
319     }
320
321     /**
322      * Returns the intro item.
323      */

324     public WelcomeItem getIntroItem() {
325         return introItem;
326     }
327
328     /**
329      * Returns the items.
330      */

331     public WelcomeItem[] getItems() {
332         return (WelcomeItem[]) items.toArray(new WelcomeItem[items.size()]);
333     }
334
335     /**
336      * Returns the title
337      */

338     public String JavaDoc getTitle() {
339         return title;
340     }
341
342     /**
343      * Returns whether or not the welcome editor input should be wrapped.
344      */

345     public boolean isFormatWrapped() {
346         return FORMAT_WRAP.equals(format);
347     }
348
349     /**
350      * Parse the contents of the input stream
351      */

352     public void parse(InputStream JavaDoc is) {
353         try {
354             parser.parse(new InputSource JavaDoc(is), this);
355         } catch (SAXException JavaDoc e) {
356             IStatus status = new Status(IStatus.ERROR,
357                     IDEWorkbenchPlugin.IDE_WORKBENCH, 1, IDEWorkbenchMessages.WelcomeParser_parseException, e);
358             IDEWorkbenchPlugin.log(IDEWorkbenchMessages.WelcomeParser_parseError, status);
359         } catch (IOException JavaDoc e) {
360             IStatus status = new Status(IStatus.ERROR,
361                     IDEWorkbenchPlugin.IDE_WORKBENCH, 1, IDEWorkbenchMessages.WelcomeParser_parseException, e);
362             IDEWorkbenchPlugin.log(IDEWorkbenchMessages.WelcomeParser_parseError, status);
363         }
364     }
365
366     /**
367      * Handles the start element
368      */

369     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName,
370             String JavaDoc qName, Attributes JavaDoc atts) throws SAXException JavaDoc {
371         if (localName.equals(TAG_WELCOME_PAGE)) {
372             WelcomeContentHandler h = new WelcomePageHandler(atts
373                     .getValue(ATT_TITLE));
374             format = atts.getValue(ATT_FORMAT);
375             h.setParent(this);
376             parser.getXMLReader().setContentHandler(h);
377         }
378     }
379 }
380
Popular Tags