/*
* JBoss, Home of Professional Open Source
* Copyright 2013, Red Hat, Inc. and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.richfaces.example;
import java.io.StringWriter;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Transformer;
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.DocumentFragment;
import org.w3c.dom.NodeList;
/**
* @author Maksim Kaszynski
*
*/
public class XMLBodySerializer {
public String serialize(NodeList childNodes, Document xmlDocument) throws ParsingException {
try {
StringWriter out;
DocumentFragment fragment = xmlDocument.createDocumentFragment();
for (int i = 0; i < childNodes.getLength(); i++) {
fragment.appendChild(childNodes.item(i).cloneNode(true));
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setErrorListener(new ErrorListener() {
public void error(TransformerException exception) throws TransformerException {
}
public void fatalError(TransformerException exception) throws TransformerException {
}
public void warning(TransformerException exception) throws TransformerException {
}
});
transformer.setOutputProperty("indent", "yes");
transformer.setOutputProperty("omit-xml-declaration", "yes");
out = new StringWriter();
StreamResult result = new StreamResult(out);
transformer.transform(new DOMSource(fragment), result);
return out.toString();
} catch (Exception e) {
throw new ParsingException(e);
}
}
}