KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xtpdoc > NavigationItem


1 /*
2  * Copyright (c) 1998-2000 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

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 JavaDoc;
39 import java.io.PrintWriter JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 public class NavigationItem {
44   private static final Logger JavaDoc log
45     = Logger.getLogger(NavigationItem.class.getName());
46
47   private Navigation _navigation;
48   private NavigationItem _parent;
49
50   private String JavaDoc _uri;
51
52   private int _maxDepth = 3;
53   private int _depth;
54   private String JavaDoc _link;
55   private String JavaDoc _title;
56   private String JavaDoc _product;
57   private Document _document;
58   private String JavaDoc _description;
59   private ContentItem _fullDescription;
60   private boolean _atocDescend;
61   private Navigation _child;
62   private ArrayList JavaDoc<NavigationItem> _items = new ArrayList JavaDoc<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 JavaDoc<NavigationItem> getChildren()
134   {
135     return _items;
136   }
137
138   void addChildren(ArrayList JavaDoc<NavigationItem> children)
139   {
140     _items.addAll(children);
141   }
142
143   String JavaDoc 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       // System.out.println("LINK: " + linkPath);
159

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 JavaDoc e) {
171           log.info("error configuring " + linkPath + ": " + e);
172         } catch (Exception JavaDoc 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 JavaDoc 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   /*
204   public ContentItem createDescription()
205   {
206     _fullDescription = new FormattedText(_document);
207     return _fullDescription;
208   }
209   */

210
211   public void setATOCDescend(boolean atocDescend)
212   {
213     _atocDescend = atocDescend;
214   }
215
216   public void setLink(String JavaDoc link)
217   {
218     _link = link;
219     _uri = _navigation.getUri() + link;
220   }
221
222   public String JavaDoc getLink()
223   {
224     return _link;
225   }
226
227   public void setTitle(String JavaDoc title)
228   {
229     _title = title;
230   }
231
232   public void setDescription(String JavaDoc description)
233   {
234     _description = description;
235   }
236
237   public void setProduct(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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(); // a
303
}
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(); // a
319
}
320
321       out.writeEndElement(); // b
322

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(); // dt
331
}
332
333     out.writeStartElement("dd");
334
335     // XXX: brief/paragraph/none
336
if (_fullDescription != null && depth + styleDepth <= 1) {
337       out.writeStartElement("p");
338       _fullDescription.writeHtml(out);
339       out.writeEndElement(); // p
340
}
341
342     if (_link != null) {
343       int p = _link.lastIndexOf('/');
344       String JavaDoc tail;
345       if (p >= 0)
346         tail = path + _link.substring(0, p + 1);
347       else
348         tail = path;
349       
350       String JavaDoc 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(); // dd
368
}
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(); // a
397

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(); // a
408
}
409
410   public void writeLaTeX(PrintWriter JavaDoc writer)
411     throws IOException JavaDoc
412   {
413   }
414 }
415
Popular Tags