KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > core > model > DefaultSiteParser


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.update.core.model;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.Calendar JavaDoc;
17 import java.util.GregorianCalendar JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Stack JavaDoc;
21 import java.util.StringTokenizer JavaDoc;
22
23 import javax.xml.parsers.DocumentBuilder JavaDoc;
24 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
25 import javax.xml.parsers.ParserConfigurationException JavaDoc;
26 import javax.xml.parsers.SAXParser JavaDoc;
27 import javax.xml.parsers.SAXParserFactory JavaDoc;
28
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.MultiStatus;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.core.runtime.Status;
33 import org.eclipse.osgi.util.NLS;
34 import org.eclipse.update.core.IURLEntry;
35 import org.eclipse.update.core.SiteFeatureReferenceModel;
36 import org.eclipse.update.core.URLEntry;
37 import org.eclipse.update.internal.core.ExtendedSite;
38 import org.eclipse.update.internal.core.Messages;
39 import org.eclipse.update.internal.core.UpdateCore;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.Element JavaDoc;
42 import org.w3c.dom.NodeList JavaDoc;
43 import org.xml.sax.Attributes JavaDoc;
44 import org.xml.sax.InputSource JavaDoc;
45 import org.xml.sax.SAXException JavaDoc;
46 import org.xml.sax.SAXParseException JavaDoc;
47 import org.xml.sax.helpers.DefaultHandler JavaDoc;
48
49 /**
50  * Default site parser.
51  * Parses the site manifest file as defined by the platform. Defers
52  * to a model factory to create the actual concrete model objects. The
53  * update framework supplies two factory implementations:
54  * <ul>
55  * <li>@see org.eclipse.update.core.model.SiteModelFactory
56  * <li>@see org.eclipse.update.core.BaseSiteFactory
57  * </ul>
58  *
59  * <p>
60  * <b>Note:</b> This class/interface is part of an interim API that is still under development and expected to
61  * change significantly before reaching stability. It is being made available at this early stage to solicit feedback
62  * from pioneering adopters on the understanding that any code that uses this API will almost certainly be broken
63  * (repeatedly) as the API evolves.
64  * </p>
65  * @since 2.0
66  */

