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