import java.io.*; import org.w3c.dom.*; import org.w3c.dom.html.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; /** * Testiluokka XML-tiedoston (tai xhml-fragmentin) validointiin ja korjaukseen. * @author Miika Nurminen / 2003-11-04 */ public class HtmlFixer { /** Elementit, joiden tyhjät esiintymät 'neutralisoidaan'. Listaa pitänee jatkaa...*/ public static String [] formatElements = {"b","i","u","strong"}; /** JTidy */ public org.w3c.tidy.Tidy tidy = new org.w3c.tidy.Tidy(); // SAXParser saxParser = null; TransformerFactory tf = null; Transformer serializer = null; Document doc= null; DOMSource domSource = null; StreamResult streamResult = null; /** Creates a new instance of HtmlFixer */ public HtmlFixer() throws TransformerConfigurationException { // alustetaan XSLT-muunnokset tf = TransformerFactory.newInstance(); serializer = tf.newTransformer(); domSource = new DOMSource(); streamResult = new StreamResult(); // HTML tidyn asetukset (nämä voisi asettaa myös konfigurointitiedostosta...) tidy.setXHTML(true); // liittää mukaan doctypen ja xml-headerin tidy.setDocType("strict"); // doctype: XHTML 1.0 Strict tidy.setXmlPi(true); // tidy.setOnlyErrors(false); tidy.setTidyMark(false); tidy.setBreakBeforeBR(true); //tidy.setEncloseBlockText(true); // lisää tarpeettomia
-elementtejä. tidy.setHideEndTags(false); tidy.setCharEncoding(org.w3c.tidy.Configuration.LATIN1); tidy.setQuiet(true); } public static String xhtmlDoctype() { return ""; } public static String xhtmlHtmlElement() { return "
loppu"); v.test("kappale
ei toimi...
"); v.test(readFile(new File("short.txt"))); // sama tiedostosta v.test(readFile(new File("validointi.html"))); // html-tiedosto (2KB) v.test(readFile(new File("w3c.txt"))); // vähän isompi (10KB) tiedosto // v.test(readFile(new File("testfile.html"))); /* huomattavasti isompi (~600KB) tiedosto. * siistiminen kestää lähes 1.5 min, suurin osa ajasta menee * dropEmptyElement-metodissa. */ System.out.println("