001: package org.objectweb.celtix.wsdl;
002:
003: import java.io.PrintWriter;
004: import java.lang.reflect.Method;
005: import java.util.Iterator;
006: import java.util.Map;
007:
008: import javax.wsdl.Definition;
009: import javax.wsdl.WSDLException;
010: import javax.wsdl.extensions.ExtensibilityElement;
011: import javax.wsdl.extensions.ExtensionDeserializer;
012: import javax.wsdl.extensions.ExtensionRegistry;
013: import javax.wsdl.extensions.ExtensionSerializer;
014: import javax.xml.bind.JAXBContext;
015: import javax.xml.bind.JAXBElement;
016: import javax.xml.bind.JAXBException;
017: import javax.xml.bind.Marshaller;
018: import javax.xml.bind.Unmarshaller;
019: import javax.xml.bind.annotation.XmlElementDecl;
020: import javax.xml.namespace.QName;
021: import javax.xml.stream.XMLStreamWriter;
022:
023: import org.w3c.dom.Element;
024:
025: /**
026: * JAXBExtensionHelper
027: * @author dkulp
028: *
029: */
030: public class JAXBExtensionHelper implements ExtensionSerializer,
031: ExtensionDeserializer {
032: final JAXBContext context;
033: final Class<? extends TExtensibilityElementImpl> typeClass;
034:
035: public JAXBExtensionHelper(JAXBContext c,
036: Class<? extends TExtensibilityElementImpl> cls) {
037: context = c;
038: typeClass = cls;
039: }
040:
041: public static void addExtensions(ExtensionRegistry registry,
042: Class<?> parentType,
043: Class<? extends TExtensibilityElementImpl> cls)
044: throws JAXBException {
045:
046: JAXBContext context = JAXBContext.newInstance(cls.getPackage()
047: .getName(), cls.getClassLoader());
048: JAXBExtensionHelper helper = new JAXBExtensionHelper(context,
049: cls);
050:
051: try {
052: Class<?> objectFactory = Class.forName(cls.getPackage()
053: .getName()
054: + ".ObjectFactory");
055: Method methods[] = objectFactory.getDeclaredMethods();
056: for (Method method : methods) {
057: if (method.getParameterTypes().length == 1
058: && method.getParameterTypes()[0].equals(cls)) {
059:
060: XmlElementDecl elementDecl = method
061: .getAnnotation(XmlElementDecl.class);
062: if (null != elementDecl) {
063: QName elementType = new QName(elementDecl
064: .namespace(), elementDecl.name());
065: registry.registerDeserializer(parentType,
066: elementType, helper);
067: registry.registerSerializer(parentType,
068: elementType, helper);
069: }
070: }
071: }
072: } catch (ClassNotFoundException ex) {
073: // TODO
074: ex.printStackTrace();
075: }
076: }
077:
078: /* (non-Javadoc)
079: * @see javax.wsdl.extensions.ExtensionSerializer#marshall(java.lang.Class,
080: * javax.xml.namespace.QName, javax.wsdl.extensions.ExtensibilityElement,
081: * java.io.PrintWriter, javax.wsdl.Definition, javax.wsdl.extensions.ExtensionRegistry)
082: */
083: public void marshall(Class parent, QName qname,
084: ExtensibilityElement obj, PrintWriter pw,
085: final Definition wsdl, ExtensionRegistry registry)
086: throws WSDLException {
087: // TODO Auto-generated method stub
088: try {
089: Marshaller u = context.createMarshaller();
090: u.setProperty("jaxb.encoding", "UTF-8");
091: u.setProperty("jaxb.fragment", Boolean.TRUE);
092: u.setProperty("jaxb.formatted.output", Boolean.TRUE);
093:
094: Object mObj = obj;
095:
096: Class<?> objectFactory = Class.forName(typeClass
097: .getPackage().getName()
098: + ".ObjectFactory");
099: Method methods[] = objectFactory.getDeclaredMethods();
100: for (Method method : methods) {
101: if (method.getParameterTypes().length == 1
102: && method.getParameterTypes()[0]
103: .equals(typeClass)) {
104:
105: mObj = method.invoke(objectFactory.newInstance(),
106: new Object[] { obj });
107: }
108: }
109:
110: javax.xml.stream.XMLOutputFactory fact = javax.xml.stream.XMLOutputFactory
111: .newInstance();
112: XMLStreamWriter writer = fact.createXMLStreamWriter(pw);
113: writer
114: .setNamespaceContext(new javax.xml.namespace.NamespaceContext() {
115:
116: public String getNamespaceURI(String arg) {
117: return wsdl.getNamespace(arg);
118: }
119:
120: public String getPrefix(String arg) {
121: for (Object ent : wsdl.getNamespaces()
122: .entrySet()) {
123: Map.Entry entry = (Map.Entry) ent;
124: if (arg.equals(entry.getValue())) {
125: return (String) entry.getKey();
126: }
127: }
128: return null;
129: }
130:
131: public Iterator getPrefixes(String arg) {
132: return wsdl.getNamespaces().keySet()
133: .iterator();
134: }
135: });
136:
137: u.marshal(mObj, writer);
138: writer.flush();
139: } catch (Exception ex) {
140: throw new WSDLException(WSDLException.PARSER_ERROR, "", ex);
141: }
142:
143: }
144:
145: /* (non-Javadoc)
146: * @see javax.wsdl.extensions.ExtensionDeserializer#unmarshall(java.lang.Class,
147: * javax.xml.namespace.QName, org.w3c.dom.Element,
148: * javax.wsdl.Definition,
149: * javax.wsdl.extensions.ExtensionRegistry)
150: */
151: public ExtensibilityElement unmarshall(Class parent, QName qname,
152: Element element, Definition wsdl, ExtensionRegistry registry)
153: throws WSDLException {
154: try {
155: Unmarshaller u = context.createUnmarshaller();
156:
157: Object o = u.unmarshal(element);
158: if (o instanceof JAXBElement<?>) {
159: JAXBElement<?> el = (JAXBElement<?>) o;
160: o = el.getValue();
161: }
162:
163: ExtensibilityElement el = o instanceof ExtensibilityElement ? (ExtensibilityElement) o
164: : null;
165: if (null != el) {
166: el.setElementType(qname);
167: }
168: return el;
169: } catch (Exception ex) {
170: throw new WSDLException(WSDLException.PARSER_ERROR,
171: "Error reading element " + qname, ex);
172: }
173: }
174:
175: }
|