1 11 package org.eclipse.help.internal; 12 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 import java.io.StringReader ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.HashSet ; 19 import java.util.List ; 20 import java.util.Set ; 21 22 import javax.xml.parsers.SAXParser ; 23 import javax.xml.parsers.SAXParserFactory ; 24 25 import org.eclipse.core.runtime.IProduct; 26 import org.eclipse.core.runtime.Platform; 27 import org.xml.sax.Attributes ; 28 import org.xml.sax.InputSource ; 29 import org.xml.sax.SAXException ; 30 import org.xml.sax.helpers.DefaultHandler ; 31 32 36 public class HelpData { 37 38 private static final String ELEMENT_TOC_ORDER = "tocOrder"; private static final String ELEMENT_HIDDEN = "hidden"; private static final String ELEMENT_TOC = "toc"; private static final String ELEMENT_CATEGORY = "category"; private static final String ELEMENT_INDEX = "index"; private static final String ATTRIBUTE_ID = "id"; 45 private static HelpData productHelpData; 46 47 private URL url; 48 private List tocOrder; 49 private Set hiddenTocs; 50 private Set hiddenIndexes; 51 52 56 public static synchronized HelpData getProductHelpData() { 57 if (productHelpData == null) { 58 String 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 url = product.getDefiningBundle().getEntry(helpDataFile); 63 productHelpData = new HelpData(url); 64 } 65 } 66 } 67 return productHelpData; 68 } 69 70 73 public HelpData(URL url) { 74 this.url = url; 75 } 76 77 81 public synchronized List getTocOrder() { 82 if (tocOrder == null) { 83 loadHelpData(); 84 } 85 return tocOrder; 86 } 87 88 92 public synchronized Set getHiddenTocs() { 93 if (hiddenTocs == null) { 94 loadHelpData(); 95 } 96 return hiddenTocs; 97 } 98 99 103 public synchronized Set getHiddenIndexes() { 104 if (hiddenIndexes == null) { 105 loadHelpData(); 106 } 107 return hiddenIndexes; 108 } 109 110 113 public InputStream getHelpDataFile(String filePath) throws IOException { 114 return Platform.getProduct().getDefiningBundle().getEntry(filePath).openStream(); 115 } 116 117 120 private void loadHelpData() { 121 tocOrder = new ArrayList (); 122 hiddenTocs = new HashSet (); 123 hiddenIndexes = new HashSet (); 124 if (url != null) { 125 try { 126 SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); 127 InputStream in = url.openStream(); 128 parser.parse(in, new Handler ()); 129 } 130 catch (Throwable t) { 131 String msg = "Error loading help data file \"" + url + "\""; HelpPlugin.logError(msg, t); 133 } 134 } 135 } 136 137 140 private class Handler extends DefaultHandler { 141 142 private boolean inTocOrder; 143 private boolean inHidden; 144 145 148 public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException { 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 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 id = attributes.getValue(ATTRIBUTE_ID); 168 if (id != null) { 169 hiddenIndexes.add(id); 170 } 171 } 172 } 173 174 177 public void endElement(String uri, String localName, String name) throws SAXException { 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 192 public InputSource resolveEntity(String publicId, String systemId) throws SAXException { 193 return new InputSource (new StringReader ("")); } 195 } 196 } 197 | Popular Tags |