Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2b126bff11e250fc29755c55ddcb4b86f4531e0f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*******************************************************************************
 * Copyright (c) 2011 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.jaxb.core.internal.context.java;

import java.util.List;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.utility.Filter;
import org.eclipse.jpt.jaxb.core.MappingKeys;
import org.eclipse.jpt.jaxb.core.context.JaxbPersistentAttribute;
import org.eclipse.jpt.jaxb.core.context.XmlElementMapping;
import org.eclipse.jpt.jaxb.core.context.XmlElementWrapper;
import org.eclipse.jpt.jaxb.core.context.XmlNsForm;
import org.eclipse.jpt.jaxb.core.internal.validation.JaxbValidationMessages;
import org.eclipse.jpt.jaxb.core.resource.java.XmlElementAnnotation;
import org.eclipse.jpt.jaxb.core.resource.java.XmlElementWrapperAnnotation;
import org.eclipse.jpt.jaxb.core.xsd.XsdComponent;
import org.eclipse.jpt.jaxb.core.xsd.XsdTypeDefinition;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;

public class GenericJavaXmlElementMapping
	extends GenericJavaContainmentMapping<XmlElementAnnotation>
	implements XmlElementMapping
{

	protected Boolean specifiedNillable;

	protected String defaultValue;

	protected String specifiedType;

	protected XmlElementWrapper xmlElementWrapper;

	public GenericJavaXmlElementMapping(JaxbPersistentAttribute parent) {
		super(parent);
		this.specifiedNillable = this.buildSpecifiedNillable();
		this.defaultValue = this.buildDefaultValue();
		this.specifiedType = this.buildSpecifiedType();
		this.initializeXmlElementWrapper();			
	}

	public String getKey() {
		return MappingKeys.XML_ELEMENT_ATTRIBUTE_MAPPING_KEY;
	}

	@Override
	protected String getAnnotationName() {
		return XmlElementAnnotation.ANNOTATION_NAME;
	}

	@Override
	public void synchronizeWithResourceModel() {
		super.synchronizeWithResourceModel();
		this.setSpecifiedNillable_(this.buildSpecifiedNillable());
		this.setDefaultValue_(this.buildDefaultValue());
		this.setSpecifiedType_(this.buildSpecifiedType());
		this.syncXmlElementWrapper();
	}

	@Override
	public void update() {
		super.update();
		this.updateXmlElementWrapper();
	}

	//************  XmlElement.nillable ***************

	public boolean isNillable() {
		return (this.getSpecifiedNillable() == null) ? this.isDefaultNillable() : this.getSpecifiedNillable().booleanValue();
	}

	public boolean isDefaultNillable() {
		return DEFAULT_NILLABLE;
	}

	public Boolean getSpecifiedNillable() {
		return this.specifiedNillable;
	}

	public void setSpecifiedNillable(Boolean newSpecifiedNillable) {
		this.getAnnotationForUpdate().setNillable(newSpecifiedNillable);
		this.setSpecifiedNillable_(newSpecifiedNillable);
	}

	protected void setSpecifiedNillable_(Boolean newSpecifiedNillable) {
		Boolean oldNillable = this.specifiedNillable;
		this.specifiedNillable = newSpecifiedNillable;
		firePropertyChanged(SPECIFIED_NILLABLE_PROPERTY, oldNillable, newSpecifiedNillable);
	}

	protected Boolean buildSpecifiedNillable() {
		return getMappingAnnotation() == null ? null : getMappingAnnotation().getNillable();
	}


	//************  XmlElement.defaultValue ***************

	public String getDefaultValue() {
		return this.defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.getAnnotationForUpdate().setDefaultValue(defaultValue);
		this.setDefaultValue_(defaultValue);
	}

	protected void setDefaultValue_(String defaultValue) {
		String oldDefaultValue = this.defaultValue;
		this.defaultValue = defaultValue;
		firePropertyChanged(DEFAULT_VALUE_PROPERTY, oldDefaultValue, defaultValue);		
	}

	protected String buildDefaultValue() {
		return getMappingAnnotation() == null ? null : getMappingAnnotation().getDefaultValue();
	}

	//************  XmlElement.type ***************

	public String getType() {
		return getSpecifiedType() == null ? getDefaultType() : getSpecifiedType();
	}

	public String getDefaultType() {
		//TODO calculate default type
		return null;
	}

	public String getSpecifiedType() {
		return this.specifiedType;
	}

	public void setSpecifiedType(String newSpecifiedType) {
		this.getAnnotationForUpdate().setType(newSpecifiedType);
		this.setSpecifiedType_(newSpecifiedType);
	}

	protected void setSpecifiedType_(String newSpecifiedType) {
		String oldType = this.specifiedType;
		this.specifiedType = newSpecifiedType;
		firePropertyChanged(SPECIFIED_TYPE_PROPERTY, oldType, newSpecifiedType);
	}

	protected String buildSpecifiedType() {
		return getMappingAnnotation() == null ? null : getMappingAnnotation().getType();
	}

	//************  XmlElementWrapper ***************

	public XmlElementWrapper getXmlElementWrapper() {
		return this.xmlElementWrapper;
	}

	public XmlElementWrapper addXmlElementWrapper() {
		if (this.xmlElementWrapper != null) {
			throw new IllegalStateException();
		}
		XmlElementWrapperAnnotation annotation = (XmlElementWrapperAnnotation) this.getJavaResourceAttribute().addAnnotation(XmlElementWrapperAnnotation.ANNOTATION_NAME);

		XmlElementWrapper xmlElementWrapper = this.buildXmlElementWrapper(annotation);
		this.setXmlElementWrapper_(xmlElementWrapper);
		return xmlElementWrapper;
	}

	protected XmlElementWrapper buildXmlElementWrapper(XmlElementWrapperAnnotation xmlElementWrapperAnnotation) {
		return new GenericJavaXmlElementWrapper(this, xmlElementWrapperAnnotation);
	}

	public void removeXmlElementWrapper() {
		if (this.xmlElementWrapper == null) {
			throw new IllegalStateException();
		}
		this.getJavaResourceAttribute().removeAnnotation(XmlElementWrapperAnnotation.ANNOTATION_NAME);
		this.setXmlElementWrapper_(null);
	}

	protected void initializeXmlElementWrapper() {
		XmlElementWrapperAnnotation annotation = this.getXmlElementWrapperAnnotation();
		if (annotation != null) {
			this.xmlElementWrapper = this.buildXmlElementWrapper(annotation);
		}
	}

	protected XmlElementWrapperAnnotation getXmlElementWrapperAnnotation() {
		return (XmlElementWrapperAnnotation) this.getJavaResourceAttribute().getAnnotation(XmlElementWrapperAnnotation.ANNOTATION_NAME);
	}

	protected void syncXmlElementWrapper() {
		XmlElementWrapperAnnotation annotation = this.getXmlElementWrapperAnnotation();
		if (annotation != null) {
			if (this.getXmlElementWrapper() != null) {
				this.getXmlElementWrapper().synchronizeWithResourceModel();
			}
			else {
				this.setXmlElementWrapper_(this.buildXmlElementWrapper(annotation));
			}
		}
		else {
			this.setXmlElementWrapper_(null);
		}
	}

	protected void updateXmlElementWrapper() {
		if (this.getXmlElementWrapper() != null) {
			this.getXmlElementWrapper().update();
		}
	}

	protected void setXmlElementWrapper_(XmlElementWrapper xmlElementWrapper) {
		XmlElementWrapper oldXmlElementWrapper = this.xmlElementWrapper;
		this.xmlElementWrapper = xmlElementWrapper;
		firePropertyChanged(XML_ELEMENT_WRAPPER_PROPERTY, oldXmlElementWrapper, xmlElementWrapper);
	}

	@Override
	public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
		super.validate(messages, reporter, astRoot);
		if (this.getXmlElementWrapper() != null) {
			this.getXmlElementWrapper().validate(messages, reporter, astRoot);
		}
	}
	
	
	// **************** misc **************************************************
	
	@Override
	public String getDefaultNamespace() {
		return (getJaxbPackage().getElementFormDefault() == XmlNsForm.QUALIFIED) ?
				getPersistentClass().getNamespace()
				: "";
	}
	
	@Override
	public XsdComponent getXsdComponent() {
		XsdTypeDefinition xsdType = getPersistentClass().getXsdTypeDefinition();
		return (xsdType == null) ? null : xsdType.getElement(getNamespace(), getName());
	}
	
	
	// **************** content assist ****************************************
	
	@Override
	protected Iterable<String> getNameProposals(XsdTypeDefinition type, String namespace, Filter<String> filter) {
		return type.getElementNameProposals(namespace, filter);
	}
	
	
	// **************** validation ********************************************
	
	@Override
	protected String getMissingNameMessage() {
		return JaxbValidationMessages.XML_ELEMENT__MISSING_NAME;
	}
	
	@Override
	protected String getUnresolvedComponentMessage() {
		return JaxbValidationMessages.XML_ELEMENT__UNRESOLVED_ELEMENT;
	}
}

Back to the top