import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;

public class DOMOutput {
	private int iLevel = 0;
	
	public static void main(String args[]) throws Exception {
		if (args.length==1){
			new DOMOutput().run(args[0], null);
		} else {
			new DOMOutput().run(args[0], args[1]);
		}
	}
	
	private void run(String pstrURI, String pstrXSL) throws Exception {
		DocumentBuilderFactory oFactory = DocumentBuilderFactory.newInstance();
		oFactory.setValidating(false);
		DocumentBuilder oParser = oFactory.newDocumentBuilder();
		Document oDoc = oParser.getDOMImplementation().createDocument(
			"http://www.2k-software.de/javaxml/outputtest",
			"config", null);

		Element oRoot = oDoc.getDocumentElement();
		oRoot.appendChild(oDoc.createComment("This is an autogenerated configuration file"));
		Element oName = oDoc.createElement("name");
		oName.appendChild(oDoc.createTextNode("myConfig"));
		oRoot.appendChild(oName);

		Element oPath1 = oDoc.createElement("path");
		oPath1.setAttribute("id", "src");
		oPath1.appendChild(oDoc.createTextNode("/usr/ingo/src"));
		oRoot.appendChild(oPath1);

		Element oPath2 = oDoc.createElement("path");
		oPath2.setAttribute("id", "tmp");
		oPath2.appendChild(oDoc.createTextNode("/tmp/ingo"));
		oRoot.appendChild(oPath2);
		
		Element oPath3 = oDoc.createElement("path");
		oPath3.setAttribute("id", "webroot");
		oPath3.appendChild(oDoc.createTextNode("/usr/ingo/webapp"));
		Element oFilter3a = oDoc.createElement("filter");
		oFilter3a.appendChild(oDoc.createTextNode("*.jsp"));
		oPath3.appendChild(oFilter3a);
		Element oFilter3b = oDoc.createElement("filter");
		oFilter3b.appendChild(oDoc.createTextNode("*.html"));
		oPath3.appendChild(oFilter3b);
		oRoot.appendChild(oPath3);

		
		TransformerFactory oTFactory = TransformerFactory.newInstance();
   		Transformer oTransformer = null;
   		if (pstrXSL==null){
   			oTransformer = oTFactory.newTransformer();
   		} else {
   			oTransformer = oTFactory.newTransformer(new StreamSource(pstrXSL));
   		}
   		StreamResult oResult = new StreamResult(pstrURI);
   		DOMSource oSource = new DOMSource(oDoc);
   		oTransformer.transform(oSource, oResult);
	}
}

