1 11 package org.eclipse.help.internal.toc; 12 13 import java.io.*; 14 import java.net.*; 15 import java.util.Enumeration ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 import java.util.Set ; 20 import java.util.zip.*; 21 22 import org.eclipse.core.runtime.*; 23 import org.eclipse.help.*; 24 import org.eclipse.help.internal.*; 25 import org.eclipse.help.internal.util.*; 26 import org.osgi.framework.*; 27 28 31 public class DirectoryToc { 32 private String dir; 33 34 37 private Map extraTopics; 38 39 private String locale; 40 41 44 protected DirectoryToc(TocFile tocFile) { 45 this(tocFile.getPluginID(), tocFile.getLocale(), tocFile.getExtraDir()); 46 } 47 48 private DirectoryToc(String pluginID, String locale, String directory) { 49 this.locale = locale; 50 this.dir = HrefUtil.normalizeDirectoryHref(pluginID, directory); 52 53 } 54 55 63 public Map getExtraTopics() { 64 if (extraTopics == null) { 65 extraTopics = createExtraTopics(); 66 dir = null; 68 } 69 70 return extraTopics; 71 } 72 73 78 private Map createExtraTopics() { 79 Map ret = new HashMap (); 80 String pluginID = HrefUtil.getPluginIDFromHref(dir); 81 if (pluginID == null) { 82 return ret; 83 } 84 Bundle pluginDesc = Platform.getBundle(pluginID); 85 if (pluginDesc == null || pluginDesc.getState() == Bundle.INSTALLED 86 || pluginDesc.getState() == Bundle.UNINSTALLED) 87 return ret; 88 String directory = HrefUtil.getResourcePathFromHref(dir); 89 if (directory == null) { 90 directory = ""; } 93 IPath iPath = new Path("$nl$/doc.zip"); Map override = new HashMap (1); 96 override.put("$nl$", locale); URL url = FileLocator.find(pluginDesc, iPath, override); 98 if (url == null) { 99 url = FileLocator.find(pluginDesc, new Path("doc.zip"), null); } 101 if (url != null) { 102 ret.putAll(createExtraTopicsFromZip(pluginID, directory, url)); 104 } 105 106 Set paths = ResourceLocator.findTopicPaths(pluginDesc, directory, 108 locale); 109 for (Iterator it = paths.iterator(); it.hasNext();) { 110 String href = "/" + pluginID + "/" + (String ) it.next(); ret.put(href, new ExtraTopic(href)); 112 } 113 return ret; 114 } 115 116 122 private Map createExtraTopicsFromZip(String pluginID, String directory, 123 URL url) { 124 Map ret = new HashMap (0); 125 URL realZipURL; 126 try { 127 realZipURL = FileLocator.toFileURL(FileLocator.resolve(url)); 128 if (realZipURL.toExternalForm().startsWith("jar:")) { return ret; 131 } 132 } catch (IOException ioe) { 133 HelpPlugin.logError("IOException occurred, when resolving URL " + url.toString() + ".", ioe); return ret; 136 } 137 ZipFile zipFile; 138 try { 139 zipFile = new ZipFile(realZipURL.getFile()); 140 ret = createExtraTopicsFromZipFile(pluginID, zipFile, directory); 141 zipFile.close(); 142 } catch (IOException ioe) { 143 HelpPlugin.logError( 144 "IOException occurred, when accessing Zip file " + realZipURL.getFile() 146 + ". File might not be locally available.", ioe); return new HashMap (0); 148 } 149 150 return ret; 151 152 } 153 154 163 private Map createExtraTopicsFromZipFile(String pluginID, ZipFile zipFile, 164 String directory) { 165 String constantHrefSegment = "/" + pluginID + "/"; Map ret = new HashMap (); 167 for (Enumeration entriesEnum = zipFile.entries(); entriesEnum.hasMoreElements();) { 168 ZipEntry zEntry = (ZipEntry) entriesEnum.nextElement(); 169 if (zEntry.isDirectory()) { 170 continue; 171 } 172 String docName = zEntry.getName(); 173 int l = directory.length(); 174 if (l == 0 || docName.length() > l && docName.charAt(l) == '/' 175 && directory.equals(docName.substring(0, l))) { 176 String href = constantHrefSegment + docName; 177 ret.put(href, new ExtraTopic(href)); 178 } 179 } 180 return ret; 181 } 182 183 class ExtraTopic implements ITopic { 184 private String topicHref; 185 186 public ExtraTopic(String href) { 187 this.topicHref = href; 188 } 189 190 public Map getFilters() { 191 return null; 193 } 194 195 public String getHref() { 196 return topicHref; 197 } 198 199 public String getLabel() { 200 return topicHref; 201 } 202 203 public ITopic[] getSubtopics() { 204 return new ITopic[0]; 205 } 206 } 207 } 208 | Popular Tags |