Source Code Cross Referenced for JavaBeanConverter.java in  » XML » xstream-1.3 » com » thoughtworks » xstream » converters » javabean » 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 » XML » xstream 1.3 » com.thoughtworks.xstream.converters.javabean 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright (C) 2005 Joe Walnes.
003:         * Copyright (C) 2006, 2007, 2008 XStream Committers.
004:         * All rights reserved.
005:         *
006:         * The software in this package is published under the terms of the BSD
007:         * style license a copy of which has been included with this distribution in
008:         * the LICENSE.txt file.
009:         * 
010:         * Created on 12. April 2005 by Joe Walnes
011:         */
012:        package com.thoughtworks.xstream.converters.javabean;
013:
014:        import com.thoughtworks.xstream.alias.ClassMapper;
015:        import com.thoughtworks.xstream.converters.ConversionException;
016:        import com.thoughtworks.xstream.converters.Converter;
017:        import com.thoughtworks.xstream.converters.MarshallingContext;
018:        import com.thoughtworks.xstream.converters.UnmarshallingContext;
019:        import com.thoughtworks.xstream.io.HierarchicalStreamReader;
020:        import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
021:        import com.thoughtworks.xstream.io.ExtendedHierarchicalStreamWriterHelper;
022:        import com.thoughtworks.xstream.mapper.Mapper;
023:
024:        /**
025:         * Can convert any bean with a public default constructor. BeanInfo are not
026:         * taken into consideration, this class looks for bean patterns for simple
027:         * properties
028:         */
029:        public class JavaBeanConverter implements  Converter {
030:
031:            /*
032:             * TODO:
033:             *  - support indexed properties
034:             */
035:            private Mapper mapper;
036:            private BeanProvider beanProvider;
037:            /**
038:             * @deprecated since 1.2, no necessity for field anymore.
039:             */
040:            private String classAttributeIdentifier;
041:
042:            public JavaBeanConverter(Mapper mapper) {
043:                this (mapper, new BeanProvider());
044:            }
045:
046:            public JavaBeanConverter(Mapper mapper, BeanProvider beanProvider) {
047:                this .mapper = mapper;
048:                this .beanProvider = beanProvider;
049:            }
050:
051:            /**
052:             * @deprecated As of 1.3, use {@link #JavaBeanConverter(Mapper)} and {@link com.thoughtworks.xstream.XStream#aliasAttribute(String, String)}
053:             */
054:            public JavaBeanConverter(Mapper mapper,
055:                    String classAttributeIdentifier) {
056:                this (mapper, new BeanProvider());
057:                this .classAttributeIdentifier = classAttributeIdentifier;
058:            }
059:
060:            /**
061:             * @deprecated As of 1.2, use {@link #JavaBeanConverter(Mapper)} and {@link com.thoughtworks.xstream.XStream#aliasAttribute(String, String)}
062:             */
063:            public JavaBeanConverter(ClassMapper classMapper,
064:                    String classAttributeIdentifier) {
065:                this ((Mapper) classMapper, classAttributeIdentifier);
066:            }
067:
068:            /**
069:             * Only checks for the availability of a public default constructor.
070:             * If you need stricter checks, subclass JavaBeanConverter
071:             */
072:            public boolean canConvert(Class type) {
073:                return beanProvider.canInstantiate(type);
074:            }
075:
076:            public void marshal(final Object source,
077:                    final HierarchicalStreamWriter writer,
078:                    final MarshallingContext context) {
079:                final String classAttributeName = classAttributeIdentifier != null ? classAttributeIdentifier
080:                        : mapper.attributeForAlias("class");
081:                beanProvider.visitSerializableProperties(source,
082:                        new BeanProvider.Visitor() {
083:                            public void visit(String propertyName,
084:                                    Class fieldType, Class definedIn,
085:                                    Object newObj) {
086:                                if (newObj != null
087:                                        && mapper.shouldSerializeMember(
088:                                                definedIn, propertyName)) {
089:                                    writeField(propertyName, fieldType, newObj,
090:                                            definedIn);
091:                                }
092:                            }
093:
094:                            private void writeField(String propertyName,
095:                                    Class fieldType, Object newObj,
096:                                    Class definedIn) {
097:                                String serializedMember = mapper
098:                                        .serializedMember(source.getClass(),
099:                                                propertyName);
100:                                ExtendedHierarchicalStreamWriterHelper
101:                                        .startNode(writer, serializedMember,
102:                                                fieldType);
103:                                Class actualType = newObj.getClass();
104:
105:                                Class defaultType = mapper
106:                                        .defaultImplementationOf(fieldType);
107:                                if (!actualType.equals(defaultType)) {
108:                                    writer.addAttribute(classAttributeName,
109:                                            mapper.serializedClass(actualType));
110:                                }
111:                                context.convertAnother(newObj);
112:
113:                                writer.endNode();
114:                            }
115:
116:                        });
117:            }
118:
119:            public Object unmarshal(final HierarchicalStreamReader reader,
120:                    final UnmarshallingContext context) {
121:                final Object result = instantiateNewInstance(context);
122:
123:                while (reader.hasMoreChildren()) {
124:                    reader.moveDown();
125:
126:                    String propertyName = mapper.realMember(result.getClass(),
127:                            reader.getNodeName());
128:
129:                    boolean propertyExistsInClass = beanProvider
130:                            .propertyDefinedInClass(propertyName, result
131:                                    .getClass());
132:
133:                    if (propertyExistsInClass) {
134:                        Class type = determineType(reader, result, propertyName);
135:                        Object value = context.convertAnother(result, type);
136:                        beanProvider.writeProperty(result, propertyName, value);
137:                    } else if (mapper.shouldSerializeMember(result.getClass(),
138:                            propertyName)) {
139:                        throw new ConversionException("Property '"
140:                                + propertyName + "' not defined in class "
141:                                + result.getClass().getName());
142:                    }
143:
144:                    reader.moveUp();
145:                }
146:
147:                return result;
148:            }
149:
150:            private Object instantiateNewInstance(UnmarshallingContext context) {
151:                Object result = context.currentObject();
152:                if (result == null) {
153:                    result = beanProvider
154:                            .newInstance(context.getRequiredType());
155:                }
156:                return result;
157:            }
158:
159:            private Class determineType(HierarchicalStreamReader reader,
160:                    Object result, String fieldName) {
161:                final String classAttributeName = classAttributeIdentifier != null ? classAttributeIdentifier
162:                        : mapper.attributeForAlias("class");
163:                String classAttribute = reader.getAttribute(classAttributeName);
164:                if (classAttribute != null) {
165:                    return mapper.realClass(classAttribute);
166:                } else {
167:                    return mapper.defaultImplementationOf(beanProvider
168:                            .getPropertyType(result, fieldName));
169:                }
170:            }
171:
172:            /**
173:             * @deprecated since 1.3
174:             */
175:            public static class DuplicateFieldException extends
176:                    ConversionException {
177:                public DuplicateFieldException(String msg) {
178:                    super(msg);
179:                }
180:            }
181:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.