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)
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
21 package gnu
.classpath
.tools
.doclets
.htmldoclet
;
23 import gnu
.classpath
.tools
.IOToolkit
;
25 import java
.io
.BufferedWriter
;
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
;
43 import com
.sun
.javadoc
.Tag
;
46 * Allows outputting an HTML document without having to build the
47 * document tree in-memory.
52 private PrintWriter out
;
53 private String pathToRoot
;
54 private String docType
;
55 private String baseUrl
;
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
)
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
)
70 OutputStream fileOut
= new FileOutputStream(file
);
72 if (null != encoding
) {
73 writer
= new OutputStreamWriter(fileOut
,
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
)
93 public void beginElement(String elementName
, String attributeName
, String attributeValue
)
101 print(attributeValue
);
106 public void beginElement(String elementName
, String
[] attributeNames
, String
[] attributeValues
)
110 for (int i
=0; i
<attributeNames
.length
; ++i
) {
111 if (null != attributeValues
[i
]) {
113 print(attributeNames
[i
]);
116 print(attributeValues
[i
]);
123 public void beginElement(String elementName
, String attributeName
, String attributeValue
, String
[] attributeNames
, String
[] attributeValues
)
128 print(attributeName
);
131 print(attributeValue
);
133 if (null != attributeNames
) {
134 for (int i
=0; i
<attributeNames
.length
; ++i
) {
135 if (null != attributeValues
[i
]) {
137 print(attributeNames
[i
]);
140 print(attributeValues
[i
]);
148 public void atomicElement(String elementName
)
155 public void atomicElement(String elementName
, String attributeName
, String attributeValue
)
160 print(attributeName
);
163 print(attributeValue
);
168 public void atomicElement(String elementName
, String
[] attributeNames
, String
[] attributeValues
)
172 for (int i
=0; i
<attributeNames
.length
; ++i
) {
173 if (null != attributeValues
[i
]) {
175 print(attributeNames
[i
]);
178 print(attributeValues
[i
]);
186 public void endElement(String elementName
)
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());
248 public void print(String text
)
253 public void print(char c
)
258 public void div(CssClass cssClass
, String contents
)
265 public void span(CssClass cssClass
, String contents
)
272 public void beginPage(String title
, String charset
, Map stylesheets
)
275 beginPage(title
, charset
, Collections
.EMPTY_SET
, stylesheets
);
278 public void beginPage(String title
, String charset
,
279 Collection keywords
, Map stylesheets
)
282 print("<?xml version=\"1.0\" encoding=\"" + charset
+ "\"?>\n");
284 print("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
285 beginElement("head");
286 beginElement("title");
289 if (null != baseUrl
&& baseUrl
.length() > 0) {
290 StringBuffer url
= new StringBuffer();
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
});
336 public void endPage()
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()
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()
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()
391 public void cell(CssClass cssClass
, String contents
)
403 public void rowDiv(CssClass cssClass
, String contents
)
406 beginCell("colspan", "2");
414 public void endTable()
419 public void beginAnchor(String href
)
421 beginElement("a", "href", href
);
424 public void beginAnchor(String href
, String title
)
427 new String
[] { "href", "title" },
428 new String
[] { href
, title
});
431 public void beginAnchor(String href
, String title
, String target
)
434 new String
[] { "href", "title", "target" },
435 new String
[] { href
, title
, target
});
438 public void endAnchor()
443 public void anchor(String href
, String label
)
450 public void anchorName(String name
)
452 atomicElement("a", new String
[] { "name", "id" }, new String
[] { name
, name
});
455 public String
getPathToRoot()
460 public void beginBody(CssClass cssClass
)
462 beginBody(cssClass
, true);
465 public void beginBody(CssClass cssClass
, boolean setTitle
)
469 new String
[] { "class", "onload" },
470 new String
[] { cssClass
.getName(), "if(parent.contentPageLoaded)parent.contentPageLoaded(document.title)" }
475 new String
[] { "class", "onload" },
476 new String
[] { cssClass
.getName(), "if(parent.contentPageLoaded)parent.contentPageLoaded()" }
481 public void endBody()
486 public void insert(Reader in
)
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=\"");
504 result
.append(" title=\"");
505 result
.append(title
);
509 result
.append(content
);
510 result
.append("</a>");
511 return result
.toString();
514 public File
getFile()