001 package biz.hammurapi.jms; 002 003 import java.io.StringWriter; 004 005 import javax.xml.parsers.DocumentBuilderFactory; 006 import javax.xml.parsers.ParserConfigurationException; 007 import javax.xml.transform.Result; 008 import javax.xml.transform.dom.DOMResult; 009 010 import org.w3c.dom.Document; 011 012 import biz.hammurapi.config.RuntimeConfigurationException; 013 import biz.hammurapi.xml.dom.DOMUtils; 014 015 public abstract class DomMessageProcessor extends StylingXmlMessageProcessor { 016 017 protected Result createResult() { 018 try { 019 return new DOMResult(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()); 020 } catch (ParserConfigurationException e) { 021 throw new RuntimeConfigurationException("Cannot create document: "+e, e); 022 } 023 } 024 025 protected String processResult(Result result) { 026 Document ret = processDocument((Document) ((DOMResult) result).getNode()); 027 if (ret==null) { 028 return null; 029 } 030 031 try { 032 StringWriter sw = new StringWriter(); 033 // XMLSerializer serializer = new XMLSerializer(sw, new OutputFormat("xml", "UTF-8", false)); 034 // serializer.setNamespaces(true); 035 // serializer.serialize(ret); 036 DOMUtils.serialize(ret, sw); 037 sw.close(); 038 return sw.toString(); 039 } catch (Exception e) { 040 throw new MessageProcessingException(e); 041 } 042 } 043 044 protected abstract Document processDocument(Document document); 045 046 }