67 public class DefaultSiteParser extends DefaultHandler JavaDoc {
68     
69     private final static SAXParserFactory JavaDoc parserFactory =
70         SAXParserFactory.newInstance();
71     
72     private SAXParser JavaDoc parser;
73     private SiteModelFactory factory;
74
75     private MultiStatus status;
76
77     private boolean DESCRIPTION_SITE_ALREADY_SEEN = false;
78
79     private static final int STATE_IGNORED_ELEMENT = -1;
80     private static final int STATE_INITIAL = 0;
81     private static final int STATE_SITE = 1;
82     private static final int STATE_FEATURE = 2;
83     private static final int STATE_ARCHIVE = 3;
84     private static final int STATE_CATEGORY = 4;
85     private static final int STATE_CATEGORY_DEF = 5;
86     private static final int STATE_DESCRIPTION_SITE = 6;
87     private static final int STATE_DESCRIPTION_CATEGORY_DEF = 7;
88     private static final String JavaDoc PLUGIN_ID = UpdateCore.getPlugin().getBundle().getSymbolicName();
89
90     private static final String JavaDoc SITE = "site"; //$NON-NLS-1$
91
private static final String JavaDoc FEATURE = "feature"; //$NON-NLS-1$
92
private static final String JavaDoc ARCHIVE = "archive"; //$NON-NLS-1$
93
private static final String JavaDoc CATEGORY_DEF = "category-def"; //$NON-NLS-1$
94
private static final String JavaDoc CATEGORY = "category"; //$NON-NLS-1$
95
private static final String JavaDoc DESCRIPTION = "description"; //$NON-NLS-1$
96
private static final String JavaDoc MIRROR = "mirror"; //$NON-NLS-1$
97
//private static final String ASSOCIATE_SITES = "associateSites"; //$NON-NLS-1$
98
private static final String JavaDoc ASSOCIATE_SITE = "associateSite"; //$NON-NLS-1$
99

100     private static final String JavaDoc DEFAULT_INFO_URL = "index.html"; //$NON-NLS-1$
101
private static final String JavaDoc FEATURES = "features/"; //$NON-NLS-1$
102

103     // Current State Information
104
Stack JavaDoc stateStack = new Stack JavaDoc();
105
106     // Current object stack (used to hold the current object we are
107
// populating in this plugin descriptor
108
Stack JavaDoc objectStack = new Stack JavaDoc();
109
110     private int currentState;
111
112     /**
113      * Constructs a site parser.
114      */

115     public DefaultSiteParser() {
116         super();
117         try {
118             parserFactory.setNamespaceAware(true);
119             this.parser = parserFactory.newSAXParser();
120         } catch (ParserConfigurationException JavaDoc e) {
121             UpdateCore.log(e);
122         } catch (SAXException JavaDoc e) {
123             UpdateCore.log(e);
124         }
125
126         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
127             debug("Created"); //$NON-NLS-1$
128
}
129
130     public void init(SiteModelFactory factory) {
131         // PERF: separate instance creation from parsing
132
this.factory = factory;
133         stateStack = new Stack JavaDoc();
134         objectStack = new Stack JavaDoc();
135         status = null;
136         DESCRIPTION_SITE_ALREADY_SEEN = false;
137     }
138
139     /**
140      * Parses the specified input steam and constructs a site model.
141      * The input stream is not closed as part of this operation.
142      *
143      * @param in input stream
144      * @return site model
145      * @exception SAXException
146      * @exception IOException
147      * @since 2.0
148      */

149     public SiteModel parse(InputStream JavaDoc in) throws SAXException JavaDoc, IOException JavaDoc {
150         stateStack.push(new Integer JavaDoc(STATE_INITIAL));
151         currentState = ((Integer JavaDoc) stateStack.peek()).intValue();
152         parser.parse(new InputSource JavaDoc(in), this);
153         if (objectStack.isEmpty())
154             throw new SAXException JavaDoc(Messages.DefaultSiteParser_NoSiteTag);
155         else {
156             if (objectStack.peek() instanceof SiteModel) {
157                 return (SiteModel) objectStack.pop();
158             } else {
159                 String JavaDoc stack = ""; //$NON-NLS-1$
160
Iterator JavaDoc iter = objectStack.iterator();
161                 while (iter.hasNext()) {
162                     stack = stack + iter.next().toString() + "\r\n"; //$NON-NLS-1$
163
}
164                 throw new SAXException JavaDoc(NLS.bind(Messages.DefaultSiteParser_WrongParsingStack, (new String JavaDoc[] { stack })));
165             }
166         }
167     }
168
169     /**
170      * Returns all status objects accumulated by the parser.
171      *
172      * @return multi-status containing accumulated status, or <code>null</code>.
173      * @since 2.0
174      */

175     public MultiStatus getStatus() {
176         return status;
177     }
178
179     /**
180      * Handle start of element tags
181      * @see DefaultHandler#startElement(String, String, String, Attributes)
182      * @since 2.0
183      */

184     public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
185
186         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) {
187             debug("State: " + currentState); //$NON-NLS-1$
188
debug("Start Element: uri:" + uri + " local Name:" + localName + " qName:" + qName);//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
189
}
190
191         switch (currentState) {
192             case STATE_IGNORED_ELEMENT :
193                 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String JavaDoc[] { localName, getState(currentState) })));
194                 break;
195             case STATE_INITIAL :
196                 handleInitialState(localName, attributes);
197                 break;
198
199             case STATE_SITE :
200                 handleSiteState(localName, attributes);
201                 break;
202
203             case STATE_FEATURE :
204                 handleFeatureState(localName, attributes);
205                 break;
206
207             case STATE_ARCHIVE :
208                 handleSiteState(localName, attributes);
209                 break;
210
211             case STATE_CATEGORY :
212                 handleCategoryState(localName, attributes);
213                 break;
214
215             case STATE_CATEGORY_DEF :
216                 handleCategoryDefState(localName, attributes);
217                 break;
218
219             case STATE_DESCRIPTION_SITE :
220                 handleSiteState(localName, attributes);
221                 break;
222
223             case STATE_DESCRIPTION_CATEGORY_DEF :
224                 handleSiteState(localName, attributes);
225                 break;
226
227             default :
228                 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownStartState, (new String JavaDoc[] { getState(currentState) })));
229                 break;
230         }
231         int newState = ((Integer JavaDoc) stateStack.peek()).intValue();
232         if (newState != STATE_IGNORED_ELEMENT)
233             currentState = newState;
234
235     }
236
237     /**
238      * Handle end of element tags
239      * @see DefaultHandler#endElement(String, String, String)
240      * @since 2.0
241      */

