Source Code Cross Referenced for HelpManager.java in  » IDE-Netbeans » html » org » netbeans » editor » ext » html » javadoc » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Netbeans » html » org.netbeans.editor.ext.html.javadoc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.