KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > toc > DirectoryToc


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.help.internal.toc;
12
13 import java.io.*;
14 import java.net.*;
15 import java.util.Enumeration JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 /**
29  * Toc created from files in a extra directory in a plugin.
30  */

31 public class DirectoryToc {
32     private String JavaDoc dir;
33
34     /**
35      * Map of ITopic by href String;
36      */

37     private Map JavaDoc extraTopics;
38
39     private String JavaDoc locale;
40
41     /**
42      * Constructor.
43      */

44     protected DirectoryToc(TocFile tocFile) {
45         this(tocFile.getPluginID(), tocFile.getLocale(), tocFile.getExtraDir());
46     }
47
48     private DirectoryToc(String JavaDoc pluginID, String JavaDoc locale, String JavaDoc directory) {
49         this.locale = locale;
50         // Obtain extra search directory if provided
51
this.dir = HrefUtil.normalizeDirectoryHref(pluginID, directory);
52
53     }
54
55     /**
56      * This public method is to be used after the build of TOCs is finished.
57      * With assumption that TOC model is not modifiable after the build, this
58      * method caches topics in an array and releases objects used only during
59      * build.
60      *
61      * @return Map of ITopic
62      */

63     public Map JavaDoc getExtraTopics() {
64         if (extraTopics == null) {
65             extraTopics = createExtraTopics();
66             // for memory foot print, release TocFile and dir
67
dir = null;
68         }
69
70         return extraTopics;
71     }
72
73     /**
74      * Obtains URLs of all documents inside given directory.
75      *
76      * @return Map of ITopic by href
77      */

78     private Map JavaDoc createExtraTopics() {
79         Map JavaDoc ret = new HashMap JavaDoc();
80         String JavaDoc 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 JavaDoc directory = HrefUtil.getResourcePathFromHref(dir);
89         if (directory == null) {
90             // the root - all files in a zip should be indexed
91
directory = ""; //$NON-NLS-1$
92
}
93         // Find doc.zip file
94
IPath iPath = new Path("$nl$/doc.zip"); //$NON-NLS-1$
95
Map JavaDoc override = new HashMap JavaDoc(1);
96         override.put("$nl$", locale); //$NON-NLS-1$
97
URL url = FileLocator.find(pluginDesc, iPath, override);
98         if (url == null) {
99             url = FileLocator.find(pluginDesc, new Path("doc.zip"), null); //$NON-NLS-1$
100
}
101         if (url != null) {
102             // collect topics from doc.zip file
103
ret.putAll(createExtraTopicsFromZip(pluginID, directory, url));
104         }
105         
106         // Find topics in plugin
107
Set JavaDoc paths = ResourceLocator.findTopicPaths(pluginDesc, directory,
108                 locale);
109         for (Iterator JavaDoc it = paths.iterator(); it.hasNext();) {
110             String JavaDoc href = "/" + pluginID + "/" + (String JavaDoc) it.next(); //$NON-NLS-1$//$NON-NLS-2$
111
ret.put(href, new ExtraTopic(href));
112         }
113         return ret;
114     }
115
116     /**
117      * @param directory
118      * path in the form "segment1/segment2...", "" will return names
119      * of all files in a zip
120      * @return Map of ITopic by href String
121      */

122     private Map JavaDoc createExtraTopicsFromZip(String JavaDoc pluginID, String JavaDoc directory,
123             URL url) {
124         Map JavaDoc ret = new HashMap JavaDoc(0);
125         URL realZipURL;
126         try {
127             realZipURL = FileLocator.toFileURL(FileLocator.resolve(url));
128             if (realZipURL.toExternalForm().startsWith("jar:")) { //$NON-NLS-1$
129
// doc.zip not allowed in jarred plug-ins.
130
return ret;
131             }
132         } catch (IOException ioe) {
133             HelpPlugin.logError("IOException occurred, when resolving URL " //$NON-NLS-1$
134
+ url.toString() + ".", ioe); //$NON-NLS-1$
135
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 " //$NON-NLS-1$
145
+ realZipURL.getFile()
146                             + ". File might not be locally available.", ioe); //$NON-NLS-1$
147
return new HashMap JavaDoc(0);
148         }
149
150         return ret;
151
152     }
153
154     /**
155      * Obtains names of files in a zip file that given directory in their path.
156      * Files in subdirectories are included as well.
157      *
158      * @param directory
159      * path in the form "segment1/segment2...", "" will return names
160      * of all files in a zip
161      * @return Map of ITopic by href String
162      */

163     private Map JavaDoc createExtraTopicsFromZipFile(String JavaDoc pluginID, ZipFile zipFile,
164             String JavaDoc directory) {
165         String JavaDoc constantHrefSegment = "/" + pluginID + "/"; //$NON-NLS-1$ //$NON-NLS-2$
166
Map JavaDoc ret = new HashMap JavaDoc();
167         for (Enumeration JavaDoc entriesEnum = zipFile.entries(); entriesEnum.hasMoreElements();) {
168             ZipEntry zEntry = (ZipEntry) entriesEnum.nextElement();
169             if (zEntry.isDirectory()) {
170                 continue;
171             }
172             String JavaDoc 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 JavaDoc 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 JavaDoc topicHref;
185
186         public ExtraTopic(String JavaDoc href) {
187             this.topicHref = href;
188         }
189
190         public Map JavaDoc getFilters() {
191             // extra topics can't have filters
192
return null;
193         }
194         
195         public String JavaDoc getHref() {
196             return topicHref;
197         }
198
199         public String JavaDoc getLabel() {
200             return topicHref;
201         }
202
203         public ITopic[] getSubtopics() {
204             return new ITopic[0];
205         }
206     }
207 }
208
Popular Tags