1 19 package org.lucane.applications.helpbrowser; 20 21 import java.io.*; 22 import java.net.URL ; 23 import javax.swing.tree.*; 24 import javax.xml.parsers.*; 25 import org.w3c.dom.*; 26 27 30 public class HelpParser 31 { 32 34 private Document document; 35 private String directory; 36 37 43 public HelpParser(String directory, String filename) 44 { 45 this.directory = directory; 46 47 try 48 { 49 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 50 InputStream is = new URL (directory + filename).openStream(); 51 this.document = builder.parse(is); 52 is.close(); 53 } 54 catch(Exception e) 55 { 56 this.document = null; 57 } 58 } 59 60 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 86 public String htmlForSection(TreePath path) 87 { 88 if(this.document == null) 89 return null; 90 91 String self = (String )((DefaultMutableTreeNode)path.getLastPathComponent()). 92 getUserObject(); 93 String 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 section = (String )((DefaultMutableTreeNode)path.getPathComponent( i)). 102 getUserObject(); 103 104 if(section != null) 105 html += "> <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 section = (String )((DefaultMutableTreeNode)path.getPathComponent(i)). 115 getUserObject(); 116 117 if(section != null) 118 html += "> <a HREF=\"#section:" + section + "\">" + section + "</a>"; 119 } 120 121 html += "</body>\n</html>"; 122 return html; 123 } 124 125 131 private void getSections(Node root, DefaultMutableTreeNode result) 132 { 133 Node section = root.getFirstChild(); 134 DefaultMutableTreeNode current = null; 135 String 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 160 private Node getSectionNode(String wanted, Node root) 161 { 162 Node result = null; 163 Node section = root.getFirstChild(); 164 String 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 192 private String htmlize(String section) 193 { 194 String html = "<h1>" + section + "</h1>\n"; 195 Node root = getSectionNode(section, document.getFirstChild()); 196 html += htmlize(root); 197 return html; 198 } 199 200 206 private String htmlize(Node root) 207 { 208 String html = ""; 209 String 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>> <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 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 e) 284 { 285 } 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 e) 309 { 310 } 312 } 313 314 current = current.getNextSibling(); 315 } 316 317 return html; 318 } 319 } | Popular Tags |