242     public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc qName) {
243
244         String JavaDoc text = null;
245         URLEntryModel info = null;
246
247         int state = ((Integer JavaDoc) stateStack.peek()).intValue();
248         switch (state) {
249             case STATE_IGNORED_ELEMENT :
250             case STATE_ARCHIVE :
251             case STATE_CATEGORY :
252                 stateStack.pop();
253                 break;
254
255             case STATE_INITIAL :
256                 internalError(Messages.DefaultSiteParser_ParsingStackBackToInitialState);
257                 break;
258
259             case STATE_SITE :
260                 stateStack.pop();
261                 if (objectStack.peek() instanceof String JavaDoc) {
262                     text = (String JavaDoc) objectStack.pop();
263                     SiteModel site = (SiteModel) objectStack.peek();
264                     site.getDescriptionModel().setAnnotation(text);
265                 }
266                 //do not pop the object
267
break;
268
269             case STATE_FEATURE :
270                 stateStack.pop();
271                 objectStack.pop();
272                 break;
273
274             case STATE_CATEGORY_DEF :
275                 stateStack.pop();
276                 if (objectStack.peek() instanceof String JavaDoc) {
277                     text = (String JavaDoc) objectStack.pop();
278                     CategoryModel category = (CategoryModel) objectStack.peek();
279                     category.getDescriptionModel().setAnnotation(text);
280                 }
281                 objectStack.pop();
282                 break;
283
284             case STATE_DESCRIPTION_SITE :
285                 stateStack.pop();
286                 text = ""; //$NON-NLS-1$
287
while (objectStack.peek() instanceof String JavaDoc) {
288                     // add text, preserving at most one space between text fragments
289
String JavaDoc newText = (String JavaDoc) objectStack.pop();
290                     if (trailingSpace(newText) && !leadingSpace(text)) {
291                         text = " " + text; //$NON-NLS-1$
292
}
293                     text = newText.trim() + text;
294                     if (leadingSpace(newText) && !leadingSpace(text)) {
295                         text = " " + text; //$NON-NLS-1$
296
}
297                 }
298                 text = text.trim();
299
300                 info = (URLEntryModel) objectStack.pop();
301                 if (text != null)
302                     info.setAnnotation(text);
303
304                 SiteModel siteModel = (SiteModel) objectStack.peek();
305                 // override description.
306
// do not raise error as previous description may be default one
307
// when parsing site tag
308
if (DESCRIPTION_SITE_ALREADY_SEEN)
309                     debug(NLS.bind(Messages.DefaultSiteParser_ElementAlreadySet, (new String JavaDoc[] { getState(state) })));
310                 siteModel.setDescriptionModel(info);
311                 DESCRIPTION_SITE_ALREADY_SEEN = true;
312                 break;
313
314             case STATE_DESCRIPTION_CATEGORY_DEF :
315                 stateStack.pop();
316                 text = ""; //$NON-NLS-1$
317
while (objectStack.peek() instanceof String JavaDoc) {
318                     // add text, preserving at most one space between text fragments
319
String JavaDoc newText = (String JavaDoc) objectStack.pop();
320                     if (trailingSpace(newText) && !leadingSpace(text)) {
321                         text = " " + text; //$NON-NLS-1$
322
}
323                     text = newText.trim() + text;
324                     if (leadingSpace(newText) && !leadingSpace(text)) {
325                         text = " " + text; //$NON-NLS-1$
326
}
327                 }
328                 text = text.trim();
329
330                 info = (URLEntryModel) objectStack.pop();
331                 if (text != null)
332                     info.setAnnotation(text);
333
334                 CategoryModel category = (CategoryModel) objectStack.peek();
335                 if (category.getDescriptionModel() != null)
336                     internalError(NLS.bind(Messages.DefaultSiteParser_ElementAlreadySet, (new String JavaDoc[] { getState(state), category.getLabel() })));
337                 else
338                     category.setDescriptionModel(info);
339                 break;
340
341             default :
342                 internalError(NLS.bind(Messages.DefaultSiteParser_UnknownEndState, (new String JavaDoc[] { getState(state) })));
343                 break;
344         }
345
346         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
347             debug("End Element:" + uri + ":" + localName + ":" + qName);//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
348
}
349
350     /**
351      * Handle character text
352      * @see DefaultHandler#characters(char[], int, int)
353      * @since 2.0
354      */

355     public void characters(char[] ch, int start, int length) {
356         String JavaDoc text = new String JavaDoc(ch, start, length);
357         //only push if description
358
int state = ((Integer JavaDoc) stateStack.peek()).intValue();
359         if (state == STATE_DESCRIPTION_SITE || state == STATE_DESCRIPTION_CATEGORY_DEF)
360             objectStack.push(text);
361
362     }
363
364     /**
365      * Handle errors
366      * @see DefaultHandler#error(SAXParseException)
367      * @since 2.0
368      */

