View | Details | Raw Unified | Return to bug 279003 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/persistence/internal/jaxb/DefaultElementConverter.java (+64 lines)
Line 0 Link Here
1
/*******************************************************************************
2
* Copyright (c) 1998, 2009 Oracle. All rights reserved.
3
* This program and the accompanying materials are made available under the
4
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
* which accompanies this distribution.
6
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
* and the Eclipse Distribution License is available at
8
* http://www.eclipse.org/org/documents/edl-v10.php.
9
*
10
* Contributors:
11
* bdoughan - July 30/2009 - 2.0 - Initial implementation
12
******************************************************************************/
13
package org.eclipse.persistence.internal.jaxb;
14
15
import org.eclipse.persistence.mappings.DatabaseMapping;
16
import org.eclipse.persistence.oxm.XMLMarshaller;
17
import org.eclipse.persistence.oxm.XMLUnmarshaller;
18
import org.eclipse.persistence.oxm.mappings.converters.XMLConverter;
19
import org.eclipse.persistence.sessions.Session;
20
import org.w3c.dom.Element;
21
22
/**
23
 * If there is no text node then apply the default value.
24
 * <pre> @XmlElement(required = true, defaultValue = "default")
25
 * protected Object a;</pre>
26
 */
27
public class DefaultElementConverter implements XMLConverter {
28
29
    private String defaultValue;
30
31
    public DefaultElementConverter(String defaultValue) {
32
        this.defaultValue = defaultValue;
33
    }
34
35
    public void initialize(DatabaseMapping mapping, Session session) {
36
    }
37
38
    public Object convertDataValueToObjectValue(Object dataValue, Session session) {
39
        if(dataValue instanceof Element) {
40
            Element element = (Element) dataValue;
41
            if(element.getTextContent().length() == 0) {
42
                element.setTextContent(defaultValue);
43
            }
44
        }
45
        return dataValue;
46
    }
47
48
    public Object convertObjectValueToDataValue(Object objectValue, Session session) {
49
        return objectValue;
50
    }
51
52
    public boolean isMutable() {
53
        return false;
54
    }
55
56
    public Object convertDataValueToObjectValue(Object dataValue, Session session, XMLUnmarshaller unmarshaller) {
57
        return convertDataValueToObjectValue(dataValue, session);
58
    }
59
60
    public Object convertObjectValueToDataValue(Object objectValue, Session session, XMLMarshaller marshaller) {
61
        return convertObjectValueToDataValue(objectValue, session);
62
    }
63
64
}
(-)src/org/eclipse/persistence/internal/jaxb/DomHandlerConverter.java (-3 / +3 lines)
Lines 101-111 Link Here
101
	}
101
	}
102
	
102
	
103
	public Object convertDataValueToObjectValue(Object dataValue, Session session) {
103
	public Object convertDataValueToObjectValue(Object dataValue, Session session) {
104
		return null;
104
		return convertDataValueToObjectValue(dataValue, session, null);
105
	}
105
	}
106
	
106
	
107
	public Object convertObjectValueToDataValue(Object objectValue, Session session) {
107
	public Object convertObjectValueToDataValue(Object objectValue, Session session) {
108
		return null;
108
		return convertObjectValueToDataValue(objectValue, session, null);
109
	}
109
	}
110
	
110
	
