KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > component > common > util > SourceHelper


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): Adrien LOUIS
21  * --------------------------------------------------------------------------
22  * $Id: SourceHelper.java 94 2006-03-26 17:11:05Z alouis $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.component.common.util;
27
28 import java.io.BufferedInputStream JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.io.PrintWriter JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.io.UnsupportedEncodingException JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import javax.xml.transform.OutputKeys JavaDoc;
36 import javax.xml.transform.Result JavaDoc;
37 import javax.xml.transform.Source JavaDoc;
38 import javax.xml.transform.Transformer JavaDoc;
39 import javax.xml.transform.TransformerConfigurationException JavaDoc;
40 import javax.xml.transform.TransformerFactory JavaDoc;
41 import javax.xml.transform.TransformerFactoryConfigurationError JavaDoc;
42 import javax.xml.transform.stream.StreamResult JavaDoc;
43 import javax.xml.transform.stream.StreamSource JavaDoc;
44
45 /**
46  * Sample class used to manage the mapping between
47  * <code>javax.xml.transform.Source</code> from String. the mapping is a
48  * quicly "string" -> "<content>string</content>" conversion.
49  *
50  * @author Adrien LOUIS - EBM WebSourcing
51  * @author Marc Dutoo - Open Wide
52  */

53 public final class SourceHelper {
54
55     private static final String JavaDoc START_TAG = "<content>";
56
57     private static final String JavaDoc END_TAG = "</content>";
58
59     private static Transformer JavaDoc transformer;
60
61     public static Source JavaDoc createContentSource(String JavaDoc msg) {
62         return createSource(START_TAG + msg + END_TAG);
63     }
64
65     private SourceHelper() {
66     }
67
68     /**
69      * Creates an XML fault with the stacktrace, message and the specified code
70      *
71      * @param e
72      * exception, used to provide faultstring and detail
73      * @param code
74      * code of the fault
75      * @return
76      */

77     public static String JavaDoc createSoapFault(Throwable JavaDoc e, String JavaDoc code) {
78         StringBuffer JavaDoc s = new StringBuffer JavaDoc();
79         /*
80          * s.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); s.append("<s:Envelope
81          * xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"");
82          * s.append("xmlns:wsmd=\"http://www.openuri.org/2003/02/soap/messagedata/\">");
83          * s.append("<s:Header>");
84          *
85          * s.append("<wsmd:MessageData><wsmd:RefToMessageId>"); if(id != null)
86          * s.append(id); s.append("</wsmd:RefToMessageId></wsmd:MessageData>");
87          *
88          * s.append("</s:Header>");
89          *
90          * s.append("<s:Body>");
91          */

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         // Wrapping stack trace in CDATA to avoid problems stemming from
102
// a constructor's "<init>" etc. (bug #306434)
103
StringWriter JavaDoc sw = new StringWriter JavaDoc();
104         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
105         e.printStackTrace(pw);
106         s.append(sw);
107
108         s.append("]]></s:detail></s:Fault>");
109         /*
110          * s.append("</s:Body></s:Envelope>");
111          */

112         return s.toString();
113     }
114
115     /**
116      * Create a Source from the String. The string must be in UTF-8 format,
117      * otherwise, the Source is not created.
118      *
119      * @param msg
120      * an UTF-8 String
121      * @return the Source built from the String, null if not an UTF-8
122      */

123     public static Source JavaDoc createSource(String JavaDoc msg) {
124         StreamSource JavaDoc source = new StreamSource JavaDoc();
125         try {
126             byte[] msgByte = msg.getBytes("UTF-8");
127             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(msgByte);
128             source.setInputStream(in);
129         } catch (UnsupportedEncodingException JavaDoc e) {
130             // nothing, return null
131
}
132         return source;
133     }
134
135     public static String JavaDoc createString(Source JavaDoc s) throws Exception JavaDoc {
136
137         String JavaDoc result = null;
138
139         if (s instanceof StreamSource JavaDoc) {
140             StreamSource JavaDoc ss = (StreamSource JavaDoc) s;
141             ss.getInputStream().reset();
142             byte[] buf = new byte[ss.getInputStream().available()];
143             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(ss
144                     .getInputStream());
145             bis.read(buf);
146             result = new String JavaDoc(buf);
147         } else {
148             StringWriter JavaDoc buffer = new StringWriter JavaDoc();
149             Result JavaDoc sresult = new StreamResult JavaDoc(buffer);
150             Transformer JavaDoc transform = getTransformer();
151             transform.transform(s, sresult);
152             result = buffer.toString();
153         }
154         return result;
155     }
156
157     protected static Transformer JavaDoc getTransformer()
158         throws TransformerConfigurationException JavaDoc,
159         TransformerFactoryConfigurationError JavaDoc {
160         if (transformer == null) {
161             transformer = TransformerFactory.newInstance().newTransformer();
162             Properties JavaDoc props = new Properties JavaDoc();
163             props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
164             transformer.setOutputProperties(props);
165         }
166         return transformer;
167     }
168
169     public static String JavaDoc createStringContent(Source JavaDoc s) throws Exception JavaDoc {
170         String JavaDoc content = createString(s);
171         int index = content.indexOf(START_TAG);
172         String JavaDoc result = content.substring(index + START_TAG.length());
173         return result.substring(0, result.indexOf(END_TAG));
174     }
175
176 }
177
Popular Tags