369     public void error(SAXParseException JavaDoc ex) {
370         logStatus(ex);
371     }
372
373     /**
374      * Handle fatal errors
375      * @see DefaultHandler#fatalError(SAXParseException)
376      * @exception SAXException
377      * @since 2.0
378      */

379     public void fatalError(SAXParseException JavaDoc ex) throws SAXException JavaDoc {
380         logStatus(ex);
381         throw ex;
382     }
383
384     private void handleInitialState(String JavaDoc elementName, Attributes JavaDoc attributes) throws SAXException JavaDoc {
385         if (elementName.equals(SITE)) {
386             stateStack.push(new Integer JavaDoc(STATE_SITE));
387             processSite(attributes);
388         } else {
389             internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String JavaDoc[] { elementName, getState(currentState) })));
390             // what we received was not a site.xml, no need to continue
391
throw new SAXException JavaDoc(Messages.DefaultSiteParser_InvalidXMLStream);
392         }
393
394     }
395
396     private void handleSiteState(String JavaDoc elementName, Attributes JavaDoc attributes) {
397         if (elementName.equals(DESCRIPTION)) {
398             stateStack.push(new Integer JavaDoc(STATE_DESCRIPTION_SITE));
399             processInfo(attributes);
400         } else if (elementName.equals(FEATURE)) {
401             stateStack.push(new Integer JavaDoc(STATE_FEATURE));
402             processFeature(attributes);
403         } else if (elementName.equals(ARCHIVE)) {
404             stateStack.push(new Integer JavaDoc(STATE_ARCHIVE));
405             processArchive(attributes);
406         } else if (elementName.equals(CATEGORY_DEF)) {
407             stateStack.push(new Integer JavaDoc(STATE_CATEGORY_DEF));
408             processCategoryDef(attributes);
409         } else
410             internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String JavaDoc[] { elementName, getState(currentState) })));
411     }
412
413     private void handleFeatureState(String JavaDoc elementName, Attributes JavaDoc attributes) {
414         if (elementName.equals(DESCRIPTION)) {
415             stateStack.push(new Integer JavaDoc(STATE_DESCRIPTION_SITE));
416             processInfo(attributes);
417         } else if (elementName.equals(FEATURE)) {
418             stateStack.push(new Integer JavaDoc(STATE_FEATURE));
419             processFeature(attributes);
420         } else if (elementName.equals(ARCHIVE)) {
421             stateStack.push(new Integer JavaDoc(STATE_ARCHIVE));
422             processArchive(attributes);
423         } else if (elementName.equals(CATEGORY_DEF)) {
424             stateStack.push(new Integer JavaDoc(STATE_CATEGORY_DEF));
425             processCategoryDef(attributes);
426         } else if (elementName.equals(CATEGORY)) {
427             stateStack.push(new Integer JavaDoc(STATE_CATEGORY));
428             processCategory(attributes);
429         } else
430             internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String JavaDoc[] { elementName, getState(currentState) })));
431     }
432
433     private void handleCategoryDefState(String JavaDoc elementName, Attributes JavaDoc attributes) {
434         if (elementName.equals(FEATURE)) {
435             stateStack.push(new Integer JavaDoc(STATE_FEATURE));
436             processFeature(attributes);
437         } else if (elementName.equals(ARCHIVE)) {
438             stateStack.push(new Integer JavaDoc(STATE_ARCHIVE));
439             processArchive(attributes);
440         } else if (elementName.equals(CATEGORY_DEF)) {
441             stateStack.push(new Integer JavaDoc(STATE_CATEGORY_DEF));
442             processCategoryDef(attributes);
443         } else if (elementName.equals(DESCRIPTION)) {
444             stateStack.push(new Integer JavaDoc(STATE_DESCRIPTION_CATEGORY_DEF));
445             processInfo(attributes);
446         } else
447             internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String JavaDoc[] { elementName, getState(currentState) })));
448     }
449
450     private void handleCategoryState(String JavaDoc elementName, Attributes JavaDoc attributes) {
451         if (elementName.equals(DESCRIPTION)) {
452             stateStack.push(new Integer JavaDoc(STATE_DESCRIPTION_SITE));
453             processInfo(attributes);
454         } else if (elementName.equals(FEATURE)) {
455             stateStack.push(new Integer JavaDoc(STATE_FEATURE));
456             processFeature(attributes);
457         } else if (elementName.equals(ARCHIVE)) {
458             stateStack.push(new Integer JavaDoc(STATE_ARCHIVE));
459             processArchive(attributes);
460         } else if (elementName.equals(CATEGORY_DEF)) {
461             stateStack.push(new Integer JavaDoc(STATE_CATEGORY_DEF));
462             processCategoryDef(attributes);
463         } else if (elementName.equals(CATEGORY)) {
464             stateStack.push(new Integer JavaDoc(STATE_CATEGORY));
465             processCategory(attributes);
466         } else
467             internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String JavaDoc[] { elementName, getState(currentState) })));
468     }
469
470     /*
471      * process site info
472      */

