// Path: src/main/java/org/dynaform/xml/XmlVisitor.java
// public interface XmlVisitor {
// 
//   /**
//    * Handle text.
//    * 
//    * @param s text.
//    * @return XML event handler to be used as next.
//    */
//   XmlVisitor text(String s);
// 
//   /**
//    * Handle XML element start.
//    * 
//    * @param element name of the XML element.
//    * @return event handler for the XML element.
//    */
//   ElementVisitor visitElement(String element);
//   
//   /**
//    * Handle end of the XML element body.
//    * 
//    * @return event handler of the XML element to be used as next.
//    */
//   ElementVisitor visitEndBody();
// 
//   /**
//    * Event handler of XML element.
//    * 
//    * @author Rein Raudjärv
//    */
//   interface ElementVisitor {
//     /**
//      * Handle XML attribute.
//      * 
//      * @param name name of the XML attribute.
//      * @param value value of the XML attribute.
//      * @return event handler for the XML element.
//      */
//     ElementVisitor visitAttribute(String name, String value);
// 
//     /**
//      * Handle start of the XML body.
//      * 
//      * @return event handler for the XML element body.
//      */
//     XmlVisitor visitBody();
// 
//     /**
//      * Handle end of the XML element.
//      * 
//      * @return XML event handler to be used as next.
//      */
//     XmlVisitor visitEndElement();
//   }
// 
// }


import org.dynaform.xml.XmlVisitor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

package org.dynaform.xml.writer;



/**
 * @author Rein Raudjärv
 * 
 * @see XmlWriter
 */
public class SequenceWriter implements XmlWriter {

	private final Collection<XmlWriter> children;
	
	public static XmlWriter newInstance(Collection<XmlWriter> writers) {
		if (writers == null || writers.isEmpty())
			return null;
		
		if (writers.size() == 1)
			return writers.iterator().next();
		
		List<XmlWriter> items = new ArrayList<XmlWriter>();
		for (Iterator<XmlWriter> it = writers.iterator(); it.hasNext();) {
      XmlWriter writer = it.next();
      if (writer instanceof SequenceWriter)
        items.addAll(((SequenceWriter) writer).children);
      else
        items.add(writer);
    }
		return new SequenceWriter(items); 
	}
	
	private SequenceWriter(Collection<XmlWriter> children) {
		this.children = children;
	}
	
	public boolean isEmpty() {
		return false;
	}


// Path: src/main/java/org/dynaform/xml/XmlVisitor.java
// public interface XmlVisitor {
// 
//   /**
//    * Handle text.
//    * 
//    * @param s text.
//    * @return XML event handler to be used as next.
//    */
//   XmlVisitor text(String s);
// 
//   /**
//    * Handle XML element start.
//    * 
//    * @param element name of the XML element.
//    * @return event handler for the XML element.
//    */
//   ElementVisitor visitElement(String element);
//   
//   /**
//    * Handle end of the XML element body.
//    * 
//    * @return event handler of the XML element to be used as next.
//    */
//   ElementVisitor visitEndBody();
// 
//   /**
//    * Event handler of XML element.
//    * 
//    * @author Rein Raudjärv
//    */
//   interface ElementVisitor {
//     /**
//      * Handle XML attribute.
//      * 
//      * @param name name of the XML attribute.
//      * @param value value of the XML attribute.
//      * @return event handler for the XML element.
//      */
//     ElementVisitor visitAttribute(String name, String value);
// 
//     /**
//      * Handle start of the XML body.
//      * 
//      * @return event handler for the XML element body.
//      */
//     XmlVisitor visitBody();
// 
//     /**
//      * Handle end of the XML element.
//      * 
//      * @return XML event handler to be used as next.
//      */
//     XmlVisitor visitEndElement();
//   }
// 
// }


// Path: src/main/java/org/dynaform/xml/writer/SequenceWriter.java
import org.dynaform.xml.XmlVisitor;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

package org.dynaform.xml.writer;



/**
 * @author Rein Raudjärv
 * 
 * @see XmlWriter
 */
public class SequenceWriter implements XmlWriter {

	private final Collection<XmlWriter> children;
	
	public static XmlWriter newInstance(Collection<XmlWriter> writers) {
		if (writers == null || writers.isEmpty())
			return null;
		
		if (writers.size() == 1)
			return writers.iterator().next();
		
		List<XmlWriter> items = new ArrayList<XmlWriter>();
		for (Iterator<XmlWriter> it = writers.iterator(); it.hasNext();) {
      XmlWriter writer = it.next();
      if (writer instanceof SequenceWriter)
        items.addAll(((SequenceWriter) writer).children);
      else
        items.add(writer);
    }
		return new SequenceWriter(items); 
	}
	
	private SequenceWriter(Collection<XmlWriter> children) {
		this.children = children;
	}
	
	public boolean isEmpty() {
		return false;
	}


	public void write(XmlVisitor xa) {
