1 11 package org.eclipse.update.core.model; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.util.Calendar ; 17 import java.util.GregorianCalendar ; 18 import java.util.Iterator ; 19 import java.util.Locale ; 20 import java.util.Stack ; 21 import java.util.StringTokenizer ; 22 23 import javax.xml.parsers.DocumentBuilder ; 24 import javax.xml.parsers.DocumentBuilderFactory ; 25 import javax.xml.parsers.ParserConfigurationException ; 26 import javax.xml.parsers.SAXParser ; 27 import javax.xml.parsers.SAXParserFactory ; 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 ; 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 67 public class DefaultSiteParser extends DefaultHandler { 68 69 private final static SAXParserFactory parserFactory = 70 SAXParserFactory.newInstance(); 71 72 private SAXParser 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 PLUGIN_ID = UpdateCore.getPlugin().getBundle().getSymbolicName(); 89 90 private static final String SITE = "site"; private static final String FEATURE = "feature"; private static final String ARCHIVE = "archive"; private static final String CATEGORY_DEF = "category-def"; private static final String CATEGORY = "category"; private static final String DESCRIPTION = "description"; private static final String MIRROR = "mirror"; private static final String ASSOCIATE_SITE = "associateSite"; 100 private static final String DEFAULT_INFO_URL = "index.html"; private static final String FEATURES = "features/"; 103 Stack stateStack = new Stack (); 105 106 Stack objectStack = new Stack (); 109 110 private int currentState; 111 112 115 public DefaultSiteParser() { 116 super(); 117 try { 118 parserFactory.setNamespaceAware(true); 119 this.parser = parserFactory.newSAXParser(); 120 } catch (ParserConfigurationException e) { 121 UpdateCore.log(e); 122 } catch (SAXException e) { 123 UpdateCore.log(e); 124 } 125 126 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) 127 debug("Created"); } 129 130 public void init(SiteModelFactory factory) { 131 this.factory = factory; 133 stateStack = new Stack (); 134 objectStack = new Stack (); 135 status = null; 136 DESCRIPTION_SITE_ALREADY_SEEN = false; 137 } 138 139 149 public SiteModel parse(InputStream in) throws SAXException , IOException { 150 stateStack.push(new Integer (STATE_INITIAL)); 151 currentState = ((Integer ) stateStack.peek()).intValue(); 152 parser.parse(new InputSource (in), this); 153 if (objectStack.isEmpty()) 154 throw new SAXException (Messages.DefaultSiteParser_NoSiteTag); 155 else { 156 if (objectStack.peek() instanceof SiteModel) { 157 return (SiteModel) objectStack.pop(); 158 } else { 159 String stack = ""; Iterator iter = objectStack.iterator(); 161 while (iter.hasNext()) { 162 stack = stack + iter.next().toString() + "\r\n"; } 164 throw new SAXException (NLS.bind(Messages.DefaultSiteParser_WrongParsingStack, (new String [] { stack }))); 165 } 166 } 167 } 168 169 175 public MultiStatus getStatus() { 176 return status; 177 } 178 179 184 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 185 186 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) { 187 debug("State: " + currentState); debug("Start Element: uri:" + uri + " local Name:" + localName + " qName:" + qName); } 190 191 switch (currentState) { 192 case STATE_IGNORED_ELEMENT : 193 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String [] { 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 [] { getState(currentState) }))); 229 break; 230 } 231 int newState = ((Integer ) stateStack.peek()).intValue(); 232 if (newState != STATE_IGNORED_ELEMENT) 233 currentState = newState; 234 235 } 236 237 242 public void endElement(String uri, String localName, String qName) { 243 244 String text = null; 245 URLEntryModel info = null; 246 247 int state = ((Integer ) 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 ) { 262 text = (String ) objectStack.pop(); 263 SiteModel site = (SiteModel) objectStack.peek(); 264 site.getDescriptionModel().setAnnotation(text); 265 } 266 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 ) { 277 text = (String ) 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 = ""; while (objectStack.peek() instanceof String ) { 288 String newText = (String ) objectStack.pop(); 290 if (trailingSpace(newText) && !leadingSpace(text)) { 291 text = " " + text; } 293 text = newText.trim() + text; 294 if (leadingSpace(newText) && !leadingSpace(text)) { 295 text = " " + text; } 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 if (DESCRIPTION_SITE_ALREADY_SEEN) 309 debug(NLS.bind(Messages.DefaultSiteParser_ElementAlreadySet, (new String [] { 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 = ""; while (objectStack.peek() instanceof String ) { 318 String newText = (String ) objectStack.pop(); 320 if (trailingSpace(newText) && !leadingSpace(text)) { 321 text = " " + text; } 323 text = newText.trim() + text; 324 if (leadingSpace(newText) && !leadingSpace(text)) { 325 text = " " + text; } 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 [] { getState(state), category.getLabel() }))); 337 else 338 category.setDescriptionModel(info); 339 break; 340 341 default : 342 internalError(NLS.bind(Messages.DefaultSiteParser_UnknownEndState, (new String [] { getState(state) }))); 343 break; 344 } 345 346 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) 347 debug("End Element:" + uri + ":" + localName + ":" + qName); } 349 350 355 public void characters(char[] ch, int start, int length) { 356 String text = new String (ch, start, length); 357 int state = ((Integer ) stateStack.peek()).intValue(); 359 if (state == STATE_DESCRIPTION_SITE || state == STATE_DESCRIPTION_CATEGORY_DEF) 360 objectStack.push(text); 361 362 } 363 364 369 public void error(SAXParseException ex) { 370 logStatus(ex); 371 } 372 373 379 public void fatalError(SAXParseException ex) throws SAXException { 380 logStatus(ex); 381 throw ex; 382 } 383 384 private void handleInitialState(String elementName, Attributes attributes) throws SAXException { 385 if (elementName.equals(SITE)) { 386 stateStack.push(new Integer (STATE_SITE)); 387 processSite(attributes); 388 } else { 389 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String [] { elementName, getState(currentState) }))); 390 throw new SAXException (Messages.DefaultSiteParser_InvalidXMLStream); 392 } 393 394 } 395 396 private void handleSiteState(String elementName, Attributes attributes) { 397 if (elementName.equals(DESCRIPTION)) { 398 stateStack.push(new Integer (STATE_DESCRIPTION_SITE)); 399 processInfo(attributes); 400 } else if (elementName.equals(FEATURE)) { 401 stateStack.push(new Integer (STATE_FEATURE)); 402 processFeature(attributes); 403 } else if (elementName.equals(ARCHIVE)) { 404 stateStack.push(new Integer (STATE_ARCHIVE)); 405 processArchive(attributes); 406 } else if (elementName.equals(CATEGORY_DEF)) { 407 stateStack.push(new Integer (STATE_CATEGORY_DEF)); 408 processCategoryDef(attributes); 409 } else 410 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String [] { elementName, getState(currentState) }))); 411 } 412 413 private void handleFeatureState(String elementName, Attributes attributes) { 414 if (elementName.equals(DESCRIPTION)) { 415 stateStack.push(new Integer (STATE_DESCRIPTION_SITE)); 416 processInfo(attributes); 417 } else if (elementName.equals(FEATURE)) { 418 stateStack.push(new Integer (STATE_FEATURE)); 419 processFeature(attributes); 420 } else if (elementName.equals(ARCHIVE)) { 421 stateStack.push(new Integer (STATE_ARCHIVE)); 422 processArchive(attributes); 423 } else if (elementName.equals(CATEGORY_DEF)) { 424 stateStack.push(new Integer (STATE_CATEGORY_DEF)); 425 processCategoryDef(attributes); 426 } else if (elementName.equals(CATEGORY)) { 427 stateStack.push(new Integer (STATE_CATEGORY)); 428 processCategory(attributes); 429 } else 430 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String [] { elementName, getState(currentState) }))); 431 } 432 433 private void handleCategoryDefState(String elementName, Attributes attributes) { 434 if (elementName.equals(FEATURE)) { 435 stateStack.push(new Integer (STATE_FEATURE)); 436 processFeature(attributes); 437 } else if (elementName.equals(ARCHIVE)) { 438 stateStack.push(new Integer (STATE_ARCHIVE)); 439 processArchive(attributes); 440 } else if (elementName.equals(CATEGORY_DEF)) { 441 stateStack.push(new Integer (STATE_CATEGORY_DEF)); 442 processCategoryDef(attributes); 443 } else if (elementName.equals(DESCRIPTION)) { 444 stateStack.push(new Integer (STATE_DESCRIPTION_CATEGORY_DEF)); 445 processInfo(attributes); 446 } else 447 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String [] { elementName, getState(currentState) }))); 448 } 449 450 private void handleCategoryState(String elementName, Attributes attributes) { 451 if (elementName.equals(DESCRIPTION)) { 452 stateStack.push(new Integer (STATE_DESCRIPTION_SITE)); 453 processInfo(attributes); 454 } else if (elementName.equals(FEATURE)) { 455 stateStack.push(new Integer (STATE_FEATURE)); 456 processFeature(attributes); 457 } else if (elementName.equals(ARCHIVE)) { 458 stateStack.push(new Integer (STATE_ARCHIVE)); 459 processArchive(attributes); 460 } else if (elementName.equals(CATEGORY_DEF)) { 461 stateStack.push(new Integer (STATE_CATEGORY_DEF)); 462 processCategoryDef(attributes); 463 } else if (elementName.equals(CATEGORY)) { 464 stateStack.push(new Integer (STATE_CATEGORY)); 465 processCategory(attributes); 466 } else 467 internalErrorUnknownTag(NLS.bind(Messages.DefaultSiteParser_UnknownElement, (new String [] { elementName, getState(currentState) }))); 468 } 469 470 473 private void processSite(Attributes attributes) throws SAXException { 474 SiteModel site = factory.createSiteMapModel(); 476 477 String siteURL = attributes.getValue("url"); if (siteURL != null && !("".equals(siteURL.trim()))) { if (!siteURL.endsWith("/") && !siteURL.endsWith(File.separator)) { siteURL += "/"; } 484 site.setLocationURLString(siteURL); 485 } 486 487 URLEntryModel description = factory.createURLEntryModel(); 490 description.setURLString(DEFAULT_INFO_URL); 491 site.setDescriptionModel(description); 492 493 String type = attributes.getValue("type"); if (!factory.canParseSiteType(type)) { 498 throw new SAXException (new InvalidSiteTypeException(type)); 499 } 500 site.setType(type); 501 502 String mirrorsURL = attributes.getValue("mirrorsURL"); 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 pack200 = attributes.getValue("pack200"); if(site instanceof ExtendedSite && pack200 != null && new Boolean (pack200).booleanValue()){ 514 ((ExtendedSite) site).setSupportsPack200(true); 515 } 516 517 if ( (site instanceof ExtendedSite) && (attributes.getValue("digestURL") != null)) { ExtendedSite extendedSite = (ExtendedSite) site; 519 extendedSite.setDigestExist(true); 520 extendedSite.setDigestURL(attributes.getValue("digestURL")); 522 if ( (attributes.getValue("availableLocales") != null) && (!attributes.getValue("availableLocales").trim().equals(""))) { StringTokenizer locals = new StringTokenizer (attributes.getValue("availableLocales"), ","); String [] availableLocals = new String [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)) { IURLEntry[] associateSites = getAssociateSites(attributes.getValue("associateSitesURL"), factory); 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); 546 } 547 548 551 private void processFeature(Attributes attributes) { 552 SiteFeatureReferenceModel feature = factory.createFeatureReferenceModel(); 553 554 String urlInfo = attributes.getValue("url"); String id = attributes.getValue("id"); String ver = attributes.getValue("version"); 560 boolean noURL = (urlInfo == null || urlInfo.trim().equals("")); boolean noId = (id == null || id.trim().equals("")); boolean noVersion = (ver == null || ver.trim().equals("")); 564 if (noURL) { 566 if (noId || noVersion) 567 internalError(NLS.bind(Messages.DefaultSiteParser_Missing, (new String [] { "url", getState(currentState) }))); else urlInfo = FEATURES + id + '_' + ver; } 571 572 feature.setURLString(urlInfo); 573 574 String type = attributes.getValue("type"); feature.setType(type); 576 577 if (noId ^ noVersion) { 579 String [] values = new String [] { 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 String label = attributes.getValue("label"); if (label != null) { 589 if ("".equals(label.trim())) label = null; 591 } 592 feature.setLabel(label); 593 594 String os = attributes.getValue("os"); feature.setOS(os); 597 598 String ws = attributes.getValue("ws"); feature.setWS(ws); 601 602 String nl = attributes.getValue("nl"); feature.setNL(nl); 605 606 String arch = attributes.getValue("arch"); feature.setArch(arch); 609 610 String patch = attributes.getValue("patch"); 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); 623 } 624 625 628 private void processArchive(Attributes attributes) { 629 ArchiveReferenceModel archive = factory.createArchiveReferenceModel(); 630 String id = attributes.getValue("path"); if (id == null || id.trim().equals("")) { internalError(NLS.bind(Messages.DefaultSiteParser_Missing, (new String [] { "path", getState(currentState) }))); } 634 635 archive.setPath(id); 636 637 String url = attributes.getValue("url"); if (url == null || url.trim().equals("")) { internalError(NLS.bind(Messages.DefaultSiteParser_Missing, (new String [] { "archive", getState(currentState) }))); } 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); 649 } 650 651 654 private void processCategory(Attributes attributes) { 655 String category = attributes.getValue("name"); 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); } 662 663 666 private void processCategoryDef(Attributes attributes) { 667 CategoryModel category = factory.createSiteCategoryModel(); 668 String name = attributes.getValue("name"); String label = attributes.getValue("label"); 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); } 680 681 684 private void processInfo(Attributes attributes) { 685 URLEntryModel inf = factory.createURLEntryModel(); 686 String infoURL = attributes.getValue("url"); inf.setURLString(infoURL); 688 689 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_PARSING) 690 debug("Processed Info: url:" + infoURL); 692 objectStack.push(inf); 693 } 694 695 698 private static void debug(String s) { 699 UpdateCore.debug("DefaultSiteParser" + s); } 701 702 705 private void logStatus(SAXParseException ex) { 706 String name = ex.getSystemId(); 707 if (name == null) 708 name = ""; else 710 name = name.substring(1 + name.lastIndexOf("/")); 712 String msg; 713 if (name.equals("")) msg = NLS.bind(Messages.DefaultSiteParser_ErrorParsing, (new String [] { ex.getMessage() })); 715 else { 716 String [] values = new String [] { 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 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 742 private void internalErrorUnknownTag(String msg) { 743 stateStack.push(new Integer (STATE_IGNORED_ELEMENT)); 744 internalError(msg); 745 } 746 747 750 private void internalError(String message) { 751 error(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, message, null)); 752 } 753 754 757 private String getState(int state) { 758 759 switch (state) { 760 case STATE_IGNORED_ELEMENT : 761 return "Ignored"; 763 case STATE_INITIAL : 764 return "Initial"; 766 case STATE_SITE : 767 return "Site"; 769 case STATE_FEATURE : 770 return "Feature"; 772 case STATE_ARCHIVE : 773 return "Archive"; 775 case STATE_CATEGORY : 776 return "Category"; 778 case STATE_CATEGORY_DEF : 779 return "Category Def"; 781 case STATE_DESCRIPTION_CATEGORY_DEF : 782 return "Description / Category Def"; 784 case STATE_DESCRIPTION_SITE : 785 return "Description / Site"; 787 default : 788 return Messages.DefaultSiteParser_UnknownState; 789 } 790 } 791 private boolean leadingSpace(String str) { 792 if (str.length() <= 0) { 793 return false; 794 } 795 return Character.isWhitespace(str.charAt(0)); 796 } 797 private boolean trailingSpace(String 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 mirrorsURL, SiteModelFactory factory) { 805 806 try { 807 String countryCode = Locale.getDefault().getCountry().toLowerCase(); 808 int timeZone = (new GregorianCalendar ()).get(Calendar.ZONE_OFFSET)/(60*60*1000); 809 810 if (mirrorsURL.indexOf("?") != -1) { mirrorsURL = mirrorsURL + "&"; } else { 813 mirrorsURL = mirrorsURL + "?"; } 815 mirrorsURL = mirrorsURL + "countryCode=" + countryCode + "&timeZone=" + timeZone + "&responseType=xml"; 817 DocumentBuilderFactory domFactory = 818 DocumentBuilderFactory.newInstance(); 819 DocumentBuilder builder = domFactory.newDocumentBuilder(); 820 Document document = builder.parse(mirrorsURL); 821 if (document == null) 822 return null; 823 NodeList mirrorNodes = document.getElementsByTagName(MIRROR); 824 URLEntryModel[] mirrors = new URLEntryModel[mirrorNodes.getLength()]; 825 for (int i=0; i<mirrorNodes.getLength(); i++) { 826 Element mirrorNode = (Element )mirrorNodes.item(i); 827 mirrors[i] = factory.createURLEntryModel(); 828 String infoURL = mirrorNode.getAttribute("url"); String label = mirrorNode.getAttribute("label"); 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); } 836 return mirrors; 837 } 838 catch (Exception e) { 839 if (mirrorsURL != null && 841 (mirrorsURL.startsWith("http://") || mirrorsURL.startsWith("https://") || mirrorsURL.startsWith("file://") || mirrorsURL.startsWith("ftp://") || mirrorsURL.startsWith("jar://"))) UpdateCore.log(Messages.DefaultSiteParser_mirrors, e); 847 return null; 848 } 849 } 850 851 private static IURLEntry[] getAssociateSites(String associateSitesURL, SiteModelFactory factory) { 852 853 try { 854 DocumentBuilderFactory domFactory = 855 DocumentBuilderFactory.newInstance(); 856 DocumentBuilder builder = domFactory.newDocumentBuilder(); 857 Document document = builder.parse(associateSitesURL); 858 if (document == null) 859 return null; 860 NodeList mirrorNodes = document.getElementsByTagName(ASSOCIATE_SITE); 861 URLEntry[] mirrors = new URLEntry[mirrorNodes.getLength()]; 862 for (int i=0; i<mirrorNodes.getLength(); i++) { 863 Element mirrorNode = (Element )mirrorNodes.item(i); 864 mirrors[i] = new URLEntry(); 865 String infoURL = mirrorNode.getAttribute("url"); String label = mirrorNode.getAttribute("label"); 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); } 873 return mirrors; 874 } 875 catch (Exception e) { 876 if (associateSitesURL != null && 878 (associateSitesURL.startsWith("http://") || associateSitesURL.startsWith("https://") || associateSitesURL.startsWith("file://") || associateSitesURL.startsWith("ftp://") || associateSitesURL.startsWith("jar://"))) UpdateCore.log(Messages.DefaultSiteParser_mirrors, e); 884 return null; 885 } 886 } 887 888 } 889 | Popular Tags |