473     private void processSite(Attributes JavaDoc attributes) throws SAXException JavaDoc {
474         // create site map
475
SiteModel site = factory.createSiteMapModel();
476
477         // if URL is specified, it replaces the URL of the site
478
// used to calculate the location of features and archives
479
String JavaDoc siteURL = attributes.getValue("url"); //$NON-NLS-1$
480
if (siteURL != null && !("".equals(siteURL.trim()))) { //$NON-NLS-1$
481
if (!siteURL.endsWith("/") && !siteURL.endsWith(File.separator)) { //$NON-NLS-1$
482
siteURL += "/"; //$NON-NLS-1$
483
}
484             site.setLocationURLString(siteURL);
485         }
486
487         // provide default description URL
488
// If <description> is specified, for the site, it takes precedence
489
URLEntryModel description = factory.createURLEntryModel();
490         description.setURLString(DEFAULT_INFO_URL);
491         site.setDescriptionModel(description);
492
493         // verify we can parse the site ...if the site has
494
// a different type throw an exception to force reparsing
495
// with the matching parser
496
String JavaDoc type = attributes.getValue("type"); //$NON-NLS-1$
497
if (!factory.canParseSiteType(type)) {
498             throw new SAXException JavaDoc(new InvalidSiteTypeException(type));
499         }
500         site.setType(type);
501         
502         // get mirrors, if any
503
String JavaDoc mirrorsURL = attributes.getValue("mirrorsURL"); //$NON-NLS-1$
504
if (mirrorsURL != null && mirrorsURL.trim().length() > 0) {
505             URLEntryModel[] mirrors = getMirrors(mirrorsURL, factory);
506             if (mirrors != null)
507                 site.setMirrorSiteEntryModels(mirrors);
508             else
509                 site.setMirrorsURLString(mirrorsURL);
510         }
511         
512         String JavaDoc pack200 = attributes.getValue("pack200"); //$NON-NLS-1$
513
if(site instanceof ExtendedSite && pack200 != null && new Boolean JavaDoc(pack200).booleanValue()){
514             ((ExtendedSite) site).setSupportsPack200(true);
515         }
516         
517         if ( (site instanceof ExtendedSite) && (attributes.getValue("digestURL") != null)) { //$NON-NLS-1$
518
ExtendedSite extendedSite = (ExtendedSite) site;
519             extendedSite.setDigestExist(true);
520             extendedSite.setDigestURL(attributes.getValue("digestURL")); //$NON-NLS-1$
521

522             if ( (attributes.getValue("availableLocales") != null) && (!attributes.getValue("availableLocales").trim().equals(""))) { //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
523
StringTokenizer JavaDoc locals = new StringTokenizer JavaDoc(attributes.getValue("availableLocales"), ","); //$NON-NLS-1$//$NON-NLS-2$
524
String JavaDoc[] availableLocals = new String JavaDoc[locals.countTokens()];
525                 int i = 0;
526                 while(locals.hasMoreTokens()) {
527                     availableLocals[i++] = locals.nextToken();
528                 }
529                 extendedSite.setAvailableLocals(availableLocals);
530             }
531         }
532         
533         if ( (site instanceof ExtendedSite) && (attributes.getValue("associateSitesURL") != null)) { //$NON-NLS-1$
534
IURLEntry[] associateSites = getAssociateSites(attributes.getValue("associateSitesURL"), factory); //$NON-NLS-1$
535
if (associateSites != null)
536                 ((ExtendedSite)site).setAssociateSites(associateSites);
537             else
538                 site.setMirrorsURLString(mirrorsURL);
539         }
540         
541         objectStack.push(site);
542
543         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
544             debug("End process Site tag: siteURL:" + siteURL + " type:" + type);//$NON-NLS-1$ //$NON-NLS-2$
545

546     }
547
548     /*
549      * process feature info
550      */

