1 /****************************************************************************
2 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the LGPL License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/lgpl.html
7 ****************************************************************************/
8 package it.imolinfo.jbi4corba.jbi.processor.transform;
9
10 import java.io.ByteArrayInputStream;
11 import java.io.InputStream;
12 import java.io.Reader;
13 import java.io.Serializable;
14 import java.io.StringReader;
15 import java.io.UnsupportedEncodingException;
16
17 import javax.xml.transform.stream.StreamSource;
18
19 /**
20 * Taken from servicemix-core.
21 */
22 public class StringSource extends StreamSource implements Serializable {
23
24 /** The Constant serialVersionUID. */
25 private static final long serialVersionUID = -2965901682811246960L;
26
27 /** The text. */
28 private final String text;
29
30 /** The encoding. */
31 private String encoding = "UTF-8";
32
33 /**
34 * Instantiates a new string source.
35 *
36 * @param text
37 * The StringSource text
38 */
39 public StringSource(String text) {
40 if (text == null) {
41 throw new NullPointerException("text can not be null");
42 }
43 this.text = text;
44 }
45
46 /**
47 * Instantiates a new string source.
48 *
49 * @param text
50 * The StringSource text
51 * @param systemId
52 * The SystemId
53 */
54 public StringSource(String text, String systemId) {
55 this(text);
56 setSystemId(systemId);
57 }
58
59 /**
60 * Instantiates a new string source.
61 *
62 * @param text
63 * The StringSource text
64 * @param systemId
65 * The SystemId
66 * @param encoding
67 * The encoding
68 */
69 public StringSource(String text, String systemId, String encoding) {
70 this.text = text;
71 this.encoding=encoding;
72 setSystemId(systemId);
73 }
74
75 /* (non-Javadoc)
76 * @see javax.xml.transform.stream.StreamSource#getInputStream()
77 */
78
79 /**
80 * Gets the <code>InptStream</code>.
81 *
82 * @return
83 * The Source InputStream
84 */
85 public InputStream getInputStream() {
86 try {
87 return new ByteArrayInputStream(text.getBytes(encoding));
88 } catch (UnsupportedEncodingException e) {
89 throw new RuntimeException(e);
90 }
91 }
92
93 /**
94 * Gets the Reader.
95 *
96 * @return te source Reader
97 */
98 public Reader getReader() {
99 return new StringReader(text);
100 }
101
102 /**
103 * The Source as String.
104 *
105 * @return the Source as String
106 */
107 public String toString() {
108 return "StringSource[" + text + "]";
109 }
110
111 /**
112 * Gets the text.
113 *
114 * @return the text
115 */
116 public String getText() {
117 return text;
118 }
119
120 }