View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.xml.transform;
18  
19  import java.io.InputStream;
20  import java.io.Reader;
21  import java.io.StringReader;
22  import javax.xml.transform.stream.StreamSource;
23  
24  import org.springframework.util.Assert;
25  
26  /**
27   * Convenient subclass of <code>StreamSource</code> that reads from a <code>StringReader</code>. The string to be read
28   * can be set via the constructor.
29   *
30   * @author Arjen Poutsma
31   * @since 1.0.0
32   */
33  public class StringSource extends StreamSource {
34  
35      private final String content;
36  
37      /**
38       * Initializes a new instance of the <code>StringSource</code> with the given string content.
39       *
40       * @param content the content
41       */
42      public StringSource(String content) {
43          Assert.notNull(content, "'content' must not be null");
44          this.content = content;
45      }
46  
47      @Override
48      public Reader getReader() {
49          return new StringReader(content);
50      }
51  
52      /**
53       * Throws {@link UnsupportedOperationException}.
54       *
55       * @throws UnsupportedOperationException always
56       */
57      @Override
58      public void setInputStream(InputStream inputStream) {
59          throw new UnsupportedOperationException("setInputStream is not supported");
60      }
61  
62      /**
63       * Returns {@code null}.
64       *
65       * @return {@code null}
66       */
67      @Override
68      public InputStream getInputStream() {
69          return null;
70      }
71  
72      /**
73       * Throws {@link UnsupportedOperationException}.
74       *
75       * @throws UnsupportedOperationException always
76       */
77      @Override
78      public void setReader(Reader reader) {
79          throw new UnsupportedOperationException("setReader is not supported");
80      }
81  
82      @Override
83      public String toString() {
84          return content;
85      }
86  }