001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 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: FullParseDOMBuilder.java 9709 2007-10-23 19:52:09Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028:
029: import javax.activation.DataHandler;
030: import javax.xml.namespace.QName;
031: import javax.xml.parsers.DocumentBuilder;
032: import javax.xml.parsers.DocumentBuilderFactory;
033: import javax.xml.parsers.ParserConfigurationException;
034:
035: import org.apache.axiom.om.OMAttribute;
036: import org.apache.axiom.om.OMElement;
037: import org.apache.axiom.om.OMNode;
038: import org.apache.axiom.om.OMText;
039: import org.apache.axiom.om.impl.dom.jaxp.DocumentBuilderFactoryImpl;
040: import org.apache.commons.logging.Log;
041: import org.apache.commons.logging.LogFactory;
042: import org.w3c.dom.Document;
043: import org.w3c.dom.Element;
044: import org.w3c.dom.Text;
045:
046: /**
047: * Creates a DOM model by re-parsing the original soap body content. This
048: * implementation does not rely on the Axiom DOM implementation and is more stable
049: * and compatible with other APIs using the resulting DOM message. The trade-off
050: * is slower performance.
051: */
052: public class FullParseDOMBuilder implements IDOMBuilder {
053:
054: protected final transient Log logger = LogFactory
055: .getLog(getClass());
056:
057: private HashMap<String, DataHandler> inputAttachments;
058: private HashMap<String, DataHandler> outputAttachments;
059: private DocumentBuilder docBuilder;
060: private Document doc;
061: // private boolean useMTOM;
062: private boolean useSwA;
063:
064: public FullParseDOMBuilder(boolean useAxiomDomImpl) {
065: try {
066: if (useAxiomDomImpl) {
067: DocumentBuilderFactoryImpl.setDOOMRequired(true);
068: }
069: DocumentBuilderFactory dbf = DocumentBuilderFactory
070: .newInstance();
071: dbf.setNamespaceAware(true);
072: docBuilder = dbf.newDocumentBuilder();
073: if (useAxiomDomImpl) {
074: DocumentBuilderFactoryImpl.setDOOMRequired(false);
075: }
076: } catch (ParserConfigurationException e) {
077: logger.error("Exception creating DocumentBuilder", e);
078: }
079: }
080:
081: /* (non-Javadoc)
082: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder#getDocument(org.apache.axiom.om.OMElement, java.util.HashMap)
083: */
084: public Document getDocument(OMElement omRoot,
085: HashMap<String, DataHandler> attachments, boolean useMTOM,
086: boolean useSwA) {
087: return getElement(omRoot, attachments, useMTOM, useSwA)
088: .getOwnerDocument();
089: }
090:
091: /* (non-Javadoc)
092: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder#getElement(org.apache.axiom.om.OMElement, java.util.HashMap)
093: */
094: public Element getElement(OMElement omRoot,
095: HashMap<String, DataHandler> attachments, boolean useMTOM,
096: boolean useSwA) {
097: this .inputAttachments = attachments;
098: this .outputAttachments = new HashMap<String, DataHandler>();
099: this .useSwA = useSwA;
100:
101: doc = docBuilder.newDocument();
102: Element root = createElement(omRoot);
103: doc.appendChild(root);
104:
105: return root;
106: }
107:
108: /* (non-Javadoc)
109: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder#getOutputAttachments()
110: */
111: public HashMap<String, DataHandler> getOutputAttachments() {
112:
113: return outputAttachments;
114: }
115:
116: private Element createElement(OMElement omElement) {
117: QName qname = omElement.getQName();
118: Element newElement = doc.createElementNS(qname
119: .getNamespaceURI(), qname.getLocalPart());
120:
121: for (Iterator iter = omElement.getAllAttributes(); iter
122: .hasNext();) {
123: OMAttribute attribute = (OMAttribute) iter.next();
124: QName attrQName = attribute.getQName();
125:
126: String text = attribute.getAttributeValue();
127: if (useSwA) {
128: //Check if this is an SwA reference
129: if (text.startsWith("cid:")) {
130: String contentId = text.substring(4);
131: if (inputAttachments.containsKey(contentId)) {
132: outputAttachments.put(contentId,
133: inputAttachments.get(contentId));
134: }
135: } else {
136: if (inputAttachments.containsKey(text)) {
137: outputAttachments.put(text, inputAttachments
138: .get(text));
139: text = "cid:" + text;
140: }
141: }
142: }
143:
144: newElement.setAttributeNS(attrQName.getNamespaceURI(),
145: attrQName.getLocalPart(), text);
146: }
147:
148: for (Iterator iter = omElement.getChildren(); iter.hasNext();) {
149: OMNode node = (OMNode) iter.next();
150: if (node.getType() == OMNode.ELEMENT_NODE) {
151: Element newChild = createElement((OMElement) node);
152: newElement.appendChild(newChild);
153: } else if (node.getType() == OMNode.TEXT_NODE) {
154: OMText omText = (OMText) node;
155: if (!omText.isBinary() && !omText.isOptimized()) {
156: String text = omText.getText();
157: if (useSwA) {
158: //Check if this is an SwA reference
159: if (text.startsWith("cid:")) {
160: String contentId = text.substring(4);
161: if (inputAttachments.containsKey(contentId)) {
162: outputAttachments
163: .put(contentId,
164: inputAttachments
165: .get(contentId));
166: }
167: } else {
168: if (inputAttachments.containsKey(text)) {
169: outputAttachments.put(text,
170: inputAttachments.get(text));
171: text = "cid:" + text;
172: }
173: }
174: }
175: Text textNode = doc.createTextNode(text);
176: newElement.appendChild(textNode);
177:
178: } else {
179: DataHandler dh = (DataHandler) omText
180: .getDataHandler();
181: String contentId = omText.getContentID();
182: outputAttachments.put(contentId, dh);
183: Text textNode = doc.createTextNode(contentId);
184: newElement.appendChild(textNode);
185: }
186:
187: }
188: }
189:
190: return newElement;
191: }
192: }
|