libjava/ChangeLog:
[official-gcc.git] / libjava / classpath / tools / gnu / classpath / tools / doclets / htmldoclet / HtmlPage.java
blob0026e0b8a460d53bc301c18cd299a4ef468ebaaa
1 /* gnu.classpath.tools.doclets.htmldoclet.HtmlPage
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 package gnu.classpath.tools.doclets.htmldoclet;
23 import gnu.classpath.tools.IOToolkit;
25 import java.io.BufferedWriter;
26 import java.io.File;
27 import java.io.FileOutputStream;
28 import java.io.FileWriter;
29 import java.io.InputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.OutputStreamWriter;
33 import java.io.PrintWriter;
34 import java.io.Reader;
35 import java.io.Writer;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.Iterator;
41 import java.util.Map;
43 import com.sun.javadoc.Tag;
45 /**
46 * Allows outputting an HTML document without having to build the
47 * document tree in-memory.
49 public class HtmlPage
51 private File file;
52 private PrintWriter out;
53 private String pathToRoot;
54 private String docType;
55 private String baseUrl;
56 private File rootDir;
58 public static final String DOCTYPE_FRAMESET = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">";
60 public HtmlPage(File file, String pathToRoot, String encoding, String baseUrl, File rootDir)
61 throws IOException
63 this(file, pathToRoot, encoding, baseUrl, rootDir, "<!DOCTYPE html PUBLIC \"-//gnu.org///DTD XHTML 1.1 plus Target 1.0//EN\" \"" + pathToRoot + "/resources/xhtml11-target10.dtd\">");
66 public HtmlPage(File file, String pathToRoot, String encoding, String baseUrl, File rootDir, String docType)
67 throws IOException
69 this.file = file;
70 OutputStream fileOut = new FileOutputStream(file);
71 Writer writer;
72 if (null != encoding) {
73 writer = new OutputStreamWriter(fileOut,
74 encoding);
76 else {
77 writer = new OutputStreamWriter(fileOut);
79 this.out = new PrintWriter(new BufferedWriter(writer));
80 this.pathToRoot = pathToRoot;
81 this.docType = docType;
82 this.baseUrl = baseUrl;
83 this.rootDir = rootDir;
86 public void beginElement(String elementName)
88 print('<');
89 print(elementName);
90 print('>');
93 public void beginElement(String elementName, String attributeName, String attributeValue)
95 print('<');
96 print(elementName);
97 print(' ');
98 print(attributeName);
99 print('=');
100 print('\"');
101 print(attributeValue);
102 print('\"');
103 print('>');
106 public void beginElement(String elementName, String[] attributeNames, String[] attributeValues)
108 print('<');
109 print(elementName);
110 for (int i=0; i<attributeNames.length; ++i) {
111 if (null != attributeValues[i]) {
112 print(' ');
113 print(attributeNames[i]);
114 print('=');
115 print('\"');
116 print(attributeValues[i]);
117 print('\"');
120 print('>');
123 public void beginElement(String elementName, String attributeName, String attributeValue, String[] attributeNames, String[] attributeValues)
125 print('<');
126 print(elementName);
127 print(' ');
128 print(attributeName);
129 print('=');
130 print('\"');
131 print(attributeValue);
132 print('\"');
133 if (null != attributeNames) {
134 for (int i=0; i<attributeNames.length; ++i) {
135 if (null != attributeValues[i]) {
136 print(' ');
137 print(attributeNames[i]);
138 print('=');
139 print('\"');
140 print(attributeValues[i]);
141 print('\"');
145 print('>');
148 public void atomicElement(String elementName)
150 print('<');
151 print(elementName);
152 print("/>");
155 public void atomicElement(String elementName, String attributeName, String attributeValue)
157 print('<');
158 print(elementName);
159 print(' ');
160 print(attributeName);
161 print('=');
162 print('\"');
163 print(attributeValue);
164 print('\"');
165 print("/>");
168 public void atomicElement(String elementName, String[] attributeNames, String[] attributeValues)
170 print('<');
171 print(elementName);
172 for (int i=0; i<attributeNames.length; ++i) {
173 if (null != attributeValues[i]) {
174 print(' ');
175 print(attributeNames[i]);
176 print('=');
177 print('\"');
178 print(attributeValues[i]);
179 print('\"');
182 print("/>");
186 public void endElement(String elementName)
188 print("</");
189 print(elementName);
190 print('>');
194 public void beginDiv(CssClass cssClass)
196 String[] divAttributeNames = cssClass.getAttributeNames();
197 String[] divAttributeValues = cssClass.getAttributeValues();
198 if (null == divAttributeNames) {
199 divAttributeNames = new String[0];
201 if (null == divAttributeValues) {
202 divAttributeValues = new String[0];
205 String[] attributeNames = new String[1 + divAttributeNames.length];
206 String[] attributeValues = new String[1 + divAttributeValues.length];
208 System.arraycopy(divAttributeNames, 0, attributeNames, 1, divAttributeNames.length);
209 System.arraycopy(divAttributeValues, 0, attributeValues, 1, divAttributeNames.length);
211 attributeNames[0] = "class";
212 attributeValues[0] = cssClass.getName();
214 beginElement(cssClass.getDivElementName(), attributeNames, attributeValues);
215 if (null != cssClass.getInnerElementName()) {
216 beginElement(cssClass.getInnerElementName());
220 public void endDiv(CssClass cssClass)
222 if (null != cssClass.getInnerElementName()) {
223 endElement(cssClass.getInnerElementName());
225 endElement(cssClass.getDivElementName());
228 public void beginSpan(CssClass cssClass)
230 beginElement(cssClass.getSpanElementName(), "class", cssClass.getName());
233 public void endSpan(CssClass cssClass)
235 endElement(cssClass.getSpanElementName());
238 public void hr()
240 atomicElement("hr");
243 public void br()
245 atomicElement("br");
248 public void print(String text)
250 out.print(text);
253 public void print(char c)
255 out.print(c);
258 public void div(CssClass cssClass, String contents)
260 beginDiv(cssClass);
261 print(contents);
262 endDiv(cssClass);
265 public void span(CssClass cssClass, String contents)
267 beginSpan(cssClass);
268 print(contents);
269 endSpan(cssClass);
272 public void beginPage(String title, String charset, Map stylesheets)
273 throws IOException
275 beginPage(title, charset, Collections.EMPTY_SET, stylesheets);
278 public void beginPage(String title, String charset,
279 Collection keywords, Map stylesheets)
280 throws IOException
282 print("<?xml version=\"1.0\" encoding=\"" + charset + "\"?>\n");
283 print(docType);
284 print("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
285 beginElement("head");
286 beginElement("title");
287 print(title);
288 endElement("title");
289 if (null != baseUrl && baseUrl.length() > 0) {
290 StringBuffer url = new StringBuffer();
291 url.append(baseUrl);
292 if ('/' == url.charAt(url.length() - 1)) {
293 url.delete(url.length() - 1, url.length());
295 url.append(file.getCanonicalPath().substring(rootDir.getCanonicalPath().length()));
296 atomicElement("base",
297 new String[] { "href" },
298 new String[] { url.toString() });
300 beginElement("script",
301 new String[] { "src", "type" },
302 new String[] { pathToRoot + "/resources/gjdoc.js", "text/javascript" });
303 print("<!-- this comment required for konqueror 3.2.2 -->");
304 endElement("script");
305 atomicElement("meta",
306 new String[] { "http-equiv", "content" },
307 new String[] { "Content-Type", "text/html; charset=" + charset });
308 atomicElement("meta",
309 new String[] { "name", "content" },
310 new String[] { "generator", "GNU Gjdoc Standard Doclet" });
311 Iterator keywordIt = keywords.iterator();
312 while (keywordIt.hasNext()) {
313 String keyword = (String)keywordIt.next();
314 atomicElement("meta",
315 new String[] { "name", "content" },
316 new String[] { "keywords", keyword });
319 Iterator cssIt = stylesheets.keySet().iterator();
320 while (cssIt.hasNext()) {
321 String sheetName = (String)cssIt.next();
322 String[] sheetFiles = (String[])stylesheets.get(sheetName);
324 for (int i=0; i<sheetFiles.length; ++i) {
325 String sheetFile = sheetFiles[i];
326 atomicElement("link",
327 new String[] { "rel", "type", "href", "title" },
328 new String[] { "stylesheet", "text/css",
329 pathToRoot + "/" + sheetFile, sheetName });
333 endElement("head");
336 public void endPage()
338 endElement("html");
341 public void close()
343 out.close();
346 public void beginTable(CssClass cssClass)
348 beginElement("table", "class", cssClass.getName());
351 public void beginTable(CssClass cssClass, String[] attributeNames, String[] attributeValues)
353 beginElement("table", "class", cssClass.getName(), attributeNames, attributeValues);
356 public void beginRow()
358 beginElement("tr");
361 public void beginRow(CssClass cssClass)
363 beginElement("tr", "class", cssClass.getName(), cssClass.getAttributeNames(), cssClass.getAttributeValues());
366 public void beginRow(String attribute, String value)
368 beginElement("tr", attribute, value);
371 public void beginCell()
373 beginElement("td");
376 public void beginCell(String attribute, String value)
378 beginElement("td", attribute, value);
381 public void beginCell(CssClass cssClass)
383 beginElement("td", "class", cssClass.getName(), cssClass.getAttributeNames(), cssClass.getAttributeValues());
386 public void endCell()
388 endElement("td");
391 public void cell(CssClass cssClass, String contents)
393 beginCell(cssClass);
394 print(contents);
395 endCell();
398 public void endRow()
400 endElement("tr");
403 public void rowDiv(CssClass cssClass, String contents)
405 beginRow(cssClass);
406 beginCell("colspan", "2");
407 beginDiv(cssClass);
408 print(contents);
409 endDiv(cssClass);
410 endCell();
411 endRow();
414 public void endTable()
416 endElement("table");
419 public void beginAnchor(String href)
421 beginElement("a", "href", href);
424 public void beginAnchor(String href, String title)
426 beginElement("a",
427 new String[] { "href", "title" },
428 new String[] { href, title });
431 public void beginAnchor(String href, String title, String target)
433 beginElement("a",
434 new String[] { "href", "title", "target" },
435 new String[] { href, title, target });
438 public void endAnchor()
440 endElement("a");
443 public void anchor(String href, String label)
445 beginAnchor(href);
446 print(label);
447 endAnchor();
450 public void anchorName(String name)
452 atomicElement("a", new String[] { "name", "id" }, new String[] { name, name });
455 public String getPathToRoot()
457 return pathToRoot;
460 public void beginBody(CssClass cssClass)
462 beginBody(cssClass, true);
465 public void beginBody(CssClass cssClass, boolean setTitle)
467 if (setTitle) {
468 beginElement("body",
469 new String[] { "class", "onload" },
470 new String[] { cssClass.getName(), "if(parent.contentPageLoaded)parent.contentPageLoaded(document.title)" }
473 else {
474 beginElement("body",
475 new String[] { "class", "onload" },
476 new String[] { cssClass.getName(), "if(parent.contentPageLoaded)parent.contentPageLoaded()" }
481 public void endBody()
483 endElement("body");
486 public void insert(Reader in)
487 throws IOException
489 IOToolkit.copyStream(in, out);
492 public String createHrefString(String url, String content)
494 return createHrefString(url, content, null);
497 public String createHrefString(String url, String content, String title)
499 StringBuffer result = new StringBuffer();
500 result.append("<a href=\"");
501 result.append(url);
502 result.append("\"");
503 if (null != title) {
504 result.append(" title=\"");
505 result.append(title);
506 result.append("\"");
508 result.append(">");
509 result.append(content);
510 result.append("</a>");
511 return result.toString();
514 public File getFile()
516 return this.file;