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: 69208 $
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        /**
088         * @deprecated will be removed in Camel 2.0
089         */
090        public void setEncoding(String encoding) {
091            this.encoding = encoding;
092        }
093    
094        /**
095         * @deprecated will be removed in Camel 2.0
096         */
097        public void setText(String text) {
098            this.text = text;
099        }
100    
101        public void writeExternal(ObjectOutput out) throws IOException {
102            int b = (text != null ? 0x01 : 0x00) + (encoding != null ? 0x02 : 0x00)
103                    + (getPublicId() != null ? 0x04 : 0x00) + (getSystemId() != null ? 0x08 : 0x00);
104            out.writeByte(b);
105            if ((b & 0x01) != 0) {
106                out.writeUTF(text);
107            }
108            if ((b & 0x02) != 0) {
109                out.writeUTF(encoding);
110            }
111            if ((b & 0x04) != 0) {
112                out.writeUTF(getPublicId());
113            }
114            if ((b & 0x08) != 0) {
115                out.writeUTF(getSystemId());
116            }
117        }
118    
119        public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
120            int b = in.readByte();
121            if ((b & 0x01) != 0) {
122                text = in.readUTF();
123            }
124            if ((b & 0x02) != 0) {
125                encoding = in.readUTF();
126            }
127            if ((b & 0x04) != 0) {
128                setPublicId(in.readUTF());
129            }
130            if ((b & 0x08) != 0) {
131                setSystemId(in.readUTF());
132            }
133        }
134    }