KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > helpbrowser > HelpParser


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2002 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.helpbrowser;
20
21 import java.io.*;
22 import java.net.URL JavaDoc;
23 import javax.swing.tree.*;
24 import javax.xml.parsers.*;
25 import org.w3c.dom.*;
26
27 /**
28  * Transform xml help files to html
29  */

30 public class HelpParser
31 {
32     //-- attributes
33

34     private Document document;
35     private String JavaDoc directory;
36     
37     /**
38      * Constructor
39      *
40      * @param directory where the file is located
41      * @param filename the help file
42      */

43     public HelpParser(String JavaDoc directory, String JavaDoc filename)
44     {
45         this.directory = directory;
46         
47         try
48         {
49             DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
50             InputStream is = new URL JavaDoc(directory + filename).openStream();
51             this.document = builder.parse(is);
52             is.close();
53         }
54         catch(Exception JavaDoc e)
55         {
56             this.document = null;
57         }
58     }
59     
60     /**
61      * Get the section tree
62      *
63      * @return the sections
64      */

65     public TreeNode getSections()
66     {
67         if(this.document == null)
68             return null;
69         
70         DefaultMutableTreeNode result = new DefaultMutableTreeNode();
71         Node root = this.document.getFirstChild();
72         
73         while(root != null && root.getNodeType() != Node.ELEMENT_NODE)
74             root = root.getNextSibling();
75         
76         getSections(root, result);
77         return result;
78     }
79     
80     /**
81      * Get the HTML text corresponding to a section
82      *
83      * @param path the section
84      * @return the html string
85      */

86     public String JavaDoc htmlForSection(TreePath path)
87     {
88         if(this.document == null)
89             return null;
90         
91         String JavaDoc self = (String JavaDoc)((DefaultMutableTreeNode)path.getLastPathComponent()).
92         getUserObject();
93         String JavaDoc html = "<html>\n<head><title>";
94         html += self;
95         html += "</title></head>\n<body>\n";
96         
97         int nbelems = path.getPathCount();
98         
99         for(int i = 0; i < nbelems; i++)
100         {
101             String JavaDoc section = (String JavaDoc)((DefaultMutableTreeNode)path.getPathComponent( i)).
102             getUserObject();
103             
104             if(section != null)
105                 html += "&gt; <a HREF=\"#section:" + section + "\">" + section + "</a>";
106         }
107         
108         html += "<hr>\n";
109         html += htmlize(self);
110         html += "<p><hr>\n";
111         
112         for(int i = 0; i < nbelems; i++)
113         {
114             String JavaDoc section = (String JavaDoc)((DefaultMutableTreeNode)path.getPathComponent(i)).
115             getUserObject();
116             
117             if(section != null)
118                 html += "&gt; <a HREF=\"#section:" + section + "\">" + section + "</a>";
119         }
120         
121         html += "</body>\n</html>";
122         return html;
123     }
124     
125     /**
126      * List the sections under a node
127      *
128      * @param root the node to look at
129      * @param result the tree to fill
130      */

131     private void getSections(Node root, DefaultMutableTreeNode result)
132     {
133         Node section = root.getFirstChild();
134         DefaultMutableTreeNode current = null;
135         String JavaDoc name = null;
136         
137         while(section != null)
138         {
139             if(section.getNodeType() == Node.ELEMENT_NODE &&
140                     section.getNodeName().equals("section"))
141             {
142                 NamedNodeMap nnm = section.getAttributes();
143                 name = nnm.getNamedItem("name").getNodeValue();
144                 current = new DefaultMutableTreeNode(name);
145                 result.add(current);
146                 getSections(section, current);
147             }
148             
149             section = section.getNextSibling();
150         }
151     }
152     
153     /**
154      * Get a section node by name
155      *
156      * @param wanted the name wanted
157      * @param root the root section
158      * @return the node
159      */

160     private Node getSectionNode(String JavaDoc wanted, Node root)
161     {
162         Node result = null;
163         Node section = root.getFirstChild();
164         String JavaDoc name = null;
165         
166         while(section != null && result == null)
167         {
168             if(section.getNodeType() == Node.ELEMENT_NODE &&
169                     section.getNodeName().equals("section"))
170             {
171                 NamedNodeMap nnm = section.getAttributes();
172                 name = nnm.getNamedItem("name").getNodeValue();
173                 
174                 if(name.equals(wanted))
175                     result = section;
176                 else
177                     result = getSectionNode(wanted, section);
178             }
179             
180             section = section.getNextSibling();
181         }
182         
183         return result;
184     }
185     
186     /**
187      * Transform a section to html
188      *
189      * @param section the section name
190      * @return the html
191      */

192     private String JavaDoc htmlize(String JavaDoc section)
193     {
194         String JavaDoc html = "<h1>" + section + "</h1>\n";
195         Node root = getSectionNode(section, document.getFirstChild());
196         html += htmlize(root);
197         return html;
198     }
199     
200     /**
201      * Transform a section content to html
202      *
203      * @param root the section node
204      * @return the html
205      */

206     private String JavaDoc htmlize(Node root)
207     {
208         String JavaDoc html = "";
209         String JavaDoc text = "";
210         Node current = root.getFirstChild();
211         
212         while(current != null)
213         {
214             if(current.getNodeType() == Node.TEXT_NODE)
215             {
216                 text = current.getNodeValue();
217                 text = text.replaceAll("\n\n", "<p>");
218                 text = text.replaceAll("\r\n\r\n", "<p>");
219                 html += text;
220             }
221             else if(current.getNodeType() == Node.ELEMENT_NODE)
222             {
223                 try
224                 {
225                     if(current.getNodeName().equals("section"))
226                     {
227                         NamedNodeMap nnm = current.getAttributes();
228                         text = nnm.getNamedItem("name").getNodeValue();
229                         html += "<br>&gt; <a HREF=\"#section:" + text + "\">" + text + "</a>";
230                     }
231                     else if(current.getNodeName().equals("info"))
232                     {
233                         NamedNodeMap nnm = current.getAttributes();
234                         text = nnm.getNamedItem("text").getNodeValue();
235                         html += "<a HREF=\"#tooltip:" + text + "\">";
236                         html += htmlize(current);
237                         html += "</a>";
238                     }
239                     else if(current.getNodeName().equals("colored"))
240                     {
241                         NamedNodeMap nnm = current.getAttributes();
242                         text = nnm.getNamedItem("color").getNodeValue();
243                         html += "<font color=\"" + text + "\">";
244                         html += htmlize(current);
245                         html += "</font>";
246                     }
247                     else if(current.getNodeName().equals("b"))
248                     {
249                         html += "<b>" + htmlize(current) + "</b>";
250                     }
251                     else if(current.getNodeName().equals("br"))
252                     {
253                         html += "<br>";
254                     }
255                     else if(current.getNodeName().equals("ul"))
256                     {
257                         html += "<ul>" + htmlize(current) + "</ul>";
258                     }
259                     else if(current.getNodeName().equals("li"))
260                     {
261                         html += "<li>" + htmlize(current) + "</li>";
262                     }
263                     else if(current.getNodeName().equals("tt"))
264                     {
265                         html += "<tt>" + htmlize(current) + "</tt>";
266                     }
267                     else if(current.getNodeName().equals("i"))
268                     {
269                         html += "<i>" + htmlize(current) + "</i>";
270                     }
271                     else if(current.getNodeName().equals("table"))
272                     {
273                         String JavaDoc border = "1";
274                         
275                         try
276                         {
277                             NamedNodeMap nnm = current.getAttributes();
278                             text = nnm.getNamedItem("border").getNodeValue();
279                             
280                             if(text.equals("no"))
281                                 border = "0";
282                         }
283                         catch(Exception JavaDoc e)
284                         {
285                             //default border
286
}
287                         
288                         html += "<table border=\"" + border + "\">";
289                         html += htmlize(current);
290                         html += "</table>";
291                     }
292                     else if(current.getNodeName().equals("tr"))
293                     {
294                         html += "<tr>" + htmlize(current) + "</tr>";
295                     }
296                     else if(current.getNodeName().equals("td"))
297                     {
298                         html += "<td>" + htmlize(current) + "</td>";
299                     }
300                     else if(current.getNodeName().equals("image"))
301                     {
302                         NamedNodeMap nnm = current.getAttributes();
303                         text = nnm.getNamedItem("file").getNodeValue();
304                         html += "<img SRC=\"" + this.directory + text + "\">";
305                         html += "</img>";
306                     }
307                 }
308                 catch(Exception JavaDoc e)
309                 {
310                     //...
311
}
312             }
313             
314             current = current.getNextSibling();
315         }
316         
317         return html;
318     }
319 }
Popular Tags