KickJava   Java API By Example, From Geeks To Geeks.

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

52 public final class SourceHelper {
53
54     private static Transformer JavaDoc transformer;
55
56     private SourceHelper() {
57         // avoid to be built
58
}
59
60     /**
61      * Create a Source from the String.
62      * The string must be in UTF-8 format, otherwise, the Source is not created.
63      * @param msg an UTF-8 String
64      * @return the Source built from the String, null if not an UTF-8
65      */

66     public static Source JavaDoc createSource(String JavaDoc msg) {
67         StreamSource JavaDoc source = new StreamSource JavaDoc();
68         try {
69             byte[] msgByte= msg.getBytes("UTF-8");
70             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(msgByte);
71             source.setInputStream(in);
72         } catch (UnsupportedEncodingException JavaDoc e) {
73             // nothing, return null
74
}
75         return source;
76     }
77
78     public static String JavaDoc createString(Source JavaDoc s) throws Exception JavaDoc {
79
80         String JavaDoc result = null;
81
82         if (s != null) {
83
84                 if (s instanceof StreamSource JavaDoc) {
85                     StreamSource JavaDoc ss = (StreamSource JavaDoc) s;
86                     if (ss.getInputStream() != null) {
87                         if (ss.getInputStream().markSupported()) {
88                                 ss.getInputStream().reset();
89                             }
90                             byte[] buf = new byte[ss.getInputStream().available()];
91                             BufferedInputStream JavaDoc bis = new BufferedInputStream JavaDoc(ss
92                                     .getInputStream());
93                             bis.read(buf);
94                             result = new String JavaDoc(buf);
95                     }
96                     else if (ss.getReader() != null) {
97                         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
98                         int readThisTime = 0;
99                         char[] cbuf = new char[4096];
100                         while (readThisTime != -1) {
101                                 readThisTime = ss.getReader().read(cbuf, 0, 4096);
102                                 if (readThisTime!= -1) {
103                                         stringBuffer.append(cbuf, 0, readThisTime);
104                                 }
105                         }
106                         result = stringBuffer.toString();
107                     }
108                     
109                 } else {
110
111                     StringWriter JavaDoc buffer = new StringWriter JavaDoc();
112                     Result JavaDoc sresult = new StreamResult JavaDoc(buffer);
113                     Transformer JavaDoc transform = getTransformer();
114                     transform.transform(s, sresult);
115                     result = buffer.toString();
116                 }
117         }
118         
119         return result;
120     }
121     public static Transformer JavaDoc getTransformer()
122         throws TransformerConfigurationException JavaDoc,
123         TransformerFactoryConfigurationError JavaDoc {
124         if (transformer == null) {
125             transformer = TransformerFactory.newInstance().newTransformer();
126             Properties JavaDoc props = new Properties JavaDoc();
127             props.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
128             transformer.setOutputProperties(props);
129         }
130         return transformer;
131     }
132
133 }
134
Popular Tags