1
2
3
4
5
6
7
8 package it.imolinfo.jbi4ejb.processor.transform;
9
10 import it.imolinfo.jbi4ejb.Logger;
11 import it.imolinfo.jbi4ejb.LoggerFactory;
12 import it.imolinfo.jbi4ejb.jbi.Messages;
13
14 import java.io.ByteArrayInputStream;
15 import java.io.InputStream;
16 import java.io.Reader;
17 import java.io.Serializable;
18 import java.io.StringReader;
19 import java.io.UnsupportedEncodingException;
20
21 import javax.xml.transform.TransformerException;
22 import javax.xml.transform.stream.StreamSource;
23
24
25
26
27 public class StringSource extends StreamSource implements Serializable {
28
29
30 private static final Logger LOG
31 = LoggerFactory.getLogger(StringSource.class);
32 private static final Messages MESSAGES
33 = Messages.getMessages(StringSource.class);
34
35
36 private static final long serialVersionUID = -2965901682811246960L;
37
38
39 private final String text;
40
41
42 private String encoding = "UTF-8";
43
44
45
46
47
48
49
50 public StringSource(String text) {
51 if (text == null) {
52 String msg=MESSAGES.getString("EJB000707_Text_can_not_be_null");
53 LOG.error(msg);
54 throw new NullPointerException(msg);
55
56 }
57 this.text = text;
58 }
59
60
61
62
63
64
65
66
67
68 public StringSource(String text, String systemId) {
69 this(text);
70 setSystemId(systemId);
71 }
72
73
74
75
76
77
78
79
80
81
82
83 public StringSource(String text, String systemId, String encoding) {
84 this.text = text;
85 this.encoding=encoding;
86 setSystemId(systemId);
87 }
88
89
90
91
92
93
94
95
96
97
98
99 public InputStream getInputStream() {
100 try {
101 return new ByteArrayInputStream(text.getBytes(encoding));
102 } catch (UnsupportedEncodingException e) {
103 String msg=MESSAGES.getString("EJB000708_Exception_in_getInputStream", new Object[]{e.getMessage()});
104 LOG.error(msg,e);
105 throw new RuntimeException(msg,e);
106 }
107 }
108
109
110
111
112
113
114 public Reader getReader() {
115 return new StringReader(text);
116 }
117
118
119
120
121
122
123 public String toString() {
124 return "StringSource[" + text + "]";
125 }
126
127
128
129
130
131
132 public String getText() {
133 return text;
134 }
135
136 }