551     private void processFeature(Attributes JavaDoc attributes) {
552         SiteFeatureReferenceModel feature = factory.createFeatureReferenceModel();
553         
554         // feature location on the site
555
String JavaDoc urlInfo = attributes.getValue("url"); //$NON-NLS-1$
556
// identifier and version
557
String JavaDoc id = attributes.getValue("id"); //$NON-NLS-1$
558
String JavaDoc ver = attributes.getValue("version"); //$NON-NLS-1$
559

560         boolean noURL = (urlInfo == null || urlInfo.trim().equals("")); //$NON-NLS-1$
561
boolean noId = (id == null || id.trim().equals("")); //$NON-NLS-1$
562
boolean noVersion = (ver == null || ver.trim().equals("")); //$NON-NLS-1$
563

564         // We need to have id and version, or the url, or both.
565
if (noURL) {
566             if (noId || noVersion)
567                 internalError(NLS.bind(Messages.DefaultSiteParser_Missing, (new String JavaDoc[] { "url", getState(currentState) }))); //$NON-NLS-1$
568
else // default url
569
urlInfo = FEATURES + id + '_' + ver; //
570
}
571         
572         feature.setURLString(urlInfo);
573
574         String JavaDoc type = attributes.getValue("type"); //$NON-NLS-1$
575
feature.setType(type);
576
577         // if one is null, and not the other
578
if (noId ^ noVersion) {
579             String JavaDoc[] values = new String JavaDoc[] { id, ver, getState(currentState)};
580             UpdateCore.warn(NLS.bind(Messages.DefaultFeatureParser_IdOrVersionInvalid, values));
581         } else {
582             feature.setFeatureIdentifier(id);
583             feature.setFeatureVersion(ver);
584         }
585
586         // get label if it exists
587
String JavaDoc label = attributes.getValue("label"); //$NON-NLS-1$
588
if (label != null) {
589             if ("".equals(label.trim())) //$NON-NLS-1$
590
label = null;
591         }
592         feature.setLabel(label);
593
594         // OS
595
String JavaDoc os = attributes.getValue("os"); //$NON-NLS-1$
596
feature.setOS(os);
597
598         // WS
599
String JavaDoc ws = attributes.getValue("ws"); //$NON-NLS-1$
600
feature.setWS(ws);
601
602         // NL
603
String JavaDoc nl = attributes.getValue("nl"); //$NON-NLS-1$
604
feature.setNL(nl);
605
606         // arch
607
String JavaDoc arch = attributes.getValue("arch"); //$NON-NLS-1$
608
feature.setArch(arch);
609
610         //patch
611
String JavaDoc patch = attributes.getValue("patch"); //$NON-NLS-1$
612
feature.setPatch(patch);
613
614         SiteModel site = (SiteModel) objectStack.peek();
615         site.addFeatureReferenceModel(feature);
616         feature.setSiteModel(site);
617
618         objectStack.push(feature);
619
620         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
621             debug("End Processing DefaultFeature Tag: url:" + urlInfo + " type:" + type); //$NON-NLS-1$ //$NON-NLS-2$
622

623     }
624
625     /*
626      * process archive info
627      */

628     private void processArchive(Attributes JavaDoc attributes) {
629         ArchiveReferenceModel archive = factory.createArchiveReferenceModel();
630         String JavaDoc id = attributes.getValue("path"); //$NON-NLS-1$
631
if (id == null || id.trim().equals("")) { //$NON-NLS-1$
632
internalError(NLS.bind(Messages.DefaultSiteParser_Missing, (new String JavaDoc[] { "path", getState(currentState) }))); //$NON-NLS-1$
633
}
634
635         archive.setPath(id);
636
637         String JavaDoc url = attributes.getValue("url"); //$NON-NLS-1$
638
if (url == null || url.trim().equals("")) { //$NON-NLS-1$
639
internalError(NLS.bind(Messages.DefaultSiteParser_Missing, (new String JavaDoc[] { "archive", getState(currentState) }))); //$NON-NLS-1$
640
} else {
641             archive.setURLString(url);
642
643             SiteModel site = (SiteModel) objectStack.peek();
644             site.addArchiveReferenceModel(archive);
645         }
646         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
647             debug("End processing Archive: path:" + id + " url:" + url);//$NON-NLS-1$ //$NON-NLS-2$
648

649     }
650
651     /*
652      * process the Category info
653      */

654     private void processCategory(Attributes JavaDoc attributes) {
655         String JavaDoc category = attributes.getValue("name"); //$NON-NLS-1$
656
SiteFeatureReferenceModel feature = (SiteFeatureReferenceModel) objectStack.peek();
657         feature.addCategoryName(category);
658
659         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
660             debug("End processing Category: name:" + category); //$NON-NLS-1$
661
}
662
663     /*
664      * process category def info
665      */

