1 11 package org.eclipse.help.internal.toc; 12 13 import java.io.IOException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.HashMap ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.zip.ZipEntry ; 23 import java.util.zip.ZipFile ; 24 25 import org.eclipse.core.runtime.FileLocator; 26 import org.eclipse.core.runtime.IPath; 27 import org.eclipse.core.runtime.Path; 28 import org.eclipse.core.runtime.Platform; 29 import org.eclipse.help.internal.HelpPlugin; 30 import org.eclipse.help.internal.util.ResourceLocator; 31 import org.osgi.framework.Bundle; 32 33 public class DocumentFinder { 34 35 public static String [] collectExtraDocuments(TocFile tocFile) { 36 String dir = HrefUtil.normalizeDirectoryHref(tocFile.getPluginId(), tocFile.getExtraDir()); 37 String locale = tocFile.getLocale(); 38 39 List result = new ArrayList (); 40 String pluginID = HrefUtil.getPluginIDFromHref(dir); 41 if (pluginID == null) { 42 return new String [0]; 43 } 44 Bundle pluginDesc = Platform.getBundle(pluginID); 45 if (pluginDesc == null || pluginDesc.getState() == Bundle.INSTALLED 46 || pluginDesc.getState() == Bundle.UNINSTALLED) 47 return new String [0]; 48 String directory = HrefUtil.getResourcePathFromHref(dir); 49 if (directory == null) { 50 directory = ""; } 53 IPath iPath = new Path("$nl$/doc.zip"); Map override = new HashMap (1); 56 override.put("$nl$", locale); URL url = FileLocator.find(pluginDesc, iPath, override); 58 if (url == null) { 59 url = FileLocator.find(pluginDesc, new Path("doc.zip"), null); } 61 if (url != null) { 62 result.addAll(collectExtraDocumentsFromZip(pluginID, directory, url)); 64 } 65 66 Set paths = ResourceLocator.findTopicPaths(pluginDesc, directory, 68 locale); 69 for (Iterator it = paths.iterator(); it.hasNext();) { 70 String href = "/" + pluginID + "/" + (String ) it.next(); href = HrefUtil.normalizeDirectoryPath(href); 72 result.add(href); 73 } 74 return (String [])result.toArray(new String [result.size()]); 75 } 76 77 private static List collectExtraDocumentsFromZip(String pluginID, String directory, 78 URL url) { 79 List result = new ArrayList (); 80 URL realZipURL; 81 try { 82 realZipURL = FileLocator.toFileURL(FileLocator.resolve(url)); 83 if (realZipURL.toExternalForm().startsWith("jar:")) { return result; 86 } 87 } catch (IOException ioe) { 88 HelpPlugin.logError("IOException occurred, when resolving URL " + url.toString() + ".", ioe); return result; 91 } 92 ZipFile zipFile; 93 try { 94 zipFile = new ZipFile (realZipURL.getFile()); 95 result = createExtraTopicsFromZipFile(pluginID, zipFile, directory); 96 zipFile.close(); 97 } catch (IOException ioe) { 98 HelpPlugin.logError( 99 "IOException occurred, when accessing Zip file " + realZipURL.getFile() 101 + ". File might not be locally available.", ioe); return new ArrayList (); 103 } 104 return result; 105 } 106 107 private static List createExtraTopicsFromZipFile(String pluginID, ZipFile zipFile, 108 String directory) { 109 String constantHrefSegment = "/" + pluginID + "/"; List result = new ArrayList (); 111 for (Enumeration entriesEnum = zipFile.entries(); entriesEnum.hasMoreElements();) { 112 ZipEntry zEntry = (ZipEntry ) entriesEnum.nextElement(); 113 if (zEntry.isDirectory()) { 114 continue; 115 } 116 String docName = zEntry.getName(); 117 int l = directory.length(); 118 if (l == 0 || docName.length() > l && docName.charAt(l) == '/' 119 && directory.equals(docName.substring(0, l))) { 120 String href = constantHrefSegment + docName; 121 result.add(href); 122 } 123 } 124 return result; 125 } 126 } 127 | Popular Tags |