1 29 30 package com.caucho.xtpdoc; 31 32 import com.caucho.config.Config; 33 import com.caucho.vfs.Path; 34 35 import javax.annotation.PostConstruct; 36 import javax.xml.stream.XMLStreamException; 37 import javax.xml.stream.XMLStreamWriter; 38 import java.io.IOException ; 39 import java.io.PrintWriter ; 40 import java.util.ArrayList ; 41 import java.util.logging.Logger ; 42 43 public class NavigationItem { 44 private static final Logger log 45 = Logger.getLogger(NavigationItem.class.getName()); 46 47 private Navigation _navigation; 48 private NavigationItem _parent; 49 50 private String _uri; 51 52 private int _maxDepth = 3; 53 private int _depth; 54 private String _link; 55 private String _title; 56 private String _product; 57 private Document _document; 58 private String _description; 59 private ContentItem _fullDescription; 60 private boolean _atocDescend; 61 private Navigation _child; 62 private ArrayList <NavigationItem> _items = new ArrayList <NavigationItem>(); 63 64 public NavigationItem(Navigation navigation, 65 NavigationItem parent, 66 int depth) 67 { 68 _navigation = navigation; 69 _parent = parent; 70 71 _document = navigation.getDocument(); 72 _depth = depth; 73 } 74 75 public Navigation getNavigation() 76 { 77 return _navigation; 78 } 79 80 NavigationItem getParent() 81 { 82 return _parent; 83 } 84 85 void setParent(NavigationItem parent) 86 { 87 _parent = parent; 88 } 89 90 NavigationItem getPrevious() 91 { 92 NavigationItem parent = getParent(); 93 94 if (parent != null) { 95 int p = parent._items.indexOf(this); 96 97 if (p > 0) { 98 NavigationItem ptr = _parent._items.get(p - 1); 99 100 while (ptr != null && ptr._items.size() > 0) { 101 ptr = ptr._items.get(ptr._items.size() - 1); 102 } 103 104 return ptr; 105 } 106 else 107 return parent; 108 } 109 110 return null; 111 } 112 113 NavigationItem getNext() 114 { 115 NavigationItem ptr = this; 116 NavigationItem child = null; 117 118 while (ptr != null) { 119 int p = ptr._items.indexOf(child); 120 121 if (p < 0 && ptr._items.size() > 0) 122 return ptr._items.get(0); 123 else if (p + 1 < ptr._items.size()) 124 return ptr._items.get(p + 1); 125 126 child = ptr; 127 ptr = ptr.getParent(); 128 } 129 130 return null; 131 } 132 133 ArrayList <NavigationItem> getChildren() 134 { 135 return _items; 136 } 137 138 void addChildren(ArrayList <NavigationItem> children) 139 { 140 _items.addAll(children); 141 } 142 143 String getUri() 144 { 145 return _uri; 146 } 147 148 protected void initSummary() 149 { 150 if (_child != null || _fullDescription != null) 151 return; 152 153 Path rootPath = _document.getDocumentPath().getParent(); 154 155 if (_uri != null) { 156 Path linkPath = _document.getRealPath(_uri); 157 158 160 if (linkPath.exists()) { 161 Config config = new Config(); 162 163 try { 164 config.configure(_document, linkPath); 165 166 if (_document.getHeader() != null) 167 _fullDescription = _document.getHeader().getDescription(); 168 else 169 _fullDescription = new Description(_document); 170 } catch (NullPointerException e) { 171 log.info("error configuring " + linkPath + ": " + e); 172 } catch (Exception e) { 173 log.info("error configuring " + linkPath + ": " + e); 174 } 175 176 if (_atocDescend) { 177 Path linkRoot = linkPath.getParent(); 178 179 if (linkRoot.equals(_navigation.getRootPath().getParent())) 180 return; 181 182 Path subToc = linkPath.getParent().lookup("toc.xml"); 183 184 if (subToc.exists()) { 185 _child = new Navigation(_navigation, 186 _uri, 187 linkRoot, 188 _depth + 1); 189 190 try { 191 config.configure(_child, subToc); 192 } catch (Exception e) { 193 log.info("Failed to configure " + subToc + ": " + e); 194 } 195 } else { 196 log.info(subToc + " does not exist!"); 197 } 198 } 199 } 200 } 201 } 202 203 210 211 public void setATOCDescend(boolean atocDescend) 212 { 213 _atocDescend = atocDescend; 214 } 215 216 public void setLink(String link) 217 { 218 _link = link; 219 _uri = _navigation.getUri() + link; 220 } 221 222 public String getLink() 223 { 224 return _link; 225 } 226 227 public void setTitle(String title) 228 { 229 _title = title; 230 } 231 232 public void setDescription(String description) 233 { 234 _description = description; 235 } 236 237 public void setProduct(String product) 238 { 239 _product = product; 240 } 241 242 public NavigationItem createItem() 243 { 244 NavigationItem item = new NavigationItem(_navigation, this, _depth + 1); 245 _items.add(item); 246 return item; 247 } 248 249 @PostConstruct 250 public void init() 251 { 252 _navigation.putItem(_navigation.getUri() + _link, this); 253 } 254 255 public void writeHtml(XMLStreamWriter out, String path) 256 throws XMLStreamException 257 { 258 initSummary(); 259 260 out.writeStartElement("dl"); 261 out.writeAttribute("class", "atoc-top"); 262 263 for (NavigationItem item : _items) 264 item.writeHtmlImpl(out, path, 0, 0, 3); 265 266 out.writeEndElement(); 267 } 268 269 public void writeHtml(XMLStreamWriter out, String path, 270 int depth, int styleDepth, int maxDepth) 271 throws XMLStreamException 272 { 273 if (depth + styleDepth <= 1) 274 initSummary(); 275 276 for (NavigationItem item : _items) 277 item.writeHtmlImpl(out, path, depth, styleDepth, maxDepth); 278 } 279 280 protected void writeHtmlImpl(XMLStreamWriter out, String path, 281 int depth, 282 int styleDepth, int maxDepth) 283 throws XMLStreamException 284 { 285 if (maxDepth <= depth) 286 return; 287 288 if (depth + styleDepth <= 1) 289 initSummary(); 290 291 if (_child != null && depth + 1 < maxDepth) 292 _child.initSummary(); 293 294 if (depth <= 1) { 295 out.writeStartElement("h2"); 296 out.writeAttribute("class", "section"); 297 298 if (_link != null) { 299 out.writeStartElement("a"); 300 out.writeAttribute("href", path + _link); 301 out.writeCharacters(_title); 302 out.writeEndElement(); } 304 else 305 out.writeCharacters(_title); 306 307 out.writeEndElement(); 308 } 309 else { 310 out.writeStartElement("dt"); 311 312 out.writeStartElement("b"); 313 314 if (_link != null) { 315 out.writeStartElement("a"); 316 out.writeAttribute("href", path + _link); 317 out.writeCharacters(_title); 318 out.writeEndElement(); } 320 321 out.writeEndElement(); 323 if (_fullDescription != null && depth + styleDepth <= 1) { 324 } 325 else if (_description != null && depth > 1) { 326 out.writeCharacters(" - "); 327 out.writeCharacters(_description); 328 } 329 330 out.writeEndElement(); } 332 333 out.writeStartElement("dd"); 334 335 if (_fullDescription != null && depth + styleDepth <= 1) { 337 out.writeStartElement("p"); 338 _fullDescription.writeHtml(out); 339 out.writeEndElement(); } 341 342 if (_link != null) { 343 int p = _link.lastIndexOf('/'); 344 String tail; 345 if (p >= 0) 346 tail = path + _link.substring(0, p + 1); 347 else 348 tail = path; 349 350 String depthString = (depth == 0) ? "top" : ("" + depth); 351 boolean hasDL = false; 352 353 if (_child != null || _items.size() > 0) { 354 out.writeStartElement("dl"); 355 out.writeAttribute("class", "atoc-" + (depth + 1)); 356 357 if (_child != null) 358 _child.writeHtml(out, tail, depth + 1, styleDepth, maxDepth); 359 else { 360 for (NavigationItem item : _items) 361 item.writeHtmlImpl(out, tail, depth + 1, styleDepth, maxDepth); 362 } 363 out.writeEndElement(); 364 } 365 } 366 367 out.writeEndElement(); } 369 370 public void writeLeftNav(XMLStreamWriter out) 371 throws XMLStreamException 372 { 373 if (_parent != null) { 374 _parent.writeLeftNav(out); 375 } 376 else { 377 writeLeftNavItem(out); 378 } 379 380 if (_items.size() > 0) { 381 out.writeEmptyElement("hr"); 382 383 for (NavigationItem item : _items) { 384 item.writeLeftNavItem(out); 385 } 386 } 387 } 388 389 public void writeLeftNavItem(XMLStreamWriter out) 390 throws XMLStreamException 391 { 392 out.writeStartElement("a"); 393 out.writeAttribute("href", _uri); 394 out.writeAttribute("class", "leftnav"); 395 out.writeCharacters(_title); 396 out.writeEndElement(); 398 out.writeEmptyElement("br"); 399 } 400 401 public void writeLink(XMLStreamWriter out) 402 throws XMLStreamException 403 { 404 out.writeStartElement("a"); 405 out.writeAttribute("href", _uri); 406 out.writeCharacters(_title); 407 out.writeEndElement(); } 409 410 public void writeLaTeX(PrintWriter writer) 411 throws IOException 412 { 413 } 414 } 415 | Popular Tags |