111
}
111
}
(-)src/org/eclipse/persistence/internal/jaxb/JAXBElementAttributeAccessor.java (-143 lines)
Lines 1-143 Link Here
1
/*******************************************************************************
2
* Copyright (c) 1998, 2009 Oracle. All rights reserved.
3
* This program and the accompanying materials are made available under the
4
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
* which accompanies this distribution.
6
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
* and the Eclipse Distribution License is available at
8
* http://www.eclipse.org/org/documents/edl-v10.php.
9
*
10
* Contributors:
11
* mmacivor - June 05/2008 - 1.0 - Initial implementation
12
******************************************************************************/
13
package org.eclipse.persistence.internal.jaxb;
14
15
import java.util.HashMap;
16
import java.util.Map;
17
18
import org.eclipse.persistence.mappings.AttributeAccessor;
19
import org.eclipse.persistence.exceptions.DescriptorException;
20
import org.eclipse.persistence.internal.helper.ClassConstants;
21
import org.eclipse.persistence.internal.queries.ContainerPolicy;
22
import org.eclipse.persistence.oxm.XMLRoot;
23
import javax.xml.bind.JAXBElement;
24
import javax.xml.namespace.QName;
25
26
public class JAXBElementAttributeAccessor extends AttributeAccessor {
27
	private AttributeAccessor nestedAccessor;
28
	private ContainerPolicy containerPolicy;
29
	private boolean isContainer;
30
	private Map<QName, Class> qNamesToScopes;
31
	private Class declaredType;
32
33
	public JAXBElementAttributeAccessor(AttributeAccessor nestedAccessor) {
34
		this.nestedAccessor = nestedAccessor;
35
		this.isContainer = false;
36
		qNamesToScopes = new HashMap<QName, Class>();
37
	}
38
	public JAXBElementAttributeAccessor(AttributeAccessor nestedAccessor, ContainerPolicy containerPolicy) {
39
		this.nestedAccessor = nestedAccessor;
40
		this.containerPolicy = containerPolicy;
41
		this.isContainer = true;
42
		qNamesToScopes = new HashMap<QName, Class>();
43
	}
44
	
45
	public Object getAttributeValueFromObject(Object object) {
46
		Object value = nestedAccessor.getAttributeValueFromObject(object);
47
		//Swap JAXBElements for XMLRoots
48
		//May need a better way to do this for perf.
49
		if(isContainer) {
50
			Object results = containerPolicy.containerInstance(containerPolicy.sizeFor(value));
51
			Object iterator = containerPolicy.iteratorFor(value);
52
			while(containerPolicy.hasNext(iterator)) {
53
				Object next = containerPolicy.next(iterator, null);
54
				if(next instanceof JAXBElement) {
55
					JAXBElement element = (JAXBElement)next;
56
					XMLRoot root = new XMLRoot();
57
					root.setLocalName(element.getName().getLocalPart());
58
					root.setNamespaceURI(element.getName().getNamespaceURI());
59
					root.setObject(element.getValue());
60
					containerPolicy.addInto(root, results, null);
61
				} else {
62
					containerPolicy.addInto(next, results, null);
63
				}
64
			}
65
			value = results;
66
        } else if(value instanceof JAXBElement) {
67
            JAXBElement element = (JAXBElement)value;
68
            XMLRoot root = new XMLRoot();
69
            root.setLocalName(element.getName().getLocalPart());
70
            root.setNamespaceURI(element.getName().getNamespaceURI());
71
            root.setObject(element.getValue());
72
            value = root;
73
        }
74
        return value;
75
    }
76
	
77
	public void setAttributeValueInObject(Object object, Object value) {
78
		Object attributeValue = value;
79
		if(isContainer) {
80
			Object results = containerPolicy.containerInstance(containerPolicy.sizeFor(attributeValue));
81
			Object iterator = containerPolicy.iteratorFor(attributeValue);
82
			while(containerPolicy.hasNext(iterator)) {
83
				Object next = containerPolicy.next(iterator, null);
84
				Object objectToAdd = unwrapObject(next);
85
				containerPolicy.addInto(objectToAdd, results, null);
86
			}
87
			attributeValue = results;
88
		} else {		
89
			attributeValue = unwrapObject(attributeValue);
90
91
		}
92
		nestedAccessor.setAttributeValueInObject(object, attributeValue);
93
	}
94
	
95
    public void initializeAttributes(Class theJavaClass) throws DescriptorException {
96
    	nestedAccessor.initializeAttributes(theJavaClass);
97
    }
98
    
99
    private Object unwrapObject(Object originalObject){
100
        if(originalObject instanceof XMLRoot) {
101
            XMLRoot root = (XMLRoot)originalObject;
102
            QName name = new QName(root.getNamespaceURI(), root.getLocalName());
103
            Object value = root.getObject();
104
            if(value == null){
105
                return createJAXBElement(name, Object.class, value);
106
            }else{
107
                return createJAXBElement(name, getDeclaredType(),value);
108
            }
109
        } else {
110
            return originalObject;
111
        }
112
    }
113
114
    private JAXBElement createJAXBElement(QName qname, Class theClass, Object value){
115
    	if(value != null && value instanceof JAXBElement){
116
    		return (JAXBElement)value;
117
    	}
118
    	if(ClassConstants.XML_GREGORIAN_CALENDAR.isAssignableFrom(theClass)){
119
    		theClass = ClassConstants.XML_GREGORIAN_CALENDAR;
120
    	}else if(ClassConstants.DURATION.isAssignableFrom(theClass)){
121
    		theClass = ClassConstants.DURATION;
122
    	}
123
    	Class scopeClass = qNamesToScopes.get(qname);
124
    	if(scopeClass == javax.xml.bind.annotation.XmlElementDecl.GLOBAL.class){
125
    		scopeClass = JAXBElement.GlobalScope.class;
126
    	}
127
    	return new JAXBElement(qname, theClass, scopeClass, value);
128
    }
129
	
130
	public Map<QName, Class> getQNamesToScopes() {
131
		return qNamesToScopes;
132
	}
133
	public void setQNamesToScopes(Map<QName, Class> namesToScopes) {
134
		qNamesToScopes = namesToScopes;
135
	}  
136
137
	public void setDeclaredType(Class declaredType) {
138
		this.declaredType = declaredType;
139
	}
140
	public Class getDeclaredType() {
141
		return declaredType;
142
	}
143
}
(-)src/org/eclipse/persistence/internal/jaxb/JAXBElementConverter.java (+120 lines)
Line 0 Link Here
1
/*******************************************************************************
2
* Copyright (c) 1998, 2009 Oracle. All rights reserved.
3
* This program and the accompanying materials are made available under the
4
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
* which accompanies this distribution.
6
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
* and the Eclipse Distribution License is available at
8
* http://www.eclipse.org/org/documents/edl-v10.php.
9
*
10
* Contributors:
11
* bdoughan - July 29/2009 - 2.0 - Initial implementation
12
******************************************************************************/
13
package org.eclipse.persistence.internal.jaxb;
14
15
import javax.xml.bind.JAXBElement;
16
import javax.xml.namespace.QName;
17
18
import org.eclipse.persistence.mappings.DatabaseMapping;
19
import org.eclipse.persistence.mappings.converters.Converter;
20
import org.eclipse.persistence.oxm.XMLMarshaller;
21
import org.eclipse.persistence.oxm.XMLRoot;
22
import org.eclipse.persistence.oxm.XMLUnmarshaller;
23
import org.eclipse.persistence.oxm.XMLField;
24
import org.eclipse.persistence.oxm.mappings.converters.XMLConverter;
25
import org.eclipse.persistence.exceptions.ConversionException;
26
import org.eclipse.persistence.internal.oxm.XPathFragment;
27
import org.eclipse.persistence.sessions.Session;
28
29
public class JAXBElementConverter implements XMLConverter {
30
31
    private XPathFragment rootFragment;
32
    private XMLField associatedField;
33
    private DatabaseMapping mapping;
34
    private Converter nestedConverter;
35
    private Class declaredType;
36
    private Class scope;
37
38
    public JAXBElementConverter(XMLField associatedField, Class declaredType, Class scope) {
39
        this.associatedField = associatedField;
40
        this.declaredType = declaredType;
41
        this.scope = scope;
42
    }
43
44
    public Converter getNestedConverter() {
45
        return nestedConverter;
46
    }
47
48
    public void setNestedConverter(Converter nestedConverter) {
49
        this.nestedConverter = nestedConverter;
50
    }
51
52
    public Object convertDataValueToObjectValue(Object dataValue,
53
            Session session, XMLUnmarshaller unmarshaller) {
54
        return convertDataValueToObjectValue(dataValue, session);
55
    }
56
57
    public Object convertObjectValueToDataValue(Object objectValue,
58
            Session session, XMLMarshaller marshaller) {
59
        return convertObjectValueToDataValue(objectValue, session);
60
    }
61
62
    public Object convertDataValueToObjectValue(Object dataValue, Session session) {
63
        QName name = new QName(rootFragment.getNamespaceURI(), rootFragment.getLocalName());
64
65
        if(mapping.isAbstractDirectMapping()){
66
            if ((dataValue == null) || (dataValue.getClass() != mapping.getAttributeClassification())) {
67
                try {
68
                    dataValue = session.getDatasourcePlatform().convertObject(dataValue, mapping.getAttributeClassification());
69
                } catch (ConversionException e) {
70
                    throw ConversionException.couldNotBeConverted(this, mapping.getDescriptor(), e);
71
                }
72
            }
73
        }
74
75
        if(null != nestedConverter) {
76
            dataValue = nestedConverter.convertDataValueToObjectValue(dataValue, session);
77
        }
78
        if(dataValue instanceof JAXBElement) {
79
            return dataValue;
80
        }
81
        if(null == declaredType) {
82
            return new JAXBElement(name, Object.class, scope, dataValue);
83
        } else {
84
            return new JAXBElement(name, declaredType, scope, dataValue);
85
        }
86
    }
87
88
    public Object convertObjectValueToDataValue(Object objectValue, Session session) {
89
        if(objectValue instanceof JAXBElement) {
90
            objectValue = ((JAXBElement)objectValue).getValue();
91
        } else if(objectValue instanceof XMLRoot) {
92
            objectValue = ((XMLRoot) objectValue).getObject();
93
        }
94
        if(null != nestedConverter) {
95
            objectValue = nestedConverter.convertObjectValueToDataValue(objectValue, session);
96
        }
97
        return objectValue;
98
    }
99
100
    public void initialize(DatabaseMapping mapping, Session session) {
101
        if(null != nestedConverter) {
102
            nestedConverter.initialize(mapping, session);
103
        }
104
        XPathFragment fragment = associatedField.getXPathFragment();
105
        while(fragment.getNextFragment() != null && !(fragment.getNextFragment().nameIsText())) {
106
            fragment = fragment.getNextFragment();
107
        }
108
        if(fragment.hasNamespace() && associatedField.getNamespaceResolver() != null){
109
            String uri = associatedField.getNamespaceResolver().resolveNamespacePrefix(fragment.getPrefix());
110
            fragment.setNamespaceURI(uri);
111
        }
112
        this.rootFragment = fragment;
113
        this.mapping = mapping;
114
    }
115
116
    public boolean isMutable() {
117
        return false;
118
    }
119
120
}
(-)src/org/eclipse/persistence/internal/jaxb/JAXBElementRootConverter.java (+111 lines)
Line 0 Link Here
1
/*******************************************************************************
2
* Copyright (c) 1998, 2009 Oracle. All rights reserved.
3
* This program and the accompanying materials are made available under the
4
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
5
* which accompanies this distribution.
6
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7
* and the Eclipse Distribution License is available at
8
* http://www.eclipse.org/org/documents/edl-v10.php.
9
*
10
* Contributors:
11
* bdoughan - August 5/2009 - 2.0 - Initial implementation
12
******************************************************************************/
13
package org.eclipse.persistence.internal.jaxb;
14
15
import javax.xml.bind.JAXBElement;
16
import javax.xml.namespace.QName;
17
18
import org.eclipse.persistence.internal.helper.ClassConstants;
19
import org.eclipse.persistence.mappings.DatabaseMapping;
20
import org.eclipse.persistence.mappings.converters.Converter;
21
import org.eclipse.persistence.oxm.XMLMarshaller;
22
import org.eclipse.persistence.oxm.XMLRoot;
23
import org.eclipse.persistence.oxm.XMLUnmarshaller;
24
import org.eclipse.persistence.oxm.mappings.converters.XMLConverter;
25
import org.eclipse.persistence.sessions.Session;
26
27
/**
28
 * Convert between instances of XMLRoot and JAXBElement 
29
 */
