1 20 21 package org.jdesktop.jdic.packager.impl; 22 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.IOException ; 28 import java.util.Iterator ; 29 import java.net.URL ; 30 31 import com.sun.deploy.xml.XMLEncoding; 32 import com.sun.deploy.xml.XMLNode; 33 import com.sun.deploy.xml.XMLParser; 34 import com.sun.javaws.exceptions.BadFieldException; 35 import com.sun.javaws.exceptions.MissingFieldException; 36 import com.sun.javaws.jnl.ExtensionDesc; 37 import com.sun.javaws.jnl.IconDesc; 38 import com.sun.javaws.jnl.InformationDesc; 39 import com.sun.javaws.jnl.JARDesc; 40 import com.sun.javaws.jnl.JREDesc; 41 import com.sun.javaws.jnl.LaunchDesc; 42 import com.sun.javaws.jnl.LaunchDescFactory; 43 import com.sun.javaws.jnl.PackageDesc; 44 import com.sun.javaws.jnl.PropertyDesc; 45 import com.sun.javaws.jnl.ResourceVisitor; 46 import com.sun.javaws.jnl.ResourcesDesc; 47 import com.sun.javaws.jnl.XMLUtils; 48 import com.sun.javaws.util.GeneralUtil; 49 50 54 public final class JnlpPackageInfo { 55 56 private static String osName = System.getProperty("os.name").toLowerCase(); 57 58 62 public JnlpPackageInfo() { 63 titles = new String [JnlpConstants.LOCALES.length]; 64 vendors = new String [JnlpConstants.LOCALES.length]; 65 licenses = new String [JnlpConstants.LOCALES.length]; 66 descriptions = new String [JnlpConstants.LOCALES.length]; 67 uniqueTmpDirPath = null; 68 enableLocalization = false; 69 bannerJpgFilePath = null; 70 panelJpgFilePath = null; 71 jnlpRefFilePaths = new ArrayList (); 72 } 73 74 77 private String [] titles; 78 81 private String [] vendors; 82 85 private String [] licenses; 86 89 private String [] descriptions; 90 91 94 private String licenseDirPath; 95 96 99 private String uniqueTmpDirPath; 100 101 104 private String packageName; 105 106 109 private String outputDirPath; 110 111 115 private String jnlpFileHref; 116 117 120 private String jnlpFilePath; 121 122 128 private String resourceDirPath; 129 130 133 private String version; 134 135 138 private String release; 139 140 144 private List jnlpRefFilePaths; 145 146 149 private String bannerJpgFilePath; 150 151 154 private String panelJpgFilePath; 155 158 private String msSDKDirPath; 159 162 private String rawMsiFilePath; 163 164 167 private boolean enableLocalization; 168 169 172 private boolean enableLicense; 173 174 177 private boolean shortcutEnabled; 178 179 182 private boolean enableAssociation; 183 184 187 private boolean enableSystemCache; 188 189 193 public String getBannerJpgFilePath() { 194 return bannerJpgFilePath; 195 } 196 197 201 public void setBannerJpgFilePath(String theBannerJpgFilePath) { 202 bannerJpgFilePath = theBannerJpgFilePath; 203 } 204 205 209 public String getPanelJpgFilePath() { 210 return panelJpgFilePath; 211 } 212 213 217 public void setPanelJpgFilePath(String thePanelJpegFilePath) { 218 panelJpgFilePath = thePanelJpegFilePath; 219 } 220 224 public String getMSSDKDirPath() { 225 return msSDKDirPath; 226 } 227 231 public void setMSSDKDirPath(String theMSSDKDirPath) { 232 msSDKDirPath = theMSSDKDirPath; 233 } 234 238 public String getRawMsiFilePath() { 239 return rawMsiFilePath; 240 } 241 242 246 public void setRawMsiFilePath(String theRawMsiFilePath) { 247 rawMsiFilePath = theRawMsiFilePath; 248 } 249 250 254 public boolean getSystemCacheEnabled() { 255 return enableSystemCache; 256 } 257 258 262 public void setSystemCacheEnabled(boolean systemcache) { 263 enableSystemCache = systemcache; 264 } 265 266 270 public String getPackageName() { 271 return packageName; 272 } 273 274 278 public void setPackageName(String theName) { 279 packageName = theName; 280 } 281 282 286 public String getOutputDirPath() { 287 return outputDirPath; 288 } 289 290 294 public void setOutputDirPath(String theOutputDirPath) { 295 outputDirPath = theOutputDirPath; 296 } 297 298 302 public String getJnlpFileHref() { 303 return jnlpFileHref; 304 } 305 306 310 public void setJnlpFileHref(String theJnlpFileHref) { 311 jnlpFileHref = theJnlpFileHref; 312 } 313 314 318 public String getJnlpFilePath() { 319 return jnlpFilePath; 320 } 321 322 326 public void setJnlpFilePath(String theJnlpFilePath) { 327 jnlpFilePath = theJnlpFilePath; 328 } 329 330 342 public String getJnlpFileName() { 343 return new File (jnlpFilePath).getName(); 344 } 345 346 350 public String getResourceDirPath() { 351 return resourceDirPath; 352 } 353 354 358 public void setResourcePath(String theResourcePath) { 359 resourceDirPath = theResourcePath; 360 } 361 362 366 public String getVersion() { 367 return version; 368 } 369 370 374 public void setVersion(String theVersion) { 375 if (osName.startsWith(JnlpConstants.OS_WINDOWS)) { 376 try { 377 Float.parseFloat(theVersion); 378 } catch (NumberFormatException e) { 379 throw new IllegalArgumentException ("Error: " + 380 "Illegal Version Number (Valid input: digits & '.')"); 381 } 382 } 383 version = theVersion; 384 } 385 386 390 public String getRelease() { 391 return release; 392 } 393 394 398 public void setRelease(String theRelease) { 399 if (osName.startsWith(JnlpConstants.OS_WINDOWS)) { 400 try { 401 Float.parseFloat(theRelease); 402 } catch (NumberFormatException e) { 403 throw new IllegalArgumentException ("Error: " + 404 "Illegal Release Number (Valid input: digits & '.')"); 405 } 406 } 407 release = theRelease; 408 } 409 410 414 public Iterator getJnlpRefFilePaths() { 415 return jnlpRefFilePaths.iterator(); 416 } 417 418 422 public boolean getShortcutEnabled() { 423 return shortcutEnabled; 424 } 425 426 430 public void setShortcutEnabled(boolean shortcut) { 431 shortcutEnabled = shortcut; 432 } 433 434 438 public boolean getAssociationEnabled() { 439 return enableAssociation; 440 } 441 442 446 public void setAssociationEnabled(boolean association) { 447 enableAssociation = association; 448 } 449 450 454 public boolean getLocalizationEnabled() { 455 return enableLocalization; 456 } 457 458 462 public void setGlocalizationEnabled(boolean globalization) { 463 enableLocalization = globalization; 464 } 465 466 470 public String getLicenseDirPath() { 471 return licenseDirPath; 472 } 473 474 478 public void setLicenseDirPath(String theLicenseDirPath) { 479 licenseDirPath = theLicenseDirPath; 480 } 481 482 486 public void setShowLicense(boolean bShow) { 487 enableLicense = bShow; 488 } 489 490 494 public boolean getShowLicense() { 495 return enableLicense; 496 } 497 498 503 public String getUniqueTmpDirPath() throws IOException { 504 if (uniqueTmpDirPath == null) { 505 uniqueTmpDirPath = FileOperUtility.createUniqueTmpDir(); 506 } 507 return uniqueTmpDirPath; 508 } 509 510 516 public String getLocalizedJnlpInfo(String locale, String info) { 517 int localeIndex = getLocaleIndex(locale); 518 String ret = null; 519 if (info.compareToIgnoreCase(JnlpConstants.JNLP_FIELD_TITLE) == 0) { 520 ret = titles[localeIndex]; 521 } else if ( 522 info.compareToIgnoreCase(JnlpConstants.JNLP_FIELD_VENDOR) == 0) { 523 ret = vendors[localeIndex]; 524 } else if ( 525 info.compareToIgnoreCase(JnlpConstants.JNLP_FIELD_LICENSE) == 0) { 526 ret = licenses[localeIndex]; 527 } else if ( 528 info.compareToIgnoreCase(JnlpConstants.JNLP_FIELD_DESCRIPTION) 529 == 0) { 530 ret = descriptions[localeIndex]; 531 } 532 return ret; 533 } 534 535 540 protected int getLocaleIndex(String locale) { 541 int index = -1; 542 for (int i = 0; i < JnlpConstants.LOCALES.length; i++) { 543 if (locale.compareToIgnoreCase(JnlpConstants.LOCALES[i]) == 0) { 544 index = i; 545 break; 546 } 547 } 548 return index; 549 } 550 551 558 public void parseRemoteJnlpInfo(URL jnlp) 559 throws IOException { 560 if (jnlp == null) 561 throw new IOException ("url is null when trying to parse JnlpInfo"); 562 String localBase = FileOperUtility.createUniqueTmpDir(); 563 URL localJnlp = FileOperUtility.getRemoteResource(jnlp, localBase); 564 setResourcePath(localBase); 565 parseLocalJnlpInfo(localJnlp); 566 } 567 568 575 public void addJnlpRefFilePath(URL url, URL codebase) throws IOException { 576 String relPath = FileOperUtility.getRelativePath( 577 url.toString(), codebase.toString()); 578 579 jnlpRefFilePaths.add(relPath); 580 } 581 582 589 public void parseLocalJnlpInfo(URL jnlp) throws IOException { 590 URL codebase = null; 591 592 if (jnlp == null) 593 throw new IOException ("url is null when trying to parse JnlpInfo"); 594 595 if (resourceDirPath == null || resourceDirPath.length() <= 0) 596 throw new IOException ("resourcePath have not been set"); 597 598 try { 599 File jnlpFile = new File (jnlp.toURI()); 600 setJnlpFilePath(jnlpFile.getPath()); 601 LaunchDesc laDesc = LaunchDescFactory.buildDescriptor(jnlp); 602 codebase = laDesc.getCodebase(); 603 setJnlpFileHref(laDesc.getCanonicalHome().toString()); 604 InformationDesc infoDesc = laDesc.getInformation(); 605 setTitle(JnlpConstants.LOCALE_EN, infoDesc.getTitle()); 606 setVendor(JnlpConstants.LOCALE_EN, infoDesc.getVendor()); 607 setDescription(JnlpConstants.LOCALE_EN, 608 infoDesc.getDescription(InformationDesc.DESC_DEFAULT)); 609 610 addJnlpRefFilePaths(jnlp); 611 } catch (Exception e) { 612 e.printStackTrace(); 613 } 614 } 615 616 public void addJnlpRefFilePaths(URL jnlp) throws IOException { 617 618 if (jnlp == null) 619 throw new IOException ("url is null when trying to parse JnlpInfo"); 620 621 if (resourceDirPath == null || resourceDirPath.length() <= 0) 622 throw new IOException ("resourcePath have not been set"); 623 624 try { 625 File jnlpFile = new File (jnlp.toURI()); 626 jnlpRefFilePaths.add(jnlpFile.getAbsolutePath()); 627 LaunchDesc laDesc = LaunchDescFactory.buildDescriptor(jnlp); 628 URL codebase = laDesc.getCodebase(); 629 InformationDesc infoDesc = laDesc.getInformation(); 630 IconDesc[] iconArray = infoDesc.getIcons(); 631 for (int i = 0; i < iconArray.length; i++) { 632 URL iconURL = iconArray[i].getLocation(); 633 this.addJnlpRefFilePath(iconURL, codebase); 634 } 635 ResourcesDesc reDesc = laDesc.getResources(); 636 reDesc.visit( 637 new JDICPackagerFileRefVisitor(codebase, 638 resourceDirPath, this)); 639 } catch (Exception e) { 640 e.printStackTrace(); 641 } 642 } 643 649 public void setTitle(int index, String title) { 650 titles[index] = title; 651 } 652 653 659 public void setVendor(int index, String vendor) { 660 vendors[index] = vendor; 661 } 662 663 669 public void setDescription(int index, String description) { 670 descriptions[index] = description; 671 } 672 673 679 public void setLicense(int index, String license) { 680 licenses[index] = license; 681 } 682 683 688 public void setLocalizedInformation() 689 throws IOException { 690 File jnlpFile = null; 691 jnlpFile = new File (getJnlpFilePath()); 692 693 if (jnlpFile == null) { 694 throw new IOException ("Cannot find local jnlp file: " + 695 jnlpFile.getPath()); 696 } 697 byte[] bits; 698 try { 699 bits = LaunchDescFactory.readBytes(new FileInputStream (jnlpFile), 700 jnlpFile.length()); 701 } catch (Exception e) { 702 throw new IOException ("Exception when build localized InfoDesc " + 703 e.getMessage()); 704 } 705 706 setLocalizedInformation(bits); 707 708 for (int i = 0; i < JnlpConstants.LOCALES.length; i++) { 709 if (titles[i] == null || titles[i].length() == 0) { 710 titles[i] = titles[JnlpConstants.LOCALE_EN]; 711 } 712 713 if (vendors[i] == null || vendors[i].length() == 0) { 714 vendors[i] = vendors[JnlpConstants.LOCALE_EN]; 715 } 716 717 if (descriptions[i] == null || descriptions[i].length() == 0) { 718 descriptions[i] = descriptions[JnlpConstants.LOCALE_EN]; 719 } 720 } 721 } 722 723 private void setLocalizedInformation(byte[] bits) 724 throws IOException { 725 String source; 726 String encoding; 727 XMLNode root; 728 729 try { 730 source = XMLEncoding.decodeXML(bits); 731 } catch (Exception e) { 732 throw new IOException ("exception determining encoding of jnlp " + 733 "file: " + e.getMessage()); 734 } 735 736 try { 737 root = (new XMLParser(source)).parse(); 738 } catch (Exception e) { 739 throw new IOException ("exception parsing jnlp file " + 740 e.getMessage()); 741 } 742 743 try { 744 XMLUtils.visitElements(root, "<information>", 745 new JDICPackagerInfoElementVisitor(this)); 746 } catch (Exception e) { 747 throw new IOException ("exception creating InformationDesc " + 748 e.getMessage()); 749 } 750 } 751 } 752 753 757 class JDICPackagerFileRefVisitor implements ResourceVisitor { 758 private URL codebase = null; 759 private String localBase = null; 760 private JnlpPackageInfo pkgInfo = null; 761 762 public JDICPackagerFileRefVisitor(URL incodebase, String inlocalbase, 763 JnlpPackageInfo inpkgInfo) { 764 codebase = incodebase; 765 localBase = inlocalbase; 766 pkgInfo = inpkgInfo; 767 } 768 public void visitJARDesc(JARDesc jad) { 769 try { 770 pkgInfo.addJnlpRefFilePath(jad.getLocation(), codebase); 771 } catch (IOException ioE) { 772 ioE.printStackTrace(); 773 } 774 } 775 776 public void visitExtensionDesc(ExtensionDesc ed) { 777 String relPath = FileOperUtility.getRelativePath( 778 ed.getLocation().toString(), codebase.toString()); 779 File tmpFile = new File (pkgInfo.getResourceDirPath() + File.separator + 780 relPath); 781 try { 782 pkgInfo.addJnlpRefFilePaths(tmpFile.toURL()); 783 } catch (IOException ioE) { 784 ioE.printStackTrace(); 785 } 786 } 787 788 public void visitPropertyDesc(PropertyDesc prd) {} 789 public void visitJREDesc(JREDesc jrd) {} 790 public void visitPackageDesc(PackageDesc pad) {} 791 } 792 793 796 class JDICPackagerInfoElementVisitor extends XMLUtils.ElementVisitor { 797 private JnlpPackageInfo pkgInfo; 798 799 public JDICPackagerInfoElementVisitor(JnlpPackageInfo inpkgInfo) { 800 pkgInfo = inpkgInfo; 801 } 802 803 public void visitElement(XMLNode e) throws BadFieldException, 804 MissingFieldException { 805 String [] locales = GeneralUtil.getStringList( 806 XMLUtils.getAttribute(e, "", "locale", null)); 807 for (int i = 0; i < JnlpConstants.LOCALES.length; i++) { 808 if (matchLocale(JnlpConstants.LOCALES[i], locales)) { 809 String title = XMLUtils.getElementContents(e, "<title>"); 810 pkgInfo.setTitle(i, title); 811 String vendor = XMLUtils.getElementContents(e, "<vendor>"); 812 pkgInfo.setVendor(i, vendor); 813 String description = XMLUtils.getElementContentsWithAttribute( 814 e, "<description>", "kind", "", null); 815 pkgInfo.setDescription(i, description); 816 } 817 } 818 } 819 820 private boolean matchLocale(String locale, String [] locales) { 821 boolean match = false; 822 if (locales == null) 823 return match; 824 825 for (int i = 0; i < locales.length; i++) { 826 if (locales[i] != null && locales[i].equals(locale)) { 827 match = true; 828 break; 829 } 830 } 831 832 return match; 833 } 834 } 835 | Popular Tags |