/*
 *  Copyright Gergely Nagy <greg@webhejj.hu>
 *
 *  Licensed under the Apache License, Version 2.0; 
 *  you may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 */
package hu.webhejj.commons.xml;

import java.io.IOException;
import java.io.Writer;
import java.util.Stack;

/**
 * Simple indented XML Writer that only allows attribute values
 */
public class SimpleXmlWriter implements XmlWriter {

	private int indent = 4;
	private Stack<String> elements = new Stack<String>();
	private boolean isOpen = false;
	private boolean isLeaf = false;

	private Writer writer;
	
	public SimpleXmlWriter(Writer writer) throws IOException {
		this.writer = writer;
		
		writer.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
	}
	
	public void startElement(String name) throws IOException {
		
		if(isOpen) {
			writer.write(">\n");
		}
		
		indent();
		elements.push(name);
		writer.write("<");
		writer.write(name);
		isOpen = true;
		isLeaf = true;
	}

	public void endElement() throws IOException {
		
		if(elements.size() > 0) {

			String element = elements.pop();
			
			if(isOpen) {
				if(isLeaf) {
					writer.write("/>\n");
				} else {
					writer.write(">\n");
				}
			} else { 
				indent();
				writer.write("</");
				writer.write(element);
				writer.write(">\n");
			}
			isOpen = false;
			isLeaf = false;
		}
	}

	public void writeAttribute(String name, String value) throws IOException {
		writer.write(" ");
		writer.write(name);
		writer.write("=\"");
		writer.write(value);
		writer.write("\"");
	}
	
	public void close() throws IOException {
		while(elements.size() > 0) {
			endElement();
		}
		writer.close();
	}

	public void flush() throws IOException {
		writer.flush();
	}

	protected void indent() throws IOException {
		for(int i = 0; i < elements.size(); i++) {
			for(int j = 0; j < indent; j++) {
				writer.write(' ');
			}
		}
	}
}

@@ -272,22 +272,119 @@
 java.io.
-Writer
+OutputStream;%0Aimport java.io.OutputStreamWriter;%0Aimport java.io.Writer;%0Aimport java.nio.charset.Charset
 ;%0Aimport
@@ -724,29 +724,31 @@
 lWriter(
-Writer writer
+OutputStream os
 ) throws
@@ -778,22 +778,68 @@
 riter = 
-writer
+new OutputStreamWriter(os, Charset.forName(%22UTF-8%22))
 ;%0A%09%09%0A%09%09w
@@ -1603,39 +1603,39 @@
 te(%22 %22);%0A%09%09write
-r.write
+Escaped
 (name);%0A%09%09writer
@@ -1660,45 +1660,494 @@
 rite
-r.write(value);%0A%09%09writer.write(%22%5C%22%22);
+Escaped(value);%0A%09%09writer.write(%22%5C%22%22);%0A%09%7D%0A%09%0A%09public void writeEscaped(String string) throws IOException %7B%0A%09%09if(string == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%09%09for(int i = 0; i %3C string.length(); i++) %7B%0A%09%09%09char c = string.charAt(i);%0A%09%09%09switch(c) %7B%0A%09%09%09case '%3C': writer.write(%22&lt;%22); break;%0A%09%09%09case '%3E': writer.write(%22&gt;%22); break;%0A%09%09%09case '&': writer.write(%22&amp;%22); break;%0A%09%09%09case '%22': writer.write(%22&quot;%22); break;%0A%09%09%09case '%5C'': writer.write(%22&apos;%22); break;%0A%09%09%09default: writer.write(c);%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A

Fix SimpleXMLWriter: make sure output is UTF-8 and properly xml-escaped.