1 10 11 package org.mule.transformers.xml; 12 13 import java.io.ByteArrayInputStream ; 14 import org.apache.commons.io.output.ByteArrayOutputStream; 15 import java.io.StringReader ; 16 import java.io.StringWriter ; 17 18 import javax.xml.transform.OutputKeys ; 19 import javax.xml.transform.Result ; 20 import javax.xml.transform.Source ; 21 import javax.xml.transform.Transformer ; 22 import javax.xml.transform.TransformerFactory ; 23 import javax.xml.transform.TransformerFactoryConfigurationError ; 24 import javax.xml.transform.dom.DOMResult ; 25 import javax.xml.transform.dom.DOMSource ; 26 import javax.xml.transform.stream.StreamResult ; 27 import javax.xml.transform.stream.StreamSource ; 28 29 import org.dom4j.Document; 30 import org.dom4j.io.DocumentResult; 31 import org.dom4j.io.DocumentSource; 32 import org.mule.transformers.AbstractTransformer; 33 34 38 39 public abstract class AbstractXmlTransformer extends AbstractTransformer 40 { 41 42 public AbstractXmlTransformer() 43 { 44 registerSourceType(String .class); 45 registerSourceType(byte[].class); 46 registerSourceType(DocumentSource.class); 47 registerSourceType(Document.class); 48 registerSourceType(org.w3c.dom.Document .class); 49 registerSourceType(org.w3c.dom.Element .class); 50 } 51 52 public Source getXmlSource(Object src) 53 { 54 if (src instanceof byte[]) 55 { 56 return new StreamSource (new ByteArrayInputStream ((byte[])src)); 57 } 58 else if (src instanceof String ) 59 { 60 return new StreamSource (new StringReader ((String )src)); 61 } 62 else if (src instanceof DocumentSource) 63 { 64 return (Source )src; 65 } 66 else if (src instanceof Document) 67 { 68 return new DocumentSource((Document)src); 69 } 70 else if (src instanceof org.w3c.dom.Document ) 71 { 72 return new DOMSource ((org.w3c.dom.Document )src); 73 } 74 else 75 return null; 76 } 77 78 81 protected static interface ResultHolder 82 { 83 84 88 Result getResult(); 89 90 93 Object getResultObject(); 94 } 95 96 101 protected static ResultHolder getResultHolder(Class desiredClass) 102 { 103 if (desiredClass == null) return null; 104 if (byte[].class.equals(desiredClass)) 105 { 106 return new ResultHolder() 107 { 108 ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); 109 StreamResult result = new StreamResult (resultStream); 110 111 public Result getResult() 112 { 113 return result; 114 } 115 116 public Object getResultObject() 117 { 118 return resultStream.toByteArray(); 119 } 120 }; 121 } 122 else if (String .class.equals(desiredClass)) 123 { 124 return new ResultHolder() 125 { 126 StringWriter writer = new StringWriter (); 127 StreamResult result = new StreamResult (writer); 128 129 public Result getResult() 130 { 131 return result; 132 } 133 134 public Object getResultObject() 135 { 136 return writer.getBuffer().toString(); 137 } 138 }; 139 } 140 else if (org.w3c.dom.Document .class.isAssignableFrom(desiredClass)) 141 { 142 return new ResultHolder() 143 { 144 DOMResult result = new DOMResult (); 145 146 public Result getResult() 147 { 148 return result; 149 } 150 151 public Object getResultObject() 152 { 153 return result.getNode(); 154 } 155 }; 156 } 157 else if (org.dom4j.io.DocumentResult.class.isAssignableFrom(desiredClass)) 158 { 159 return new ResultHolder() 160 { 161 DocumentResult result = new DocumentResult(); 162 163 public Result getResult() 164 { 165 return result; 166 } 167 168 public Object getResultObject() 169 { 170 return result; 171 } 172 }; 173 } 174 else if (org.dom4j.Document.class.isAssignableFrom(desiredClass)) 175 { 176 return new ResultHolder() 177 { 178 DocumentResult result = new DocumentResult(); 179 180 public Result getResult() 181 { 182 return result; 183 } 184 185 public Object getResultObject() 186 { 187 return result.getDocument(); 188 } 189 }; 190 } 191 return null; 192 } 193 194 protected String convertToText(Object obj) 195 throws TransformerFactoryConfigurationError , javax.xml.transform.TransformerException 196 { 197 if (obj instanceof String ) 199 { 200 return (String )obj; 201 } 202 else if (obj instanceof Document) 203 { 204 return ((Document)obj).asXML(); 205 } 206 Source src = getXmlSource(obj); 208 if (src == null) return null; 209 210 StringWriter writer = new StringWriter (); 211 StreamResult result = new StreamResult (writer); 212 213 TransformerFactory.newInstance().newTransformer().transform(src, result); 214 return writer.getBuffer().toString(); 215 } 216 217 protected String convertToBytes(Object obj, String preferredEncoding) 218 throws TransformerFactoryConfigurationError , javax.xml.transform.TransformerException 219 { 220 Source src = getXmlSource(obj); 222 if (src == null) return null; 223 224 StringWriter writer = new StringWriter (); 225 StreamResult result = new StreamResult (writer); 226 227 Transformer idTransformer = TransformerFactory.newInstance().newTransformer(); 228 idTransformer.setOutputProperty(OutputKeys.ENCODING, preferredEncoding); 229 idTransformer.transform(src, result); 230 return writer.getBuffer().toString(); 231 } 232 233 } 234 | Popular Tags |