KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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.IOException JavaDoc;
14 import java.net.URL JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.zip.ZipEntry JavaDoc;
23 import java.util.zip.ZipFile JavaDoc;
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 JavaDoc[] collectExtraDocuments(TocFile tocFile) {
36         String JavaDoc dir = HrefUtil.normalizeDirectoryHref(tocFile.getPluginId(), tocFile.getExtraDir());
37         String JavaDoc locale = tocFile.getLocale();
38         
39         List JavaDoc result = new ArrayList JavaDoc();
40         String JavaDoc pluginID = HrefUtil.getPluginIDFromHref(dir);
41         if (pluginID == null) {
42             return new String JavaDoc[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 JavaDoc[0];
48         String JavaDoc directory = HrefUtil.getResourcePathFromHref(dir);
49         if (directory == null) {
50             // the root - all files in a zip should be indexed
51
directory = ""; //$NON-NLS-1$
52
}
53         // Find doc.zip file
54
IPath iPath = new Path("$nl$/doc.zip"); //$NON-NLS-1$
55
Map JavaDoc override = new HashMap JavaDoc(1);
56         override.put("$nl$", locale); //$NON-NLS-1$
57
URL JavaDoc url = FileLocator.find(pluginDesc, iPath, override);
58         if (url == null) {
59             url = FileLocator.find(pluginDesc, new Path("doc.zip"), null); //$NON-NLS-1$
60
}
61         if (url != null) {
62             // collect topics from doc.zip file
63
result.addAll(collectExtraDocumentsFromZip(pluginID, directory, url));
64         }
65         
66         // Find topics in plugin
67
Set JavaDoc paths = ResourceLocator.findTopicPaths(pluginDesc, directory,
68                 locale);
69         for (Iterator JavaDoc it = paths.iterator(); it.hasNext();) {
70             String JavaDoc href = "/" + pluginID + "/" + (String JavaDoc) it.next(); //$NON-NLS-1$//$NON-NLS-2$
71
href = HrefUtil.normalizeDirectoryPath(href);
72             result.add(href);
73         }
74         return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
75     }
76     
77     private static List JavaDoc collectExtraDocumentsFromZip(String JavaDoc pluginID, String JavaDoc directory,
78             URL JavaDoc url) {
79         List JavaDoc result = new ArrayList JavaDoc();
80         URL JavaDoc realZipURL;
81         try {
82             realZipURL = FileLocator.toFileURL(FileLocator.resolve(url));
83             if (realZipURL.toExternalForm().startsWith("jar:")) { //$NON-NLS-1$
84
// doc.zip not allowed in jarred plug-ins.
85
return result;
86             }
87         } catch (IOException JavaDoc ioe) {
88             HelpPlugin.logError("IOException occurred, when resolving URL " //$NON-NLS-1$
89
+ url.toString() + ".", ioe); //$NON-NLS-1$
90
return result;
91         }
92         ZipFile JavaDoc zipFile;
93         try {
94             zipFile = new ZipFile JavaDoc(realZipURL.getFile());
95             result = createExtraTopicsFromZipFile(pluginID, zipFile, directory);
96             zipFile.close();
97         } catch (IOException JavaDoc ioe) {
98             HelpPlugin.logError(
99                     "IOException occurred, when accessing Zip file " //$NON-NLS-1$
100
+ realZipURL.getFile()
101                             + ". File might not be locally available.", ioe); //$NON-NLS-1$
102
return new ArrayList JavaDoc();
103         }
104         return result;
105     }
106     
107     private static List JavaDoc createExtraTopicsFromZipFile(String JavaDoc pluginID, ZipFile JavaDoc zipFile,
108             String JavaDoc directory) {
109         String JavaDoc constantHrefSegment = "/" + pluginID + "/"; //$NON-NLS-1$ //$NON-NLS-2$
110
List JavaDoc result = new ArrayList JavaDoc();
111         for (Enumeration JavaDoc entriesEnum = zipFile.entries(); entriesEnum.hasMoreElements();) {
112             ZipEntry JavaDoc zEntry = (ZipEntry JavaDoc) entriesEnum.nextElement();
113             if (zEntry.isDirectory()) {
114                 continue;
115             }
116             String JavaDoc 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 JavaDoc href = constantHrefSegment + docName;
121                 result.add(href);
122             }
123         }
124         return result;
125     }
126 }
127
Popular Tags