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: StringDataSource.java 5222 2007-02-02 21:45:36Z tvolle $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.nmhandler;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.ByteArrayOutputStream;
028: import java.io.IOException;
029: import java.io.InputStream;
030: import java.io.OutputStream;
031:
032: import javax.activation.DataSource;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerConfigurationException;
035: import javax.xml.transform.TransformerException;
036: import javax.xml.transform.TransformerFactory;
037: import javax.xml.transform.dom.DOMSource;
038: import javax.xml.transform.stream.StreamResult;
039:
040: /**
041: * DataSource implementation to hold a parsed DOMSource object.
042: * @author mpreston
043: *
044: */
045: public class DOMDataSource implements DataSource {
046:
047: private DOMSource src;
048:
049: private String name = "unused";
050:
051: public DOMDataSource(DOMSource domSrc) {
052: this .src = domSrc;
053: }
054:
055: public DOMDataSource(DOMSource domSrc, String name) {
056: this .src = domSrc;
057: this .name = name;
058: }
059:
060: /* (non-Javadoc)
061: * @see javax.activation.DataSource#getContentType()
062: */
063: public String getContentType() {
064: return "text/xml";
065: }
066:
067: /* (non-Javadoc)
068: * @see javax.activation.DataSource#getInputStream()
069: */
070: public InputStream getInputStream() throws IOException {
071: try {
072: ByteArrayOutputStream baos = new ByteArrayOutputStream();
073:
074: TransformerFactory tf = TransformerFactory.newInstance();
075: Transformer transformer = tf.newTransformer();
076: transformer.transform(src, new StreamResult(baos));
077: return new ByteArrayInputStream(baos.toByteArray());
078: } catch (TransformerConfigurationException e) {
079: throw new IOException("Unable to create input stream.");
080: } catch (TransformerException e) {
081: throw new IOException("Unable to create input stream.");
082: }
083: }
084:
085: /* (non-Javadoc)
086: * @see javax.activation.DataSource#getName()
087: */
088: public String getName() {
089: return name;
090: }
091:
092: /* (non-Javadoc)
093: * @see javax.activation.DataSource#getOutputStream()
094: */
095: public OutputStream getOutputStream() throws IOException {
096: throw new IOException("getOutputStream() not supported.");
097: }
098:
099: public DOMSource getSrc() {
100: return src;
101: }
102:
103: }
|