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    import org.apache.camel.util.ObjectHelper;
032    
033    /**
034     * A helper class which provides a JAXP {@link javax.xml.transform.Source Source} from a String which can
035     * be read as many times as required. Encoding is default UTF-8.
036     *
037     * @version $Revision: 740298 $
038     */
039    public class StringSource extends StreamSource implements Externalizable {
040        private String text;
041        private String encoding = "UTF-8";
042    
043        public StringSource() {
044        }
045    
046        public StringSource(String text) {
047            ObjectHelper.notNull(text, "text");
048            this.text = text;
049        }
050    
051        public StringSource(String text, String systemId) {
052            this(text);
053            ObjectHelper.notNull(systemId, "systemId");
054            setSystemId(systemId);
055        }
056    
057        public StringSource(String text, String systemId, String encoding) {
058            this(text, systemId);
059            ObjectHelper.notNull(encoding, "encoding");
060            this.encoding = encoding;
061        }
062    
063        public InputStream getInputStream() {
064            try {
065                return new ByteArrayInputStream(text.getBytes(encoding));
066            } catch (UnsupportedEncodingException e) {
067                throw new RuntimeException(e);
068            }
069        }
070    
071        public Reader getReader() {
072            return new StringReader(text);
073        }
074    
075        public String toString() {
076            return "StringSource[" + text + "]";
077        }
078    
079        public String getText() {
080            return text;
081        }
082    
083        public String getEncoding() {
084            return encoding;
085        }
086    
087        public void writeExternal(ObjectOutput out) throws IOException {
088            int b = (text != null ? 0x01 : 0x00) + (encoding != null ? 0x02 : 0x00)
089                    + (getPublicId() != null ? 0x04 : 0x00) + (getSystemId() != null ? 0x08 : 0x00);
090            out.writeByte(b);
091            if ((b & 0x01) != 0) {
092                out.writeUTF(text);
093            }
094            if ((b & 0x02) != 0) {
095                out.writeUTF(encoding);
096            }
097            if ((b & 0x04) != 0) {
098                out.writeUTF(getPublicId());
099            }
100            if ((b & 0x08) != 0) {
101                out.writeUTF(getSystemId());
102            }
103        }
104    
105        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
106            int b = in.readByte();
107            if ((b & 0x01) != 0) {
108                text = in.readUTF();
109            }
110            if ((b & 0x02) != 0) {
111                encoding = in.readUTF();
112            }
113            if ((b & 0x04) != 0) {
114                setPublicId(in.readUTF());
115            }
116            if ((b & 0x08) != 0) {
117                setSystemId(in.readUTF());
118            }
119        }
120    }