1 19 20 package org.netbeans.editor.ext.html.javadoc; 21 22 import java.io.BufferedReader ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.StringReader ; 28 import java.net.URL ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.StringTokenizer ; 32 import javax.xml.parsers.SAXParser ; 33 import javax.xml.parsers.SAXParserFactory ; 34 import org.openide.ErrorManager; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.modules.InstalledFileLocator; 37 38 42 public class HelpManager { 43 44 private static HelpManager manager = null; 45 46 private Hashtable helpMap; 47 private long lastChange; 48 private String helpZipURL; 49 private URL lastURL; 50 51 52 private HelpManager() { 53 helpMap = null; 54 lastChange = 0; 55 helpZipURL = null; 56 lastURL = null; 57 } 58 59 static public HelpManager getDefault(){ 60 if (manager == null){ 61 manager = new HelpManager(); 62 } 63 return manager; 64 } 65 66 private void init(){ 67 if (helpMap != null) 68 return; 69 String help = ""; 71 try{ 72 79 if (helpMap == null){ 80 InputStream in = HelpManager.class.getClassLoader() 82 .getResourceAsStream("org/netbeans/editor/ext/html/javadoc/resources/HtmlHelp.xml"); if (in == null){ 84 helpMap = new Hashtable (); 85 return; 86 } 87 SAXParserFactory factory = SAXParserFactory.newInstance(); 88 SAXParser parser = factory.newSAXParser(); 89 90 SAXHelpHandler handler = new SAXHelpHandler(); 91 java.util.Date start = new java.util.Date (); 92 parser.parse(in, handler); 93 in.close(); 94 95 97 help = handler.getHelpFile(); 99 if (help == null || help.equals("")){ 100 help = null; 101 helpMap = new Hashtable (); 102 return; 103 } 104 105 helpMap = handler.getMap(); 106 107 String url=""; 108 109 File f = InstalledFileLocator.getDefault().locate(help, null, false); if (f != null){ 111 try { 112 URL urll = f.toURL(); 113 urll = FileUtil.getArchiveRoot(urll); 114 helpZipURL = urll.toString(); 115 } catch (java.net.MalformedURLException e){ 116 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 117 helpMap = new Hashtable (); 118 return; 119 } 120 } 121 } 122 } catch (Exception e){ 123 e.printStackTrace(); 124 ErrorManager.getDefault().log(e.toString()); 125 } 126 } 127 128 public URL getRelativeHelpToLast(String link){ 129 return getRelativeURL(lastURL, link); 130 } 131 132 public URL getRelativeURL(URL baseurl, String link){ 133 String url = baseurl.toString(); 134 int index; 135 if (link.trim().charAt(0) == '#'){ 136 index = url.indexOf('#'); 137 if (index > -1) 138 url = url.substring(0,url.indexOf('#')); 139 url = url + link; 140 } else { 141 index = 0; 142 url = url.substring(0, url.lastIndexOf('/')); 143 while ((index = link.indexOf("../", index)) > -1){ url = url.substring(0, url.lastIndexOf('/')); 145 link = link.substring(index+3); 146 } 147 url = url + "/" + link; } 149 URL newURL = null; 150 try{ 151 newURL = new URL (url); 152 } catch (java.net.MalformedURLException e){ 153 ErrorManager.getDefault().log(e.toString()); 154 return null; 155 } 156 return newURL; 157 } 158 159 public String getHelp(String key){ 160 if (key == null) return null; 161 return getHelp(findHelpItem(key)); 162 } 163 164 public String getHelp(TagHelpItem helpItem){ 165 URL url = getHelpURL(helpItem); 166 if (url == null) 167 return null; 168 169 lastURL = url; 170 String help = getHelpText(url); 171 int offset = 0; 172 if (help != null){ 174 if (helpItem.getStartText() != null){ 176 offset = help.indexOf(helpItem.getStartText()); 177 if (offset > 0){ 178 offset = offset + helpItem.getStartTextOffset(); 179 help = help.substring(offset); 180 } 181 } 182 if (helpItem.getEndText() != null){ 183 offset = help.indexOf(helpItem.getEndText()); 184 if (offset > 0 ) { 185 offset = offset + helpItem.getEndTextOffset(); 186 help = help.substring(0, offset); 187 } 188 } 189 } else { 190 help = ""; 191 } 192 if (helpItem.getTextBefore() != null) 193 help = helpItem.getTextBefore() + help; 194 if (helpItem.getTextAfter() != null) 195 help = help + helpItem.getTextAfter(); 196 return help; 200 } 201 202 211 public String getHelpText(URL url){ 212 if (url == null ) 213 return null; 214 try{ 215 InputStream is = url.openStream(); 216 byte buffer[] = new byte[1000]; 217 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 218 int count = 0; 219 do { 220 count = is.read(buffer); 221 if (count > 0) baos.write(buffer, 0, count); 222 } while (count > 0); 223 224 is.close(); 225 String text = baos.toString(); 226 baos.close(); 227 return text; 228 } catch (java.io.IOException e){ 229 e.printStackTrace(); 230 return null; 231 } 232 } 233 234 public URL getHelpURL(String key){ 235 return getHelpURL(findHelpItem(key)); 236 } 237 238 public URL getHelpURLForLink(String link) { 239 URL url = null; 240 241 if(link != null){ 242 String surl = helpZipURL + link; 243 try{ 244 url = new URL (surl); 245 } catch (java.net.MalformedURLException e){ 246 ErrorManager.getDefault().log(e.toString()); 247 return null; 248 } 249 } 250 251 return url; 252 } 253 254 public URL getHelpURL(TagHelpItem helpItem){ 255 URL url = null; 256 257 if(helpItem != null){ 258 String surl = helpZipURL + helpItem.getFile(); 259 try{ 260 url = new URL (surl); 261 } catch (java.net.MalformedURLException e){ 262 ErrorManager.getDefault().log(e.toString()); 263 return null; 264 } 265 } 266 267 return url; 268 } 269 270 271 public TagHelpItem findHelpItem(String key){ 272 if (key == null) return null; 273 init(); 274 Object o = helpMap.get(key.toUpperCase()); 275 if (o != null){ 276 TagHelpItem helpItem = (TagHelpItem)o; 277 278 if (helpItem != null) 279 while (helpItem != null && helpItem.getIdentical() != null){ 280 helpItem = (TagHelpItem)helpMap.get(helpItem.getIdentical().toUpperCase()); 281 } 282 283 return helpItem; 284 } 285 return null; 286 } 287 288 public String getHelpText(URL url, String anchor) { 289 String pattern = "<a name=\"" + anchor + "\""; 290 String text = getHelpText(url); 291 BufferedReader br = new BufferedReader (new StringReader (text)); 292 String line = null; 293 StringBuffer textAfterAnchor = null; 294 int prestack = 0; 295 try { 296 while((line = br.readLine()) != null) { 297 if(line.indexOf(pattern) != -1) { 298 textAfterAnchor = new StringBuffer (); 300 textAfterAnchor.append(line.substring(line.indexOf(pattern))); 301 } else if(textAfterAnchor != null) { 302 if(line.indexOf("<pre") != -1) prestack++; 304 if(line.indexOf("</pre") != -1) prestack--; 305 306 textAfterAnchor.append(line+"\n"); 307 } 308 } 309 }catch(IOException ioe ) { 310 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 311 312 } 313 return textAfterAnchor == null ? null : "<html><body>" + (prestack < 0 ? "<pre>" : "") + textAfterAnchor.toString(); 314 } 315 316 public String getAnchorText(URL url) { 317 String link = url.toExternalForm(); 318 if(link.indexOf('#') != -1) return link.substring(link.indexOf('#') + 1); 319 else return null; 320 } 321 322 } 323 | Popular Tags |