666     private void processCategoryDef(Attributes JavaDoc attributes) {
667         CategoryModel category = factory.createSiteCategoryModel();
668         String JavaDoc name = attributes.getValue("name"); //$NON-NLS-1$
669
String JavaDoc label = attributes.getValue("label"); //$NON-NLS-1$
670
category.setName(name);
671         category.setLabel(label);
672
673         SiteModel site = (SiteModel) objectStack.peek();
674         site.addCategoryModel(category);
675         objectStack.push(category);
676
677         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
678             debug("End processing CategoryDef: name:" + name + " label:" + label); //$NON-NLS-1$ //$NON-NLS-2$
679
}
680
681     /*
682      * process URL info with element text
683      */

684     private void processInfo(Attributes JavaDoc attributes) {
685         URLEntryModel inf = factory.createURLEntryModel();
686         String JavaDoc infoURL = attributes.getValue("url"); //$NON-NLS-1$
687
inf.setURLString(infoURL);
688
689         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
690             debug("Processed Info: url:" + infoURL); //$NON-NLS-1$
691

692         objectStack.push(inf);
693     }
694
695     /*
696      *
697      */

698     private static void debug(String JavaDoc s) {
699         UpdateCore.debug("DefaultSiteParser" + s); //$NON-NLS-1$
700
}
701
702     /*
703      *
704      */

705     private void logStatus(SAXParseException JavaDoc ex) {
706         String JavaDoc name = ex.getSystemId();
707         if (name == null)
708             name = ""; //$NON-NLS-1$
709
else
710             name = name.substring(1 + name.lastIndexOf("/")); //$NON-NLS-1$
711

712         String JavaDoc msg;
713         if (name.equals("")) //$NON-NLS-1$
714
msg = NLS.bind(Messages.DefaultSiteParser_ErrorParsing, (new String JavaDoc[] { ex.getMessage() }));
715         else {
716             String JavaDoc[] values = new String JavaDoc[] { name, Integer.toString(ex.getLineNumber()), Integer.toString(ex.getColumnNumber()), ex.getMessage()};
717             msg = NLS.bind(Messages.DefaultSiteParser_ErrorlineColumnMessage, values);
718         }
719         error(new Status(IStatus.ERROR, PLUGIN_ID, Platform.PARSE_PROBLEM, msg, ex));
720     }
721
722     /*
723      * Handles an error state specified by the status. The collection of all logged status
724      * objects can be accessed using <code>getStatus()</code>.
725      *
726      * @param error a status detailing the error condition
727      */

728     private void error(IStatus error) {
729
730         if (status == null) {
731             status = new MultiStatus(PLUGIN_ID, Platform.PARSE_PROBLEM, Messages.DefaultSiteParser_ErrorParsingSite, null);
732         }
733
734         status.add(error);
735         if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
736             UpdateCore.log(error);
737     }
738
739     /*
740      *
741      */

742     private void internalErrorUnknownTag(String JavaDoc msg) {
743         stateStack.push(new Integer JavaDoc(STATE_IGNORED_ELEMENT));
744         internalError(msg);
745     }
746
747     /*
748      *
749      */

750     private void internalError(String JavaDoc message) {
751         error(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, message, null));
752     }
753
754     /*
755      * return the state as String
756      */

