001: package com.bostechcorp.cbesb.runtime.ccsl.lib;
002:
003: import java.io.InputStream;
004: import java.io.StringWriter;
005:
006: import javax.jbi.messaging.MessagingException;
007: import javax.jbi.messaging.NormalizedMessage;
008: import javax.xml.transform.Source;
009: import javax.xml.transform.Transformer;
010: import javax.xml.transform.TransformerConfigurationException;
011: import javax.xml.transform.TransformerException;
012: import javax.xml.transform.TransformerFactory;
013: import javax.xml.transform.dom.DOMResult;
014: import javax.xml.transform.dom.DOMSource;
015: import javax.xml.transform.stream.StreamResult;
016: import javax.xml.transform.stream.StreamSource;
017:
018: import org.w3c.dom.Document;
019: import org.w3c.dom.Node;
020:
021: import com.bostechcorp.cbesb.common.runtime.CbesbException;
022: import com.bostechcorp.cbesb.common.runtime.DataContentException;
023: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
024: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
025:
026: public class NormalizedMessageUtil {
027:
028: /**
029: *
030: * @param content
031: * @return
032: * @throws CbesbException
033: */
034: public static org.w3c.dom.Document getDocFromSource(Source content)
035: throws CbesbException {
036: if (content == null)
037: return null;
038:
039: org.w3c.dom.Node domNode;
040: if (content instanceof DOMSource) {
041: domNode = ((DOMSource) content).getNode();
042: } else {
043: DOMResult dr = new DOMResult();
044: TransformerFactory tf = TransformerFactory.newInstance();
045: try {
046:
047: Transformer t = tf.newTransformer();
048: t.transform(content, dr);
049: } catch (Exception e) {
050: throw new DataContentException(
051: "Fail to convert the source in Normalized Message into DOMSource.",
052: "Check the source in the message to make sure it is XML formatted.",
053: e);
054:
055: }
056: domNode = dr.getNode();
057: }
058:
059: if (domNode instanceof org.w3c.dom.Document) {
060: return (org.w3c.dom.Document) domNode;
061: } else {
062: return ((org.w3c.dom.Element) domNode).getOwnerDocument();
063: }
064: }
065:
066: //
067: // Get the message content as a DOM Node
068: //
069: public static Node getNodeFromSource(Source content)
070: throws CbesbException {
071:
072: Document doc = getDocFromSource(content);
073: if (doc != null)
074: return doc.getDocumentElement();
075: else
076: return null;
077:
078: //
079: // if (content == null) return null;
080: //
081: // /*
082: // * If the content is StreamSource or StringSource then we convert it to DOM
083: // * and get the node.
084: // */
085: // if (content instanceof StreamSource || content instanceof StringSource) {
086: // DOMResult dr = new DOMResult();
087: // TransformerFactory tf = TransformerFactory.newInstance();
088: // Transformer t = tf.newTransformer();
089: // t.transform(content, dr);
090: // return dr.getNode();
091: // } else if (content instanceof DOMSource) {
092: //DOMSource ds = (DOMSource)content;
093: //Document doc = (Document)ds.getNode();
094: // return doc.getDocumentElement();
095: // } else {
096: // throw new TransformerException("Content "+content+" can not be converted to Node");
097: // }
098:
099: }
100:
101: /**
102: * Convert the source content into DOMSource
103: * @param content
104: * @throws CbesbException
105: */
106: public static void convertContentAsDOMSource(Source content)
107: throws CbesbException {
108:
109: if (!(content instanceof DOMSource)) {
110: Document doc = getDocFromSource(content);
111:
112: content = new DOMSource(doc);
113: }
114: }
115:
116: //
117: // Get the message content as a DOM Node
118: //
119: public static Node getContentAsNode(NormalizedMessage nm)
120: throws TransformerException, MessagingException {
121: Source content = nm.getContent();
122: if (content == null)
123: return null;
124:
125: /*
126: * If the content is StreamSource or StringSource then we convert it to DOM
127: * and get the node.
128: */
129: if (content instanceof StreamSource
130: || content instanceof StringSource) {
131: DOMResult dr = new DOMResult();
132: TransformerFactory tf = TransformerFactory.newInstance();
133: Transformer t = tf.newTransformer();
134: t.transform(content, dr);
135: // If the content is streamsource then replace it with the DOM since the
136: // stream can only be read once.
137: if (content instanceof StreamSource) {
138: DOMSource ds = new DOMSource(dr.getNode());
139: nm.setContent(ds);
140: }
141: return dr.getNode();
142: } else if (content instanceof DOMSource) {
143: DOMSource ds = (DOMSource) content;
144: Document doc = (Document) ds.getNode();
145: return doc.getDocumentElement();
146: } else {
147: throw new TransformerException("Content " + content
148: + " can not be converted to Node");
149: }
150: }
151:
152: public static String getContentAsString(NormalizedMessage nm) {
153: Source content = nm.getContent();
154: String s = "";
155: /*
156: * If the content is StreamSource then we convert it to StringSource to have
157: * an in-memory copy
158: */
159: if (content instanceof StreamSource) {
160: try {
161: StringBuffer sb = new StringBuffer();
162: InputStream is = ((StreamSource) content)
163: .getInputStream();
164: for (int ch = 0; (ch = is.read()) >= 0;) {
165: sb.append((char) ch);
166: }
167: is.close();
168: StringSource ss = new StringSource(new String(sb));
169: nm.setContent(ss);
170: s = s.concat(ss.getText());
171: } catch (Exception e) {
172: s = s
173: .concat("Exception converting content to DOMSource: "
174: + e + "\n");
175: }
176: }
177: if (content instanceof DOMSource) {
178: try {
179: StringWriter sw = new StringWriter();
180: StreamResult result = new StreamResult(sw);
181: TransformerFactory tf = TransformerFactory
182: .newInstance();
183: Transformer t = tf.newTransformer();
184: t.setOutputProperty("indent", "yes");
185: t.transform(content, result);
186: s = s.concat(sw.toString() + "\n");
187: } catch (Exception e) {
188: s = s.concat("Exception printing content: " + e + "\n");
189: }
190: } else {
191: s = s.concat("ERROR - content is not valid\n");
192: }
193: return s;
194: }
195:
196: public static void setContent(NormalizedMessage nm, String s)
197: throws MessagingException {
198: StringSource ss = new StringSource(s);
199: nm.setContent(ss);
200: }
201:
202: public static String getTextContent(NormalizedMessage nm) {
203: String s = "";
204: Node n = null;
205: try {
206: n = getContentAsNode(nm);
207: s = n.getTextContent();
208: } catch (Exception e) {
209: s = "Exception getting text content " + e;
210: }
211: return s;
212: }
213:
214: public static void addXmlRecord(NormalizedMessage nm, String xml)
215: throws CbesbException {
216: try {
217: NormalizedMessageHandler nmh = new NormalizedMessageHandler(
218: nm);
219:
220: StringSource ss = new StringSource(xml);
221: DOMResult dr = new DOMResult();
222: TransformerFactory tf = TransformerFactory.newInstance();
223: Transformer t = tf.newTransformer();
224: t.transform(ss, dr);
225: DOMSource ds = new DOMSource(dr.getNode());
226: nmh.addRecord(ds);
227:
228: nmh.generateMessageContent();
229: } catch (Exception ex) {
230: throw new CbesbException(
231: "AddXmlRecord to NormalizedMessage exception:"
232: + ex.toString());
233: }
234:
235: }
236:
237: }
|