1 11 12 package org.eclipse.ui.internal.ide.dialogs; 13 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.ArrayList ; 17 18 import javax.xml.parsers.FactoryConfigurationError ; 19 import javax.xml.parsers.ParserConfigurationException ; 20 import javax.xml.parsers.SAXParser ; 21 import javax.xml.parsers.SAXParserFactory ; 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 ; 28 import org.xml.sax.ContentHandler ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.Locator ; 31 import org.xml.sax.SAXException ; 32 import org.xml.sax.helpers.DefaultHandler ; 33 34 37 public class WelcomeParser extends DefaultHandler { 38 private static final String TAG_WELCOME_PAGE = "welcomePage"; 40 private static final String TAG_INTRO = "intro"; 42 private static final String TAG_ITEM = "item"; 44 private static final String TAG_BOLD = "b"; 46 private static final String TAG_ACTION = "action"; 48 private static final String TAG_PARAGRAPH = "p"; 50 private static final String TAG_TOPIC = "topic"; 52 private static final String ATT_TITLE = "title"; 54 private static final String ATT_FORMAT = "format"; 56 private static final String ATT_PLUGIN_ID = "pluginId"; 58 private static final String ATT_CLASS = "class"; 60 private static final String ATT_ID = "id"; 62 private static final String ATT_HREF = "href"; 64 private static final String FORMAT_WRAP = "wrap"; 66 private static final char DELIMITER = '\n'; 68 private SAXParser parser; 69 70 private String title; 71 72 private WelcomeItem introItem; 73 74 private ArrayList items = new ArrayList (); 75 76 private String format; 77 78 private class WelcomeContentHandler implements ContentHandler { 79 protected ContentHandler parent; 80 81 public void setParent(ContentHandler p) { 82 parent = p; 83 } 84 85 public void characters(char[] ch, int start, int length) 86 throws SAXException { 87 } 88 89 public void endDocument() throws SAXException { 90 } 91 92 public void endElement(String namespaceURI, String localName, 93 String qName) throws SAXException { 94 } 95 96 public void endPrefixMapping(String prefix) throws SAXException { 97 } 98 99 public void ignorableWhitespace(char[] ch, int start, int length) 100 throws SAXException { 101 } 102 103 public void processingInstruction(String target, String data) 104 throws SAXException { 105 } 106 107 public void setDocumentLocator(Locator locator) { 108 } 109 110 public void skippedEntity(String name) throws SAXException { 111 } 112 113 public void startDocument() throws SAXException { 114 } 115 116 public void startElement(String namespaceURI, String localName, 117 String qName, Attributes atts) throws SAXException { 118 } 119 120 public void startPrefixMapping(String prefix, String uri) 121 throws SAXException { 122 } 123 } 124 125 private class WelcomePageHandler extends WelcomeContentHandler { 126 public WelcomePageHandler(String newTitle) { 127 title = newTitle; 128 } 129 130 public void startElement(String namespaceURI, String localName, 131 String qName, Attributes atts) throws SAXException { 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 boldRanges = new ArrayList (); 146 147 protected ArrayList wrapRanges = new ArrayList (); 148 149 private ArrayList actionRanges = new ArrayList (); 150 151 private ArrayList pluginIds = new ArrayList (); 152 153 private ArrayList classes = new ArrayList (); 154 155 private ArrayList helpRanges = new ArrayList (); 156 157 private ArrayList helpIds = new ArrayList (); 158 159 private ArrayList helpHrefs = new ArrayList (); 160 161 private StringBuffer text = new StringBuffer (); 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 { 172 ItemHandler.this.characters(ch, start, length); 173 } 174 175 public void endElement(String namespaceURI, String localName, 176 String qName) throws SAXException { 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 pluginId, String className) { 186 pluginIds.add(pluginId); 187 classes.add(className); 188 } 189 190 public void characters(char[] ch, int start, int length) 191 throws SAXException { 192 ItemHandler.this.characters(ch, start, length); 193 } 194 195 public void endElement(String namespaceURI, String localName, 196 String qName) throws SAXException { 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 helpId, String href) { 207 helpIds.add(helpId); 208 helpHrefs.add(href); 209 } 210 211 public void characters(char[] ch, int start, int length) 212 throws SAXException { 213 ItemHandler.this.characters(ch, start, length); 214 } 215 216 public void endElement(String namespaceURI, String localName, 217 String qName) throws SAXException { 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 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, " "); } 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 []) pluginIds.toArray(new String [pluginIds.size()]), 246 (String []) classes.toArray(new String [classes.size()]), 247 (int[][]) helpRanges.toArray(new int[helpRanges.size()][2]), 248 (String []) helpIds.toArray(new String [helpIds.size()]), 249 (String []) helpHrefs.toArray(new String [helpHrefs.size()])); 250 } 251 252 public void characters(char[] ch, int start, int length) 253 throws SAXException { 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 namespaceURI, String localName, 261 String qName, Attributes atts) throws SAXException { 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 namespaceURI, String localName, 283 String qName) throws SAXException { 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 namespaceURI, String localName, 295 String qName) throws SAXException { 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 308 public WelcomeParser() throws ParserConfigurationException , SAXException , 309 FactoryConfigurationError { 310 super(); 311 SAXParserFactory factory = SAXParserFactory.newInstance(); 312 factory.setFeature("http://xml.org/sax/features/namespaces", true); 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 324 public WelcomeItem getIntroItem() { 325 return introItem; 326 } 327 328 331 public WelcomeItem[] getItems() { 332 return (WelcomeItem[]) items.toArray(new WelcomeItem[items.size()]); 333 } 334 335 338 public String getTitle() { 339 return title; 340 } 341 342 345 public boolean isFormatWrapped() { 346 return FORMAT_WRAP.equals(format); 347 } 348 349 352 public void parse(InputStream is) { 353 try { 354 parser.parse(new InputSource (is), this); 355 } catch (SAXException 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 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 369 public void startElement(String namespaceURI, String localName, 370 String qName, Attributes atts) throws SAXException { 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 |