/**
 * 
 */
package com.googlecode.yqltwitter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URISyntaxException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

/**
 * @author Sutra Zhou
 */
/* package */class XmlUtils {
	private static ThreadLocal<DocumentBuilder> builders = new ThreadLocal<DocumentBuilder>() {
		@Override
		protected DocumentBuilder initialValue() {
			try {
				return DocumentBuilderFactory.newInstance()
						.newDocumentBuilder();
			} catch (ParserConfigurationException ex) {
				throw new ExceptionInInitializerError(ex);
			}
		}
	};

	private static ThreadLocal<Transformer> transformers = new ThreadLocal<Transformer>() {
		/**
		 * {@inheritDoc}
		 */
		@Override
		protected Transformer initialValue() {
			try {
				return TransformerFactory.newInstance().newTransformer();
			} catch (TransformerConfigurationException ex) {
				throw new ExceptionInInitializerError(ex);
			}
		}

	};

	public static Document toDocument(String str)
			throws UnsupportedEncodingException, SAXException, IOException {
		return builders.get().parse(
				new ByteArrayInputStream(str.getBytes("UTF-8")));
	}

	public static Document toDocument(InputStream is) throws SAXException,
			IOException {
		return builders.get().parse(is);
	}

	public static Document toDocument(URL url) throws SAXException,
			IOException, URISyntaxException {
		return builders.get().parse(url.toURI().toString());
	}

	/**
	 * Write node to a string.
	 * 
	 * @param node
	 *            the node to write
	 * @return string
	 * @throws TransformerException
	 *             if write failed
	 */
	public static String toString(Node node) throws TransformerException {
		DOMSource source = new DOMSource(node);
		Writer w = new StringWriter();
		StreamResult result = new StreamResult(w);
		transformers.get().transform(source, result);
		return w.toString();
	}

}

@@ -816,16 +816,49 @@
 n;%0A%0A/**%0A
+ * Operations to assist XML.%0A * %0A
  * @auth
@@ -905,16 +905,48 @@
 Utils %7B%0A
+%09/**%0A%09 * Document builder.%0A%09 */%0A
 %09private
@@ -1281,16 +1281,47 @@
 %09%7D%0A%09%7D;%0A%0A
+%09/**%0A%09 * XML transformer.%0A%09 */%0A
 %09private
@@ -1675,338 +1675,1374 @@
 ;%0A%0A%09
-public static Document toDocument(String str)%0A%09%09%09throws UnsupportedEncodingException, SAXException, IOException %7B%0A%09%09return builders.get().parse(%0A%09%09%09%09new ByteArrayInputStream(str.getBytes(%22UTF-8%22)));%0A%09%7D%0A%0A%09public static Document toDocument(InputStream is) throws SAXException,%0A%09%09%09IOException %7B%0A%09%09return builders.get().parse(is);%0A%09%7D%0A
+/**%0A%09 * Transform string to XML document.%0A%09 * %0A%09 * @param str%0A%09 *            the string to transform%0A%09 * @return XML document transformed from the string%0A%09 * @throws UnsupportedEncodingException%0A%09 *             encoding %22UTF-8%22 does not support on the platform%0A%09 * @throws SAXException%0A%09 *             indicate SAX error%0A%09 * @throws IOException%0A%09 *             indicate IO error%0A%09 */%0A%09public static Document toDocument(String str)%0A%09%09%09throws UnsupportedEncodingException, SAXException, IOException %7B%0A%09%09return builders.get().parse(%0A%09%09%09%09new ByteArrayInputStream(str.getBytes(%22UTF-8%22)));%0A%09%7D%0A%0A%09/**%0A%09 * Transform input stream to XML document.%0A%09 * %0A%09 * @param is%0A%09 *            the input stream to transform%0A%09 * @return XML document transformed from the input stream%0A%09 * @throws SAXException%0A%09 *             indicate SAX error%0A%09 * @throws IOException%0A%09 *             indicate IO error%0A%09 */%0A%09public static Document toDocument(InputStream is) throws SAXException,%0A%09%09%09IOException %7B%0A%09%09return builders.get().parse(is);%0A%09%7D%0A%0A%09/**%0A%09 * Transform to XML document from the URL.%0A%09 * %0A%09 * @param url%0A%09 *            the url to transform%0A%09 * @return XML document transformed from the URL.%0A%09 * @throws SAXException%0A%09 *             indicate SAX error%0A%09 * @throws IOException%0A%09 *             indicate IO error%0A%09 * @throws URISyntaxException%0A%09 *             indicate the URL syntax error%0A%09 */
 %0A%09pu

Add comment for XmlUtils.