~ubuntu-branches/ubuntu/precise/xom/precise

« back to all changes in this revision

Viewing changes to src/nu/xom/tools/XHTMLJavaDoc.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2007-11-25 15:50:40 UTC
  • Revision ID: james.westby@ubuntu.com-20071125155040-r75ikcqf1vu0cei7
Tags: upstream-1.1
ImportĀ upstreamĀ versionĀ 1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
25
import java.io.FileFilter;
 
26
import java.io.FileOutputStream;
 
27
import java.io.IOException;
 
28
import java.io.OutputStream;
 
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 {
 
59
 
 
60
        public boolean accept(File 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[] args) {
 
70
        
 
71
        try {
 
72
            File indir = new File(args[0]);
 
73
            process(indir);
 
74
        }
 
75
        catch (Exception ex) {
 
76
            ex.printStackTrace();
 
77
        }
 
78
        
 
79
    }
 
80
 
 
81
 
 
82
    private static void process(File indir) {
 
83
        
 
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++) {
 
88
                File 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(f));
 
113
                        serializer.write(doc);
 
114
                        serializer.flush();
 
115
                    } 
 
116
                    catch (ParsingException ex) {
 
117
                        ex.printStackTrace();
 
118
                    } 
 
119
                    catch (IOException 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 out) {
 
155
            super(out);
 
156
        }
 
157
        
 
158
        protected void writeXMLDeclaration() {
 
159
        }
 
160
        
 
161
        protected void writeEmptyElementTag(Element element) 
 
162
          throws IOException {
 
163
             super.writeStartTag(element);
 
164
             super.writeEndTag(element);
 
165
        }
 
166
        
 
167
    }
 
168
    
 
169
    
 
170
}