KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > ext > html > javadoc > HelpManager


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.editor.ext.html.javadoc;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Enumeration JavaDoc;
30 import java.util.Hashtable JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32 import javax.xml.parsers.SAXParser JavaDoc;
33 import javax.xml.parsers.SAXParserFactory JavaDoc;
34 import org.openide.ErrorManager;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.modules.InstalledFileLocator;
37
38 /**
39  *
40  * @author Petr Pisl
41  */

42 public class HelpManager {
43     
44     private static HelpManager manager = null;
45     
46     private Hashtable JavaDoc helpMap;
47     private long lastChange;
48     private String JavaDoc helpZipURL;
49     private URL JavaDoc lastURL;
50     
51     /** HelpManager a new instance of HelpManager */
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         // This part of the code is for the easy way how to define config file.
70
String JavaDoc help = "";
71         try{
72             //File file = InstalledFileLocator.getDefault().locate("docs/HtmlHelp.xml", null, false);
73
/*File file = new File ("/space/cvs/trunk/HtmlHelp.xml");
74             if (file != null && lastChange != file.lastModified()){
75                 System.out.println("Config file was changed");
76                 helpMap = null;
77                 lastChange = file.lastModified();
78             }*/

79             if (helpMap == null){
80                 //Parse the config file
81
InputStream JavaDoc in = HelpManager.class.getClassLoader()
82                 .getResourceAsStream("org/netbeans/editor/ext/html/javadoc/resources/HtmlHelp.xml"); //NOI18N
83
if (in == null){
84                     helpMap = new Hashtable JavaDoc();
85                     return;
86                 }
87                 SAXParserFactory JavaDoc factory = SAXParserFactory.newInstance();
88                 SAXParser JavaDoc parser = factory.newSAXParser();
89                 
90                 SAXHelpHandler handler = new SAXHelpHandler();
91                 java.util.Date JavaDoc start = new java.util.Date JavaDoc();
92                 parser.parse(in, handler);
93                 in.close();
94                 
95                 //parser.parse(file, handler);
96

97                 //System.out.println("Parsing config file takes " + (end.getTime() - start.getTime()));
98
help = handler.getHelpFile();
99                 if (help == null || help.equals("")){
100                     help = null;
101                     helpMap = new Hashtable JavaDoc();
102                     return;
103                 }
104                 
105                 helpMap = handler.getMap();
106                 
107                 String JavaDoc url="";
108                 
109                 File JavaDoc f = InstalledFileLocator.getDefault().locate(help, null, false); //NoI18N
110
if (f != null){
111                     try {
112                         URL JavaDoc urll = f.toURL();
113                         urll = FileUtil.getArchiveRoot(urll);
114                         helpZipURL = urll.toString();
115                     } catch (java.net.MalformedURLException JavaDoc e){
116                         ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
117                         helpMap = new Hashtable JavaDoc();
118                         return;
119                     }
120                 }
121             }
122         } catch (Exception JavaDoc e){
123             e.printStackTrace();
124             ErrorManager.getDefault().log(e.toString());
125         }
126     }
127     
128     public URL JavaDoc getRelativeHelpToLast(String JavaDoc link){
129         return getRelativeURL(lastURL, link);
130     }
131     
132     public URL JavaDoc getRelativeURL(URL JavaDoc baseurl, String JavaDoc link){
133         String JavaDoc 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){ //NOI18N
144
url = url.substring(0, url.lastIndexOf('/'));
145                 link = link.substring(index+3);
146             }
147             url = url + "/" + link; // NOI18N
148
}
149         URL JavaDoc newURL = null;
150         try{
151             newURL = new URL JavaDoc(url);
152         } catch (java.net.MalformedURLException JavaDoc e){
153             ErrorManager.getDefault().log(e.toString());
154             return null;
155         }
156         return newURL;
157     }
158     
159     public String JavaDoc getHelp(String JavaDoc key){
160         if (key == null) return null;
161         return getHelp(findHelpItem(key));
162     }
163     
164     public String JavaDoc getHelp(TagHelpItem helpItem){
165         URL JavaDoc url = getHelpURL(helpItem);
166         if (url == null)
167             return null;
168         
169         lastURL = url;
170         String JavaDoc help = getHelpText(url);
171         int offset = 0;
172         //String head = null;
173
if (help != null){
174             //head = getHead(help);
175
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         //if (help.length() > 0){
197
// help = head + help + "</body></html>";
198
//}
199
return help;
200     }
201     
202     /*private String getHead(String help){
203         String head = null;
204         int index = help.indexOf ("</head>");
205         if (index > 0){
206             head = help.substring(0, index);
207             head = head + "</head><body>";
208         }
209         return head;
210     }*/

211     public String JavaDoc getHelpText(URL JavaDoc url){
212         if (url == null )
213             return null;
214         try{
215             InputStream JavaDoc is = url.openStream();
216             byte buffer[] = new byte[1000];
217             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc text = baos.toString();
226             baos.close();
227             return text;
228         } catch (java.io.IOException JavaDoc e){
229             e.printStackTrace();
230             return null;
231         }
232     }
233     
234     public URL JavaDoc getHelpURL(String JavaDoc key){
235         return getHelpURL(findHelpItem(key));
236     }
237     
238     public URL JavaDoc getHelpURLForLink(String JavaDoc link) {
239         URL JavaDoc url = null;
240         
241         if(link != null){
242             String JavaDoc surl = helpZipURL + link;
243             try{
244                 url = new URL JavaDoc(surl);
245             } catch (java.net.MalformedURLException JavaDoc e){
246                 ErrorManager.getDefault().log(e.toString());
247                 return null;
248             }
249         }
250         
251         return url;
252     }
253     
254     public URL JavaDoc getHelpURL(TagHelpItem helpItem){
255         URL JavaDoc url = null;
256         
257         if(helpItem != null){
258             String JavaDoc surl = helpZipURL + helpItem.getFile();
259             try{
260                 url = new URL JavaDoc(surl);
261             } catch (java.net.MalformedURLException JavaDoc e){
262                 ErrorManager.getDefault().log(e.toString());
263                 return null;
264             }
265         }
266         
267         return url;
268     }
269     
270     
271     public TagHelpItem findHelpItem(String JavaDoc key){
272         if (key == null) return null;
273         init();
274         Object JavaDoc 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 JavaDoc getHelpText(URL JavaDoc url, String JavaDoc anchor) {
289         String JavaDoc pattern = "<a name=\"" + anchor + "\"";
290         String JavaDoc text = getHelpText(url);
291         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new StringReader JavaDoc(text));
292         String JavaDoc line = null;
293         StringBuffer JavaDoc textAfterAnchor = null;
294         int prestack = 0;
295         try {
296             while((line = br.readLine()) != null) {
297                 if(line.indexOf(pattern) != -1) {
298                     //found the anchor -> cut off everything before
299
textAfterAnchor = new StringBuffer JavaDoc();
300                     textAfterAnchor.append(line.substring(line.indexOf(pattern)));
301                 } else if(textAfterAnchor != null) {
302                     //missing <pre> tag hack
303
if(line.indexOf("<pre") != -1) prestack++;
304                     if(line.indexOf("</pre") != -1) prestack--;
305                     
306                     textAfterAnchor.append(line+"\n");
307                 }
308             }
309         }catch(IOException JavaDoc 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 JavaDoc getAnchorText(URL JavaDoc url) {
317         String JavaDoc link = url.toExternalForm();
318         if(link.indexOf('#') != -1) return link.substring(link.indexOf('#') + 1);
319         else return null;
320     }
321     
322 }
323
Popular Tags