Source Code Cross Referenced for JAXBExtensionHelper.java in  » ESB » celtix-1.0 » org » objectweb » celtix » wsdl » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » ESB » celtix 1.0 » org.objectweb.celtix.wsdl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.