30
public class JAXBElementRootConverter implements XMLConverter {
31
32
    private Class declaredType;
33
    private XMLConverter nestedConverter;
34
35
    public JAXBElementRootConverter(Class declaredType) {
36
        this.declaredType = declaredType;
37
    }
38
39
    public Converter getNestedConverter() {
40
        return nestedConverter;
41
    }
42
43
    public void setNestedConverter(XMLConverter nestedConverter) {
44
        this.nestedConverter = nestedConverter;
45
    }
46
47
    public void initialize(DatabaseMapping mapping, Session session) {
48
        if(null != nestedConverter) {
49
            nestedConverter.initialize(mapping, session);
50
        }
51
    }
52
53
    public Object convertDataValueToObjectValue(Object dataValue, Session session) {
54
        return this.convertDataValueToObjectValue(dataValue, session, null);
55
    }
56
57
    public Object convertDataValueToObjectValue(Object dataValue, Session session, XMLUnmarshaller unmarshaller) {
58
        if(null != nestedConverter) {
59
            dataValue = nestedConverter.convertDataValueToObjectValue(dataValue, session, unmarshaller);
60
        }
61
        if(dataValue instanceof JAXBElement) {
62
            return dataValue;
63
        } else if(dataValue instanceof XMLRoot) {
64
            XMLRoot root = (XMLRoot)dataValue;
65
            QName name = new QName(root.getNamespaceURI(), root.getLocalName());
66
            dataValue = root.getObject();
67
            if(null == dataValue) {
68
                return createJAXBElement(name, Object.class, dataValue);
69
            }else{
70
                return createJAXBElement(name, declaredType, dataValue);
71
            }
72
        }
73
        return dataValue;
74
    }
75
76
    public Object convertObjectValueToDataValue(Object objectValue, Session session) {
77
        return this.convertObjectValueToDataValue(objectValue, session, null);
78
    }
79
80
    public Object convertObjectValueToDataValue(Object objectValue, Session session, XMLMarshaller marshaller) {
81
        if(null != nestedConverter) {
82
            objectValue = nestedConverter.convertObjectValueToDataValue(objectValue, session, marshaller);
83
        }
84
        if(objectValue instanceof JAXBElement && !(objectValue instanceof WrappedValue)) {
85
            JAXBElement element = (JAXBElement) objectValue;
86
            XMLRoot root = new XMLRoot();
87
            root.setLocalName(element.getName().getLocalPart());
88
            root.setNamespaceURI(element.getName().getNamespaceURI());
89
            root.setObject(element.getValue());
90
            return root;
91
        }
92
        return objectValue;
93
    }
94
95
    public boolean isMutable() {
96
        return false;
97
    }
98
99
    private JAXBElement createJAXBElement(QName qname, Class theClass, Object value){
100
        if(value != null && value instanceof JAXBElement){
101
            return (JAXBElement)value;
102
        }
103
        if(ClassConstants.XML_GREGORIAN_CALENDAR.isAssignableFrom(theClass)){
104
            theClass = ClassConstants.XML_GREGORIAN_CALENDAR;
105
        }else if(ClassConstants.DURATION.isAssignableFrom(theClass)){
106
            theClass = ClassConstants.DURATION;
107
        }
108
        return new JAXBElement(qname, theClass, value);
109
    }
110
111
}
(-)src/org/eclipse/persistence/internal/jaxb/many/MapValueAttributeAccessor.java (+3 lines)
Lines 42-47 Link Here
42
    
