001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.editor.ext.html.javadoc;
043:
044: import java.io.BufferedReader;
045: import java.io.ByteArrayOutputStream;
046: import java.io.File;
047: import java.io.IOException;
048: import java.io.InputStream;
049: import java.io.StringReader;
050: import java.net.URL;
051: import java.util.Enumeration;
052: import java.util.Hashtable;
053: import java.util.StringTokenizer;
054: import javax.xml.parsers.SAXParser;
055: import javax.xml.parsers.SAXParserFactory;
056: import org.openide.ErrorManager;
057: import org.openide.filesystems.FileUtil;
058: import org.openide.modules.InstalledFileLocator;
059:
060: /**
061: *
062: * @author Petr Pisl
063: */
064: public class HelpManager {
065:
066: private static HelpManager manager = null;
067:
068: private Hashtable helpMap;
069: private long lastChange;
070: private String helpZipURL;
071: private URL lastURL;
072:
073: /** HelpManager a new instance of HelpManager */
074: private HelpManager() {
075: helpMap = null;
076: lastChange = 0;
077: helpZipURL = null;
078: lastURL = null;
079: }
080:
081: static public HelpManager getDefault() {
082: if (manager == null) {
083: manager = new HelpManager();
084: }
085: return manager;
086: }
087:
088: private void init() {
089: if (helpMap != null)
090: return;
091: // This part of the code is for the easy way how to define config file.
092: String help = "";
093: try {
094: //File file = InstalledFileLocator.getDefault().locate("docs/HtmlHelp.xml", null, false);
095: /*File file = new File ("/space/cvs/trunk/HtmlHelp.xml");
096: if (file != null && lastChange != file.lastModified()){
097: System.out.println("Config file was changed");
098: helpMap = null;
099: lastChange = file.lastModified();
100: }*/
101: if (helpMap == null) {
102: //Parse the config file
103: InputStream in = HelpManager.class
104: .getClassLoader()
105: .getResourceAsStream(
106: "org/netbeans/editor/ext/html/javadoc/resources/HtmlHelp.xml"); //NOI18N
107: if (in == null) {
108: helpMap = new Hashtable();
109: return;
110: }
111: SAXParserFactory factory = SAXParserFactory
112: .newInstance();
113: SAXParser parser = factory.newSAXParser();
114:
115: SAXHelpHandler handler = new SAXHelpHandler();
116: java.util.Date start = new java.util.Date();
117: parser.parse(in, handler);
118: in.close();
119:
120: //parser.parse(file, handler);
121:
122: //System.out.println("Parsing config file takes " + (end.getTime() - start.getTime()));
123: help = handler.getHelpFile();
124: if (help == null || help.equals("")) {
125: help = null;
126: helpMap = new Hashtable();
127: return;
128: }
129:
130: helpMap = handler.getMap();
131:
132: String url = "";
133:
134: File f = InstalledFileLocator.getDefault().locate(help,
135: null, false); //NoI18N
136: if (f != null) {
137: try {
138: URL urll = f.toURL();
139: urll = FileUtil.getArchiveRoot(urll);
140: helpZipURL = urll.toString();
141: } catch (java.net.MalformedURLException e) {
142: ErrorManager.getDefault().notify(
143: ErrorManager.EXCEPTION, e);
144: helpMap = new Hashtable();
145: return;
146: }
147: }
148: }
149: } catch (Exception e) {
150: e.printStackTrace();
151: ErrorManager.getDefault().log(e.toString());
152: }
153: }
154:
155: public URL getRelativeHelpToLast(String link) {
156: return getRelativeURL(lastURL, link);
157: }
158:
159: public URL getRelativeURL(URL baseurl, String link) {
160: String url = baseurl.toString();
161: int index;
162: if (link.trim().charAt(0) == '#') {
163: index = url.indexOf('#');
164: if (index > -1)
165: url = url.substring(0, url.indexOf('#'));
166: url = url + link;
167: } else {
168: index = 0;
169: url = url.substring(0, url.lastIndexOf('/'));
170: while ((index = link.indexOf("../", index)) > -1) { //NOI18N
171: url = url.substring(0, url.lastIndexOf('/'));
172: link = link.substring(index + 3);
173: }
174: url = url + "/" + link; // NOI18N
175: }
176: URL newURL = null;
177: try {
178: newURL = new URL(url);
179: } catch (java.net.MalformedURLException e) {
180: ErrorManager.getDefault().log(e.toString());
181: return null;
182: }
183: return newURL;
184: }
185:
186: public String getHelp(String key) {
187: if (key == null)
188: return null;
189: return getHelp(findHelpItem(key));
190: }
191:
192: public String getHelp(TagHelpItem helpItem) {
193: URL url = getHelpURL(helpItem);
194: if (url == null)
195: return null;
196:
197: lastURL = url;
198: String help = getHelpText(url);
199: int offset = 0;
200: //String head = null;
201: if (help != null) {
202: //head = getHead(help);
203: if (helpItem.getStartText() != null) {
204: offset = help.indexOf(helpItem.getStartText());
205: if (offset > 0) {
206: offset = offset + helpItem.getStartTextOffset();
207: help = help.substring(offset);
208: }
209: }
210: if (helpItem.getEndText() != null) {
211: offset = help.indexOf(helpItem.getEndText());
212: if (offset > 0) {
213: offset = offset + helpItem.getEndTextOffset();
214: help = help.substring(0, offset);
215: }
216: }
217: } else {
218: help = "";
219: }
220: if (helpItem.getTextBefore() != null)
221: help = helpItem.getTextBefore() + help;
222: if (helpItem.getTextAfter() != null)
223: help = help + helpItem.getTextAfter();
224: //if (help.length() > 0){
225: // help = head + help + "</body></html>";
226: //}
227: return help;
228: }
229:
230: /*private String getHead(String help){
231: String head = null;
232: int index = help.indexOf ("</head>");
233: if (index > 0){
234: head = help.substring(0, index);
235: head = head + "</head><body>";
236: }
237: return head;
238: }*/
239: public String getHelpText(URL url) {
240: if (url == null)
241: return null;
242: try {
243: InputStream is = url.openStream();
244: byte buffer[] = new byte[1000];
245: ByteArrayOutputStream baos = new ByteArrayOutputStream();
246: int count = 0;
247: do {
248: count = is.read(buffer);
249: if (count > 0)
250: baos.write(buffer, 0, count);
251: } while (count > 0);
252:
253: is.close();
254: String text = baos.toString();
255: baos.close();
256: return text;
257: } catch (java.io.IOException e) {
258: e.printStackTrace();
259: return null;
260: }
261: }
262:
263: public URL getHelpURL(String key) {
264: return getHelpURL(findHelpItem(key));
265: }
266:
267: public URL getHelpURLForLink(String link) {
268: URL url = null;
269:
270: if (link != null) {
271: String surl = helpZipURL + link;
272: try {
273: url = new URL(surl);
274: } catch (java.net.MalformedURLException e) {
275: ErrorManager.getDefault().log(e.toString());
276: return null;
277: }
278: }
279:
280: return url;
281: }
282:
283: public URL getHelpURL(TagHelpItem helpItem) {
284: URL url = null;
285:
286: if (helpItem != null) {
287: String surl = helpZipURL + helpItem.getFile();
288: try {
289: url = new URL(surl);
290: } catch (java.net.MalformedURLException e) {
291: ErrorManager.getDefault().log(e.toString());
292: return null;
293: }
294: }
295:
296: return url;
297: }
298:
299: public TagHelpItem findHelpItem(String key) {
300: if (key == null)
301: return null;
302: init();
303: Object o = helpMap.get(key.toUpperCase());
304: if (o != null) {
305: TagHelpItem helpItem = (TagHelpItem) o;
306:
307: if (helpItem != null)
308: while (helpItem != null
309: && helpItem.getIdentical() != null) {
310: helpItem = (TagHelpItem) helpMap.get(helpItem
311: .getIdentical().toUpperCase());
312: }
313:
314: return helpItem;
315: }
316: return null;
317: }
318:
319: public String getHelpText(URL url, String anchor) {
320: String pattern = "<a name=\"" + anchor + "\"";
321: String text = getHelpText(url);
322: BufferedReader br = new BufferedReader(new StringReader(text));
323: String line = null;
324: StringBuffer textAfterAnchor = null;
325: int prestack = 0;
326: try {
327: while ((line = br.readLine()) != null) {
328: if (line.indexOf(pattern) != -1) {
329: //found the anchor -> cut off everything before
330: textAfterAnchor = new StringBuffer();
331: textAfterAnchor.append(line.substring(line
332: .indexOf(pattern)));
333: } else if (textAfterAnchor != null) {
334: //missing <pre> tag hack
335: if (line.indexOf("<pre") != -1)
336: prestack++;
337: if (line.indexOf("</pre") != -1)
338: prestack--;
339:
340: textAfterAnchor.append(line + "\n");
341: }
342: }
343: } catch (IOException ioe) {
344: ErrorManager.getDefault().notify(
345: ErrorManager.INFORMATIONAL, ioe);
346:
347: }
348: return textAfterAnchor == null ? null : "<html><body>"
349: + (prestack < 0 ? "<pre>" : "")
350: + textAfterAnchor.toString();
351: }
352:
353: public String getAnchorText(URL url) {
354: String link = url.toExternalForm();
355: if (link.indexOf('#') != -1)
356: return link.substring(link.indexOf('#') + 1);
357: else
358: return null;
359: }
360:
361: }
|