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: SAXAxiomBuilder.java 9709 2007-10-23 19:52:09Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom;
025:
026: import java.io.ByteArrayInputStream;
027: import java.io.IOException;
028: import java.util.HashMap;
029:
030: import javax.activation.DataHandler;
031: import javax.jbi.messaging.MessagingException;
032: import javax.xml.parsers.ParserConfigurationException;
033: import javax.xml.parsers.SAXParser;
034: import javax.xml.parsers.SAXParserFactory;
035: import javax.xml.transform.Source;
036:
037: import org.apache.axiom.om.OMAbstractFactory;
038: import org.apache.axiom.om.OMContainer;
039: import org.apache.axiom.om.OMElement;
040: import org.apache.axiom.om.OMException;
041: import org.apache.axiom.om.OMFactory;
042: import org.apache.axiom.om.OMNode;
043: import org.apache.axiom.om.impl.OMContainerEx;
044: import org.apache.axiom.om.impl.OMNodeEx;
045: import org.xml.sax.Attributes;
046: import org.xml.sax.InputSource;
047: import org.xml.sax.Locator;
048: import org.xml.sax.SAXException;
049: import org.xml.sax.helpers.DefaultHandler;
050:
051: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.SourceHelper;
052:
053: /**
054: * General use IAxiomBuilder that is most reliable. The trade off is slower performance.
055: */
056: public class SAXAxiomBuilder extends DefaultHandler implements
057: IAxiomBuilder {
058:
059: private boolean useMTOM = false;
060: private boolean useSwA = false;
061:
062: private OMElement root = null;
063:
064: private OMNode lastNode = null;
065:
066: private OMElement nextElem = null;
067:
068: private OMFactory factory = OMAbstractFactory.getOMFactory();
069:
070: private HashMap<String, DataHandler> inputAttachments;
071: private HashMap<String, DataHandler> outputAttachments;
072:
073: /* (non-Javadoc)
074: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IAxiomBuilder#getOMTree(javax.xml.transform.Source, java.util.HashMap, boolean, boolean)
075: */
076: public OMElement getOMTree(Source source,
077: HashMap<String, DataHandler> attachments, boolean useMTOM,
078: boolean useSwA) throws MessagingException {
079: try {
080: this .useMTOM = useMTOM;
081: this .useSwA = useSwA;
082: this .inputAttachments = attachments;
083: this .outputAttachments = new HashMap<String, DataHandler>();
084: SAXParserFactory factory = SAXParserFactory.newInstance();
085: factory.setNamespaceAware(true);
086: SAXParser saxParser = factory.newSAXParser();
087: saxParser.parse(SourceToInputSource(source), this );
088: return root;
089: } catch (SAXException se) {
090: // If this SAXException is wrapping an MessagingException, unwrap it and re-throw
091: // If not, then create a new MessagingException
092: if ((se.getException() != null)
093: && (se.getException() instanceof MessagingException)) {
094: MessagingException me = (MessagingException) se
095: .getException();
096: throw me;
097: } else {
098: MessagingException me = new MessagingException(se);
099: throw me;
100: }
101: } catch (ParserConfigurationException pce) {
102: MessagingException me = new MessagingException(pce);
103: throw me;
104: } catch (IOException ioe) {
105: MessagingException me = new MessagingException(ioe);
106: throw me;
107: } catch (Exception e) {
108: MessagingException me = new MessagingException(e);
109: throw me;
110: }
111: }
112:
113: /* (non-Javadoc)
114: * @see com.bostechcorp.cbesb.runtime.component.http.marshallers.axiom.IAxiomBuilder#getOutputAttachments()
115: */
116: public HashMap<String, DataHandler> getOutputAttachments() {
117:
118: return outputAttachments;
119: }
120:
121: public void setDocumentLocator(Locator arg0) {
122: }
123:
124: public void startDocument() throws SAXException {
125:
126: }
127:
128: public void endDocument() throws SAXException {
129: }
130:
131: protected OMElement createNextElement(String localName)
132: throws OMException {
133: OMElement e;
134: if (lastNode == null) {
135: root = e = factory.createOMElement(localName, null, null,
136: null);
137: } else if (lastNode.isComplete()) {
138: e = factory.createOMElement(localName, null, lastNode
139: .getParent(), null);
140: ((OMNodeEx) lastNode).setNextOMSibling(e);
141: ((OMNodeEx) e).setPreviousOMSibling(lastNode);
142: } else {
143: OMContainerEx parent = (OMContainerEx) lastNode;
144: e = factory.createOMElement(localName, null,
145: (OMElement) lastNode, null);
146: parent.setFirstChild(e);
147: }
148: return e;
149: }
150:
151: /*
152: * (non-Javadoc)
153: *
154: * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
155: * java.lang.String)
156: */
157: public void startPrefixMapping(String prefix, String uri)
158: throws SAXException {
159: if (nextElem == null)
160: nextElem = createNextElement(null);
161: nextElem.declareNamespace(uri, prefix);
162: }
163:
164: public void endPrefixMapping(String arg0) throws SAXException {
165: }
166:
167: /*
168: * (non-Javadoc)
169: *
170: * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
171: * java.lang.String, java.lang.String, org.xml.sax.Attributes)
172: */
173: public void startElement(String namespaceURI, String localName,
174: String qName, Attributes atts) throws SAXException {
175: if (localName == null || localName.trim().equals(""))
176: localName = qName.substring(qName.indexOf(':') + 1);
177: if (nextElem == null)
178: nextElem = createNextElement(localName);
179: else
180: nextElem.setLocalName(localName);
181: nextElem.setNamespace(nextElem
182: .findNamespace(namespaceURI, null));
183: int j = atts.getLength();
184: for (int i = 0; i < j; i++) {
185: nextElem.addAttribute(atts.getLocalName(i), atts
186: .getValue(i), nextElem.findNamespace(
187: atts.getURI(i), null));
188: if (useSwA) {
189: DataHandler dh = getAttachment(atts.getValue(i));
190: if (dh != null) {
191: outputAttachments.put(atts.getValue(i), dh);
192: }
193: }
194: }
195: lastNode = nextElem;
196: nextElem = null;
197: }
198:
199: /*
200: * (non-Javadoc)
201: *
202: * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
203: * java.lang.String, java.lang.String)
204: */
205: public void endElement(String arg0, String arg1, String arg2)
206: throws SAXException {
207: if (lastNode.isComplete()) {
208: OMContainer parent = lastNode.getParent();
209: ((OMNodeEx) parent).setComplete(true);
210: lastNode = (OMNode) parent;
211: } else {
212: OMElement e = (OMElement) lastNode;
213: ((OMNodeEx) e).setComplete(true);
214: }
215: }
216:
217: /*
218: * (non-Javadoc)
219: *
220: * @see org.xml.sax.ContentHandler#characters(char[], int, int)
221: */
222: public void characters(char[] ch, int start, int length)
223: throws SAXException {
224: if (lastNode == null) {
225: throw new SAXException("");
226: }
227: OMNode node;
228: String text = new String(ch, start, length);
229: DataHandler dh = null;
230: if (useMTOM || useSwA) {
231: dh = getAttachment(text);
232: if (dh != null) {
233: outputAttachments.put(text, dh);
234: }
235: }
236: if (useMTOM && dh != null) {
237: node = factory.createOMText(dh, true);
238: } else {
239: node = factory.createOMText(text);
240: }
241:
242: if (lastNode.isComplete()) {
243: lastNode.getParent().addChild(node);
244: ((OMNodeEx) lastNode).setNextOMSibling(node);
245: ((OMNodeEx) node).setPreviousOMSibling(lastNode);
246: } else {
247: OMContainerEx e = (OMContainerEx) lastNode;
248: e.addChild(node);
249: e.setFirstChild(node);
250: }
251: lastNode = node;
252: }
253:
254: public void ignorableWhitespace(char[] arg0, int arg1, int arg2)
255: throws SAXException {
256: }
257:
258: public void processingInstruction(String arg0, String arg1)
259: throws SAXException {
260: }
261:
262: public void skippedEntity(String arg0) throws SAXException {
263: }
264:
265: private DataHandler getAttachment(String contentId) {
266: if (inputAttachments.containsKey(contentId)) {
267: return inputAttachments.get(contentId);
268: } else if (contentId.startsWith("cid:")
269: && inputAttachments.containsKey(contentId.substring(4))) {
270: return inputAttachments.get(contentId.substring(4));
271: }
272: return null;
273: }
274:
275: protected static InputSource SourceToInputSource(Source src)
276: throws Exception {
277: String tmpStr = SourceHelper.createString(src);
278: ByteArrayInputStream inputStream = new ByteArrayInputStream(
279: tmpStr.getBytes());
280: return new InputSource(inputStream);
281: }
282:
283: }
|