KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.caucho.vfs.Vfs;
35
36 import javax.servlet.ServletContext JavaDoc;
37 import javax.xml.stream.XMLStreamException;
38 import javax.xml.stream.XMLStreamWriter;
39 import java.io.IOException JavaDoc;
40 import java.io.PrintWriter JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 public class Document {
46   private static Logger JavaDoc log = Logger.getLogger(ResinDocServlet.class.getName());
47
48   private ServletContext JavaDoc _webApp;
49   private Header _header;
50   private Body _body;
51   private Path _documentPath;
52   private String JavaDoc _contextPath;
53   private String JavaDoc _uri;
54   private int _level;
55   private Navigation _navigation;
56   private NavigationItem _navItem;
57   private String JavaDoc _encoding;
58   private boolean _hasChildren;
59
60   Document()
61   {
62     this(null, null, null, null, "utf-8");
63   }
64
65   public Document(Path documentPath, String JavaDoc contextPath)
66   {
67     this(null, documentPath, contextPath, null, "utf-8");
68   }
69
70   public Document(ServletContext JavaDoc webApp,
71                   Path documentPath,
72                   String JavaDoc contextPath,
73                   String JavaDoc uri,
74                   String JavaDoc encoding)
75   {
76     _webApp = webApp;
77     _documentPath = documentPath;
78     _contextPath = contextPath;
79     _uri = uri;
80     _encoding = encoding;
81   }
82
83   public Path getRealPath(String JavaDoc uri)
84   {
85     if (_webApp != null) {
86       String JavaDoc contextPath = _webApp.getContextPath();
87
88       if (uri.startsWith(contextPath))
89     uri = uri.substring(contextPath.length());
90       
91       return Vfs.lookup(_webApp.getRealPath(uri));
92     }
93     else
94       return Vfs.lookup("./" + uri);
95   }
96
97   NavigationItem getNavigation()
98   {
99     if (_navItem != null)
100       return _navItem;
101     
102     ArrayList JavaDoc<Navigation> navList = new ArrayList JavaDoc();
103
104     String JavaDoc uri = _uri;
105
106     int p = uri.lastIndexOf('/');
107     if (p > 0)
108       uri = uri.substring(0, p + 1);
109
110     ServletContext JavaDoc rootWebApp = _webApp.getContext("/");
111
112     NavigationItem child = null;
113
114     while (! uri.equals("")) {
115       String JavaDoc realPath = rootWebApp.getRealPath(uri);
116       
117       Path path = Vfs.lookup(realPath);
118
119       Path toc = path.lookup("toc.xml");
120
121       if (toc.canRead()) {
122     Config config = new Config();
123
124     Navigation navigation = new Navigation(this, uri, path, 0);
125       
126     navigation.setChild(child);
127
128     try {
129       config.configure(navigation, toc);
130
131       navList.add(navigation);
132     } catch (Exception JavaDoc e) {
133       log.log(Level.FINE, e.toString(), e);
134     
135       navigation = null;
136     }
137
138     if (navigation != null)
139       child = navigation.getRootItem();
140     else
141       child = null;
142       }
143
144       p = uri.lastIndexOf('/', uri.length() - 2);
145       if (p >= 0)
146     uri = uri.substring(0, p + 1);
147       else
148     break;
149     }
150
151     if (navList.size() > 0) {
152       Navigation nav = navList.get(0);
153       
154       _navItem = nav.getItem(_uri);
155     }
156
157     return _navItem;
158   }
159
160   void fillChildNavigation()
161   {
162     getNavigation();
163
164     if (! _hasChildren) {
165       _hasChildren = true;
166       fillChildNavigation(_navItem);
167     }
168   }
169
170   void fillChildNavigation(NavigationItem navItem)
171   {
172     for (NavigationItem child : navItem.getChildren()) {
173       fillChildNavigation(child);
174     }
175     
176     String JavaDoc link = navItem.getLink();
177
178     if (link.indexOf('/') > 0) {
179       ServletContext JavaDoc rootWebApp = _webApp.getContext("/");
180       String JavaDoc uri = navItem.getUri();
181       String JavaDoc realPath = rootWebApp.getRealPath(uri);
182       
183       Path path = Vfs.lookup(realPath);
184
185       Path pwd = path.getParent();
186       Path toc = pwd.lookup("toc.xml");
187
188       if (toc.canRead()) {
189     Config config = new Config();
190
191     int p = uri.lastIndexOf('/');
192     if (p > 0)
193       uri = uri.substring(0, p + 1);
194
195     Navigation navigation = new Navigation(this, uri, pwd, 0);
196       
197     navigation.setChild(navItem);
198
199     try {
200       config.configure(navigation, toc);
201
202       if (navigation.getRootItem() != null)
203         navItem.addChildren(navigation.getRootItem().getChildren());
204
205       //navList.add(navigation);
206
} catch (Exception JavaDoc e) {
207       e.printStackTrace();
208       log.log(Level.FINE, e.toString(), e);
209     
210       navigation = null;
211     }
212
213     /*
214     if (navigation != null)
215       child = navigation.getRootItem();
216     else
217       child = null;
218     */

219       }
220     }
221   }
222
223   public Path getDocumentPath()
224   {
225     return _documentPath;
226   }
227
228   public String JavaDoc getContextPath()
229   {
230     return _contextPath;
231   }
232
233   public String JavaDoc getURI()
234   {
235     return _uri;
236   }
237
238   public Header getHeader()
239   {
240     return _header;
241   }
242
243   public String JavaDoc getName()
244   {
245     // XXX
246
return "";
247   }
248
249   public Header createHeader()
250   {
251     _header = new Header(this);
252     return _header;
253   }
254
255   public Body createBody()
256   {
257     _body = new Body(this);
258     return _body;
259   }
260
261   public Body getBody()
262   {
263     return _body;
264   }
265
266   public void writeHtml(XMLStreamWriter out)
267     throws XMLStreamException
268   {
269     out.writeStartDocument("1.0", _encoding);
270     /*
271     out.writeDTD("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " +
272                  "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");*/

273
274     out.writeStartElement("html");
275     // XXX: workaround until writeNamespace gets fixed
276
out.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
277
278     if (_header != null)
279       _header.writeHtml(out);
280     _body.writeHtml(out);
281
282     out.writeEndElement(); // html
283
}
284
285   public void writeLeftNav(XMLStreamWriter out)
286     throws XMLStreamException
287   {
288     NavigationItem item = getNavigation();
289
290     if (item != null) {
291       item.writeLeftNav(out);
292     }
293   }
294
295   public void writeLaTeXTop(PrintWriter JavaDoc out)
296     throws IOException JavaDoc
297   {
298     out.println("\\documentclass{article}");
299
300     _header.writeLaTeXTop(out);
301     _body.writeLaTeXTop(out);
302   }
303
304   public void writeLaTeX(PrintWriter JavaDoc out)
305     throws IOException JavaDoc
306   {
307     _header.writeLaTeX(out);
308     _body.writeLaTeX(out);
309   }
310
311   public void writeLaTeXEnclosed(PrintWriter JavaDoc out)
312     throws IOException JavaDoc
313   {
314     _header.writeLaTeXEnclosed(out);
315     _body.writeLaTeXEnclosed(out);
316   }
317
318   public String JavaDoc toString()
319   {
320     return "Document[" + _documentPath + "]";
321   }
322 }
323
Popular Tags