Source Code Cross Referenced for BeanUtil.java in  » Web-Services-AXIS2 » adb » org » apache » axis2 » databinding » utils » 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 » Web Services AXIS2 » adb » org.apache.axis2.databinding.utils 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one
003:         * or more contributor license agreements. See the NOTICE file
004:         * distributed with this work for additional information
005:         * regarding copyright ownership. The ASF licenses this file
006:         * to you under the Apache License, Version 2.0 (the
007:         * "License"); you may not use this file except in compliance
008:         * with the License. You may obtain a copy of the License at
009:         *
010:         * http://www.apache.org/licenses/LICENSE-2.0
011:         *
012:         * Unless required by applicable law or agreed to in writing,
013:         * software distributed under the License is distributed on an
014:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015:         * KIND, either express or implied. See the License for the
016:         * specific language governing permissions and limitations
017:         * under the License.
018:         */
019:        package org.apache.axis2.databinding.utils;
020:
021:        import org.apache.axiom.om.*;
022:        import org.apache.axiom.om.impl.builder.StAXOMBuilder;
023:        import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
024:        import org.apache.axiom.om.util.Base64;
025:        import org.apache.axis2.AxisFault;
026:        import org.apache.axis2.description.java2wsdl.TypeTable;
027:        import org.apache.axis2.databinding.typemapping.SimpleTypeMapper;
028:        import org.apache.axis2.databinding.utils.reader.ADBXMLStreamReaderImpl;
029:        import org.apache.axis2.engine.ObjectSupplier;
030:        import org.apache.axis2.util.StreamWrapper;
031:        import org.apache.axis2.util.Loader;
032:        import org.codehaus.jam.JClass;
033:        import org.codehaus.jam.JProperty;
034:        import org.codehaus.jam.JamClassIterator;
035:        import org.codehaus.jam.JamService;
036:        import org.codehaus.jam.JamServiceFactory;
037:        import org.codehaus.jam.JamServiceParams;
038:
039:        import javax.xml.namespace.QName;
040:        import javax.xml.stream.XMLStreamReader;
041:        import java.beans.BeanInfo;
042:        import java.beans.IntrospectionException;
043:        import java.beans.Introspector;
044:        import java.beans.PropertyDescriptor;
045:        import java.lang.reflect.Array;
046:        import java.lang.reflect.InvocationTargetException;
047:        import java.lang.reflect.Method;
048:        import java.util.ArrayList;
049:        import java.util.Arrays;
050:        import java.util.Collection;
051:        import java.util.HashMap;
052:        import java.util.Iterator;
053:        import java.util.Locale;
054:
055:        public class BeanUtil {
056:
057:            private static int nsCount = 1;
058:
059:            /**
060:             * To Serilize Bean object this method is used, this will create an object array using given
061:             * bean object
062:             *
063:             */
064:            public static XMLStreamReader getPullParser(Object beanObject,
065:                    QName beanName, TypeTable typeTable, boolean qualified,
066:                    boolean processingDocLitBare) {
067:                try {
068:                    JamServiceFactory factory = JamServiceFactory.getInstance();
069:                    JamServiceParams jam_service_parms = factory
070:                            .createServiceParams();
071:                    ClassLoader cl = beanObject.getClass().getClassLoader();
072:                    if (cl == null)
073:                        cl = ClassLoader.getSystemClassLoader();
074:                    jam_service_parms.addClassLoader(cl);
075:
076:                    jam_service_parms.includeClass(beanObject.getClass()
077:                            .getName());
078:                    JamService service = factory
079:                            .createService(jam_service_parms);
080:                    JamClassIterator jClassIter = service.getClasses();
081:                    JClass jClass;
082:                    if (jClassIter.hasNext()) {
083:                        jClass = (JClass) jClassIter.next();
084:                    } else {
085:                        throw new AxisFault(
086:                                "No service class found , exception from JAM");
087:                    }
088:                    QName elemntNameSpace = null;
089:                    if (typeTable != null && qualified) {
090:                        QName qNamefortheType = typeTable
091:                                .getQNamefortheType(beanObject.getClass()
092:                                        .getName());
093:                        if (qNamefortheType == null) {
094:                            qNamefortheType = typeTable
095:                                    .getQNamefortheType(beanObject.getClass()
096:                                            .getPackage().getName());
097:                        }
098:                        if (qNamefortheType == null) {
099:                            throw new AxisFault(
100:                                    "Mapping qname not fond for the package: "
101:                                            + beanObject.getClass()
102:                                                    .getPackage().getName());
103:                        }
104:
105:                        elemntNameSpace = new QName(qNamefortheType
106:                                .getNamespaceURI(), "elementName");
107:                    }
108:
109:                    // properties from JAM
110:                    ArrayList propertyList = new ArrayList();
111:                    JProperty properties[] = jClass.getDeclaredProperties();
112:                    for (int i = 0; i < properties.length; i++) {
113:                        JProperty property = properties[i];
114:                        propertyList.add(property);
115:                    }
116:                    JClass supClass = jClass.getSuperclass();
117:                    while (!"java.lang.Object".equals(supClass
118:                            .getQualifiedName())) {
119:                        properties = supClass.getDeclaredProperties();
120:                        for (int i = 0; i < properties.length; i++) {
121:                            JProperty property = properties[i];
122:                            propertyList.add(property);
123:                        }
124:                        supClass = supClass.getSuperclass();
125:                    }
126:                    properties = new JProperty[propertyList.size()];
127:                    for (int i = 0; i < propertyList.size(); i++) {
128:                        JProperty jProperty = (JProperty) propertyList.get(i);
129:                        properties[i] = jProperty;
130:                    }
131:                    Arrays.sort(properties);
132:                    BeanInfo beanInfo = Introspector.getBeanInfo(beanObject
133:                            .getClass());
134:                    PropertyDescriptor[] propDescs = beanInfo
135:                            .getPropertyDescriptors();
136:                    HashMap propertMap = new HashMap();
137:                    for (int i = 0; i < propDescs.length; i++) {
138:                        PropertyDescriptor propDesc = propDescs[i];
139:                        propertMap.put(propDesc.getName(), propDesc);
140:                    }
141:                    ArrayList object = new ArrayList();
142:                    for (int i = 0; i < properties.length; i++) {
143:                        JProperty property = properties[i];
144:                        PropertyDescriptor propDesc = (PropertyDescriptor) propertMap
145:                                .get(getCorrectName(property.getSimpleName()));
146:                        if (propDesc == null) {
147:                            // JAM does bad thing so I need to add this
148:                            continue;
149:                        }
150:                        Class ptype = propDesc.getPropertyType();
151:                        if (propDesc.getName().equals("class")) {
152:                            continue;
153:                        }
154:                        if (SimpleTypeMapper.isSimpleType(ptype)) {
155:                            Method readMethod = propDesc.getReadMethod();
156:                            Object value;
157:                            if (readMethod != null) {
158:                                value = readMethod.invoke(beanObject, null);
159:                            } else {
160:                                throw new AxisFault(
161:                                        "can not find read method for : "
162:                                                + propDesc.getName());
163:                            }
164:
165:                            addTypeQname(elemntNameSpace, object, propDesc,
166:                                    beanName, processingDocLitBare);
167:                            object.add(value == null ? null : SimpleTypeMapper
168:                                    .getStringValue(value));
169:                        } else if (ptype.isArray()) {
170:                            if (SimpleTypeMapper.isSimpleType(ptype
171:                                    .getComponentType())) {
172:                                Method readMethod = propDesc.getReadMethod();
173:                                Object value;
174:                                if (readMethod != null) {
175:                                    value = readMethod.invoke(beanObject, null);
176:                                } else {
177:                                    throw new AxisFault(
178:                                            "can not find read method for : "
179:                                                    + propDesc.getName());
180:                                }
181:                                if (value != null) {
182:                                    if ("byte".equals(ptype.getComponentType()
183:                                            .getName())) {
184:                                        addTypeQname(elemntNameSpace, object,
185:                                                propDesc, beanName,
186:                                                processingDocLitBare);
187:                                        object.add(Base64
188:                                                .encode((byte[]) value));
189:                                    } else {
190:                                        int i1 = Array.getLength(value);
191:                                        for (int j = 0; j < i1; j++) {
192:                                            Object o = Array.get(value, j);
193:                                            addTypeQname(elemntNameSpace,
194:                                                    object, propDesc, beanName,
195:                                                    processingDocLitBare);
196:                                            object.add(o == null ? null
197:                                                    : SimpleTypeMapper
198:                                                            .getStringValue(o));
199:                                        }
200:                                    }
201:                                } else {
202:                                    addTypeQname(elemntNameSpace, object,
203:                                            propDesc, beanName,
204:                                            processingDocLitBare);
205:                                    object.add(value);
206:                                }
207:                            } else {
208:                                Object value[] = (Object[]) propDesc
209:                                        .getReadMethod().invoke(beanObject,
210:                                                null);
211:                                if (value != null) {
212:                                    for (int j = 0; j < value.length; j++) {
213:                                        Object o = value[j];
214:                                        addTypeQname(elemntNameSpace, object,
215:                                                propDesc, beanName,
216:                                                processingDocLitBare);
217:                                        object.add(o);
218:                                    }
219:                                } else {
220:                                    addTypeQname(elemntNameSpace, object,
221:                                            propDesc, beanName,
222:                                            processingDocLitBare);
223:                                    object.add(value);
224:                                }
225:                            }
226:                        } else if (SimpleTypeMapper.isCollection(ptype)) {
227:                            Object value = propDesc.getReadMethod().invoke(
228:                                    beanObject, null);
229:                            Collection objList = (Collection) value;
230:                            if (objList != null && objList.size() > 0) {
231:                                //this was given error , when the array.size = 0
232:                                // and if the array contain simple type , then the ADBPullParser asked
233:                                // PullParser from That simpel type
234:                                for (Iterator j = objList.iterator(); j
235:                                        .hasNext();) {
236:                                    Object o = j.next();
237:                                    if (SimpleTypeMapper.isSimpleType(o)) {
238:                                        addTypeQname(elemntNameSpace, object,
239:                                                propDesc, beanName,
240:                                                processingDocLitBare);
241:                                        object.add(o);
242:                                    } else {
243:                                        addTypeQname(elemntNameSpace, object,
244:                                                propDesc, beanName,
245:                                                processingDocLitBare);
246:                                        object.add(o);
247:                                    }
248:                                }
249:
250:                            } else {
251:                                addTypeQname(elemntNameSpace, object, propDesc,
252:                                        beanName, processingDocLitBare);
253:                                object.add(value);
254:                            }
255:                        } else {
256:                            addTypeQname(elemntNameSpace, object, propDesc,
257:                                    beanName, processingDocLitBare);
258:                            Object value = propDesc.getReadMethod().invoke(
259:                                    beanObject, null);
260:                            object.add(value);
261:                        }
262:                    }
263:                    // Added objectAttributes as a fix for issues AXIS2-2055 and AXIS2-1899 to 
264:                    // support polymorphism in POJO approach.
265:                    // For some reason, using QName(Constants.XSI_NAMESPACE, "type", "xsi") does not generate
266:                    // an xsi:type attribtue properly for inner objects. So just using a simple QName("type").
267:                    ArrayList objectAttributes = new ArrayList();
268:                    objectAttributes.add(new QName("type"));
269:                    objectAttributes.add(beanObject.getClass().getName());
270:                    return new ADBXMLStreamReaderImpl(beanName, object
271:                            .toArray(), objectAttributes.toArray(), typeTable,
272:                            qualified);
273:
274:                } catch (java.io.IOException e) {
275:                    throw new RuntimeException(e);
276:                } catch (java.beans.IntrospectionException e) {
277:                    throw new RuntimeException(e);
278:                } catch (java.lang.reflect.InvocationTargetException e) {
279:                    throw new RuntimeException(e);
280:                } catch (java.lang.IllegalAccessException e) {
281:                    throw new RuntimeException(e);
282:                }
283:            }
284:
285:            private static void addTypeQname(QName elemntNameSpace,
286:                    ArrayList object, PropertyDescriptor propDesc,
287:                    QName beanName, boolean processingDocLitBare) {
288:                if (elemntNameSpace != null) {
289:                    object.add(new QName(elemntNameSpace.getNamespaceURI(),
290:                            propDesc.getName(), elemntNameSpace.getPrefix()));
291:                } else {
292:                    if (processingDocLitBare) {
293:                        object.add(new QName(propDesc.getName()));
294:                    } else {
295:                        object.add(new QName(beanName.getNamespaceURI(),
296:                                propDesc.getName(), beanName.getPrefix()));
297:                    }
298:
299:                }
300:            }
301:
302:            /**
303:             * to get the pull parser for a given bean object , generate the wrpper element using class
304:             * name
305:             *
306:             * @param beanObject
307:             */
308:            public static XMLStreamReader getPullParser(Object beanObject) {
309:                String className = beanObject.getClass().getName();
310:                if (className.indexOf(".") > 0) {
311:                    className = className.substring(
312:                            className.lastIndexOf('.') + 1, className.length());
313:                }
314:                return getPullParser(beanObject, new QName(className), null,
315:                        false, false);
316:            }
317:
318:            public static Object deserialize(Class beanClass,
319:                    OMElement beanElement, ObjectSupplier objectSupplier,
320:                    String arrayLocalName) throws AxisFault {
321:                Object beanObj = null;
322:                try {
323:                    // Added this block as a fix for issues AXIS2-2055 and AXIS2-1899
324:                    // to support polymorphism in POJO approach.
325:                    // Retrieve the type name of the instance from the 'type' attribute
326:                    // and retrieve the class.
327:                    String instanceTypeName = beanElement
328:                            .getAttributeValue(new QName("type"));
329:                    if ((instanceTypeName != null) && (!beanClass.isArray())) {
330:                        try {
331:                            beanClass = Loader.loadClass(beanClass
332:                                    .getClassLoader(), instanceTypeName);
333:                        } catch (ClassNotFoundException ce) {
334:                            throw AxisFault.makeFault(ce);
335:                        }
336:                    }
337:
338:                    if (beanClass.isArray()) {
339:                        ArrayList valueList = new ArrayList();
340:                        Class arrayClassType = beanClass.getComponentType();
341:                        if ("byte".equals(arrayClassType.getName())) {
342:                            return Base64.decode(beanElement.getFirstElement()
343:                                    .getText());
344:                        } else {
345:                            Iterator parts = beanElement.getChildElements();
346:                            OMElement omElement;
347:                            while (parts.hasNext()) {
348:                                Object objValue = parts.next();
349:                                if (objValue instanceof  OMElement) {
350:                                    omElement = (OMElement) objValue;
351:                                    if (!arrayLocalName.equals(omElement
352:                                            .getLocalName())) {
353:                                        continue;
354:                                    }
355:                                    Object obj = deserialize(arrayClassType,
356:                                            omElement, objectSupplier, null);
357:                                    if (obj != null) {
358:                                        valueList.add(obj);
359:                                    }
360:                                }
361:                            }
362:                            return ConverterUtil.convertToArray(arrayClassType,
363:                                    valueList);
364:                        }
365:                    } else {
366:                        if (SimpleTypeMapper.isSimpleType(beanClass)) {
367:                            return SimpleTypeMapper.getSimpleTypeObject(
368:                                    beanClass, beanElement);
369:                        } else if ("java.lang.Object".equals(beanClass
370:                                .getName())) {
371:                            return beanElement.getFirstOMChild();
372:                        }
373:                        HashMap properties = new HashMap();
374:                        BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
375:                        PropertyDescriptor[] propDescs = beanInfo
376:                                .getPropertyDescriptors();
377:                        for (int i = 0; i < propDescs.length; i++) {
378:                            PropertyDescriptor proprty = propDescs[i];
379:                            properties.put(proprty.getName(), proprty);
380:                        }
381:                        Iterator elements = beanElement.getChildren();
382:                        if (beanObj == null) {
383:                            beanObj = objectSupplier.getObject(beanClass);
384:                        }
385:                        while (elements.hasNext()) {
386:                            // the beanClass could be an abstract one.
387:                            // so create an instance only if there are elements, in
388:                            // which case a concrete subclass is available to instantiate.
389:                            OMElement parts;
390:                            Object objValue = elements.next();
391:                            if (objValue instanceof  OMElement) {
392:                                parts = (OMElement) objValue;
393:                            } else {
394:                                continue;
395:                            }
396:                            OMAttribute attribute = parts
397:                                    .getAttribute(new QName(
398:                                            "http://www.w3.org/2001/XMLSchema-instance",
399:                                            "nil", "xsi"));
400:
401:                            // if parts/@href != null then need to find element with id and deserialize.
402:                            // before that first check whether we already have it in the hashtable
403:                            String partsLocalName = parts.getLocalName();
404:                            PropertyDescriptor prty = (PropertyDescriptor) properties
405:                                    .remove(partsLocalName);
406:                            if (prty != null) {
407:                                Class parameters = prty.getPropertyType();
408:                                if (prty.equals("class"))
409:                                    continue;
410:
411:                                Object partObj;
412:                                if (attribute != null) {
413:                                    partObj = null;
414:                                } else {
415:                                    if (SimpleTypeMapper
416:                                            .isSimpleType(parameters)) {
417:                                        partObj = SimpleTypeMapper
418:                                                .getSimpleTypeObject(
419:                                                        parameters, parts);
420:                                    } else if (SimpleTypeMapper
421:                                            .isCollection(parameters)) {
422:                                        partObj = SimpleTypeMapper
423:                                                .getArrayList((OMElement) parts
424:                                                        .getParent(), prty
425:                                                        .getName());
426:                                    } else if (SimpleTypeMapper
427:                                            .isDataHandler(parameters)) {
428:                                        partObj = SimpleTypeMapper
429:                                                .getDataHandler(parts);
430:                                    } else if (parameters.isArray()) {
431:                                        partObj = deserialize(parameters,
432:                                                (OMElement) parts.getParent(),
433:                                                objectSupplier, prty.getName());
434:                                    } else {
435:                                        partObj = deserialize(parameters,
436:                                                parts, objectSupplier, null);
437:                                    }
438:                                }
439:                                Object[] parms = new Object[] { partObj };
440:                                Method writeMethod = prty.getWriteMethod();
441:                                if (writeMethod != null) {
442:                                    writeMethod.invoke(beanObj, parms);
443:                                }
444:                            }
445:                        }
446:                        return beanObj;
447:                    }
448:                } catch (IllegalAccessException e) {
449:                    throw new AxisFault("IllegalAccessException : " + e);
450:                } catch (InvocationTargetException e) {
451:                    throw new AxisFault("InvocationTargetException : " + e);
452:                } catch (IntrospectionException e) {
453:                    throw new AxisFault("IntrospectionException : " + e);
454:                }
455:
456:            }
457:
458:            public static Object deserialize(Class beanClass,
459:                    OMElement beanElement, MultirefHelper helper,
460:                    ObjectSupplier objectSupplier) throws AxisFault {
461:                Object beanObj;
462:                try {
463:                    HashMap properties = new HashMap();
464:                    BeanInfo beanInfo = Introspector.getBeanInfo(beanClass);
465:                    PropertyDescriptor[] propDescs = beanInfo
466:                            .getPropertyDescriptors();
467:                    for (int i = 0; i < propDescs.length; i++) {
468:                        PropertyDescriptor proprty = propDescs[i];
469:                        properties.put(proprty.getName(), proprty);
470:                    }
471:
472:                    beanObj = objectSupplier.getObject(beanClass);
473:                    Iterator elements = beanElement.getChildren();
474:                    while (elements.hasNext()) {
475:                        Object child = elements.next();
476:                        OMElement parts;
477:                        if (child instanceof  OMElement) {
478:                            parts = (OMElement) child;
479:                        } else {
480:                            continue;
481:                        }
482:                        String partsLocalName = parts.getLocalName();
483:                        PropertyDescriptor prty = (PropertyDescriptor) properties
484:                                .get(partsLocalName.toLowerCase());
485:                        if (prty != null) {
486:                            Class parameters = prty.getPropertyType();
487:                            if (prty.equals("class"))
488:                                continue;
489:                            Object partObj;
490:                            OMAttribute attr = MultirefHelper
491:                                    .processRefAtt(parts);
492:                            if (attr != null) {
493:                                String refId = MultirefHelper.getAttvalue(attr);
494:                                partObj = helper.getObject(refId);
495:                                if (partObj == null) {
496:                                    partObj = helper.processRef(parameters,
497:                                            refId, objectSupplier);
498:                                }
499:                            } else {
500:                                partObj = SimpleTypeMapper.getSimpleTypeObject(
501:                                        parameters, parts);
502:                                if (partObj == null) {
503:                                    partObj = deserialize(parameters, parts,
504:                                            objectSupplier, null);
505:                                }
506:                            }
507:                            Object[] parms = new Object[] { partObj };
508:                            Method writeMethod = prty.getWriteMethod();
509:                            if (writeMethod != null) {
510:                                writeMethod.invoke(beanObj, parms);
511:                            }
512:                        }
513:                    }
514:                } catch (IllegalAccessException e) {
515:                    throw new AxisFault("IllegalAccessException : " + e);
516:                } catch (InvocationTargetException e) {
517:                    throw new AxisFault("InvocationTargetException : " + e);
518:                } catch (IntrospectionException e) {
519:                    throw new AxisFault("IntrospectionException : " + e);
520:                }
521:                return beanObj;
522:            }
523:
524:            /**
525:             * To get JavaObjects from XML elemnt , the element most of the time contains only one element
526:             * in that case that element will be converted to the JavaType specified by the javaTypes array
527:             * The algo is as follows, get the childerns of the response element , and if it conatian more
528:             * than one element then check the retuen type of that element and conver that to corresponding
529:             * JavaType
530:             *
531:             * @param response  OMElement
532:             * @param javaTypes Array of JavaTypes
533:             * @return Array of objects
534:             * @throws AxisFault
535:             */
536:            public static Object[] deserialize(OMElement response,
537:                    Object[] javaTypes, ObjectSupplier objectSupplier)
538:                    throws AxisFault {
539:                /*
540:                 * Take the number of parameters in the method and , only take that much of child elements
541:                 * from the OMElement , other are ignore , as an example
542:                 * if the method is , foo(String a , int b)
543:                 * and if the OMElemet
544:                 * <foo>
545:                 *  <arg0>Val1</arg0>
546:                 *  <arg1>Val2</arg1>
547:                 *  <arg2>Val3</arg2>
548:                 *
549:                 * only the val1 and Val2 take into account
550:                 */
551:                int length = javaTypes.length;
552:                int count = 0;
553:                Object[] retObjs = new Object[length];
554:
555:                /*
556:                 * If the body first child contains , then there can not be any other element withot
557:                 * refs , so I can assume if the first child of the body first element has ref then
558:                 * the message has to handle as mutiref message.
559:                 * as an exmple if the body is like below
560:                 * <foo>
561:                 *  <arg0 href="#0"/>
562:                 * </foo>
563:                 *
564:                 * then there can not be any element without refs , meaning following we are not handling
565:                 * <foo>
566:                 *  <arg0 href="#0"/>
567:                 *  <arg1>absbsbs</arg1>
568:                 * </foo>
569:                 */
570:                Iterator parts = response.getChildren();
571:                //to handle multirefs
572:                //have to check the instanceof
573:                MultirefHelper helper = new MultirefHelper((OMElement) response
574:                        .getParent());
575:                //to support array . if the parameter type is array , then all the omelemnts with that paramtre name
576:                // has to  get and add to the list
577:                Class classType;
578:                String currentLocalName;
579:                while (parts.hasNext() && count < length) {
580:                    Object objValue = parts.next();
581:                    OMElement omElement;
582:                    if (objValue instanceof  OMElement) {
583:                        omElement = (OMElement) objValue;
584:                    } else {
585:                        continue;
586:                    }
587:                    currentLocalName = omElement.getLocalName();
588:                    classType = (Class) javaTypes[count];
589:                    omElement = ProcessElement(classType, omElement, helper,
590:                            parts, currentLocalName, retObjs, count,
591:                            objectSupplier);
592:                    while (omElement != null) {
593:                        count++;
594:                        omElement = ProcessElement((Class) javaTypes[count],
595:                                omElement, helper, parts, omElement
596:                                        .getLocalName(), retObjs, count,
597:                                objectSupplier);
598:                    }
599:                    count++;
600:                }
601:
602:                // Ensure that we have at least a zero element array
603:                for (int i = 0; i < length; i++) {
604:                    Class clazz = (Class) javaTypes[i];
605:                    if (retObjs[i] == null && clazz.isArray()) {
606:                        retObjs[i] = Array.newInstance(
607:                                clazz.getComponentType(), 0);
608:                    }
609:                }
610:
611:                helper.clean();
612:                return retObjs;
613:            }
614:
615:            private static OMElement ProcessElement(Class classType,
616:                    OMElement omElement, MultirefHelper helper, Iterator parts,
617:                    String currentLocalName, Object[] retObjs, int count,
618:                    ObjectSupplier objectSupplier) throws AxisFault {
619:                Object objValue;
620:                if (classType.isArray()) {
621:                    boolean done = true;
622:                    ArrayList valueList = new ArrayList();
623:                    Class arrayClassType = classType.getComponentType();
624:                    if ("byte".equals(arrayClassType.getName())) {
625:                        retObjs[count] = processObject(omElement,
626:                                arrayClassType, helper, true, objectSupplier);
627:                        return null;
628:                    } else {
629:                        valueList.add(processObject(omElement, arrayClassType,
630:                                helper, true, objectSupplier));
631:                    }
632:                    while (parts.hasNext()) {
633:                        objValue = parts.next();
634:                        if (objValue instanceof  OMElement) {
635:                            omElement = (OMElement) objValue;
636:                        } else {
637:                            continue;
638:                        }
639:                        if (!currentLocalName.equals(omElement.getLocalName())) {
640:                            done = false;
641:                            break;
642:                        }
643:                        Object o = processObject(omElement, arrayClassType,
644:                                helper, true, objectSupplier);
645:                        valueList.add(o);
646:                    }
647:                    if (valueList.get(0) == null) {
648:                        retObjs[count] = null;
649:                    } else {
650:                        retObjs[count] = ConverterUtil.convertToArray(
651:                                arrayClassType, valueList);
652:                    }
653:                    if (!done) {
654:                        return omElement;
655:                    }
656:                } else {
657:                    //handling refs
658:                    retObjs[count] = processObject(omElement, classType,
659:                            helper, false, objectSupplier);
660:                }
661:                return null;
662:            }
663:
664:            public static Object processObject(OMElement omElement,
665:                    Class classType, MultirefHelper helper,
666:                    boolean isArrayType, ObjectSupplier objectSupplier)
667:                    throws AxisFault {
668:                boolean hasRef = false;
669:                OMAttribute omatribute = MultirefHelper
670:                        .processRefAtt(omElement);
671:                String ref = null;
672:                if (omatribute != null) {
673:                    hasRef = true;
674:                    ref = MultirefHelper.getAttvalue(omatribute);
675:                }
676:                if (OMElement.class.isAssignableFrom(classType)) {
677:                    if (hasRef) {
678:                        OMElement elemnt = helper.getOMElement(ref);
679:                        if (elemnt == null) {
680:                            return helper.processOMElementRef(ref);
681:                        } else {
682:                            return elemnt;
683:                        }
684:                    } else
685:                        return omElement;
686:                } else {
687:                    if (hasRef) {
688:                        if (helper.getObject(ref) != null) {
689:                            return helper.getObject(ref);
690:                        } else {
691:                            return helper.processRef(classType, ref,
692:                                    objectSupplier);
693:                        }
694:                    } else {
695:                        OMAttribute attribute = omElement
696:                                .getAttribute(new QName(
697:                                        "http://www.w3.org/2001/XMLSchema-instance",
698:                                        "nil", "xsi"));
699:                        if (attribute != null) {
700:                            return null;
701:                        }
702:                        if (SimpleTypeMapper.isSimpleType(classType)) {
703:                            if (isArrayType
704:                                    && "byte".equals(classType.getName())) {
705:                                String value = omElement.getText();
706:                                return Base64.decode(value);
707:                            } else {
708:                                return SimpleTypeMapper.getSimpleTypeObject(
709:                                        classType, omElement);
710:                            }
711:                        } else if (SimpleTypeMapper.isCollection(classType)) {
712:                            return SimpleTypeMapper.getArrayList(omElement);
713:                        } else if (SimpleTypeMapper.isDataHandler(classType)) {
714:                            return SimpleTypeMapper.getDataHandler(omElement);
715:                        } else {
716:                            return BeanUtil.deserialize(classType, omElement,
717:                                    objectSupplier, null);
718:                        }
719:                    }
720:                }
721:            }
722:
723:            public static OMElement getOMElement(QName opName, Object[] args,
724:                    QName partName, boolean qualifed, TypeTable typeTable) {
725:                ArrayList objects;
726:                objects = new ArrayList();
727:                int argCount = 0;
728:                for (int i = 0; i < args.length; i++) {
729:                    Object arg = args[i];
730:                    if (arg == null) {
731:                        if (partName == null) {
732:                            objects.add("item" + argCount);
733:                        } else {
734:                            objects.add(partName);
735:                        }
736:                        objects.add(arg);
737:                        continue;
738:                    }
739:                    //todo if the request parameter has name other than argi (0<i<n) , there should be a
740:                    //way to do that , to solve that problem we need to have RPCRequestParameter
741:                    //note that The value of request parameter can either be simple type or JavaBean
742:                    if (arg instanceof  Object[]) {
743:                        Object array[] = (Object[]) arg;
744:                        for (int j = 0; j < array.length; j++) {
745:                            Object o = array[j];
746:                            if (o == null) {
747:                                objects.add("item" + argCount);
748:                                objects.add(o);
749:                            } else {
750:                                if (SimpleTypeMapper.isSimpleType(o)) {
751:                                    objects.add("item" + argCount);
752:                                    objects.add(SimpleTypeMapper
753:                                            .getStringValue(o));
754:                                } else {
755:                                    objects.add(new QName("item" + argCount));
756:                                    if (o instanceof  OMElement) {
757:                                        OMFactory fac = OMAbstractFactory
758:                                                .getOMFactory();
759:                                        OMElement wrappingElement;
760:                                        if (partName == null) {
761:                                            wrappingElement = fac
762:                                                    .createOMElement("item"
763:                                                            + argCount, null);
764:                                            wrappingElement
765:                                                    .addChild((OMElement) o);
766:                                        } else {
767:                                            wrappingElement = fac
768:                                                    .createOMElement(partName,
769:                                                            null);
770:                                            wrappingElement
771:                                                    .addChild((OMElement) o);
772:                                        }
773:                                        objects.add(wrappingElement);
774:                                    } else {
775:                                        objects.add(o);
776:                                    }
777:                                }
778:                            }
779:                        }
780:                    } else {
781:                        if (SimpleTypeMapper.isSimpleType(arg)) {
782:                            if (partName == null) {
783:                                objects.add("arg" + argCount);
784:                            } else {
785:                                objects.add(partName);
786:                            }
787:                            objects.add(SimpleTypeMapper.getStringValue(arg));
788:                        } else {
789:                            if (partName == null) {
790:                                objects.add(new QName("arg" + argCount));
791:                            } else {
792:                                objects.add(partName);
793:                            }
794:                            if (arg instanceof  OMElement) {
795:                                OMFactory fac = OMAbstractFactory
796:                                        .getOMFactory();
797:                                OMElement wrappingElement;
798:                                if (partName == null) {
799:                                    wrappingElement = fac.createOMElement("arg"
800:                                            + argCount, null);
801:                                    wrappingElement.addChild((OMElement) arg);
802:                                } else {
803:                                    wrappingElement = fac.createOMElement(
804:                                            partName, null);
805:                                    wrappingElement.addChild((OMElement) arg);
806:                                }
807:                                objects.add(wrappingElement);
808:                            } else if (arg instanceof  byte[]) {
809:                                objects.add(Base64.encode((byte[]) arg));
810:                            } else if (SimpleTypeMapper.isDataHandler(arg
811:                                    .getClass())) {
812:                                OMElement resElemt;
813:                                OMFactory fac = OMAbstractFactory
814:                                        .getOMFactory();
815:                                OMElement wrappingElement;
816:                                if (partName == null) {
817:                                    wrappingElement = fac.createOMElement("arg"
818:                                            + argCount, null);
819:                                } else {
820:                                    wrappingElement = fac.createOMElement(
821:                                            partName, null);
822:                                }
823:                                OMText text = fac.createOMText(arg, true);
824:                                wrappingElement.addChild(text);
825:                                objects.add(wrappingElement);
826:                            } else {
827:                                objects.add(arg);
828:                            }
829:                        }
830:                    }
831:                    argCount++;
832:                }
833:
834:                XMLStreamReader xr = new ADBXMLStreamReaderImpl(opName, objects
835:                        .toArray(), null, typeTable, qualifed);
836:
837:                StreamWrapper parser = new StreamWrapper(xr);
838:                StAXOMBuilder stAXOMBuilder = OMXMLBuilderFactory
839:                        .createStAXOMBuilder(OMAbstractFactory
840:                                .getSOAP11Factory(), parser);
841:                return stAXOMBuilder.getDocumentElement();
842:            }
843:
844:            /** @deprecated Please use getUniquePrefix */
845:            public static synchronized String getUniquePrifix() {
846:                return getUniquePrefix();
847:            }
848:
849:            /**
850:             * increments the namespace counter and returns a new prefix
851:             *
852:             * @return unique prefix
853:             */
854:            public static synchronized String getUniquePrefix() {
855:                if (nsCount > 1000) {
856:                    nsCount = 1;
857:                }
858:                return "s" + nsCount++;
859:            }
860:
861:            /**
862:             * JAM convert first name of an attribute into UpperCase as an example if there is a instance
863:             * variable called foo in a bean , then Jam give that as Foo so this method is to correct that
864:             * error
865:             *
866:             * @param wrongName
867:             * @return the right name, using english as the locale for case conversion
868:             */
869:            private static String getCorrectName(String wrongName) {
870:                if (wrongName.length() > 1) {
871:                    return wrongName.substring(0, 1)
872:                            .toLowerCase(Locale.ENGLISH)
873:                            + wrongName.substring(1, wrongName.length());
874:                } else {
875:                    return wrongName.substring(0, 1)
876:                            .toLowerCase(Locale.ENGLISH);
877:                }
878:            }
879:
880:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.