/*
 * Created on 13.11.2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package de.kksoftware.demo.xml;

import java.io.*;

import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.JDOMSource;

/**
 * @author administrator
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class JDomCreateFile {

	public static void main(String[] args) throws Exception {
		// create empty document with root element <root>
		Element oRoot = new Element("config");
		Document oDoc = new Document(oRoot);
		/*
		 * Document oDoc = new Document();
		 * oDoc.setRootElement(oRoot);
		 */
		
		// add <level1> nodes to the document:
		//  first, create an empty element and add content
		Element oName = new Element("name");
		oName.setText("myConfig");
		oRoot.addContent(oName);
		// create another element, setting the content in one step
		// and adding an attribute
		Element oPath1 = new Element("path").addContent("/usr/ingo/src");
		oPath1.setAttribute("id", "src");
		oRoot.addContent(oPath1);
		Element oPath2 = new Element("path").addContent("/tmp/ingo");
		oPath2.setAttribute("id", "tmp");
		oRoot.addContent(oPath2);
		Element oPath3 = new Element("path").addContent("/usr/ingo/webapp");
		oPath3.setAttribute("id", "webroot");
		oPath3.addContent(new Element("filter").addContent("*.jsp"));
		oPath3.addContent(new Element("filter").addContent("*.html"));
		oRoot.addContent(oPath3);

		// write XML to console and file, using four spaces for
		// indentation and newlines for line-breaking		
		XMLOutputter oOut = new XMLOutputter("    ", true);
		oOut.output(oDoc, System.out);
		oOut.output(oDoc, new FileOutputStream(new File("myconfig.xml")));
		
		// transform the created document using XSLT, write it to 
		// the console
		JDOMSource oSource = new JDOMSource(oDoc);
		StreamResult oResult = new StreamResult(System.out);
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Templates oStylesheet = transformerFactory.newTemplates(
			new StreamSource(new File("mystyle.xsl")));
		Transformer oTransformer = oStylesheet.newTransformer();
		oTransformer.transform(oSource, oResult);
	}
}

