/* 
 * section:  Tree
 * synopsis: Creates a tree
 * purpose:  Shows how to create document, nodes and dump it to stdout or file.
 * usage:    tree2 <filename>  -Default output: stdout
 * test:     tree2 > tree2.tmp ; diff tree2.tmp tree2.res ; rm tree2.tmp
 * author:   Lucas Brasilino <brasilino@recife.pe.gov.br>
 * copy:     see Copyright for the status of this software
 */

#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

#if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)

unsigned char*
convert (unsigned char *in, char *encoding);
/*
 *To compile this file using gcc you can type
 *gcc `xml2-config --cflags --libs` -o tree2 tree2.c
 gcc `xml2-config --cflags --libs` -o test test2.c
 */

/* A simple example how to create DOM. Libxml2 automagically 
 * allocates the necessary amount of memory to it.
*/
int
main(int argc, char **argv)
{
    xmlDocPtr doc = NULL;       /* document pointer */
    xmlNodePtr root_node = NULL, param = NULL, agent = NULL, titre = NULL, table = NULL, tligne = NULL, tlibelle = NULL, row = NULL, libelle = NULL;/* node pointers */
    xmlDtdPtr dtd = NULL;       /* DTD pointer */
	
	xmlDocPtr doc2 = NULL;       /* document pointer */
	xmlNodePtr root_node2 = NULL, param2 = NULL, agent2 = NULL, titre2 = NULL, table2 = NULL, tligne2 = NULL, tlibelle2 = NULL, row2 = NULL, libelle2 = NULL;/* node pointers */
	
	xmlDocPtr doc3 = NULL;       /* document pointer */
	xmlNodePtr root_node3 = NULL, param3 = NULL, agent3 = NULL, titre3 = NULL, table3 = NULL, tligne3 = NULL, tlibelle3 = NULL, row3 = NULL, libelle3 = NULL;/* node pointers */
    char buff[256];
    int i, j, numPage=1;

    LIBXML_TEST_VERSION;

    /* 
     * Creates a new document, a node and set it as a root node
     */
    doc = xmlNewDoc(BAD_CAST "1.0");
    root_node = xmlNewNode(NULL, BAD_CAST "root");
    xmlDocSetRootElement(doc, root_node);

    /*
     * Creates a DTD declaration. Isn't mandatory. 
     */
    /*dtd = xmlCreateIntSubset(doc, BAD_CAST "root", NULL, BAD_CAST "tree2.dtd");*/

    /* 
     * xmlNewChild() creates a new node, which is "attached" as child node
     * of root_node node. 
     */
    param = xmlNewChild(root_node, NULL, BAD_CAST "param",
                NULL);

	/* NOEUD PARAM/AGENT */
    agent = xmlNewChild(param, NULL, BAD_CAST "agent", NULL);

	xmlNewChild(agent, NULL, BAD_CAST "identifiant", BAD_CAST "Identifiant :");
	xmlNewChild(agent, NULL, BAD_CAST "nom", BAD_CAST "Nom : USER");
	xmlNewChild(agent, NULL, BAD_CAST "prenom", BAD_CAST "Prenom : Profil");
	
	/*xmlAddChild(param, agent);*/
	
	xmlNewChild(param, NULL, BAD_CAST "date", BAD_CAST "Date-heure de l'edition : 03/04/2012 15:05");
	xmlNewChild(param, NULL, BAD_CAST "page", BAD_CAST "Page");
	
	xmlNewChild(root_node, NULL, BAD_CAST "entete", BAD_CAST "Liste");
	
	/* NOEUD TITRE */
	titre = xmlNewChild(root_node, NULL, BAD_CAST "titre", NULL);
	
	xmlNewChild(titre, NULL, BAD_CAST "ligne", BAD_CAST "Liste");
	xmlNewChild(titre, NULL, BAD_CAST "ligne", BAD_CAST "journee du 03/04/2012");
	
	/* NOEUD TABLE */
	table = xmlNewChild(root_node, NULL, BAD_CAST "table", NULL);
	
	/* NOEUD TABLE/TLIGNE */
	tligne = xmlNewChild(table, NULL, BAD_CAST "tligne", NULL);
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "MEX");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "12");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "CR");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "3");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "CS");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "3");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "DET ANS");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "4");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "ECM");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "4");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "LOC");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "4");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "Cartouche REV");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "43");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "Cartouche ATS");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "19");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "Derniere REV");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "8");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "DTRF N");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "8");
	
	tlibelle = xmlNewChild(tligne, NULL, BAD_CAST "tlibelle", BAD_CAST "DTRF S");
	xmlNewProp(tlibelle, BAD_CAST "width", BAD_CAST "8");
	
	/* NOEUD TABLE/ROW */
	row = xmlNewChild(table, NULL, BAD_CAST "row", NULL);
	
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "338706922360");
	xmlNewChild(row, NULL, BAD_CAST "libelle", NULL);
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "M40");
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "2588");
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "1360");
	xmlNewChild(row, NULL, BAD_CAST "libelle", NULL);
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "05 REV UFO. 05.12.09 4 B 04 4 SUR 04");
	xmlNewChild(row, NULL, BAD_CAST "libelle", NULL);
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "09/04/2004");
	xmlNewChild(row, NULL, BAD_CAST "libelle", BAD_CAST "21/12/2013");
	xmlNewChild(row, NULL, BAD_CAST "libelle", NULL);
		
			
	
	/*xmlAddChild(root_node, titre);*/

	/*xmlAddChild(root_node, param);*/

	/* 
     * Dumping document to stdio or file
     */
    xmlSaveFormatFileEnc(argc > 1 ? argv[1] : "-", doc, "ISO-8859-1", 1);

    /*free the document */
    xmlFreeDoc(doc );
	
    /*
     *Free the global variables that may
     *have been allocated by the parser.
     */
    xmlCleanupParser();

    /*
     * this is to debug memory for regression tests
     */
    xmlMemoryDump();
    return(0);
}


#else
int main(void) {
    fprintf(stderr, "tree support not compiled in\n");
    exit(1);
}
#endif
