// Path: src/main/java/org/dynaform/xml/reader/InvalidXmlException.java
// public class InvalidXmlException extends RuntimeException {
// 
//   private static final long serialVersionUID = 1L;
// 
//   public InvalidXmlException() {
//     super();
//   }
//   
//   public InvalidXmlException(String message) {
//     super(message);
//   }
//   
// }
// 
// Path: src/main/java/org/dynaform/xml/reader/XmlReader.java
// public interface XmlReader {
// 
//   /**
//    * Creates XML event handler for consuming the XML.
//    * 
//    * @param next next XML reader.
//    * @return XML event handler.
//    */
//   XmlVisitor create(XmlReader next);
//   
// }


import org.dynaform.xml.reader.InvalidXmlException;
import org.dynaform.xml.reader.XmlParser;
import org.dynaform.xml.reader.XmlReader;
import org.dynaform.xml.writer.PrettyXmlAppender;
import org.dynaform.xml.writer.XmlAppender;
import org.dynaform.xml.writer.XmlWriter;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;

package org.dynaform.xml;



/**
 * @author Rein Raudjärv
 * 
 * @see XmlVisitor
 * @see XmlParser
 * @see DefaultHandler 
 */
public class XmlUtil {
	
  /**
   * Uses an XmlWriter to produce a pretty XML String. 
   * 
   * @param form an abstract form 
   * @return an XML String.
   */
  public static String writeXml(XmlWriter writer) {
    try {
      XmlVisitor xa = PrettyXmlAppender.newInstance();
      writer.write(xa);
      return xa.toString();
    } catch (Exception e) {
      throw new RuntimeException("Failed to produce XML String from XML writer '" + writer + "'", e);
    }
  }
  
  /**
   * Uses an XmlWriter to produce an XML String without additional whitespace. 
   * 
   * @param form an abstract form 
   * @return an XML String.
   */
  public static String writeCompactXml(XmlWriter writer) {
    try {
      XmlVisitor xa = XmlAppender.newInstance();
      writer.write(xa);
      return xa.toString();
    } catch (Exception e) {
      throw new RuntimeException("Failed to produce XML String from XML writer '" + writer + "'", e);
    }
  }
	
	/**
	 * Parses XML String using the given {@link XmlReader}.
	 * 
	 * @param xml XML String.
	 * @param xv an XML reader.
	 */
	public static void readXml(String xml, XmlReader xr) {
    try {
      readXml(xml, xr.create(null));

// Path: src/main/java/org/dynaform/xml/reader/InvalidXmlException.java
// public class InvalidXmlException extends RuntimeException {
// 
//   private static final long serialVersionUID = 1L;
// 
//   public InvalidXmlException() {
//     super();
//   }
//   
//   public InvalidXmlException(String message) {
//     super(message);
//   }
//   
// }
// 
// Path: src/main/java/org/dynaform/xml/reader/XmlReader.java
// public interface XmlReader {
// 
//   /**
//    * Creates XML event handler for consuming the XML.
//    * 
//    * @param next next XML reader.
//    * @return XML event handler.
//    */
//   XmlVisitor create(XmlReader next);
//   
// }


// Path: src/main/java/org/dynaform/xml/XmlUtil.java
import org.dynaform.xml.reader.InvalidXmlException;
import org.dynaform.xml.reader.XmlParser;
import org.dynaform.xml.reader.XmlReader;
import org.dynaform.xml.writer.PrettyXmlAppender;
import org.dynaform.xml.writer.XmlAppender;
import org.dynaform.xml.writer.XmlWriter;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.helpers.DefaultHandler;

package org.dynaform.xml;



/**
 * @author Rein Raudjärv
 * 
 * @see XmlVisitor
 * @see XmlParser
 * @see DefaultHandler 
 */
public class XmlUtil {
	
  /**
   * Uses an XmlWriter to produce a pretty XML String. 
   * 
   * @param form an abstract form 
   * @return an XML String.
   */
  public static String writeXml(XmlWriter writer) {
    try {
      XmlVisitor xa = PrettyXmlAppender.newInstance();
      writer.write(xa);
      return xa.toString();
    } catch (Exception e) {
      throw new RuntimeException("Failed to produce XML String from XML writer '" + writer + "'", e);
    }
  }
  
  /**
   * Uses an XmlWriter to produce an XML String without additional whitespace. 
   * 
   * @param form an abstract form 
   * @return an XML String.
   */
  public static String writeCompactXml(XmlWriter writer) {
    try {
      XmlVisitor xa = XmlAppender.newInstance();
      writer.write(xa);
      return xa.toString();
    } catch (Exception e) {
      throw new RuntimeException("Failed to produce XML String from XML writer '" + writer + "'", e);
    }
  }
	
	/**
	 * Parses XML String using the given {@link XmlReader}.
	 * 
	 * @param xml XML String.
	 * @param xv an XML reader.
	 */
	public static void readXml(String xml, XmlReader xr) {
    try {
      readXml(xml, xr.create(null));

    } catch (InvalidXmlException e) {
