package com.dw.lib.test;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.tidy.Tidy;

public class jtidyTest {

	public static final void prettyPrint(Document xml) throws Exception {
		Transformer tf = TransformerFactory.newInstance().newTransformer();
		tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
		tf.setOutputProperty(OutputKeys.INDENT, "yes");
		Writer out = new StringWriter();
		tf.transform(new DOMSource(xml), new StreamResult(out));
		System.out.println(out.toString());
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Tidy tidy = new Tidy();
		tidy.setQuiet(true);
		tidy.setShowWarnings(false);
		tidy.setShowErrors(0);
		tidy.setMakeClean(true);
		try {
			String search = "stub";
			URL urlObj = new URL("http://www.dict.org/bin/Dict?Form=Dict1&Query=" + search + "&Strategy=*&Database=*");
			URLConnection con = urlObj.openConnection();

			con.setDoOutput(true); // we want the response
			con.setRequestProperty("Cookie", "myCookie=test123");
			con.connect();
			Document document = tidy.parseDOM(con.getInputStream(), null);

			Node e = document.getElementsByTagName("form").item(0);

			e.getParentNode().removeChild(e);

			document.normalize();
			prettyPrint(document);
			// System.out.println(document.toString());

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

	}
}

@@ -384,24 +384,19 @@
 org.
-w3c.dom.Document
+jsoup.Jsoup
 ;%0Aim
@@ -416,15 +416,15 @@
 dom.
-NodeLis
+Documen
 t;%0Ai
@@ -521,20 +521,22 @@
 c final 
-void
+String
  prettyP
@@ -847,41 +847,120 @@
 ;%0A%09%09
-System.out.println(out.toString()
+return (out.toString());%0A%09%7D%0A%09%0A%09public static String html2text(String html) %7B%0A%09    return Jsoup.parse(html).text(
 );%0A%09
@@ -1687,16 +1687,45 @@
 e();%0A%09%09%09
+System.out.println(html2text(
 prettyPr
@@ -1737,16 +1737,18 @@
 ocument)
+))
 ;%0A%09%09%09// 

add jsoup html to text