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: JbiWrapperHandler.java 9472 2007-10-09 15:24:17Z elu $
023: */
024: package com.bostechcorp.cbesb.runtime.ccsl.messages;
025:
026: import java.util.HashMap;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Vector;
030:
031: import javax.xml.namespace.QName;
032: import javax.xml.transform.Source;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerFactory;
035: import javax.xml.transform.dom.DOMResult;
036: import javax.xml.transform.dom.DOMSource;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040: import org.jdom.Attribute;
041: import org.jdom.Document;
042: import org.jdom.Element;
043: import org.jdom.Namespace;
044: import org.jdom.input.DOMBuilder;
045: import org.jdom.output.DOMOutputter;
046:
047: /**
048: * This one is not used.
049: *
050: * @deprecated
051: *
052: * @author elu
053: *
054: */
055: public class JbiWrapperHandler {
056:
057: public static final String JBI_WRAPPER_NS = "http://java.sun.com/xml/ns/jbi/wsdl-11-wrapper";
058: public static final String JBI_MESSAGE = "message";
059: public static final String JBI_VERSION = "version";
060: public static final String JBI_TYPE = "type";
061: public static final String JBI_NAME = "name";
062: public static final String JBI_PART = "part";
063:
064: protected transient Log log = LogFactory.getLog(getClass());
065:
066: private QName type;
067: private String name;
068: private Vector<Source> parts;
069:
070: private Namespace jbiNamespace;
071: private HashMap<String, Namespace> namespaceUriMap;
072: private HashMap<String, Namespace> namespacePrefixMap;
073: private int namespaceCounter;
074:
075: public JbiWrapperHandler() {
076: type = null;
077: name = null;
078: parts = new Vector<Source>();
079: namespaceUriMap = new HashMap<String, Namespace>();
080: namespacePrefixMap = new HashMap<String, Namespace>();
081: jbiNamespace = Namespace.getNamespace("jbi", JBI_WRAPPER_NS);
082: }
083:
084: /**
085: * @return the name
086: */
087: public String getName() {
088: return name;
089: }
090:
091: /**
092: * @param name the name to set
093: */
094: public void setName(String name) {
095: this .name = name;
096: }
097:
098: /**
099: * @return the type
100: */
101: public QName getType() {
102: return type;
103: }
104:
105: /**
106: * @param type the type to set
107: */
108: public void setType(QName type) {
109: this .type = type;
110: }
111:
112: public void appendPart(Source src) {
113: parts.add(src);
114: }
115:
116: public int getPartCount() {
117: return parts.size();
118: }
119:
120: public Source getPartAtIndex(int index) {
121: return parts.elementAt(index);
122: }
123:
124: public static JbiWrapperHandler createFromSource(Source src)
125: throws Exception {
126: JbiWrapperHandler jbiWrapper = null;
127: DOMBuilder domBuilder = new DOMBuilder();
128: Document jdomDoc = domBuilder.build(getDocFromSource(src));
129: Element rootElement = jdomDoc.getRootElement();
130:
131: if (!JBI_WRAPPER_NS.equals(rootElement.getNamespaceURI())
132: && !JBI_MESSAGE.equals(rootElement.getName())) {
133: throw new Exception("Unexpected root element: {"
134: + rootElement.getNamespaceURI() + "}"
135: + rootElement.getName() + " - should be: {"
136: + JBI_WRAPPER_NS + "}" + JBI_MESSAGE);
137: }
138: jbiWrapper = new JbiWrapperHandler();
139:
140: List nsList = rootElement.getAdditionalNamespaces();
141: for (Iterator iter = nsList.iterator(); iter.hasNext();) {
142: Namespace ns = (Namespace) iter.next();
143: jbiWrapper.addNamespaceToMaps(ns);
144: }
145:
146: List attrList = rootElement.getAttributes();
147: for (Iterator iter = attrList.iterator(); iter.hasNext();) {
148: Attribute attr = (Attribute) iter.next();
149: if (JBI_TYPE.equals(attr.getName())) {
150: QName type = jbiWrapper.prefixedNameToQName(attr
151: .getValue());
152: jbiWrapper.setType(type);
153: } else if (JBI_NAME.equals(attr.getName())) {
154: jbiWrapper.setName(attr.getValue());
155: }
156: }
157:
158: List children = rootElement.getChildren(JBI_PART, Namespace
159: .getNamespace(JBI_WRAPPER_NS));
160: for (Iterator iter = children.iterator(); iter.hasNext();) {
161: Element partElem = (Element) iter.next();
162: if (partElem.getChildren().size() > 0) {
163: Element child = (Element) partElem.getChildren().get(0);
164: Document partDoc = new Document((Element) child
165: .detach());
166: DOMOutputter domOut = new DOMOutputter();
167: org.w3c.dom.Document doc = domOut.output(partDoc);
168: DOMSource domSrc = new DOMSource(doc);
169: jbiWrapper.appendPart(domSrc);
170: }
171: }
172:
173: return jbiWrapper;
174: }
175:
176: public Source toSource() throws Exception {
177: Element rootElement = new Element(JBI_MESSAGE, jbiNamespace);
178: Document jdomDoc = new Document(rootElement);
179: rootElement.setAttribute(JBI_VERSION, "1.0");
180: rootElement.setAttribute(JBI_TYPE, QNameToPrefixedName(type));
181: if (name != null) {
182: rootElement.setAttribute(JBI_NAME, name);
183: }
184: AddNamespaceDeclarationsToRoot(rootElement);
185:
186: DOMBuilder domBuilder = new DOMBuilder();
187: for (int i = 0; i < parts.size(); i++) {
188: Source src = parts.elementAt(i);
189: Document jdomPartDoc = domBuilder
190: .build(getDocFromSource(src));
191: Element partElem = new Element(JBI_PART, jbiNamespace);
192: rootElement.addContent(partElem);
193: partElem.addContent(jdomPartDoc.detachRootElement());
194: }
195: DOMOutputter domOut = new DOMOutputter();
196: org.w3c.dom.Document doc = domOut.output(jdomDoc);
197:
198: return new DOMSource(doc);
199: }
200:
201: protected QName prefixedNameToQName(String prefixedName) {
202: String prefix;
203: String localName;
204: QName qname;
205: int index = prefixedName.indexOf(':');
206: if (index > 0) {
207: prefix = prefixedName.substring(0, index);
208: localName = prefixedName.substring(index + 1);
209: } else {
210: prefix = null;
211: localName = prefixedName;
212: }
213: Namespace ns = namespacePrefixMap.get(prefix);
214: if (ns != null) {
215: qname = new QName(ns.getURI(), localName);
216: } else {
217: log.error("Unable to resolve namespace prefix " + prefix);
218: qname = null;
219: }
220: return qname;
221: }
222:
223: protected String QNameToPrefixedName(QName qname) {
224: addNamespace(qname.getNamespaceURI());
225: Namespace ns = namespaceUriMap.get(qname.getNamespaceURI());
226: String prefix = ns.getPrefix();
227: if (prefix == null || prefix.equals("")) {
228: return qname.getLocalPart();
229: } else {
230: return prefix + ":" + qname.getLocalPart();
231: }
232:
233: }
234:
235: protected void addNamespace(String NamespaceURI) {
236: if (!namespaceUriMap.containsKey(NamespaceURI)) {
237: String prefix = "q" + namespaceCounter;
238: namespaceCounter++;
239: Namespace ns = Namespace.getNamespace(prefix, NamespaceURI);
240: addNamespaceToMaps(ns);
241: }
242: }
243:
244: protected void AddNamespaceDeclarationsToRoot(Element root) {
245: for (Iterator iter = namespaceUriMap.values().iterator(); iter
246: .hasNext();) {
247: root.addNamespaceDeclaration((Namespace) iter.next());
248: }
249: }
250:
251: private void addNamespaceToMaps(Namespace ns) {
252: namespaceUriMap.put(ns.getURI(), ns);
253: namespacePrefixMap.put(ns.getPrefix(), ns);
254: }
255:
256: protected static org.w3c.dom.Document getDocFromSource(
257: Source originalSource) throws Exception {
258: org.w3c.dom.Node domNode;
259: if (originalSource instanceof DOMSource) {
260: domNode = ((DOMSource) originalSource).getNode();
261: } else {
262: DOMResult dr = new DOMResult();
263: TransformerFactory tf = TransformerFactory.newInstance();
264: Transformer t = tf.newTransformer();
265: t.transform(originalSource, dr);
266: domNode = dr.getNode();
267: }
268: if (domNode instanceof org.w3c.dom.Document) {
269: return (org.w3c.dom.Document) domNode;
270: } else {
271: return ((org.w3c.dom.Element) domNode).getOwnerDocument();
272: }
273: }
274:
275: }
|