001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: StringSource.java 9472 2007-10-09 15:24:17Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.nmhandler;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.InputStream;
028: import java.io.Reader;
029: import java.io.StringReader;
030:
031: import javax.xml.transform.Source;
032: import javax.xml.transform.TransformerException;
033: import javax.xml.transform.dom.DOMSource;
034: import javax.xml.transform.stream.StreamSource;
035:
036: import com.bostechcorp.cbesb.common.runtime.CbesbException;
037: import com.bostechcorp.cbesb.common.runtime.DataContentException;
038: import com.bostechcorp.cbesb.common.util.Dom;
039: import com.bostechcorp.cbesb.common.util.ErrorUtil;
040:
041: /**
042: * A helper class which provides a JAXP {@link Source} from a String
043: * which can be read as many times as required.
044:
045: */
046: public class StringSource extends StreamSource {
047: private String text;
048:
049: public StringSource(String text) {
050: this .text = text;
051: }
052:
053: public StringSource(String text, String systemId) {
054: this .text = text;
055: setSystemId(systemId);
056: }
057:
058: public InputStream getInputStream() {
059: ByteArrayInputStream bais = null;
060: try {
061: bais = new ByteArrayInputStream(text.getBytes("utf-8"));
062: } catch (Exception e) {
063: ErrorUtil.printError("Invalid Encoding Exception: ", e);
064: }
065: return bais;
066: }
067:
068: public Reader getReader() {
069: return new StringReader(text);
070: }
071:
072: public String toString() {
073: return "StringSource[" + text + "]";
074: }
075:
076: public String getText() {
077: return text;
078: }
079:
080: static public StringSource convertToStringSource(Source source)
081: throws CbesbException {
082: StringSource strSrc = null;
083: if (source instanceof StringSource) {
084: //No conversion necessary
085: strSrc = (StringSource) source;
086: } else if (source instanceof DOMSource) {
087: try {
088: String data = Dom.parseToString(((DOMSource) source)
089: .getNode());
090: strSrc = new StringSource(data);
091: } catch (TransformerException e) {
092: throw new DataContentException(
093: "Cannot convert data from DOMSource into a StringSource.",
094: e);
095: }
096:
097: } else {
098: throw new DataContentException(
099: "Unsupported source type. Cannot convert data from '"
100: + source.getClass().getName()
101: + "' into a StringSource.");
102: }
103: return strSrc;
104: }
105: }
|