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 java.io.ByteArrayInputStream;
020    import java.io.Externalizable;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.ObjectInput;
024    import java.io.ObjectOutput;
025    import java.io.Reader;
026    import java.io.StringReader;
027    import java.io.UnsupportedEncodingException;
028    
029    import javax.xml.transform.stream.StreamSource;
030    
031    /**
032     * A helper class which provides a JAXP {@link javax.xml.transform.Source Source} from a String which can
033     * be read as many times as required.
034     *
035     * @version $Revision: 42987 $
036     */
037    public class StringSource extends StreamSource implements Externalizable {
038        private String text;
039        private String encoding = "UTF-8";
040    
041        public StringSource() {
042        }
043    
044        public StringSource(String text) {
045            if (text == null) {
046                throw new NullPointerException("text can not be null");
047            }
048            this.text = text;
049        }
050    
051        public StringSource(String text, String systemId) {
052            this(text);
053            setSystemId(systemId);
054        }
055    
056        public StringSource(String text, String systemId, String encoding) {
057            this.text = text;
058            this.encoding = encoding;
059            setSystemId(systemId);
060        }
061    
062        public InputStream getInputStream() {
063            try {
064                return new ByteArrayInputStream(text.getBytes(encoding));
065            } catch (UnsupportedEncodingException e) {
066                throw new RuntimeException(e);
067            }
068        }
069    
070        public Reader getReader() {
071            return new StringReader(text);
072        }
073    
074        public String toString() {
075            return "StringSource[" + text + "]";
076        }
077    
078        public String getText() {
079            return text;
080        }
081    
082        public String getEncoding() {
083            return encoding;
084        }
085    
086        public void setEncoding(String encoding) {
087            this.encoding = encoding;
088        }
089    
090        public void setText(String text) {
091            this.text = text;
092        }
093    
094        public void writeExternal(ObjectOutput out) throws IOException {
095            out.writeUTF(text);
096            out.writeUTF(encoding);
097            out.writeUTF(getPublicId());
098            out.writeUTF(getSystemId());
099        }
100    
101        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
102            text = in.readUTF();
103            encoding = in.readUTF();
104            setPublicId(in.readUTF());
105            setSystemId(in.readUTF());
106        }
107    }