Source Code Cross Referenced for NormalizedMessageUtil.java in  » ESB » cbesb-1.2 » com » bostechcorp » cbesb » runtime » ccsl » lib » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » cbesb 1.2 » com.bostechcorp.cbesb.runtime.ccsl.lib 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.