KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nu > xom > tools > XHTMLJavaDoc


1 /* Copyright 2004 Elliotte Rusty Harold
2    
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.
6    
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.
11    
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
16    
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/
20 */

21    
22 package nu.xom.tools;
23
24 import java.io.File JavaDoc;
25 import java.io.FileFilter JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.OutputStream JavaDoc;
29
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;
36 import nu.xom.Nodes;
37 import nu.xom.ParsingException;
38 import nu.xom.Serializer;
39
40
41 /**
42  * <p>
43  * This class converts standard Sun JavaDoc to well-formed
44  * XHTML. It requires the use of John Cowan's TagSoup.
45  * </p>
46  *
47  * @author Elliotte Rusty Harold
48  * @version 1.0
49  *
50  */

51 class XHTMLJavaDoc {
52     
53     private static Builder builder
54       = new Builder(new org.ccil.cowan.tagsoup.Parser(),
55          false, new HTMLFixFactory());
56
57
58     private static class HTMLFilter implements FileFilter JavaDoc {
59
60         public boolean accept(File JavaDoc pathname) {
61             if (pathname.getName().endsWith(".html")) return true;
62             if (pathname.isDirectory()) return true;
63             return false;
64         }
65
66     }
67     
68     
69     public static void main(String JavaDoc[] args) {
70         
71         try {
72             File JavaDoc indir = new File JavaDoc(args[0]);
73             process(indir);
74         }
75         catch (Exception JavaDoc ex) {
76             ex.printStackTrace();
77         }
78         
79     }
80
81
82     private static void process(File JavaDoc indir) {
83         
84         FileFilter JavaDoc htmlfilter = new HTMLFilter();
85         if (indir.exists() && indir.isDirectory()) {
86             File JavaDoc[] files = indir.listFiles(htmlfilter);
87             for (int i = 0; i < files.length; i++) {
88                 File JavaDoc f = files[i];
89                 if (f.isDirectory()) {
90                     process(f);
91                 }
92                 else {
93                     try {
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);
111                         }
112                         Serializer serializer = new HTMLSerializer(new FileOutputStream JavaDoc(f));
113                         serializer.write(doc);
114                         serializer.flush();
115                     }
116                     catch (ParsingException ex) {
117                         ex.printStackTrace();
118                     }
119                     catch (IOException JavaDoc ex) {
120                         ex.printStackTrace();
121                     }
122                 }
123             }
124         }
125         else {
126             System.err.println("Could not locate source directory: " + indir);
127         }
128         
129     }
130     
131     
132     private static class HTMLFixFactory extends NodeFactory {
133         
134         public Nodes finishMakingElement(Element element) {
135             
136             if (element.getLocalName().equals("i")) {
137                 element.setLocalName("span");
138                 element.addAttribute(new Attribute("style", "font-style: italic"));
139             }
140             else if (element.getLocalName().equals("b")) {
141                 element.setLocalName("span");
142                 element.addAttribute(new Attribute("style", "font-weight: bold"));
143             }
144                 
145             return new Nodes(element);
146             
147         }
148         
149     }
150     
151     
152     private static class HTMLSerializer extends Serializer {
153         
154         HTMLSerializer(OutputStream JavaDoc out) {
155             super(out);
156         }
157         
158         protected void writeXMLDeclaration() {
159         }
160         
161         protected void writeEmptyElementTag(Element element)
162           throws IOException JavaDoc {
163              super.writeStartTag(element);
164              super.writeEndTag(element);
165         }
166         
167     }
168     
169     
170 }
171
Popular Tags