1 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 ; 37 import javax.xml.stream.XMLStreamException; 38 import javax.xml.stream.XMLStreamWriter; 39 import java.io.IOException ; 40 import java.io.PrintWriter ; 41 import java.util.ArrayList ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 public class Document { 46 private static Logger log = Logger.getLogger(ResinDocServlet.class.getName()); 47 48 private ServletContext _webApp; 49 private Header _header; 50 private Body _body; 51 private Path _documentPath; 52 private String _contextPath; 53 private String _uri; 54 private int _level; 55 private Navigation _navigation; 56 private NavigationItem _navItem; 57 private String _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 contextPath) 66 { 67 this(null, documentPath, contextPath, null, "utf-8"); 68 } 69 70 public Document(ServletContext webApp, 71 Path documentPath, 72 String contextPath, 73 String uri, 74 String encoding) 75 { 76 _webApp = webApp; 77 _documentPath = documentPath; 78 _contextPath = contextPath; 79 _uri = uri; 80 _encoding = encoding; 81 } 82 83 public Path getRealPath(String uri) 84 { 85 if (_webApp != null) { 86 String 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 <Navigation> navList = new ArrayList (); 103 104 String uri = _uri; 105 106 int p = uri.lastIndexOf('/'); 107 if (p > 0) 108 uri = uri.substring(0, p + 1); 109 110 ServletContext rootWebApp = _webApp.getContext("/"); 111 112 NavigationItem child = null; 113 114 while (! uri.equals("")) { 115 String 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 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 link = navItem.getLink(); 177 178 if (link.indexOf('/') > 0) { 179 ServletContext rootWebApp = _webApp.getContext("/"); 180 String uri = navItem.getUri(); 181 String 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 } catch (Exception e) { 207 e.printStackTrace(); 208 log.log(Level.FINE, e.toString(), e); 209 210 navigation = null; 211 } 212 213 219 } 220 } 221 } 222 223 public Path getDocumentPath() 224 { 225 return _documentPath; 226 } 227 228 public String getContextPath() 229 { 230 return _contextPath; 231 } 232 233 public String getURI() 234 { 235 return _uri; 236 } 237 238 public Header getHeader() 239 { 240 return _header; 241 } 242 243 public String getName() 244 { 245 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 273 274 out.writeStartElement("html"); 275 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(); } 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 out) 296 throws IOException 297 { 298 out.println("\\documentclass{article}"); 299 300 _header.writeLaTeXTop(out); 301 _body.writeLaTeXTop(out); 302 } 303 304 public void writeLaTeX(PrintWriter out) 305 throws IOException 306 { 307 _header.writeLaTeX(out); 308 _body.writeLaTeX(out); 309 } 310 311 public void writeLaTeXEnclosed(PrintWriter out) 312 throws IOException 313 { 314 _header.writeLaTeXEnclosed(out); 315 _body.writeLaTeXEnclosed(out); 316 } 317 318 public String toString() 319 { 320 return "Document[" + _documentPath + "]"; 321 } 322 } 323 | Popular Tags |