001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xtpdoc;
031:
032: import com.caucho.config.Config;
033: import com.caucho.vfs.Path;
034: import com.caucho.vfs.Vfs;
035:
036: import javax.servlet.ServletContext;
037: import javax.xml.stream.XMLStreamException;
038: import javax.xml.stream.XMLStreamWriter;
039: import java.io.IOException;
040: import java.io.PrintWriter;
041: import java.util.ArrayList;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: public class Document {
046: private static Logger log = Logger.getLogger(ResinDocServlet.class
047: .getName());
048:
049: private ServletContext _webApp;
050: private Header _header;
051: private Body _body;
052: private Path _documentPath;
053: private String _contextPath;
054: private String _uri;
055: private int _level;
056: private Navigation _navigation;
057: private NavigationItem _navItem;
058: private String _encoding;
059: private boolean _hasChildren;
060:
061: private String _redirect;
062:
063: Document() {
064: this (null, null, null, null, "utf-8");
065: }
066:
067: public Document(Path documentPath, String contextPath) {
068: this (null, documentPath, contextPath, null, "utf-8");
069: }
070:
071: public Document(ServletContext webApp, Path documentPath,
072: String contextPath, String uri, String encoding) {
073: _webApp = webApp;
074: _documentPath = documentPath;
075: _contextPath = contextPath;
076: _uri = uri;
077: _encoding = encoding;
078: }
079:
080: public Path getRealPath(String uri) {
081: if (_webApp != null) {
082: String contextPath = _webApp.getContextPath();
083:
084: if (uri.startsWith(contextPath))
085: uri = uri.substring(contextPath.length());
086:
087: return Vfs.lookup(_webApp.getRealPath(uri));
088: } else
089: return Vfs.lookup("./" + uri);
090: }
091:
092: NavigationItem getNavigation() {
093: if (_navItem != null)
094: return _navItem;
095:
096: ArrayList<Navigation> navList = new ArrayList();
097:
098: String uri = _uri;
099:
100: int p = uri.lastIndexOf('/');
101: if (p > 0)
102: uri = uri.substring(0, p + 1);
103:
104: ServletContext rootWebApp = _webApp.getContext("/");
105:
106: NavigationItem child = null;
107:
108: while (!uri.equals("") && rootWebApp != null) {
109: String realPath = rootWebApp.getRealPath(uri);
110:
111: Path path = Vfs.lookup(realPath);
112:
113: Path toc = path.lookup("toc.xml");
114:
115: if (toc.canRead()) {
116: Config config = new Config();
117:
118: Navigation navigation = new Navigation(this , uri, path,
119: 0);
120:
121: navigation.setChild(child);
122:
123: try {
124: config.configure(navigation, toc);
125:
126: navList.add(navigation);
127: } catch (Exception e) {
128: log.log(Level.FINE, e.toString(), e);
129:
130: navigation = null;
131: }
132:
133: if (navigation != null)
134: child = navigation.getRootItem();
135: else
136: child = null;
137: }
138:
139: p = uri.lastIndexOf('/', uri.length() - 2);
140: if (p >= 0)
141: uri = uri.substring(0, p + 1);
142: else
143: break;
144: }
145:
146: if (navList.size() > 0) {
147: Navigation nav = navList.get(0);
148:
149: _navItem = nav.getItem(_uri);
150: }
151:
152: return _navItem;
153: }
154:
155: void fillChildNavigation() {
156: getNavigation();
157:
158: if (!_hasChildren) {
159: _hasChildren = true;
160: fillChildNavigation(_navItem);
161: }
162: }
163:
164: void fillChildNavigation(NavigationItem navItem) {
165: if (navItem == null)
166: return;
167:
168: for (NavigationItem child : navItem.getChildren()) {
169: fillChildNavigation(child);
170: }
171:
172: try {
173: String link = navItem.getLink();
174:
175: if (link.indexOf('/') > 0) {
176: ServletContext rootWebApp = _webApp.getContext("/");
177: String uri = navItem.getUri();
178: String realPath = rootWebApp.getRealPath(uri);
179:
180: Path path = Vfs.lookup(realPath);
181:
182: Path pwd = path.getParent();
183: Path toc = pwd.lookup("toc.xml");
184:
185: if (toc.canRead()) {
186: Config config = new Config();
187:
188: int p = uri.lastIndexOf('/');
189: if (p > 0)
190: uri = uri.substring(0, p + 1);
191:
192: Navigation navigation = new Navigation(this , uri,
193: pwd, 0);
194:
195: navigation.setChild(navItem);
196:
197: config.configure(navigation, toc);
198:
199: if (navigation.getRootItem() != null)
200: navItem.addChildren(navigation.getRootItem()
201: .getChildren());
202:
203: //navList.add(navigation);
204:
205: /*
206: if (navigation != null)
207: child = navigation.getRootItem();
208: else
209: child = null;
210: */
211: }
212: }
213: } catch (Exception e) {
214: log.log(Level.FINE, e.toString(), e);
215: }
216: }
217:
218: public Path getDocumentPath() {
219: return _documentPath;
220: }
221:
222: public String getContextPath() {
223: return _contextPath;
224: }
225:
226: public String getURI() {
227: return _uri;
228: }
229:
230: public Header getHeader() {
231: return _header;
232: }
233:
234: public String getName() {
235: // XXX
236: return "";
237: }
238:
239: public Header createHeader() {
240: _header = new Header(this );
241: return _header;
242: }
243:
244: public Body createBody() {
245: _body = new Body(this );
246: return _body;
247: }
248:
249: public Body getBody() {
250: return _body;
251: }
252:
253: public void setRedirect(String href) {
254: _redirect = href;
255: }
256:
257: public String getRedirect() {
258: return _redirect;
259: }
260:
261: public void writeHtml(XMLStreamWriter out)
262: throws XMLStreamException {
263: out.writeStartDocument("1.0", _encoding);
264: /*
265: out.writeDTD("html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" " +
266: "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"");*/
267:
268: out.writeStartElement("html");
269: // XXX: workaround until writeNamespace gets fixed
270: out.writeAttribute("xmlns", "http://www.w3.org/1999/xhtml");
271:
272: if (_header != null)
273: _header.writeHtml(out);
274:
275: if (_body != null)
276: _body.writeHtml(out);
277:
278: out.writeEndElement(); // html
279: }
280:
281: public void writeLeftNav(XMLStreamWriter out)
282: throws XMLStreamException {
283: NavigationItem item = getNavigation();
284:
285: if (item != null) {
286: item.writeLeftNav(out);
287: }
288: }
289:
290: public void writeLaTeXTop(PrintWriter out) throws IOException {
291: out.println("\\documentclass{article}");
292:
293: _header.writeLaTeXTop(out);
294: _body.writeLaTeXTop(out);
295: }
296:
297: public void writeLaTeX(PrintWriter out) throws IOException {
298: _header.writeLaTeX(out);
299: _body.writeLaTeX(out);
300: }
301:
302: public void writeLaTeXEnclosed(PrintWriter out) throws IOException {
303: _header.writeLaTeXEnclosed(out);
304: _body.writeLaTeXEnclosed(out);
305: }
306:
307: public String toString() {
308: return "Document[" + _documentPath + "]";
309: }
310: }
|