1 29 30 package com.caucho.xtpdoc; 31 32 import com.caucho.vfs.Path; 33 import com.caucho.vfs.Vfs; 34 import com.caucho.xml.LooseHtml; 35 import com.caucho.xml.QElement; 36 import com.caucho.xml.QName; 37 import com.caucho.xml.XmlPrinter; 38 39 import org.xml.sax.SAXException ; 40 41 import java.io.IOException ; 42 import java.io.OutputStream ; 43 44 public class LooseToStrictHtml { 45 private static void renameSections(org.w3c.dom.Node node, int level) 46 { 47 org.w3c.dom.NodeList nodeList = node.getChildNodes(); 48 49 for (int i = 0; i < nodeList.getLength(); i++) { 50 int thisLevel = level; 51 52 org.w3c.dom.Node child = nodeList.item(i); 53 54 if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 55 org.w3c.dom.Element subElement = (org.w3c.dom.Element ) child; 56 57 if (subElement.getTagName().equals("section")) { 58 ((QElement) subElement).setName(new QName("s" + level)); 59 60 thisLevel = level + 1; 61 } 62 else if (subElement.getTagName().equals("defun")) { 63 ((QElement) subElement).setName(new QName("s" + level)); 64 subElement.setAttribute("type", "defun"); 65 66 thisLevel = level + 1; 67 } 68 else if (subElement.getTagName().equals("faq")) { 69 ((QElement) subElement).setName(new QName("s" + level)); 70 subElement.setAttribute("type", "faq"); 71 72 thisLevel = level + 1; 73 } 74 } 75 76 renameSections(child, thisLevel); 77 } 78 } 79 80 private 81 static org.w3c.dom.Element getXTPDocumentElement(org.w3c.dom.Node node) 82 { 83 org.w3c.dom.NodeList nodeList = node.getChildNodes(); 84 85 for (int i = 0; i < nodeList.getLength(); i++) { 86 org.w3c.dom.Node child = nodeList.item(i); 87 88 if (child.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) { 89 org.w3c.dom.Element subElement = (org.w3c.dom.Element ) child; 90 91 if (subElement.getTagName().equals("document")) 92 return subElement; 93 } 94 95 org.w3c.dom.Element subdocument = getXTPDocumentElement(child); 96 97 if (subdocument != null) 98 return subdocument; 99 } 100 101 return null; 102 } 103 104 public static org.w3c.dom.Element looseToStrictHtml(Path path) 105 throws IOException ,SAXException 106 { 107 if (! path.exists()) 108 throw new IOException (path + " does not exist"); 109 110 org.w3c.dom.Document document = new LooseHtml().parseDocument(path); 111 112 renameSections(document.getDocumentElement(), 1); 113 114 return getXTPDocumentElement(document.getDocumentElement()); 115 } 116 117 public static void main(String []args) 118 throws Exception 119 { 120 if (args.length == 0) { 121 System.err.println("usage: " + LooseToStrictHtml.class.getName() + 122 " <xtp file>"); 123 System.exit(1); 124 } 125 126 Path path = Vfs.lookup(args[0]); 127 128 org.w3c.dom.Element xtpDocument = looseToStrictHtml(path); 129 130 OutputStream fileOut = path.openWrite(); 131 132 XmlPrinter printer = new XmlPrinter(fileOut); 133 134 printer.printXml(xtpDocument); 135 136 fileOut.close(); 137 } 138 } 139 | Popular Tags |