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
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) 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
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: *
023: * $Id: OutputWriter.java 9243 2007-09-25 00:49:54Z lzheng $
024: */
025: package com.bostechcorp.cbesb.runtime.ccsl.lib;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.InputStreamReader;
029: import java.io.OutputStream;
030:
031: import javax.xml.transform.Source;
032: import javax.xml.transform.dom.DOMSource;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import com.bostechcorp.cbesb.common.util.Dom;
038: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.ByteArraySource;
039: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
040:
041: /**
042: * @author j.zhang
043: * @version 1.0.0
044: */
045: public class OutputWriter {
046:
047: protected static transient Log logger = LogFactory
048: .getLog(OutputWriter.class);
049:
050: /**
051: * Write the StringSource to OutputStream.
052: *
053: * @param src The StringSource.
054: * @param os The OutputStream.
055: * @throws Exception
056: */
057: public static void writeStringSource(StringSource src,
058: OutputStream os, String charset) throws Exception {
059: // System.out.println(src.getText());
060: // Writer out = new OutputStreamWriter(os,charset);
061: // out.write(src.getText());
062: // out.close();
063: os.write(src.getText().getBytes(charset));
064: }
065:
066: /**
067: * Write the ByteArraySource to OutputStream.
068: *
069: * @param src The ByteArraySource.
070: * @param os The OutputStream.
071: * @throws Exception
072: */
073: public static void writeByteArraySource(ByteArraySource src,
074: OutputStream os) throws Exception {
075: os.write(src.getBytes());
076: }
077:
078: /**
079: * Process the src to the OutputStream.
080: *
081: * @param src The Source object.
082: * @param os OutputStream.
083: * @param writeStyle The value of WriteStyle.
084: * @param charset The value of Charset.
085: */
086: public static void processOutputStream(Source src, OutputStream os,
087: String writeStyle, String charset) {
088: try {
089:
090: if (charset == null || charset.length() < 1) {
091: charset = (new InputStreamReader(
092: new ByteArrayInputStream(new byte[0])))
093: .getEncoding();
094: }
095: if (src instanceof DOMSource) {
096: Dom.writeDOMSource((DOMSource) src, os, charset);
097: } else if (src instanceof StringSource) {
098: writeStringSource((StringSource) src, os, charset);
099: } else if (src instanceof ByteArraySource) {
100: writeByteArraySource((ByteArraySource) src, os);
101: }
102: } catch (Exception e) {
103: logger.error("error process src to OutputStream "
104: + e.getMessage());
105: if (logger.isDebugEnabled()) {
106: logger.debug("error process src to OutputStream ", e);
107: }
108: }
109: }
110: }
|