import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import java.sql.*;
import java.io.*;

public class Turismitalud {

  public static DOMSource getDom() {
    DOMSource domsrc = null;
    try{
      Document d=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
      Element juur=d.createElement("talud");
      d.appendChild(juur);

      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      Connection cn=DriverManager.getConnection("jdbc:odbc:turismitalud", "", "");
      Statement st=cn.createStatement();

        String lause = "SELECT * FROM turismitalud";
        ResultSet rs = st.executeQuery(lause);
        while(rs.next()){
          Element talu=d.createElement("talu");
          
            Element id=d.createElement("id");
            id.appendChild(d.createTextNode(rs.getString("id")));
            talu.appendChild(id);

            Element nimi=d.createElement("nimi");
            nimi.appendChild(d.createTextNode(rs.getString("nimi")));
            talu.appendChild(nimi);

            Element web=d.createElement("web");
            web.appendChild(d.createTextNode(rs.getString("web")));
            talu.appendChild(web);

          juur.appendChild(talu);
        }

      domsrc = new DOMSource(d);
      
    }catch(Exception viga){
      viga.printStackTrace();
    }
    return domsrc;
  }

  public static void sendXMLToStream(OutputStream os) //throws IOException, ServletException
  {
    try{    
      Transformer t=TransformerFactory.newInstance().newTransformer();
      t.transform(getDom(), new StreamResult(os));
    }catch(Exception viga){
      //viga.printStackTrace(new PrintWriter(new FileOutputStream("viga.sendXMLToStream.txt")));
    }
  }

  public static void sendHTMLToStream(OutputStream os) throws Exception{
 
      Transformer tolkija=TransformerFactory.newInstance().
        newTransformer(new StreamSource("c:\\temp\\talud.xsl"));
        
      //InputStreamReader isr = new InputStreamReader();  
        
      tolkija.transform(
        getDom(), 
        new StreamResult(os)    
      );    
  }

  public static void main(String argumendid[]) throws Exception{
    sendXMLToStream(System.out);
    sendHTMLToStream(System.out);
  }

}