42
    
43
    public Object getAttributeValueFromObject(Object object)throws DescriptorException {
43
    public Object getAttributeValueFromObject(Object object)throws DescriptorException {
44
        Object value = nestedAccessor.getAttributeValueFromObject(object);
44
        Object value = nestedAccessor.getAttributeValueFromObject(object);
45
        if(null == value) {
46
            return null;
47
        }
45
    
48
    
46
        Object results = containerPolicy.containerInstance(((Map)value).size());            
49
        Object results = containerPolicy.containerInstance(((Map)value).size());            
47
        Iterator iter =  ((Map)value).keySet().iterator();
50
        Iterator iter =  ((Map)value).keySet().iterator();
(-)src/org/eclipse/persistence/jaxb/compiler/MappingsGenerator.java (-64 / +57 lines)
Lines 34-52 Link Here
34
import org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter;
34
import org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter;
35
import org.eclipse.persistence.jaxb.JAXBEnumTypeConverter;
35
import org.eclipse.persistence.jaxb.JAXBEnumTypeConverter;
36
import org.eclipse.persistence.internal.helper.ClassConstants;
36
import org.eclipse.persistence.internal.helper.ClassConstants;
37
import org.eclipse.persistence.internal.jaxb.DefaultElementConverter;
38
import org.eclipse.persistence.internal.jaxb.JAXBElementConverter;
39
import org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter;
37
import org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor;
40
import org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor;
38
import org.eclipse.persistence.internal.jaxb.JaxbClassLoader;
41
import org.eclipse.persistence.internal.jaxb.JaxbClassLoader;
39
import org.eclipse.persistence.internal.jaxb.DomHandlerConverter;
42
import org.eclipse.persistence.internal.jaxb.DomHandlerConverter;
40
import org.eclipse.persistence.internal.jaxb.MultiArgInstantiationPolicy;
43
import org.eclipse.persistence.internal.jaxb.MultiArgInstantiationPolicy;
41
import org.eclipse.persistence.internal.jaxb.WrappedValue;
44
import org.eclipse.persistence.internal.jaxb.WrappedValue;
42
import org.eclipse.persistence.internal.jaxb.JAXBElementAttributeAccessor;
43
import org.eclipse.persistence.mappings.AttributeAccessor;
44
import org.eclipse.persistence.mappings.DatabaseMapping;
45
import org.eclipse.persistence.mappings.DatabaseMapping;
46
import org.eclipse.persistence.mappings.converters.Converter;
45
47
46
import org.eclipse.persistence.oxm.*;
48
import org.eclipse.persistence.oxm.*;
47
import org.eclipse.persistence.oxm.mappings.*;
49
import org.eclipse.persistence.oxm.mappings.*;
48
import org.eclipse.persistence.oxm.mappings.converters.XMLListConverter;
50
import org.eclipse.persistence.oxm.mappings.converters.XMLListConverter;
49
import org.eclipse.persistence.oxm.mappings.converters.XMLRootConverter;
50
import org.eclipse.persistence.oxm.mappings.nullpolicy.IsSetNullPolicy;
51
import org.eclipse.persistence.oxm.mappings.nullpolicy.IsSetNullPolicy;
51
import org.eclipse.persistence.oxm.mappings.nullpolicy.NullPolicy;
52
import org.eclipse.persistence.oxm.mappings.nullpolicy.NullPolicy;
52
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
53
import org.eclipse.persistence.oxm.mappings.nullpolicy.XMLNullRepresentationType;
Lines 368-373 Link Here
368
    public XMLChoiceCollectionMapping generateChoiceCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespace) {
369
    public XMLChoiceCollectionMapping generateChoiceCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespace) {
369
        ChoiceProperty prop = (ChoiceProperty)property;
370
        ChoiceProperty prop = (ChoiceProperty)property;
370
        XMLChoiceCollectionMapping mapping = new XMLChoiceCollectionMapping();
371
        XMLChoiceCollectionMapping mapping = new XMLChoiceCollectionMapping();
372
        mapping.setReuseContainer(true);
371
        mapping.setAttributeName(property.getPropertyName());
373
        mapping.setAttributeName(property.getPropertyName());
372
        if (property.isMethodProperty()) {
374
        if (property.isMethodProperty()) {
373
            if (property.getGetMethodName() == null) {
375
            if (property.getGetMethodName() == null) {
Lines 413-420 Link Here
413
        DatabaseMapping mapping;
415
        DatabaseMapping mapping;
414
        if(isCollection) {
416
        if(isCollection) {
415
            mapping = new XMLChoiceCollectionMapping();
417
            mapping = new XMLChoiceCollectionMapping();
418
            ((XMLChoiceCollectionMapping) mapping).setReuseContainer(true);
419
            ((XMLChoiceCollectionMapping) mapping).setConverter(new JAXBElementRootConverter(Object.class));
416
        } else {
420
        } else {
417
            mapping = new XMLChoiceObjectMapping();
421
            mapping = new XMLChoiceObjectMapping();
422
            ((XMLChoiceObjectMapping) mapping).setConverter(new JAXBElementRootConverter(Object.class));
418
        }
423
        }
419
        mapping.setAttributeName(property.getPropertyName());
424
        mapping.setAttributeName(property.getPropertyName());
420
        if (property.isMethodProperty()) {
425
        if (property.isMethodProperty()) {
Lines 434-447 Link Here
434
        }
439
        }
435
440
436
        List<ElementDeclaration> referencedElements = property.getReferencedElements();
441
        List<ElementDeclaration> referencedElements = property.getReferencedElements();
437
        boolean hasJAXBElements = false;
438
        AttributeAccessor mappingAccessor = mapping.getAttributeAccessor();
439
        if(property.getType().isArray()) {
442
        if(property.getType().isArray()) {
440
            JAXBObjectArrayAttributeAccessor accessor = new JAXBObjectArrayAttributeAccessor(mappingAccessor, mapping.getContainerPolicy());           
443
            JAXBObjectArrayAttributeAccessor accessor = new JAXBObjectArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy());           
441
            accessor.setComponentClassName(property.getType().getComponentType().getRawName());
444
            accessor.setComponentClassName(property.getType().getComponentType().getRawName());
442
            mappingAccessor = accessor;         
445
            mapping.setAttributeAccessor(accessor);
443
        }
446
        }
444
        Map<QName, Class> qNamesToScopeClass = new HashMap<QName, Class>();
445
        for(ElementDeclaration element:referencedElements) {
447
        for(ElementDeclaration element:referencedElements) {
446
            QName elementName = element.getElementName();
448
            QName elementName = element.getElementName();
447
            boolean isText = !(this.typeInfo.containsKey(element.getJavaTypeName())) && !(element.getJavaTypeName().equals(OBJECT_CLASS_NAME));            
449
            boolean isText = !(this.typeInfo.containsKey(element.getJavaTypeName())) && !(element.getJavaTypeName().equals(OBJECT_CLASS_NAME));            
Lines 450-526 Link Here
450
            if(helper.getXMLToJavaTypeMap().get(element.getJavaType().getRawName()) == XMLConstants.BASE_64_BINARY_QNAME) {
452
            if(helper.getXMLToJavaTypeMap().get(element.getJavaType().getRawName()) == XMLConstants.BASE_64_BINARY_QNAME) {
451
                xmlField.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);
453
                xmlField.setSchemaType(XMLConstants.BASE_64_BINARY_QNAME);
452
            }
454
            }
453
            XMLMapping nestedMapping;
455
            DatabaseMapping nestedMapping;
454
            JAXBElementAttributeAccessor nestedAccessor;
455
            if(isCollection){
456
            if(isCollection){
456
457
                XMLChoiceCollectionMapping xmlChoiceCollectionMapping = (XMLChoiceCollectionMapping) mapping;
457
            	nestedAccessor = new JAXBElementAttributeAccessor(mappingAccessor, mapping.getContainerPolicy());
458
                xmlChoiceCollectionMapping.addChoiceElement(xmlField, element.getJavaTypeName());
458
                ((XMLChoiceCollectionMapping)mapping).addChoiceElement(xmlField, element.getJavaTypeName());
459
                nestedMapping = (DatabaseMapping) xmlChoiceCollectionMapping.getChoiceElementMappings().get(xmlField);
459
                nestedMapping = ((XMLChoiceCollectionMapping)mapping).getChoiceElementMappings().get(xmlField);
460
                if(nestedMapping.isAbstractCompositeCollectionMapping()){
460
                if(((DatabaseMapping)nestedMapping).isAbstractCompositeCollectionMapping()){
461
                    ((XMLCompositeCollectionMapping)nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
461
                    ((XMLCompositeCollectionMapping)nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
462
                }
462
                }
463
463
464
                if (((DatabaseMapping) nestedMapping).isAbstractCompositeDirectCollectionMapping()) {
464
                if (nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
465
                    ((XMLCompositeDirectCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
465
                    ((XMLCompositeDirectCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
466
                }
466
                }
467
467
468
                if (element.isList() && ((DatabaseMapping)nestedMapping).isAbstractCompositeDirectCollectionMapping()) {
468
                if (element.isList() && nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
469
                    XMLListConverter listConverter = new XMLListConverter();
469
                    XMLListConverter listConverter = new XMLListConverter();
470
                    listConverter.setObjectClassName(element.getJavaType().getQualifiedName());
470
                    listConverter.setObjectClassName(element.getJavaType().getQualifiedName());
471
                    ((XMLCompositeDirectCollectionMapping)nestedMapping).setValueConverter(listConverter);
471
                    ((XMLCompositeDirectCollectionMapping)nestedMapping).setValueConverter(listConverter);
472
                }
472
                }
473
            }else{
473
            }else{
474
            	nestedAccessor = new JAXBElementAttributeAccessor(mappingAccessor);
474
                XMLChoiceObjectMapping xmlChoiceObjectMapping = (XMLChoiceObjectMapping) mapping;
475
                ((XMLChoiceObjectMapping)mapping).addChoiceElement(xmlField, element.getJavaTypeName());
475
                xmlChoiceObjectMapping.addChoiceElement(xmlField, element.getJavaTypeName());
476
                nestedMapping = ((XMLChoiceObjectMapping)mapping).getChoiceElementMappings().get(xmlField);
476
                nestedMapping = (DatabaseMapping) xmlChoiceObjectMapping.getChoiceElementMappings().get(xmlField);
477
                if(((DatabaseMapping)nestedMapping).isAbstractCompositeObjectMapping()){
477
                if(nestedMapping.isAbstractCompositeObjectMapping()){
478
                    ((XMLCompositeObjectMapping)nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
478
                    ((XMLCompositeObjectMapping)nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
479
                }
479
                }
480
            }
480
            }
481
            
481
482
            if(!element.isXmlRootElement()) {
482
            if(!element.isXmlRootElement()) {
483
                XMLRootConverter converter = new XMLRootConverter(xmlField);
483
                Class scopeClass = element.getScopeClass(); 
484
                if(scopeClass == javax.xml.bind.annotation.XmlElementDecl.GLOBAL.class){
485
                    scopeClass = JAXBElement.GlobalScope.class;
486
                }
487
                Class declaredType = helper.getClassForJavaClass(element.getJavaType());
488
                JAXBElementConverter converter = new JAXBElementConverter(xmlField, declaredType, scopeClass);
484
                if(isCollection){
489
                if(isCollection){
485
                    ((XMLChoiceCollectionMapping)mapping).addConverter(xmlField, converter);
490
                    XMLChoiceCollectionMapping xmlChoiceCollectionMapping = (XMLChoiceCollectionMapping) mapping;
491
                    Converter originalConverter = xmlChoiceCollectionMapping.getConverter(xmlField);
492
                    converter.setNestedConverter(originalConverter);
493
                    xmlChoiceCollectionMapping.addConverter(xmlField, converter);
486
                }else{
494
                }else{
487
                    ((XMLChoiceObjectMapping)mapping).addConverter(xmlField, converter);
495
                    XMLChoiceObjectMapping xmlChoiceObjectMapping = (XMLChoiceObjectMapping) mapping;
496
                    Converter originalConverter = xmlChoiceObjectMapping.getConverter(xmlField);
497
                    converter.setNestedConverter(originalConverter);
498
                    xmlChoiceObjectMapping.addConverter(xmlField, converter);
488
                }
499
                }
489
                Class scopeClass = element.getScopeClass(); 
490
            	if(scopeClass == javax.xml.bind.annotation.XmlElementDecl.GLOBAL.class){
491
            		scopeClass = JAXBElement.GlobalScope.class;
492
            	}
493
                qNamesToScopeClass.put(elementName, scopeClass);
494
            }
500
            }
495
            hasJAXBElements = hasJAXBElements || !element.isXmlRootElement();   
496
            Class theClass;
497
			theClass = helper.getClassForJavaClass(element.getJavaType());
498
            
499
            nestedAccessor.setDeclaredType(theClass);
500
                        
501
            nestedAccessor.setQNamesToScopes(qNamesToScopeClass);
502
            ((DatabaseMapping)nestedMapping).setAttributeAccessor(nestedAccessor);           
503
        }
501
        }
504
        if(hasJAXBElements) {
505
            JAXBElementAttributeAccessor accessor;
506
            if(isCollection){
507
                accessor = new JAXBElementAttributeAccessor(mappingAccessor, mapping.getContainerPolicy());
508
            }else{
509
                accessor = new JAXBElementAttributeAccessor(mappingAccessor);
510
            }
511
            
512
            Class theClass = helper.getClassForJavaClass(property.getActualType());
513
            accessor.setDeclaredType(theClass);
514
515
            accessor.setQNamesToScopes(qNamesToScopeClass);
516
            mapping.setAttributeAccessor(accessor);
517
        }
518
        descriptor.addMapping(mapping);
502
        descriptor.addMapping(mapping);
519
        return (XMLMapping)mapping;
503
        return (XMLMapping)mapping;
520
504
521
    }
505
    }
522
    
506
523
    
524
    public XMLAnyCollectionMapping generateAnyCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, boolean isMixed) {
507
    public XMLAnyCollectionMapping generateAnyCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, boolean isMixed) {
525
        boolean isLax = false;
508
        boolean isLax = false;
526
        Class domHandlerClass = null;
509
        Class domHandlerClass = null;
Lines 532-537 Link Here
532
        }
515
        }
533
        XMLAnyCollectionMapping  mapping = new XMLAnyCollectionMapping();
516
        XMLAnyCollectionMapping  mapping = new XMLAnyCollectionMapping();
534
        mapping.setAttributeName(property.getPropertyName());
517
        mapping.setAttributeName(property.getPropertyName());
518
        mapping.setReuseContainer(true);
535
        if (property.isMethodProperty()) {
519
        if (property.isMethodProperty()) {
536
            if (property.getGetMethodName() == null) {
520
            if (property.getGetMethodName() == null) {
537
                // handle case of set with no get method 
521
                // handle case of set with no get method 
Lines 550-568 Link Here
550
        if(!isMixed) {
534
        if(!isMixed) {
551
            mapping.setUseXMLRoot(true);
535
            mapping.setUseXMLRoot(true);
552
        }
536
        }
553
        JAXBElementAttributeAccessor accessor = new JAXBElementAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy());
554
537
555
        Class theClass = helper.getClassForJavaClass(property.getActualType());
538
        Class declaredType = helper.getClassForJavaClass(property.getActualType());
556
        accessor.setDeclaredType(theClass);
539
        JAXBElementRootConverter jaxbElementRootConverter = new JAXBElementRootConverter(declaredType);
557
        mapping.setAttributeAccessor(accessor);
540
        mapping.setConverter(jaxbElementRootConverter);
541
558
        if(isLax) {
542
        if(isLax) {
559
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
543
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
560
        } else {
544
        } else {
561
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT);
545
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT);
562
        }
546
        }
563
        if(domHandlerClass != null) {
547
        if(domHandlerClass != null) {
564
            DomHandlerConverter converter = new DomHandlerConverter(domHandlerClass);
548
            jaxbElementRootConverter.setNestedConverter(new DomHandlerConverter(domHandlerClass));
565
            mapping.setConverter(converter);
566
        }
549
        }
567
        descriptor.addMapping(mapping);
550
        descriptor.addMapping(mapping);
568
        mapping.setMixedContent(isMixed);
551
        mapping.setMixedContent(isMixed);
Lines 599-604 Link Here
599
        if(referenceClassName == null){
582
        if(referenceClassName == null){
600
        	((XMLField)mapping.getField()).setIsTypedTextField(true);        	
583
        	((XMLField)mapping.getField()).setIsTypedTextField(true);        	
601
        	((XMLField)mapping.getField()).setSchemaType(XMLConstants.ANY_TYPE_QNAME);
584
        	((XMLField)mapping.getField()).setSchemaType(XMLConstants.ANY_TYPE_QNAME);
585
        	String defaultValue = property.getDefaultValue();
586
        	if(null != defaultValue) {
587
        	    mapping.setConverter(new DefaultElementConverter(defaultValue));
588
        	}
602
        }else{
589
        }else{
603
        	mapping.setReferenceClassName(referenceClassName);
590
        	mapping.setReferenceClassName(referenceClassName);
604
        }
591
        }
Lines 764-769 Link Here
764
    public void generateEnumCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, EnumTypeInfo info) {
751
    public void generateEnumCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, EnumTypeInfo info) {
765
        XMLCompositeDirectCollectionMapping mapping = new XMLCompositeDirectCollectionMapping();
752
        XMLCompositeDirectCollectionMapping mapping = new XMLCompositeDirectCollectionMapping();
766
        mapping.setAttributeName(property.getPropertyName());
753
        mapping.setAttributeName(property.getPropertyName());
754
        mapping.setReuseContainer(true);
767
        if (property.isMethodProperty()) {
755
        if (property.isMethodProperty()) {
768
            if (property.getGetMethodName() == null) {
756
            if (property.getGetMethodName() == null) {
769
                // handle case of set with no get method 
757
                // handle case of set with no get method 
Lines 801-806 Link Here
801
    public void generateAnyAttributeMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
789
    public void generateAnyAttributeMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
802
        XMLAnyAttributeMapping mapping = new XMLAnyAttributeMapping();
790
        XMLAnyAttributeMapping mapping = new XMLAnyAttributeMapping();
803
        mapping.setAttributeName(property.getPropertyName());
791
        mapping.setAttributeName(property.getPropertyName());
792
        mapping.setReuseContainer(true);
804
        if (property.isMethodProperty()) {
793
        if (property.isMethodProperty()) {
805
            if (property.getGetMethodName() == null) {
794
            if (property.getGetMethodName() == null) {
806
                // handle case of set with no get method 
795
                // handle case of set with no get method 
Lines 846-855 Link Here
846
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);   
835
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);   
847
        }
836
        }
848
        mapping.setUseXMLRoot(true);
837
        mapping.setUseXMLRoot(true);
849
        JAXBElementAttributeAccessor accessor = new JAXBElementAttributeAccessor(mapping.getAttributeAccessor());
838
        Class declaredType = helper.getClassForJavaClass(property.getActualType());
850
        Class theClass = helper.getClassForJavaClass(property.getActualType());
839
        JAXBElementRootConverter jaxbElementRootConverter = new JAXBElementRootConverter(declaredType);
851
        accessor.setDeclaredType(theClass);
840
        mapping.setConverter(jaxbElementRootConverter);
852
        mapping.setAttributeAccessor(accessor);
853
        descriptor.addMapping(mapping);
841
        descriptor.addMapping(mapping);
854
    }
842
    }
855
    
843
    
Lines 879-884 Link Here
879
    public XMLCompositeCollectionMapping generateMapMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
867
    public XMLCompositeCollectionMapping generateMapMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
880
    	XMLCompositeCollectionMapping mapping = new XMLCompositeCollectionMapping();
868
    	XMLCompositeCollectionMapping mapping = new XMLCompositeCollectionMapping();
881
        mapping.setAttributeName(property.getPropertyName());
869
        mapping.setAttributeName(property.getPropertyName());
870
        mapping.setReuseContainer(true);
882
        XMLField field = getXPathForField(property, namespaceInfo, false);
871
        XMLField field = getXPathForField(property, namespaceInfo, false);
883
        JavaClass descriptorClass = helper.getJavaClass(descriptor.getJavaClassName());
872
        JavaClass descriptorClass = helper.getJavaClass(descriptor.getJavaClassName());
884
        JavaClass mapValueClass = helper.getJavaClass(MapValue.class);
873
        JavaClass mapValueClass = helper.getJavaClass(MapValue.class);
Lines 1058-1063 Link Here
1058
    public XMLCompositeCollectionMapping generateCompositeCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, String referenceClassName) {
1047
    public XMLCompositeCollectionMapping generateCompositeCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo, String referenceClassName) {
1059
        XMLCompositeCollectionMapping mapping = new XMLCompositeCollectionMapping();
1048
        XMLCompositeCollectionMapping mapping = new XMLCompositeCollectionMapping();
1060
        mapping.setAttributeName(property.getPropertyName());
1049
        mapping.setAttributeName(property.getPropertyName());
1050
        mapping.setReuseContainer(true);
1061
        if (property.isMethodProperty()) {
1051
        if (property.isMethodProperty()) {
1062
            if (property.getGetMethodName() == null) {
1052
            if (property.getGetMethodName() == null) {
1063
                // handle case of set with no get method 
1053
                // handle case of set with no get method 
Lines 1120-1125 Link Here
1120
    public XMLCompositeDirectCollectionMapping generateDirectCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
1110
    public XMLCompositeDirectCollectionMapping generateDirectCollectionMapping(Property property, XMLDescriptor descriptor, NamespaceInfo namespaceInfo) {
1121
        XMLCompositeDirectCollectionMapping mapping = new XMLCompositeDirectCollectionMapping();
1111
        XMLCompositeDirectCollectionMapping mapping = new XMLCompositeDirectCollectionMapping();
1122
        mapping.setAttributeName(property.getPropertyName());
1112
        mapping.setAttributeName(property.getPropertyName());
1113
        mapping.setReuseContainer(true);
1123
        if (property.isMethodProperty()) {
1114
        if (property.isMethodProperty()) {
1124
            if (property.getGetMethodName() == null) {
1115
            if (property.getGetMethodName() == null) {
1125
                // handle case of set with no get method 
1116
                // handle case of set with no get method 
Lines 1367-1372 Link Here
1367
        XMLField srcXPath = getXPathForField(property, namespaceInfo, true);
1358
        XMLField srcXPath = getXPathForField(property, namespaceInfo, true);
1368
        XMLCollectionReferenceMapping mapping = new XMLCollectionReferenceMapping();
1359
        XMLCollectionReferenceMapping mapping = new XMLCollectionReferenceMapping();
1369
        mapping.setAttributeName(property.getPropertyName());
1360
        mapping.setAttributeName(property.getPropertyName());
1361
        mapping.setReuseContainer(true);
1370
        if (property.isMethodProperty()) {
1362
        if (property.isMethodProperty()) {
1371
            if (property.getGetMethodName() == null) {
1363
            if (property.getGetMethodName() == null) {
1372
                // handle case of set with no get method 
1364
                // handle case of set with no get method 
Lines 1673-1678 Link Here
1673
              mapping.setAttributeName("value");
1665
              mapping.setAttributeName("value");
1674
              mapping.setXPath("text()");
1666
              mapping.setXPath("text()");
1675
              mapping.setUsesSingleNode(true);
1667
              mapping.setUsesSingleNode(true);
1668
              mapping.setReuseContainer(true);
1676
              
1669
              
1677
              if(type != null && type.isEnumerationType()){
1670
              if(type != null && type.isEnumerationType()){
1678
                  mapping.setValueConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo)type));
1671
                  mapping.setValueConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo)type));
(-)src/org/eclipse/persistence/jaxb/compiler/ReferenceProperty.java (-1 / +3 lines)
Lines 27-33 Link Here
27
		if(referencedElements == null) {
27
		if(referencedElements == null) {
28
			referencedElements = new ArrayList<ElementDeclaration>();
28
			referencedElements = new ArrayList<ElementDeclaration>();
29
		}
29
		}
30
		referencedElements.add(element);
30
		if(!referencedElements.contains(element)) {
31
			referencedElements.add(element);
32
		}
31
	}
33
	}
32
	
34
	
33
	public boolean isReference() {
35
	public boolean isReference() {

Return to bug 279003