1 25 26 package org.objectweb.petals.component.common.util; 27 28 import java.io.BufferedInputStream ; 29 import java.io.ByteArrayInputStream ; 30 import java.io.PrintWriter ; 31 import java.io.StringWriter ; 32 import java.io.UnsupportedEncodingException ; 33 import java.util.Properties ; 34 35 import javax.xml.transform.OutputKeys ; 36 import javax.xml.transform.Result ; 37 import javax.xml.transform.Source ; 38 import javax.xml.transform.Transformer ; 39 import javax.xml.transform.TransformerConfigurationException ; 40 import javax.xml.transform.TransformerFactory ; 41 import javax.xml.transform.TransformerFactoryConfigurationError ; 42 import javax.xml.transform.stream.StreamResult ; 43 import javax.xml.transform.stream.StreamSource ; 44 45 53 public final class SourceHelper { 54 55 private static final String START_TAG = "<content>"; 56 57 private static final String END_TAG = "</content>"; 58 59 private static Transformer transformer; 60 61 public static Source createContentSource(String msg) { 62 return createSource(START_TAG + msg + END_TAG); 63 } 64 65 private SourceHelper() { 66 } 67 68 77 public static String createSoapFault(Throwable e, String code) { 78 StringBuffer s = new StringBuffer (); 79 92 s 93 .append("<s:Fault xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:faultcode>"); 94 if (code != null) { 95 s.append(code); 96 } 97 s.append("</s:faultcode><s:faultstring>"); 98 s.append(e.getMessage()); 99 s.append("</s:faultstring><s:detail><![CDATA["); 100 101 StringWriter sw = new StringWriter (); 104 PrintWriter pw = new PrintWriter (sw); 105 e.printStackTrace(pw); 106 s.append(sw); 107 108 s.append("]]></s:detail></s:Fault>"); 109 112 return s.toString(); 113 } 114 115 123 public static Source createSource(String msg) { 124 StreamSource source = new StreamSource (); 125 try { 126 byte[] msgByte = msg.getBytes("UTF-8"); 127 ByteArrayInputStream in = new ByteArrayInputStream (msgByte); 128 source.setInputStream(in); 129 } catch (UnsupportedEncodingException e) { 130 } 132 return source; 133 } 134 135 public static String createString(Source s) throws Exception { 136 137 String result = null; 138 139 if (s instanceof StreamSource ) { 140 StreamSource ss = (StreamSource ) s; 141 ss.getInputStream().reset(); 142 byte[] buf = new byte[ss.getInputStream().available()]; 143 BufferedInputStream bis = new BufferedInputStream (ss 144 .getInputStream()); 145 bis.read(buf); 146 result = new String (buf); 147 } else { 148 StringWriter buffer = new StringWriter (); 149 Result sresult = new StreamResult (buffer); 150 Transformer transform = getTransformer(); 151 transform.transform(s, sresult); 152 result = buffer.toString(); 153 } 154 return result; 155 } 156 157 protected static Transformer getTransformer() 158 throws TransformerConfigurationException , 159 TransformerFactoryConfigurationError { 160 if (transformer == null) { 161 transformer = TransformerFactory.newInstance().newTransformer(); 162 Properties props = new Properties (); 163 props.put(OutputKeys.OMIT_XML_DECLARATION, "yes"); 164 transformer.setOutputProperties(props); 165 } 166 return transformer; 167 } 168 169 public static String createStringContent(Source s) throws Exception { 170 String content = createString(s); 171 int index = content.indexOf(START_TAG); 172 String result = content.substring(index + START_TAG.length()); 173 return result.substring(0, result.indexOf(END_TAG)); 174 } 175 176 } 177 | Popular Tags |