package com.alethis.xml;

import org.xml.sax.*;
import org.xml.sax.helpers.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.sax.*; 
import java.io.*;

public class GenerateUsingTransformerHandler {
    
    private final static String OUTPUT_FILE = "C:\\project\\xml\\out.xml";
    
    public final static void main(String[] args) {   
        try {

            File f = new File(OUTPUT_FILE);
            FileWriter fw = new FileWriter(f);
            BufferedWriter bw = new BufferedWriter(fw);
            StreamResult result = new StreamResult(bw);
            
            SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
            TransformerHandler handler = tf.newTransformerHandler();
            Transformer transformer = handler.getTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT,"yes");
            handler.setResult(result);
            
            new GeneratorUsingTransformerHandler(handler).generate();
            
            System.out.println("DONE");
        }
        catch (TransformerConfigurationException e) {
            System.out.println("Transformer Configuration Exception: " + e.getMessage());
        }
        catch (TransformerException e) {
            System.out.println("Transformer Exception: " + e.getMessage());
        }
        catch (SAXException e) {
            System.out.println("SAX Exception: " + e.getMessage());
        }           
        catch (IOException e) {
            System.out.println("IO Exception: " + e.getMessage());
        }           
    }
}

class GeneratorUsingTransformerHandler {
    
    private final static Attributes EMPTY_ATTRS = new AttributesImpl();
    
    private final static String FAMILY = "family";
    private final static String FATHER = "father";
    private final static String MOTHER = "mother";
    private final static String CHILDREN = "children";
    private final static String CHILD = "child";
    
    private final static String AGE = "age";
    
    private TransformerHandler handler;
    
    public GeneratorUsingTransformerHandler(TransformerHandler handler) {
        this.handler = handler;
    }
    
    public void generate() throws SAXException {
        handler.startDocument();
        
        handler.startElement("", FAMILY, FAMILY, EMPTY_ATTRS);
        
        addElement(FATHER, "Michael J.", "49");
        addElement(MOTHER, "Margaret A.", "46");
        
        handler.startElement("", CHILDREN, CHILDREN, EMPTY_ATTRS);
        addElement(CHILD, "Chloe C.", "12");
        addElement(CHILD, "Adrian K.", "10");
        handler.endElement("", CHILDREN, CHILDREN);
        
        handler.endElement("", FAMILY, FAMILY);

        handler.endDocument();
    }
    
    private void addElement(String element, String name, String age) throws SAXException {
        AttributesImpl ageAttrs = new AttributesImpl();
        ageAttrs.addAttribute("", AGE, AGE, "CDATA", age);
        handler.startElement("", element, element, ageAttrs);
        handler.characters(name.toCharArray(), 0, name.length());
        handler.endElement("", element, element);
    }
}