Debian Codegraph @

bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/DefaultSiteParser.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2000, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * Red Hat Inc. - 378338: Support for "bundle" element
14 *******************************************************************************/
15 package org.eclipse.equinox.internal.p2.updatesite;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.net.URI;
21 import java.util.ArrayList;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Stack;
25
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.parsers.SAXParser;
30 import javax.xml.parsers.SAXParserFactory;
31
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.MultiStatus;
34 import org.eclipse.core.runtime.Status;
35 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
36 import org.eclipse.equinox.internal.p2.core.helpers.SecureXMLUtil;
37 import org.eclipse.equinox.internal.p2.core.helpers.Tracing;
38 import org.eclipse.equinox.p2.publisher.eclipse.URLEntry;
39 import org.eclipse.osgi.util.NLS;
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Element;
42 import org.w3c.dom.NodeList;
43 import org.xml.sax.Attributes;
44 import org.xml.sax.InputSource;
45 import org.xml.sax.SAXException;
46 import org.xml.sax.SAXParseException;
47 import org.xml.sax.helpers.DefaultHandler;
48
49 /**
50 * Parses a site.xml file. This class was initially copied from
51 * org.eclipse.update.core.model.DefaultSiteParser.
52 */
53 public class DefaultSiteParser extends DefaultHandler {
54
55 private static final String ARCHIVE = "archive"; //$NON-NLS-1$
56 private static final String CATEGORY = "category"; //$NON-NLS-1$
57 private static final String CATEGORY_DEF = "category-def"; //$NON-NLS-1$
58
59 private static final String ASSOCIATE_SITES_URL = "associateSitesURL"; //$NON-NLS-1$
60 private static final String ASSOCIATE_SITE = "associateSite"; //$NON-NLS-1$
61 private static final String DEFAULT_INFO_URL = "index.html"; //$NON-NLS-1$
62 private static final String DESCRIPTION = "description"; //$NON-NLS-1$
63 private static final String FEATURE = "feature"; //$NON-NLS-1$
64 private static final String BUNDLE = "bundle"; //$NON-NLS-1$
65 private static final String FEATURES = "features/"; //$NON-NLS-1$
66 private static final String PLUGINS = "plugins/"; //$NON-NLS-1$
67 private static final String PLUGIN_ID = Activator.ID;
68 private static final String SITE = "site"; //$NON-NLS-1$
69
70 private static final int STATE_ARCHIVE = 3;
71 private static final int STATE_CATEGORY = 4;
72 private static final int STATE_CATEGORY_DEF = 5;
73 private static final int STATE_DESCRIPTION_CATEGORY_DEF = 7;
74 private static final int STATE_DESCRIPTION_SITE = 6;
75 private static final int STATE_FEATURE = 2;
76 private static final int STATE_BUNDLE = 8;
77 private static final int STATE_IGNORED_ELEMENT = -1;
78 private static final int STATE_INITIAL = 0;
79 private static final int STATE_SITE = 1;
80
81 private int currentState;
82
83 private boolean DESCRIPTION_SITE_ALREADY_SEEN = false;
84 // Current object stack (used to hold the current object we are
85 // populating in this plugin descriptor
86 Stack<Object> objectStack = new Stack<>();
87
88 private SAXParser parser;
89
90 // Current State Information
91 Stack<Integer> stateStack = new Stack<>();
92
93 // List of string keys for translated strings
94 private final List<String> messageKeys = new ArrayList<>(4);
95
96 private MultiStatus status;
97 private final URI siteLocation;
98
99 /*
100 *
101 */
102 private static void debug(String s) {
103 Tracing.debug("DefaultSiteParser: " + s); //$NON-NLS-1$
104 }
105
106 private static URLEntry[] getAssociateSites(String associateSitesURL) {
107
108 try {
109 DocumentBuilderFactory domFactory = SecureXMLUtil.newSecureDocumentBuilderFactory();
110 DocumentBuilder builder = domFactory.newDocumentBuilder();
111 Document document = builder.parse(associateSitesURL);
112 if (document == null)
113 return null;
114 NodeList mirrorNodes = document.getElementsByTagName(ASSOCIATE_SITE);
115 URLEntry[] mirrors = new URLEntry[mirrorNodes.getLength()];
116 for (int i = 0; i < mirrorNodes.getLength(); i++) {
117 Element mirrorNode = (Element) mirrorNodes.item(i);
118 mirrors[i] = new URLEntry();
119 String infoURL = mirrorNode.getAttribute("url"); //$NON-NLS-1$
120 String label = mirrorNode.getAttribute("label"); //$NON-NLS-1$
121 mirrors[i].setURL(infoURL);
122 mirrors[i].setAnnotation(label);
123
124 if (Tracing.DEBUG_GENERATOR_PARSING)
125 debug("Processed mirror: url:" + infoURL + " label:" + label); //$NON-NLS-1$ //$NON-NLS-2$
126 }
127 return mirrors;
128 } catch (Exception e) {
129 // log if absolute url
130 if (associateSitesURL != null && (associateSitesURL.startsWith("http://") //$NON-NLS-1$
131 || associateSitesURL.startsWith("https://") //$NON-NLS-1$
132 || associateSitesURL.startsWith("file://") //$NON-NLS-1$
133 || associateSitesURL.startsWith("ftp://") //$NON-NLS-1$
134 || associateSitesURL.startsWith("jar://"))) //$NON-NLS-1$
135 log(Messages.DefaultSiteParser_mirrors, e);
136 return null;
137 }
138 }
139
140 static void log(Exception e) {
141 LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Internal Error", e)); //$NON-NLS-1$
142 }
143
144 static void log(String message) {
145 LogHelper.log(new Status(IStatus.WARNING, Activator.ID, message, null));
146 }
147
148 static void log(String message, Exception e) {
149 LogHelper.log(new Status(IStatus.WARNING, Activator.ID, message, e));
150 }
151
152 /**
153 * Constructs a site parser.
154 */
155 public DefaultSiteParser(URI siteLocation) {
156 super();
157 this.siteLocation = siteLocation;
158 stateStack = new Stack<>();
159 objectStack = new Stack<>();
160 status = null;
161 DESCRIPTION_SITE_ALREADY_SEEN = false;
162 try {
163 SAXParserFactory parserFactory = SecureXMLUtil.newSecureSAXParserFactory();
164 parserFactory.setNamespaceAware(true);
165 this.parser = parserFactory.newSAXParser();
166 } catch (ParserConfigurationException e) {
167 log(e);
168 } catch (SAXException e) {
169 log(e);
170 }
171
172 if (Tracing.DEBUG_GENERATOR_PARSING)
173 debug("Created"); //$NON-NLS-1$
174 }
175
176 /**
177 * Handle character text
178 *
179 * @see DefaultHandler#characters(char[], int, int)
180 * @since 2.0
181 */
182 @Override
183 public void characters(char[] ch, int start, int length) {
184 String text = new String(ch, start, length);
185 // only push if description
186 int state = stateStack.peek().intValue();
187 if (state == STATE_DESCRIPTION_SITE || state == STATE_DESCRIPTION_CATEGORY_DEF)
188 objectStack.push(text);
189
190 }
191
192 /**
193 * Handle end of element tags
194 *
195 * @see DefaultHandler#endElement(String, String, String)
196 * @since 2.0
197 */
198 @Override
199 public void endElement(String uri, String localName, String qName) {
200
201 String text = null;
202 URLEntry info = null;
203
204 int state = stateStack.peek().intValue();
205 switch (state) {
206 case STATE_IGNORED_ELEMENT:
207 case STATE_ARCHIVE:
208 case STATE_CATEGORY:
209 stateStack.pop();
210 break;
211
212 case STATE_INITIAL:
213 internalError(Messages.DefaultSiteParser_ParsingStackBackToInitialState);
214 break;
215
216 case STATE_SITE:
217 stateStack.pop();
218 if (objectStack.peek() instanceof String) {
219 text = (String) objectStack.pop();
220 SiteModel site = (SiteModel) objectStack.peek();
221 site.getDescription().setAnnotation(text);
222 }
223 // do not pop the object
224 break;
225
226 case STATE_FEATURE:
227 stateStack.pop();
228 objectStack.pop();
229 break;
230
231 case STATE_BUNDLE:
232 stateStack.pop();
233 objectStack.pop();
234 break;
235
236 case STATE_CATEGORY_DEF:
237 stateStack.pop();
238 if (objectStack.peek() instanceof String) {
239 text = (String) objectStack.pop();
240 SiteCategory category = (SiteCategory) objectStack.peek();
241 category.setDescription(text);
242 }
243 objectStack.pop();
244 break;
245
246 case STATE_DESCRIPTION_SITE:
247 stateStack.pop();
248 text = ""; //$NON-NLS-1$
249 while (objectStack.peek() instanceof String) {
250 // add text, preserving at most one space between text fragments
251 String newText = (String) objectStack.pop();
252 if (trailingSpace(newText) && !leadingSpace(text)) {
253 text = " " + text; //$NON-NLS-1$
254 }
255 text = newText.trim() + text;
256 if (leadingSpace(newText) && !leadingSpace(text)) {
257 text = " " + text; //$NON-NLS-1$
258 }
259 }
260 text = text.trim();
261
262 info = (URLEntry) objectStack.pop();
263 if (text != null)
264 info.setAnnotation(text);
265
266 SiteModel siteModel = (SiteModel) objectStack.peek();
267 // override description.
268 // do not raise error as previous description may be default one
269 // when parsing site tag
270 if (DESCRIPTION_SITE_ALREADY_SEEN)
271 debug(NLS.bind(Messages.DefaultSiteParser_ElementAlreadySet, (new String[] { getState(state) })));
272 siteModel.setDescription(info);
273 DESCRIPTION_SITE_ALREADY_SEEN = true;
274 break;
275
276 case STATE_DESCRIPTION_CATEGORY_DEF:
277 stateStack.pop();
278 text = ""; //$NON-NLS-1$
279 while (objectStack.peek() instanceof String) {
280 // add text, preserving at most one space between text fragments
281 String newText = (String) objectStack.pop();
282 if (trailingSpace(newText) && !leadingSpace(text)) {
283 text = " " + text; //$NON-NLS-1$
284 }
285 text = newText.trim() + text;
286 if (leadingSpace(newText) && !leadingSpace(text)) {
287 text = " " + text; //$NON-NLS-1$
288 }
289 }
290 text = text.trim();
291
292 info = (URLEntry) objectStack.pop();
293 if (text != null)
294 info.setAnnotation(text);
295
296 SiteCategory category = (SiteCategory) objectStack.peek();
297 if (category.getDescription() != null)
298 internalError(NLS.bind(Messages.DefaultSiteParser_ElementAlreadySet,
299 (new String[] { getState(state), category.getLabel() })));
300 else {
301 checkTranslated(info.getAnnotation());
302 category.setDescription(info.getAnnotation());
303 }
304 break;
305
306 default:
307 internalError(NLS.bind(Messages.DefaultSiteParser_UnknownEndState, (new String[] { getState(state) })));
308 break;
309 }
310
311 if (Tracing.DEBUG_GENERATOR_PARSING)
312 debug("End Element:" + uri + ":" + localName + ":" + qName);//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
313 }
314
315 /*
316 * Handles an error state specified by the status. The collection of all logged
317 * status objects can be accessed using <code>getStatus()</code>.
318 *
319 * @param error a status detailing the error condition
320 */
321 private void error(IStatus error) {
322
323 if (status == null) {
324 status = new MultiStatus(PLUGIN_ID, 0, Messages.DefaultSiteParser_ErrorParsingSite, null);
325 }
326
327 status.add(error);
328 if (Tracing.DEBUG_GENERATOR_PARSING)
329 LogHelper.log(error);
330 }
331
332 /**
333 * Handle errors
334 *
335 * @see DefaultHandler#error(SAXParseException)
336 * @since 2.0
337 */
338 @Override
339 public void error(SAXParseException ex) {
340 logStatus(ex);
341 }
342
343 /**
344 * Handle fatal errors
345 *
346 * @see DefaultHandler#fatalError(SAXParseException)
347 * @exception SAXException
348 * @since 2.0
349 */
350 @Override
351 public void fatalError(SAXParseException ex) throws SAXException {
352 logStatus(ex);
353 throw ex;
354 }
355
356 /*
357 * return the state as String
358 */
359 private String getState(int state) {
360
361 switch (state) {
362 case STATE_IGNORED_ELEMENT:
363 return "Ignored"; //$NON-NLS-1$
364
365 case STATE_INITIAL:
366 return "Initial"; //$NON-NLS-1$
367
368 case STATE_SITE:
369 return "Site"; //$NON-NLS-1$
370
371 case STATE_FEATURE:
372 return "Feature"; //$NON-NLS-1$
373
374 case STATE_BUNDLE:
375 return "Bundle"; //$NON-NLS-1$
376
377 case STATE_ARCHIVE:
378 return "Archive"; //$NON-NLS-1$
379
380 case STATE_CATEGORY:
381 return "Category"; //$NON-NLS-1$
382
383 case STATE_CATEGORY_DEF:
384 return "Category Def"; //$NON-NLS-1$
385
386 case STATE_DESCRIPTION_CATEGORY_DEF:
387 return "Description / Category Def"; //$NON-NLS-1$
388
389 case STATE_DESCRIPTION_SITE:
390 return "Description / Site"; //$NON-NLS-1$
391
392 default:
393 return Messages.DefaultSiteParser_UnknownState;
394 }
395 }
396
397 /**
398 * Returns all status objects accumulated by the parser.
399 *
400 * @return multi-status containing accumulated status, or <code>null</code>.
401 * @since 2.0
402 */
403 public MultiStatus getStatus() {
404 return status;
405 }
406
407 private void handleCategoryDefState(String elementName, Attributes attributes) {
408 switch (elementName) {
409 case FEATURE:
410 stateStack.push(Integer.valueOf(STATE_FEATURE));
411 processFeature(attributes);
412 break;
413 case BUNDLE:
414 stateStack.push(Integer.valueOf(STATE_BUNDLE));
415 processBundle(attributes);
416 break;
417 case ARCHIVE:
418 stateStack.push(Integer.valueOf(STATE_ARCHIVE));
419 processArchive(attributes);
420 break;
421 case CATEGORY_DEF:
422 stateStack.push(Integer.valueOf(STATE_CATEGORY_DEF));
423 processCategoryDef(attributes);
424 break;
425 case DESCRIPTION:
426 stateStack.push(Integer.valueOf(STATE_DESCRIPTION_CATEGORY_DEF));
427 processInfo(attributes);
428 break;
429 default:
430 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
431 (new String[] { elementName, getState(currentState) })));
432 break;
433 }
434 }
435
436 private void handleCategoryState(String elementName, Attributes attributes) {
437 switch (elementName) {
438 case DESCRIPTION:
439 stateStack.push(Integer.valueOf(STATE_DESCRIPTION_SITE));
440 processInfo(attributes);
441 break;
442 case FEATURE:
443 stateStack.push(Integer.valueOf(STATE_FEATURE));
444 processFeature(attributes);
445 break;
446 case BUNDLE:
447 stateStack.push(Integer.valueOf(STATE_BUNDLE));
448 processBundle(attributes);
449 break;
450 case ARCHIVE:
451 stateStack.push(Integer.valueOf(STATE_ARCHIVE));
452 processArchive(attributes);
453 break;
454 case CATEGORY_DEF:
455 stateStack.push(Integer.valueOf(STATE_CATEGORY_DEF));
456 processCategoryDef(attributes);
457 break;
458 case CATEGORY:
459 stateStack.push(Integer.valueOf(STATE_CATEGORY));
460 processCategory(attributes);
461 break;
462 default:
463 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
464 (new String[] { elementName, getState(currentState) })));
465 break;
466 }
467 }
468
469 private void handleFeatureState(String elementName, Attributes attributes) {
470 switch (elementName) {
471 case DESCRIPTION:
472 stateStack.push(Integer.valueOf(STATE_DESCRIPTION_SITE));
473 processInfo(attributes);
474 break;
475 case FEATURE:
476 stateStack.push(Integer.valueOf(STATE_FEATURE));
477 processFeature(attributes);
478 break;
479 case ARCHIVE:
480 stateStack.push(Integer.valueOf(STATE_ARCHIVE));
481 processArchive(attributes);
482 break;
483 case CATEGORY_DEF:
484 stateStack.push(Integer.valueOf(STATE_CATEGORY_DEF));
485 processCategoryDef(attributes);
486 break;
487 case CATEGORY:
488 stateStack.push(Integer.valueOf(STATE_CATEGORY));
489 processCategory(attributes);
490 break;
491 default:
492 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
493 (new String[] { elementName, getState(currentState) })));
494 break;
495 }
496 }
497
498 private void handleBundleState(String elementName, Attributes attributes) {
499 switch (elementName) {
500 case DESCRIPTION:
501 stateStack.push(Integer.valueOf(STATE_DESCRIPTION_SITE));
502 processInfo(attributes);
503 break;
504 case ARCHIVE:
505 stateStack.push(Integer.valueOf(STATE_ARCHIVE));
506 processArchive(attributes);
507 break;
508 case CATEGORY_DEF:
509 stateStack.push(Integer.valueOf(STATE_CATEGORY_DEF));
510 processCategoryDef(attributes);
511 break;
512 case CATEGORY:
513 stateStack.push(Integer.valueOf(STATE_CATEGORY));
514 processCategory(attributes);
515 break;
516 default:
517 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
518 (new String[] { elementName, getState(currentState) })));
519 break;
520 }
521 }
522
523 private void handleInitialState(String elementName, Attributes attributes) throws SAXException {
524 if (elementName.equals(SITE)) {
525 stateStack.push(Integer.valueOf(STATE_SITE));
526 processSite(attributes);
527 } else {
528 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
529 (new String[] { elementName, getState(currentState) })));
530 // what we received was not a site.xml, no need to continue
531 throw new SAXException(Messages.DefaultSiteParser_InvalidXMLStream);
532 }
533
534 }
535
536 private void handleSiteState(String elementName, Attributes attributes) {
537 switch (elementName) {
538 case DESCRIPTION:
539 stateStack.push(Integer.valueOf(STATE_DESCRIPTION_SITE));
540 processInfo(attributes);
541 break;
542 case FEATURE:
543 stateStack.push(Integer.valueOf(STATE_FEATURE));
544 processFeature(attributes);
545 break;
546 case BUNDLE:
547 stateStack.push(Integer.valueOf(STATE_BUNDLE));
548 processBundle(attributes);
549 break;
550 case ARCHIVE:
551 stateStack.push(Integer.valueOf(STATE_ARCHIVE));
552 processArchive(attributes);
553 break;
554 case CATEGORY_DEF:
555 stateStack.push(Integer.valueOf(STATE_CATEGORY_DEF));
556 processCategoryDef(attributes);
557 break;
558 default:
559 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
560 (new String[] { elementName, getState(currentState) })));
561 break;
562 }
563 }
564
565 /*
566 *
567 */
568 private void internalError(String message) {
569 error(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, message, null));
570 }
571
572 /*
573 *
574 */
575 private void internalErrorUnknownTag(String msg) {
576 stateStack.push(Integer.valueOf(STATE_IGNORED_ELEMENT));
577 internalError(msg);
578 }
579
580 private boolean leadingSpace(String str) {
581 if (str.length() <= 0) {
582 return false;
583 }
584 return Character.isWhitespace(str.charAt(0));
585 }
586
587 /*
588 *
589 */
590 private void logStatus(SAXParseException ex) {
591 String name = ex.getSystemId();
592 if (name == null)
593 name = ""; //$NON-NLS-1$
594 else
595 name = name.substring(1 + name.lastIndexOf("/")); //$NON-NLS-1$
596
597 String msg;
598 if (name.equals("")) //$NON-NLS-1$
599 name = siteLocation.toString();
600 String[] values = new String[] { name, Integer.toString(ex.getLineNumber()),
601 Integer.toString(ex.getColumnNumber()), ex.getMessage() };
602 msg = NLS.bind(Messages.DefaultSiteParser_ErrorlineColumnMessage, values);
603 error(new Status(IStatus.ERROR, PLUGIN_ID, msg, ex));
604 }
605
606 /**
607 * Parses the specified input steam and constructs a site model. The input
608 * stream is not closed as part of this operation.
609 *
610 * @param in input stream
611 * @return site model
612 * @exception SAXException
613 * @exception IOException
614 * @since 2.0
615 */
616 public SiteModel parse(InputStream in) throws SAXException, IOException {
617 stateStack.push(Integer.valueOf(STATE_INITIAL));
618 currentState = stateStack.peek().intValue();
619 parser.parse(new InputSource(in), this);
620 if (objectStack.isEmpty())
621 throw new SAXException(Messages.DefaultSiteParser_NoSiteTag);
622 if (objectStack.peek() instanceof SiteModel) {
623 SiteModel site = (SiteModel) objectStack.pop();
624 site.setMessageKeys(messageKeys);
625 return site;
626 }
627 String stack = ""; //$NON-NLS-1$
628 Iterator<Object> iter = objectStack.iterator();
629 while (iter.hasNext()) {
630 stack = stack + iter.next().toString() + "\r\n"; //$NON-NLS-1$
631 }
632 throw new SAXException(NLS.bind(Messages.DefaultSiteParser_WrongParsingStack, (new String[] { stack })));
633 }
634
635 /*
636 * process archive info
637 */
638 private void processArchive(Attributes attributes) {
639 URLEntry archive = new URLEntry();
640 String id = attributes.getValue("path"); //$NON-NLS-1$
641 if (id == null || id.trim().equals("")) { //$NON-NLS-1$
642 internalError(
643 NLS.bind(Messages.DefaultSiteParser_Missing, (new String[] { "path", getState(currentState) }))); //$NON-NLS-1$
644 }
645
646 archive.setAnnotation(id);
647
648 String url = attributes.getValue("url"); //$NON-NLS-1$
649 if (url == null || url.trim().equals("")) { //$NON-NLS-1$
650 internalError(
651 NLS.bind(Messages.DefaultSiteParser_Missing, (new String[] { "archive", getState(currentState) }))); //$NON-NLS-1$
652 } else {
653 archive.setURL(url);
654
655 SiteModel site = (SiteModel) objectStack.peek();
656 site.addArchive(archive);
657 }
658 if (Tracing.DEBUG_GENERATOR_PARSING)
659 debug("End processing Archive: path:" + id + " url:" + url);//$NON-NLS-1$ //$NON-NLS-2$
660
661 }
662
663 /*
664 * process the Category info
665 */
666 private void processCategory(Attributes attributes) {
667 String category = attributes.getValue("name"); //$NON-NLS-1$
668 Object item = objectStack.peek();
669 if (item instanceof SiteFeature) {
670 SiteFeature feature = (SiteFeature) item;
671 feature.addCategoryName(category);
672 } else if (item instanceof SiteBundle) {
673 SiteBundle bundle = (SiteBundle) item;
674 bundle.addCategoryName(category);
675 }
676
677 if (Tracing.DEBUG_GENERATOR_PARSING)
678 debug("End processing Category: name:" + category); //$NON-NLS-1$
679 }
680
681 /*
682 * process category def info
683 */
684 private void processCategoryDef(Attributes attributes) {
685 SiteCategory category = new SiteCategory();
686 String name = attributes.getValue("name"); //$NON-NLS-1$
687 String label = attributes.getValue("label"); //$NON-NLS-1$
688 checkTranslated(label);
689 category.setName(name);
690 category.setLabel(label);
691
692 SiteModel site = (SiteModel) objectStack.peek();
693 site.addCategory(category);
694 objectStack.push(category);
695
696 if (Tracing.DEBUG_GENERATOR_PARSING)
697 debug("End processing CategoryDef: name:" + name + " label:" + label); //$NON-NLS-1$ //$NON-NLS-2$
698 }
699
700 /*
701 * process feature info
702 */
703 private void processFeature(Attributes attributes) {
704 SiteFeature feature = new SiteFeature();
705
706 // feature location on the site
707 String urlInfo = attributes.getValue("url"); //$NON-NLS-1$
708 // identifier and version
709 String id = attributes.getValue("id"); //$NON-NLS-1$
710 String ver = attributes.getValue("version"); //$NON-NLS-1$
711
712 boolean noURL = (urlInfo == null || urlInfo.trim().equals("")); //$NON-NLS-1$
713 boolean noId = (id == null || id.trim().equals("")); //$NON-NLS-1$
714 boolean noVersion = (ver == null || ver.trim().equals("")); //$NON-NLS-1$
715
716 // We need to have id and version, or the url, or both.
717 if (noURL) {
718 if (noId || noVersion)
719 internalError(
720 NLS.bind(Messages.DefaultSiteParser_Missing, (new String[] { "url", getState(currentState) }))); //$NON-NLS-1$
721 else
722 // default url
723 urlInfo = FEATURES + id + '_' + ver; //
724 }
725
726 feature.setURLString(urlInfo);
727
728 String type = attributes.getValue("type"); //$NON-NLS-1$
729 feature.setType(type);
730
731 // if one is null, and not the other
732 if (noId ^ noVersion) {
733 String[] values = new String[] { id, ver, getState(currentState) };
734 log(NLS.bind(Messages.DefaultFeatureParser_IdOrVersionInvalid, values));
735 } else {
736 feature.setFeatureIdentifier(id);
737 feature.setFeatureVersion(ver);
738 }
739
740 // get label if it exists
741 String label = attributes.getValue("label"); //$NON-NLS-1$
742 if (label != null) {
743 if ("".equals(label.trim())) //$NON-NLS-1$
744 label = null;
745 checkTranslated(label);
746 }
747 feature.setLabel(label);
748
749 // OS
750 String os = attributes.getValue("os"); //$NON-NLS-1$
751 feature.setOS(os);
752
753 // WS
754 String ws = attributes.getValue("ws"); //$NON-NLS-1$
755 feature.setWS(ws);
756
757 // NL
758 String nl = attributes.getValue("nl"); //$NON-NLS-1$
759 feature.setNL(nl);
760
761 // arch
762 String arch = attributes.getValue("arch"); //$NON-NLS-1$
763 feature.setArch(arch);
764
765 // patch
766 String patch = attributes.getValue("patch"); //$NON-NLS-1$
767 feature.setPatch(patch);
768
769 SiteModel site = (SiteModel) objectStack.peek();
770 site.addFeature(feature);
771 feature.setSiteModel(site);
772
773 objectStack.push(feature);
774
775 if (Tracing.DEBUG_GENERATOR_PARSING)
776 debug("End Processing DefaultFeature Tag: url:" + urlInfo + " type:" + type); //$NON-NLS-1$ //$NON-NLS-2$
777
778 }
779
780 /*
781 * process feature info
782 */
783 private void processBundle(Attributes attributes) {
784 SiteBundle bundle = new SiteBundle();
785
786 // feature location on the site
787 String urlInfo = attributes.getValue("url"); //$NON-NLS-1$
788 // identifier and version
789 String id = attributes.getValue("id"); //$NON-NLS-1$
790 String ver = attributes.getValue("version"); //$NON-NLS-1$
791
792 boolean noURL = (urlInfo == null || urlInfo.trim().equals("")); //$NON-NLS-1$
793 boolean noId = (id == null || id.trim().equals("")); //$NON-NLS-1$
794 boolean noVersion = (ver == null || ver.trim().equals("")); //$NON-NLS-1$
795
796 // We need to have id and version, or the url, or both.
797 if (noURL) {
798 if (noId || noVersion)
799 internalError(
800 NLS.bind(Messages.DefaultSiteParser_Missing, (new String[] { "url", getState(currentState) }))); //$NON-NLS-1$
801 else
802 // default url
803 urlInfo = PLUGINS + id + '_' + ver; //
804 }
805
806 bundle.setURLString(urlInfo);
807
808 String type = attributes.getValue("type"); //$NON-NLS-1$
809 bundle.setType(type);
810
811 // if one is null, and not the other
812 if (noId ^ noVersion) {
813 String[] values = new String[] { id, ver, getState(currentState) };
814 log(NLS.bind(Messages.DefaultFeatureParser_IdOrVersionInvalid, values));
815 } else {
816 bundle.setBundleIdentifier(id);
817 bundle.setBundleVersion(ver);
818 }
819
820 // get label if it exists
821 String label = attributes.getValue("label"); //$NON-NLS-1$
822 if (label != null) {
823 if ("".equals(label.trim())) //$NON-NLS-1$
824 label = null;
825 checkTranslated(label);
826 }
827 bundle.setLabel(label);
828
829 // OS
830 String os = attributes.getValue("os"); //$NON-NLS-1$
831 bundle.setOS(os);
832
833 // WS
834 String ws = attributes.getValue("ws"); //$NON-NLS-1$
835 bundle.setWS(ws);
836
837 // NL
838 String nl = attributes.getValue("nl"); //$NON-NLS-1$
839 bundle.setNL(nl);
840
841 // arch
842 String arch = attributes.getValue("arch"); //$NON-NLS-1$
843 bundle.setArch(arch);
844
845 // patch
846 String patch = attributes.getValue("patch"); //$NON-NLS-1$
847 bundle.setPatch(patch);
848
849 SiteModel site = (SiteModel) objectStack.peek();
850 site.addBundle(bundle);
851 bundle.setSiteModel(site);
852
853 objectStack.push(bundle);
854
855 if (Tracing.DEBUG_GENERATOR_PARSING)
856 debug("End Processing DefaultFeature Tag: url:" + urlInfo + " type:" + type); //$NON-NLS-1$ //$NON-NLS-2$
857
858 }
859
860 /*
861 * process URL info with element text
862 */
863 private void processInfo(Attributes attributes) {
864 URLEntry inf = new URLEntry();
865 String infoURL = attributes.getValue("url"); //$NON-NLS-1$
866 inf.setURL(infoURL);
867
868 if (Tracing.DEBUG_GENERATOR_PARSING)
869 debug("Processed Info: url:" + infoURL); //$NON-NLS-1$
870
871 objectStack.push(inf);
872 }
873
874 /*
875 * process site info
876 */
877 private void processSite(Attributes attributes) {
878 // create site map
879 SiteModel site = new SiteModel();
880
881 // if URL is specified, it replaces the URL of the site
882 // used to calculate the location of features and archives
883 String siteURL = attributes.getValue("url"); //$NON-NLS-1$
884 if (siteURL != null && !("".equals(siteURL.trim()))) { //$NON-NLS-1$
885 if (!siteURL.endsWith("/") && !siteURL.endsWith(File.separator)) { //$NON-NLS-1$
886 siteURL += "/"; //$NON-NLS-1$
887 }
888 site.setLocationURIString(siteURL);
889 }
890
891 // provide default description URL
892 // If <description> is specified, for the site, it takes precedence
893 URLEntry description = new URLEntry();
894 description.setURL(DEFAULT_INFO_URL);
895 site.setDescription(description);
896
897 // verify we can parse the site ...if the site has
898 // a different type throw an exception to force reparsing
899 // with the matching parser
900 String type = attributes.getValue("type"); //$NON-NLS-1$
901 site.setType(type);
902
903 // get mirrors, if any
904 String mirrorsURL = attributes.getValue("mirrorsURL"); //$NON-NLS-1$
905 if (mirrorsURL != null && mirrorsURL.trim().length() > 0) {
906 // URLEntry[] mirrors = getMirrors(mirrorsURL);
907 // if (mirrors != null)
908 // site.setMirrors(mirrors);
909 // else
910
911 // Since we are parsing the site at p2 generation time and the
912 // mirrors may change, there is no point doing the mirror expansion now
913 site.setMirrorsURIString(mirrorsURL);
914 }
915
916 String digestURL = attributes.getValue("digestURL"); //$NON-NLS-1$
917 if (digestURL != null)
918 site.setDigestURIString(digestURL);
919
920 // TODO: Digest locales
921 // if ((attributes.getValue("availableLocales") != null) &&
922 // (!attributes.getValue("availableLocales").trim().equals(""))) {
923 // //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
924 // StringTokenizer locals = new
925 // StringTokenizer(attributes.getValue("availableLocales"), ",");
926 // //$NON-NLS-1$//$NON-NLS-2$
927 // String[] availableLocals = new String[locals.countTokens()];
928 // int i = 0;
929 // while (locals.hasMoreTokens()) {
930 // availableLocals[i++] = locals.nextToken();
931 // }
932 // extendedSite.setAvailableLocals(availableLocals);
933 // }
934 // }
935 //
936 final String associateURL = attributes.getValue(ASSOCIATE_SITES_URL);
937 if (associateURL != null) {
938 // resolve the URI relative to the site location
939 URI resolvedLocation = siteLocation.resolve(associateURL);
940 site.setAssociateSites(getAssociateSites(resolvedLocation.toString()));
941 }
942
943 objectStack.push(site);
944
945 if (Tracing.DEBUG_GENERATOR_PARSING)
946 debug("End process Site tag: siteURL:" + siteURL + " type:" + type);//$NON-NLS-1$ //$NON-NLS-2$
947
948 }
949
950 /**
951 * Handle start of element tags
952 *
953 * @see DefaultHandler#startElement(String, String, String, Attributes)
954 * @since 2.0
955 */
956 @Override
957 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
958
959 if (Tracing.DEBUG_GENERATOR_PARSING) {
960 debug("State: " + currentState); //$NON-NLS-1$
961 debug("Start Element: uri:" + uri + " local Name:" + localName + " qName:" + qName);//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
962 }
963
964 switch (currentState) {
965 case STATE_IGNORED_ELEMENT:
966 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement,
967 (new String[] { localName, getState(currentState) })));
968 break;
969 case STATE_INITIAL:
970 handleInitialState(localName, attributes);
971 break;
972
973 case STATE_SITE:
974 handleSiteState(localName, attributes);
975 break;
976
977 case STATE_FEATURE:
978 handleFeatureState(localName, attributes);
979 break;
980
981 case STATE_BUNDLE:
982 handleBundleState(localName, attributes);
983 break;
984
985 case STATE_ARCHIVE:
986 handleSiteState(localName, attributes);
987 break;
988
989 case STATE_CATEGORY:
990 handleCategoryState(localName, attributes);
991 break;
992
993 case STATE_CATEGORY_DEF:
994 handleCategoryDefState(localName, attributes);
995 break;
996
997 case STATE_DESCRIPTION_SITE:
998 handleSiteState(localName, attributes);
999 break;
1000
1001 case STATE_DESCRIPTION_CATEGORY_DEF:
1002 handleSiteState(localName, attributes);
1003 break;
1004
1005 default:
1006 internalErrorUnknownTag(
1007 NLS.bind(Messages.DefaultSiteParser_UnknownStartState, (new String[] { getState(currentState) })));
1008 break;
1009 }
1010 int newState = stateStack.peek().intValue();
1011 if (newState != STATE_IGNORED_ELEMENT)
1012 currentState = newState;
1013
1014 }
1015
1016 private boolean trailingSpace(String str) {
1017 if (str.length() <= 0) {
1018 return false;
1019 }
1020 return Character.isWhitespace(str.charAt(str.length() - 1));
1021 }
1022
1023 // Add translatable strings from the site.xml
1024 // to the list of message keys.
1025 private void checkTranslated(String value) {
1026 if (value != null && value.length() > 1 && value.startsWith("%")) //$NON-NLS-1$
1027 messageKeys.add(value.substring(1));
1028 }
1029 }