1 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 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 } 171
| Popular Tags
|