1
/* Copyright 2004 Elliotte Rusty Harold
3
This library is free software; you can redistribute it and/or modify
4
it under the terms of version 2.1 of the GNU Lesser General Public
5
License as published by the Free Software Foundation.
7
This library is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU Lesser General Public License for more details.
12
You should have received a copy of the GNU Lesser General Public
13
License along with this library; if not, write to the
14
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
15
Boston, MA 02111-1307 USA
17
You can contact Elliotte Rusty Harold by sending e-mail to
18
elharo@metalab.unc.edu. Please include the word "XOM" in the
19
subject line. The XOM home page is located at http://www.xom.nu/
25
import java.io.FileFilter;
26
import java.io.FileOutputStream;
27
import java.io.IOException;
28
import java.io.OutputStream;
30
import nu.xom.Attribute;
31
import nu.xom.Builder;
32
import nu.xom.DocType;
33
import nu.xom.Document;
34
import nu.xom.Element;
35
import nu.xom.NodeFactory;
37
import nu.xom.ParsingException;
38
import nu.xom.Serializer;
43
* This class converts standard Sun JavaDoc to well-formed
44
* XHTML. It requires the use of John Cowan's TagSoup.
47
* @author Elliotte Rusty Harold
53
private static Builder builder
54
= new Builder(new org.ccil.cowan.tagsoup.Parser(),
55
false, new HTMLFixFactory());
58
private static class HTMLFilter implements FileFilter {
60
public boolean accept(File pathname) {
61
if (pathname.getName().endsWith(".html")) return true;
62
if (pathname.isDirectory()) return true;
69
public static void main(String[] args) {
72
File indir = new File(args[0]);
75
catch (Exception ex) {
82
private static void process(File indir) {
84
FileFilter htmlfilter = new HTMLFilter();
85
if (indir.exists() && indir.isDirectory()) {
86
File[] files = indir.listFiles(htmlfilter);
87
for (int i = 0; i < files.length; i++) {
89
if (f.isDirectory()) {
94
Document doc = builder.build(f);
95
DocType doctype = new DocType("html",
96
"-//W3C//DTD XHTML 1.0 Frameset//EN",
97
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-frameset.dtd");
98
doc.setDocType(doctype);
99
Attribute en = new Attribute("lang", "en-US");
100
Attribute xmlen = new Attribute("xml:lang",
101
"http://www.w3.org/XML/1998/namespace", "en-US");
102
Element root = doc.getRootElement();
103
root.addAttribute(en);
104
root.addAttribute(xmlen);
105
Attribute version = root.getAttribute("version");
106
if (version != null) root.removeAttribute(version);
107
Element body = root.getFirstChildElement("body", "http://www.w3.org/1999/xhtml");
108
Element frameset = root.getFirstChildElement("frameset", "http://www.w3.org/1999/xhtml");
109
if (frameset != null && body != null) {
110
root.removeChild(body);
112
Serializer serializer = new HTMLSerializer(new FileOutputStream(f));
113
serializer.write(doc);
116
catch (ParsingException ex) {
117
ex.printStackTrace();
119
catch (IOException ex) {
120
ex.printStackTrace();
126
System.err.println("Could not locate source directory: " + indir);
132
private static class HTMLFixFactory extends NodeFactory {
134
public Nodes finishMakingElement(Element element) {
136
if (element.getLocalName().equals("i")) {
137
element.setLocalName("span");
138
element.addAttribute(new Attribute("style", "font-style: italic"));
140
else if (element.getLocalName().equals("b")) {
141
element.setLocalName("span");
142
element.addAttribute(new Attribute("style", "font-weight: bold"));
145
return new Nodes(element);
152
private static class HTMLSerializer extends Serializer {
154
HTMLSerializer(OutputStream out) {
158
protected void writeXMLDeclaration() {
161
protected void writeEmptyElementTag(Element element)
163
super.writeStartTag(element);
164
super.writeEndTag(element);