757     private String JavaDoc getState(int state) {
758
759         switch (state) {
760             case STATE_IGNORED_ELEMENT :
761                 return "Ignored"; //$NON-NLS-1$
762

763             case STATE_INITIAL :
764                 return "Initial"; //$NON-NLS-1$
765

766             case STATE_SITE :
767                 return "Site"; //$NON-NLS-1$
768

769             case STATE_FEATURE :
770                 return "Feature"; //$NON-NLS-1$
771

772             case STATE_ARCHIVE :
773                 return "Archive"; //$NON-NLS-1$
774

775             case STATE_CATEGORY :
776                 return "Category"; //$NON-NLS-1$
777

778             case STATE_CATEGORY_DEF :
779                 return "Category Def"; //$NON-NLS-1$
780

781             case STATE_DESCRIPTION_CATEGORY_DEF :
782                 return "Description / Category Def"; //$NON-NLS-1$
783

784             case STATE_DESCRIPTION_SITE :
785                 return "Description / Site"; //$NON-NLS-1$
786

787             default :
788                 return Messages.DefaultSiteParser_UnknownState;
789         }
790     }
791     private boolean leadingSpace(String JavaDoc str) {
792         if (str.length() <= 0) {
793             return false;
794         }
795         return Character.isWhitespace(str.charAt(0));
796     }
797     private boolean trailingSpace(String JavaDoc str) {
798         if (str.length() <= 0) {
799             return false;
800         }
801         return Character.isWhitespace(str.charAt(str.length() - 1));
802     }
803     
804     static URLEntryModel[] getMirrors(String JavaDoc mirrorsURL, SiteModelFactory factory) {
805         
806         try {
807             String JavaDoc countryCode = Locale.getDefault().getCountry().toLowerCase();
808             int timeZone = (new GregorianCalendar JavaDoc()).get(Calendar.ZONE_OFFSET)/(60*60*1000);
809
810             if (mirrorsURL.indexOf("?") != -1) { //$NON-NLS-1$
811
mirrorsURL = mirrorsURL + "&"; //$NON-NLS-1$
812
} else {
813                 mirrorsURL = mirrorsURL + "?"; //$NON-NLS-1$
814
}
815             mirrorsURL = mirrorsURL + "countryCode=" + countryCode + "&timeZone=" + timeZone + "&responseType=xml"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
816

817             DocumentBuilderFactory JavaDoc domFactory =
818             DocumentBuilderFactory.newInstance();
819             DocumentBuilder JavaDoc builder = domFactory.newDocumentBuilder();
820             Document JavaDoc document = builder.parse(mirrorsURL);
821             if (document == null)
822                 return null;
823             NodeList JavaDoc mirrorNodes = document.getElementsByTagName(MIRROR);
824             URLEntryModel[] mirrors = new URLEntryModel[mirrorNodes.getLength()];
825             for (int i=0; i<mirrorNodes.getLength(); i++) {
826                 Element JavaDoc mirrorNode = (Element JavaDoc)mirrorNodes.item(i);
827                 mirrors[i] = factory.createURLEntryModel();
828                 String JavaDoc infoURL = mirrorNode.getAttribute("url"); //$NON-NLS-1$
829
String JavaDoc label = mirrorNode.getAttribute("label"); //$NON-NLS-1$
830
mirrors[i].setURLString(infoURL);
831                 mirrors[i].setAnnotation(label);
832
833                 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
834                     debug("Processed mirror: url:" + infoURL + " label:" + label); //$NON-NLS-1$ //$NON-NLS-2$
835
}
836             return mirrors;
837         }
838         catch (Exception JavaDoc e) {
839             // log if absolute url
840
if (mirrorsURL != null &&
841                     (mirrorsURL.startsWith("http://") //$NON-NLS-1$
842
|| mirrorsURL.startsWith("https://") //$NON-NLS-1$
843
|| mirrorsURL.startsWith("file://") //$NON-NLS-1$
844
|| mirrorsURL.startsWith("ftp://") //$NON-NLS-1$
845
|| mirrorsURL.startsWith("jar://"))) //$NON-NLS-1$
846
UpdateCore.log(Messages.DefaultSiteParser_mirrors, e);
847             return null;
848         }
849     }
850     
851     private static IURLEntry[] getAssociateSites(String JavaDoc associateSitesURL, SiteModelFactory factory) {
852         
853         try {
854             DocumentBuilderFactory JavaDoc domFactory =
855             DocumentBuilderFactory.newInstance();
856             DocumentBuilder JavaDoc builder = domFactory.newDocumentBuilder();
857             Document JavaDoc document = builder.parse(associateSitesURL);
858             if (document == null)
859                 return null;
860             NodeList JavaDoc mirrorNodes = document.getElementsByTagName(ASSOCIATE_SITE);
861             URLEntry[] mirrors = new URLEntry[mirrorNodes.getLength()];
862             for (int i=0; i<mirrorNodes.getLength(); i++) {
863                 Element JavaDoc mirrorNode = (Element JavaDoc)mirrorNodes.item(i);
864                 mirrors[i] = new URLEntry();
865                 String JavaDoc infoURL = mirrorNode.getAttribute("url"); //$NON-NLS-1$
866
String JavaDoc label = mirrorNode.getAttribute("label"); //$NON-NLS-1$
867
mirrors[i].setURLString(infoURL);
868                 mirrors[i].setAnnotation(label);
869
870                 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING)
871                     debug("Processed mirror: url:" + infoURL + " label:" + label); //$NON-NLS-1$ //$NON-NLS-2$
872
}
873             return mirrors;
874         }
875         catch (Exception JavaDoc e) {
876             // log if absolute url
877
if (associateSitesURL != null &&
878                     (associateSitesURL.startsWith("http://") //$NON-NLS-1$
879
|| associateSitesURL.startsWith("https://") //$NON-NLS-1$
880
|| associateSitesURL.startsWith("file://") //$NON-NLS-1$
881
|| associateSitesURL.startsWith("ftp://") //$NON-NLS-1$
882
|| associateSitesURL.startsWith("jar://"))) //$NON-NLS-1$
883
UpdateCore.log(Messages.DefaultSiteParser_mirrors, e);
884             return null;
885         }
886     }
887     
888 }
889
Popular Tags