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: AxiomDOMBuilder.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.stream.XMLStreamReader;
031:
032: import org.apache.axiom.om.OMAttribute;
033: import org.apache.axiom.om.OMElement;
034: import org.apache.axiom.om.OMFactory;
035: import org.apache.axiom.om.OMNode;
036: import org.apache.axiom.om.OMText;
037: import org.apache.axiom.om.impl.builder.StAXOMBuilder;
038: import org.apache.axiom.om.impl.dom.DOOMAbstractFactory;
039: import org.apache.commons.logging.Log;
040: import org.apache.commons.logging.LogFactory;
041: import org.w3c.dom.Document;
042: import org.w3c.dom.Element;
043:
044: /**
045: * Creates a DOM model by using the Axiom DOM OMFactory implementation.
046: * This creates a model that is both the Axiom OM interface and org.w3c.dom
047: * interface. This is best performance in most cases, but the resulting
048: * DOM model does not support DOM level 3 methods and there appears to be
049: * bugs in the MTOM support.
050: */
051: public class AxiomDOMBuilder implements IDOMBuilder {
052:
053: protected final transient Log logger = LogFactory
054: .getLog(getClass());
055:
056: private HashMap<String, DataHandler> inputAttachments;
057: private HashMap<String, DataHandler> outputAttachments;
058:
059: // private OMFactory factory = DOOMAbstractFactory.getOMFactory();
060:
061: /* (non-Javadoc)
062: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder#getDocument(org.apache.axiom.om.OMElement, java.util.HashMap)
063: */
064: public Document getDocument(OMElement omRoot,
065: HashMap<String, DataHandler> attachments, boolean useMTOM,
066: boolean useSwA) {
067: return getElement(omRoot, attachments, useMTOM, useSwA)
068: .getOwnerDocument();
069: }
070:
071: /* (non-Javadoc)
072: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder#getElement(org.apache.axiom.om.OMElement, java.util.HashMap)
073: */
074: public Element getElement(OMElement omRoot,
075: HashMap<String, DataHandler> attachments, boolean useMTOM,
076: boolean useSwA) {
077: this .inputAttachments = attachments;
078: this .outputAttachments = new HashMap<String, DataHandler>();
079: if (!(omRoot instanceof Element)) {
080: //Convert to DOM OM model
081: XMLStreamReader xmlStreamReader = omRoot
082: .getXMLStreamReader();
083: OMFactory doomFactory = DOOMAbstractFactory.getOMFactory(); //Create the new builder StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory, llomReader); //Get the document element OMElement newElem = doomBuilder.getDocumentElement();
084: StAXOMBuilder doomBuilder = new StAXOMBuilder(doomFactory,
085: xmlStreamReader);
086: omRoot = doomBuilder.getDocumentElement();
087: }
088:
089: if (useMTOM || useSwA) {
090: processAttachments(omRoot);
091: }
092:
093: return (Element) omRoot;
094:
095: }
096:
097: /* (non-Javadoc)
098: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IDOMBuilder#getOutputAttachments()
099: */
100: public HashMap<String, DataHandler> getOutputAttachments() {
101: return outputAttachments;
102: }
103:
104: private void processAttachments(OMElement omElement) {
105: //Check all attributes for SwA references
106: for (Iterator iter = omElement.getAllAttributes(); iter
107: .hasNext();) {
108: OMAttribute attr = (OMAttribute) iter.next();
109: DataHandler dh = getAttachment(attr.getAttributeValue());
110: if (dh != null) {
111: outputAttachments.put(attr.getAttributeValue(), dh);
112: }
113: }
114: OMNode node = omElement.getFirstOMChild();
115: while (node != null) {
116: if (node.getType() == OMNode.ELEMENT_NODE) {
117: processAttachments((OMElement) node);
118: } else if (node.getType() == OMNode.TEXT_NODE) {
119: OMText omText = (OMText) node;
120: if (!omText.isBinary() && !omText.isOptimized()) {
121: String text = omText.getText();
122: //Check if this is an SwA reference
123: DataHandler dh = getAttachment(text);
124: if (dh != null) {
125: outputAttachments.put(text, dh);
126: }
127:
128: } else {
129: //Change MTOM to SWA reference
130: //DOES NOT CURRENTLY WORK due to Axiom bug
131: logger
132: .warn("The selected marshaller does not support MTOM messages");
133: // DataHandler dh = (DataHandler)omText.getDataHandler();
134: // String contentId = omText.getContentID();
135: // outputAttachments.put(contentId, dh);
136: // OMText newNode = factory.createOMText(contentId);
137: // node.insertSiblingBefore(newNode);
138: // ((OMNodeEx)newNode).setNextOMSibling(node.getNextOMSibling());
139: // node.detach();
140: }
141: }
142:
143: node = node.getNextOMSibling();
144: }
145: }
146:
147: private DataHandler getAttachment(String contentId) {
148: if (inputAttachments.containsKey(contentId)) {
149: return inputAttachments.get(contentId);
150: } else if (contentId.startsWith("cid:")
151: && inputAttachments.containsKey(contentId.substring(4))) {
152: return inputAttachments.get(contentId.substring(4));
153: }
154: return null;
155: }
156: }
|