001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.converter.jaxp;
018    
019    import javax.xml.transform.stream.StreamSource;
020    import java.io.*;
021    
022    /**
023     * A helper class which provides a JAXP {@link javax.xml.transform.Source Source} from a String which can
024     * be read as many times as required.
025     *
026     * @version $Revision: 42505 $
027     */
028    public class StringSource extends StreamSource implements Externalizable {
029        private String text;
030        private String encoding = "UTF-8";
031    
032        public StringSource() {
033        }
034    
035        public StringSource(String text) {
036            if (text == null) {
037                throw new NullPointerException("text can not be null");
038            }
039            this.text = text;
040        }
041    
042        public StringSource(String text, String systemId) {
043            this(text);
044            setSystemId(systemId);
045        }
046    
047        public StringSource(String text, String systemId, String encoding) {
048            this.text = text;
049            this.encoding = encoding;
050            setSystemId(systemId);
051        }
052    
053        public InputStream getInputStream() {
054            try {
055                return new ByteArrayInputStream(text.getBytes(encoding));
056            } catch (UnsupportedEncodingException e) {
057                throw new RuntimeException(e);
058            }
059        }
060    
061        public Reader getReader() {
062            return new StringReader(text);
063        }
064    
065        public String toString() {
066            return "StringSource[" + text + "]";
067        }
068    
069        public String getText() {
070            return text;
071        }
072    
073        public String getEncoding() {
074            return encoding;
075        }
076    
077        public void setEncoding(String encoding) {
078            this.encoding = encoding;
079        }
080    
081        public void setText(String text) {
082            this.text = text;
083        }
084    
085        public void writeExternal(ObjectOutput out) throws IOException {
086            out.writeUTF(text);
087            out.writeUTF(encoding);
088            out.writeUTF(getPublicId());
089            out.writeUTF(getSystemId());
090        }
091    
092        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
093            text = in.readUTF();
094            encoding = in.readUTF();
095            setPublicId(in.readUTF());
096            setSystemId(in.readUTF());
097        }
098    }