View Javadoc

1   /*******************************************************************************
2    *  Copyright (c) 2005, 2006, 2007 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.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   * Taken from servicemix-core.
26   */
27  public class StringSource extends StreamSource implements Serializable {
28  
29  	/** The Constant LOG. */
30      private static final Logger LOG
31      = LoggerFactory.getLogger(StringSource.class); 
32      private static final Messages MESSAGES
33      = Messages.getMessages(StringSource.class);
34  	
35  	/** The Constant serialVersionUID. */
36      private static final long serialVersionUID = -2965901682811246960L;
37  
38      /** The text. */
39      private final String text;
40      
41      /** The encoding. */
42      private String encoding = "UTF-8";
43  
44      /**
45       * Instantiates a new string source.
46       * 
47       * @param text
48       *          The StringSource text
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       * Instantiates a new string source.
62       * 
63       * @param text
64       *          The StringSource text
65       * @param systemId
66       *          The SystemId
67       */
68      public StringSource(String text, String systemId) {
69          this(text);
70          setSystemId(systemId);
71      }
72  
73      /**
74       * Instantiates a new string source.
75       * 
76       * @param text
77       *          The StringSource text
78       * @param systemId
79       *          The SystemId
80       * @param encoding
81       *          The encoding
82       */
83      public StringSource(String text, String systemId, String encoding) {
84          this.text = text;
85          this.encoding=encoding;
86          setSystemId(systemId);
87      }
88  
89      /* (non-Javadoc)
90       * @see javax.xml.transform.stream.StreamSource#getInputStream()
91       */
92      
93      /**
94       * Gets the <code>InptStream</code>.
95       * 
96       * @return
97       *      The Source InputStream
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      * Gets the Reader.
111      * 
112      * @return te source Reader
113      */
114     public Reader getReader() {
115         return new StringReader(text);
116     }
117     
118     /**
119      * The Source as String.
120      * 
121      * @return the Source as String
122      */
123     public String toString() {
124         return "StringSource[" + text + "]";
125     }
126 
127     /**
128      * Gets the text.
129      * 
130      * @return the text
131      */
132     public String getText() {
133         return text;
134     }
135 
136 }