KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > internal > HelpData


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;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.StringReader JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.HashSet JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Set JavaDoc;
21
22 import javax.xml.parsers.SAXParser JavaDoc;
23 import javax.xml.parsers.SAXParserFactory JavaDoc;
24
25 import org.eclipse.core.runtime.IProduct;
26 import org.eclipse.core.runtime.Platform;
27 import org.xml.sax.Attributes JavaDoc;
28 import org.xml.sax.InputSource JavaDoc;
29 import org.xml.sax.SAXException JavaDoc;
30 import org.xml.sax.helpers.DefaultHandler JavaDoc;
31
32 /*
33  * Interface to the help data file contents defined in plugin_customization.ini under
34  * key org.eclipse.help/HELP_DATA.
35  */

36 public class HelpData {
37
38     private static final String JavaDoc ELEMENT_TOC_ORDER = "tocOrder"; //$NON-NLS-1$
39
private static final String JavaDoc ELEMENT_HIDDEN = "hidden"; //$NON-NLS-1$
40
private static final String JavaDoc ELEMENT_TOC = "toc"; //$NON-NLS-1$
41
private static final String JavaDoc ELEMENT_CATEGORY = "category"; //$NON-NLS-1$
42
private static final String JavaDoc ELEMENT_INDEX = "index"; //$NON-NLS-1$
43
private static final String JavaDoc ATTRIBUTE_ID = "id"; //$NON-NLS-1$
44

45     private static HelpData productHelpData;
46     
47     private URL JavaDoc url;
48     private List JavaDoc tocOrder;
49     private Set JavaDoc hiddenTocs;
50     private Set JavaDoc hiddenIndexes;
51
52     /*
53      * Get the active product's help data, or null if the product doesn't have
54      * help data or there is no active product.
55      */

56     public static synchronized HelpData getProductHelpData() {
57         if (productHelpData == null) {
58             String JavaDoc helpDataFile = HelpPlugin.getDefault().getPluginPreferences().getString(HelpPlugin.HELP_DATA_KEY);
59             if (helpDataFile.length() != 0) {
60                 IProduct product = Platform.getProduct();
61                 if (product != null) {
62                     URL JavaDoc url = product.getDefiningBundle().getEntry(helpDataFile);
63                     productHelpData = new HelpData(url);
64                 }
65             }
66         }
67         return productHelpData;
68     }
69
70     /*
71      * Constructs help data from the XML at the given URL.
72      */

73     public HelpData(URL JavaDoc url) {
74         this.url = url;
75     }
76     
77     /*
78      * Returns a list of strings which are the IDs of the tocs/categories listed
79      * in tocOrder.
80      */

81     public synchronized List JavaDoc getTocOrder() {
82         if (tocOrder == null) {
83             loadHelpData();
84         }
85         return tocOrder;
86     }
87
88     /*
89      * Returns a set of strings which are the IDs of the tocs/categories listed
90      * in the hidden section.
91      */

92     public synchronized Set JavaDoc getHiddenTocs() {
93         if (hiddenTocs == null) {
94             loadHelpData();
95         }
96         return hiddenTocs;
97     }
98
99     /*
100      * Returns a set of strings which are the IDs of the indexes listed
101      * in the hidden section.
102      */

103     public synchronized Set JavaDoc getHiddenIndexes() {
104         if (hiddenIndexes == null) {
105             loadHelpData();
106         }
107         return hiddenIndexes;
108     }
109
110     /*
111      * Allow unit tests to override for providing test data.
112      */

113     public InputStream JavaDoc getHelpDataFile(String JavaDoc filePath) throws IOException JavaDoc {
114         return Platform.getProduct().getDefiningBundle().getEntry(filePath).openStream();
115     }
116
117     /*
118      * Loads and parses the file and populates the data structures.
119      */

120     private void loadHelpData() {
121         tocOrder = new ArrayList JavaDoc();
122         hiddenTocs = new HashSet JavaDoc();
123         hiddenIndexes = new HashSet JavaDoc();
124         if (url != null) {
125             try {
126                 SAXParser JavaDoc parser = SAXParserFactory.newInstance().newSAXParser();
127                 InputStream JavaDoc in = url.openStream();
128                 parser.parse(in, new Handler JavaDoc());
129             }
130             catch (Throwable JavaDoc t) {
131                 String JavaDoc msg = "Error loading help data file \"" + url + "\""; //$NON-NLS-1$ //$NON-NLS-2$
132
HelpPlugin.logError(msg, t);
133             }
134         }
135     }
136
137     /*
138      * SAX Handler for parsing the help data file.
139      */

140     private class Handler extends DefaultHandler JavaDoc {
141         
142         private boolean inTocOrder;
143         private boolean inHidden;
144         
145         /* (non-Javadoc)
146          * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
147          */

148         public void startElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name, Attributes JavaDoc attributes) throws SAXException JavaDoc {
149             if (ELEMENT_TOC_ORDER.equals(name)) {
150                 inTocOrder = true;
151             }
152             else if (ELEMENT_HIDDEN.equals(name)) {
153                 inHidden = true;
154             }
155             else if (ELEMENT_TOC.equals(name) || ELEMENT_CATEGORY.equals(name)) {
156                 String JavaDoc id = attributes.getValue(ATTRIBUTE_ID);
157                 if (id != null) {
158                     if (inTocOrder) {
159                         tocOrder.add(id);
160                     }
161                     else if (inHidden) {
162                         hiddenTocs.add(id);
163                     }
164                 }
165             }
166             else if (ELEMENT_INDEX.equals(name) && inHidden) {
167                 String JavaDoc id = attributes.getValue(ATTRIBUTE_ID);
168                 if (id != null) {
169                     hiddenIndexes.add(id);
170                 }
171             }
172         }
173         
174         /* (non-Javadoc)
175          * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
176          */

177         public void endElement(String JavaDoc uri, String JavaDoc localName, String JavaDoc name) throws SAXException JavaDoc {
178             if (ELEMENT_TOC_ORDER.equals(name)) {
179                 inTocOrder = false;
180             }
181             else if (ELEMENT_HIDDEN.equals(name)) {
182                 inHidden = false;
183             }
184         }
185         
186         /*
187          * Note: throws clause does not declare IOException due to a bug in
188          * sun jdk: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6327149
189          *
190          * @see org.xml.sax.helpers.DefaultHandler#resolveEntity(java.lang.String, java.lang.String)
191          */

192         public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc {
193             return new InputSource JavaDoc(new StringReader JavaDoc("")); //$NON-NLS-1$
194
}
195     }
196 }
197
Popular Tags