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