package ru.k0r0tk0ff.main;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

/**
 * Class for connect to db, and generate xml file from db's data
 *
 */

public class XmlGenerator {

    /**
     * method for generate XML document
     */

    String generateDocument(ResultSet resultSet) throws SQLException, ParserConfigurationException, FileNotFoundException {

        String dataXml;

        /**
         * Convert to string db Query Result
         */
        List<String> dbQueryResult = new ArrayList();

        while (resultSet.next()) {
            dbQueryResult.add(resultSet.getString(1));
        }

        /**
         * Generate xml
         */
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.newDocument();

        Element entries = document.createElement("entries");
        document.appendChild(entries);

        // for debug
        //System.out.println( String.format(("Arraylist's dimension is %s"),dbQueryResult.size()));

        for (int i=0; i<dbQueryResult.size() ; i++) {
            Element entry = document.createElement("entry");
            Element field = document.createElement("field");

            Text text = document.createTextNode(dbQueryResult.get(i));
            entries.appendChild(entry);
            entry.appendChild(field);
            field.appendChild(text);
        }


        DOMImplementation impl = document.getImplementation();
        DOMImplementationLS implLS = (DOMImplementationLS)impl.getFeature("LS", "3.0");
        LSSerializer ser = implLS.createLSSerializer();
        ser.getDomConfig().setParameter("format-pretty-print", true);

        LSOutput out = implLS.createLSOutput();
        out.setEncoding("UTF-8");
        System.out.println("Convert to UTF-8 encoding success.");
        out.setByteStream(new FileOutputStream( new File("1.xml")));
        System.out.println("Create file \"1.xml\" success.");
        ser.write(document, out);

        String a = ser.writeToString(document);

        System.out.println("SEE SUBSTRING");
        System.out.println("-------------------------------------");
        System.out.println(a.substring(a.indexOf(System.getProperty("line.separator"))+1));
        System.out.println("-------------------------------------");

        //dataXml = ser.writeToString(document);
        // dataXml = a.substring(a.indexOf(System.getProperty("line.separator"))+1);
         dataXml = a.substring(2);



        //------------------------------

       /* XStream xstream = new XStream();

        List<String> entries = new ArrayList();

        while (resultSet.next()) {
            entries.add(String.valueOf(resultSet.getInt(1)));

        }*/

        // Change "list" to "entries"
        //xstream.alias("entries", List.class);

        // Change "String" to "entry"
        //xstream.alias("entry", java.lang.String.class);

        // Create XML document
        //String dataXml = xstream.toXML(entries);

        return dataXml;

        //System.out.println("\nPreview of xml output: ");
        //System.out.println(dataXml);

    }


}





@@ -22,16 +22,53 @@
 main;%0A%0A%0A
+import java.io.BufferedOutputStream;%0A
 import j
@@ -893,24 +893,26 @@
  %7B%0A%0A        
+//
 String dataX
@@ -1180,32 +1180,59 @@
  xml%0A         */
+%0A%0A        //create document
 %0A        Documen
@@ -1407,24 +1407,54 @@
 ocument();%0A%0A
+        //create root element%0A
         Elem
@@ -1660,24 +1660,172 @@
 .size()));%0A%0A
+        //create elements with values%0A        //     %3Centry%3E%0A        //          %3Cfield%3Evalue=dbQueryResult.get(i)%3C/field%3E%0A        //      %3C/entry%3E%0A
         for 
@@ -2186,16 +2186,76 @@
      %7D%0A%0A
+        //serialise and set encoding %22UTF-8%22. Use DOM method
 %0A       
@@ -2368,16 +2368,17 @@
 ationLS)
+ 
 impl.get
@@ -2674,24 +2674,25 @@
 ess.%22);%0A
+%0A
         
 out.setB
@@ -2687,748 +2687,152 @@
     
-out.setByteStream(new FileOutputStream( new File(%221.xml%22)));%0A        System.out.println(%22Create file %5C%221.xml%5C%22 success.%22);%0A        ser.write(document, out);%0A%0A        String a = ser.writeToString(document);%0A%0A        System.out.println(%22SEE SUBSTRING%22);%0A        System.out.println(%22-------------------------------------%22);%0A        System.out.println(a.substring(a.indexOf(System.getProperty(%22line.separator%22))+1));%0A        System.out.println(%22-------------------------------------%22);%0A%0A        //dataXml = ser.writeToString(document);%0A        // dataXml = a.substring(a.indexOf(System.getProperty(%22line.separator%22))+1);%0A         dataXml = a.substring(2);%0A%0A%0A%0A        //------------------------------%0A%0A       /* XStream xstream = new X
+// use BufferedOutputStream for more fast write to file%0A        out.setByteStream(new BufferedOutputStream%0A                (new FileOutput
 Stream
-();%0A
 %0A   
@@ -2840,352 +2840,117 @@
     
-List%3CString%3E entries = new ArrayList();%0A%0A        while (resultSet.next()) %7B%0A            entries.add(String.valueOf(resultSet.getInt(1
+                (new File(%221.xml%22)
 )));%0A
-%0A 
        
-%7D*/%0A%0A        // Change %22list%22 to %22entries%22%0A        //xstream.alias(%22entries%22, List.class);%0A%0A        // Change %22String%22 to %22entry%22%0A        //xstream.alias(%22entry%22, java.lang.String.class
+ System.out.println(%22Create file %5C%221.xml%5C%22 success.%22
 );%0A
-%0A
         
 // C
@@ -2949,30 +2949,34 @@
     
-// Create XML document
+ser.write(document, out);%0A
 %0A   
@@ -2978,31 +2978,24 @@
 %0A%0A        //
-String 
 dataXml = xs
@@ -2996,165 +2996,88 @@
 l = 
-xstream.toXML(entries);%0A%0A        return dataXml;%0A%0A        //System.out.println(%22%5CnPreview of xml output: %22);%0A        //System.out.println(dataXml
+ser.writeToString(document);%0A%0A        return ser.writeToString(document
 );%0A
-%0A
     %7D%0A
-%0A%0A
 %7D%0A%0A%0A

save file with first string - use buffered reader