Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ea5391cd7ea3ca5d93d21330533ed7eb09bb628a (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*******************************************************************************
 * Copyright (c) 2010 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.resource.java.source;

import java.util.Arrays;
import java.util.Vector;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.core.internal.resource.java.source.SourceAnnotation;
import org.eclipse.jpt.core.internal.utility.jdt.ASTTools;
import org.eclipse.jpt.core.internal.utility.jdt.AnnotationStringArrayExpressionConverter;
import org.eclipse.jpt.core.internal.utility.jdt.ConversionDeclarationAnnotationElementAdapter;
import org.eclipse.jpt.core.internal.utility.jdt.AnnotatedElementAnnotationElementAdapter;
import org.eclipse.jpt.core.internal.utility.jdt.SimpleDeclarationAnnotationAdapter;
import org.eclipse.jpt.core.internal.utility.jdt.SimpleTypeStringExpressionConverter;
import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType;
import org.eclipse.jpt.core.utility.TextRange;
import org.eclipse.jpt.core.utility.jdt.AnnotationElementAdapter;
import org.eclipse.jpt.core.utility.jdt.DeclarationAnnotationAdapter;
import org.eclipse.jpt.core.utility.jdt.DeclarationAnnotationElementAdapter;
import org.eclipse.jpt.core.utility.jdt.ExpressionConverter;
import org.eclipse.jpt.core.utility.jdt.Type;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.XmlTypeAnnotation;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.iterables.ListIterable;
import org.eclipse.jpt.utility.internal.iterables.LiveCloneListIterable;

/**
 * javax.xml.bind.annotation.XmlType
 */
public final class SourceXmlTypeAnnotation
	extends SourceAnnotation<Type>
	implements XmlTypeAnnotation
{
	public static final DeclarationAnnotationAdapter DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(ANNOTATION_NAME);

	private static final DeclarationAnnotationElementAdapter<String> FACTORY_CLASS_ADAPTER = buildFactoryClassAdapter();
	private final AnnotationElementAdapter<String> factoryClassAdapter;
	private String factoryClass;
	
	private String fullyQualifiedFactoryClassName;

	private static final DeclarationAnnotationElementAdapter<String> FACTORY_METHOD_ADAPTER = buildFactoryMethodAdapter();
	private final AnnotationElementAdapter<String> factoryMethodAdapter;
	private String factoryMethod;

	private static final DeclarationAnnotationElementAdapter<String> NAME_ADAPTER = buildNameAdapter();
	private final AnnotationElementAdapter<String> nameAdapter;
	private String name;

	private static final DeclarationAnnotationElementAdapter<String> NAMESPACE_ADAPTER = buildNamespaceAdapter();
	private final AnnotationElementAdapter<String> namespaceAdapter;
	private String namespace;

	private final DeclarationAnnotationElementAdapter<String[]> propOrderDeclarationAdapter;
	private final AnnotationElementAdapter<String[]> propOrderAdapter;
	private final Vector<String> propOrder = new Vector<String>();

	public SourceXmlTypeAnnotation(JavaResourcePersistentType parent, Type type) {
		super(parent, type, DECLARATION_ANNOTATION_ADAPTER);
		this.factoryClassAdapter = this.buildAnnotationElementAdapter(FACTORY_CLASS_ADAPTER);
		this.factoryMethodAdapter = this.buildAnnotationElementAdapter(FACTORY_METHOD_ADAPTER);
		this.nameAdapter = this.buildAnnotationElementAdapter(NAME_ADAPTER);
		this.namespaceAdapter = this.buildAnnotationElementAdapter(NAMESPACE_ADAPTER);
		this.propOrderDeclarationAdapter = buildArrayAnnotationElementAdapter(DECLARATION_ANNOTATION_ADAPTER, JAXB.XML_TYPE__PROP_ORDER);
		this.propOrderAdapter = this.buildArrayAnnotationElementAdapter(this.propOrderDeclarationAdapter);
	}

	protected AnnotationElementAdapter<String> buildAnnotationElementAdapter(DeclarationAnnotationElementAdapter<String> daea) {
		return new AnnotatedElementAnnotationElementAdapter<String>(this.annotatedElement, daea);
	}

	private static DeclarationAnnotationElementAdapter<String[]> buildArrayAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, String elementName) {
		return buildArrayAnnotationElementAdapter(annotationAdapter, elementName, AnnotationStringArrayExpressionConverter.forStrings());
	}

	private static DeclarationAnnotationElementAdapter<String[]> buildArrayAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, String elementName, ExpressionConverter<String[]> converter) {
		return new ConversionDeclarationAnnotationElementAdapter<String[]>(annotationAdapter, elementName, false, converter);
	}

	private AnnotationElementAdapter<String[]> buildArrayAnnotationElementAdapter(DeclarationAnnotationElementAdapter<String[]> daea) {
		return new AnnotatedElementAnnotationElementAdapter<String[]>(this.annotatedElement, daea);
	}

	public String getAnnotationName() {
		return ANNOTATION_NAME;
	}

	public void initialize(CompilationUnit astRoot) {
		this.factoryClass = this.buildFactoryClass(astRoot);
		this.fullyQualifiedFactoryClassName = this.buildFullyQualifiedFactoryClassName(astRoot);
		this.factoryMethod = this.buildFactoryMethod(astRoot);
		this.name = this.buildName(astRoot);
		this.namespace = this.buildNamespace(astRoot);
		this.initializePropOrder(astRoot);
	}

	public void synchronizeWith(CompilationUnit astRoot) {
		this.syncFactoryClass(this.buildFactoryClass(astRoot));
		this.syncFullyQualifiedFactoryClassName(this.buildFullyQualifiedFactoryClassName(astRoot));
		this.syncFactoryMethod(this.buildFactoryMethod(astRoot));
		this.syncName(this.buildName(astRoot));
		this.syncNamespace(this.buildNamespace(astRoot));
		this.syncPropOrder(astRoot);
	}

	@Override
	public void toString(StringBuilder sb) {
		sb.append(this.name);
	}


	// ********** XmlTypeAnnotation implementation **********

	// ***** factoryClass
	public String getFactoryClass() {
		return this.factoryClass;
	}

	public void setFactoryClass(String factoryClass) {
		if (this.attributeValueHasChanged(this.factoryClass, factoryClass)) {
			this.factoryClass = factoryClass;
			this.factoryClassAdapter.setValue(factoryClass);
		}
	}

	private void syncFactoryClass(String astFactoryClass) {
		String old = this.factoryClass;
		this.factoryClass = astFactoryClass;
		this.firePropertyChanged(FACTORY_CLASS_PROPERTY, old, astFactoryClass);
	}

	private String buildFactoryClass(CompilationUnit astRoot) {
		return this.factoryClassAdapter.getValue(astRoot);
	}

	public TextRange getFactoryClassTextRange(CompilationUnit astRoot) {
		return this.getElementTextRange(FACTORY_CLASS_ADAPTER, astRoot);
	}
	
	// ***** fully-qualified factory class name
	public String getFullyQualifiedFactoryClassName() {
		return this.fullyQualifiedFactoryClassName;
	}

	private void syncFullyQualifiedFactoryClassName(String name) {
		String old = this.fullyQualifiedFactoryClassName;
		this.fullyQualifiedFactoryClassName = name;
		this.firePropertyChanged(FULLY_QUALIFIED_FACTORY_CLASS_NAME_PROPERTY, old, name);
	}

	private String buildFullyQualifiedFactoryClassName(CompilationUnit astRoot) {
		return (this.factoryClass == null) ? null : ASTTools.resolveFullyQualifiedName(this.factoryClassAdapter.getExpression(astRoot));
	}

	// ***** factoryMethod
	public String getFactoryMethod() {
		return this.factoryMethod;
	}

	public void setFactoryMethod(String factoryMethod) {
		if (this.attributeValueHasChanged(this.factoryMethod, factoryMethod)) {
			this.factoryMethod = factoryMethod;
			this.factoryMethodAdapter.setValue(factoryMethod);
		}
	}

	private void syncFactoryMethod(String astFactoryMethod) {
		String old = this.factoryMethod;
		this.factoryMethod = astFactoryMethod;
		this.firePropertyChanged(FACTORY_METHOD_PROPERTY, old, astFactoryMethod);
	}

	private String buildFactoryMethod(CompilationUnit astRoot) {
		return this.factoryMethodAdapter.getValue(astRoot);
	}

	public TextRange getFactoryMethodTextRange(CompilationUnit astRoot) {
		return this.getElementTextRange(FACTORY_METHOD_ADAPTER, astRoot);
	}

	// ***** name
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		if (this.attributeValueHasChanged(this.name, name)) {
			this.name = name;
			this.nameAdapter.setValue(name);
		}
	}

	private void syncName(String astName) {
		String old = this.name;
		this.name = astName;
		this.firePropertyChanged(NAME_PROPERTY, old, astName);
	}

	private String buildName(CompilationUnit astRoot) {
		return this.nameAdapter.getValue(astRoot);
	}

	public TextRange getNameTextRange(CompilationUnit astRoot) {
		return this.getElementTextRange(NAME_ADAPTER, astRoot);
	}

	// ***** name
	public String getNamespace() {
		return this.namespace;
	}

	public void setNamespace(String namespace) {
		if (this.attributeValueHasChanged(this.namespace, namespace)) {
			this.namespace = namespace;
			this.namespaceAdapter.setValue(namespace);
		}
	}

	private void syncNamespace(String astNamespace) {
		String old = this.namespace;
		this.namespace = astNamespace;
		this.firePropertyChanged(NAMESPACE_PROPERTY, old, astNamespace);
	}

	private String buildNamespace(CompilationUnit astRoot) {
		return this.namespaceAdapter.getValue(astRoot);
	}

	public TextRange getNamespaceTextRange(CompilationUnit astRoot) {
		return this.getElementTextRange(NAMESPACE_ADAPTER, astRoot);
	}

	// ***** prop order
	public ListIterable<String> getPropOrder() {
		return new LiveCloneListIterable<String>(this.propOrder);
	}

	public int getPropOrderSize() {
		return this.propOrder.size();
	}

	public void addProp(String prop) {
		this.addProp(this.propOrder.size(), prop);
	}

	public void addProp(int index, String prop) {
		this.propOrder.add(index, prop);
		this.writePropOrder();
	}

	public void moveProp(int targetIndex, int sourceIndex) {
		CollectionTools.move(this.propOrder, targetIndex, sourceIndex);
		this.writePropOrder();
	}

	public void removeProp(String prop) {
		this.propOrder.remove(prop);
		this.writePropOrder();
	}

	public void removeProp(int index) {
		this.propOrder.remove(index);
		this.writePropOrder();
	}

	private void writePropOrder() {
		this.propOrderAdapter.setValue(this.propOrder.toArray(new String[this.propOrder.size()]));
	}

	private void initializePropOrder(CompilationUnit astRoot) {
		String[] astPropOrder = this.propOrderAdapter.getValue(astRoot);
		for (int i = 0; i < astPropOrder.length; i++) {
			this.propOrder.add(astPropOrder[i]);
		}
	}

	private void syncPropOrder(CompilationUnit astRoot) {
		String[] astPropOrder = this.propOrderAdapter.getValue(astRoot);
		this.synchronizeList(Arrays.asList(astPropOrder), this.propOrder, PROP_ORDER_LIST);
	}


	//*********** static methods ****************

	private static DeclarationAnnotationElementAdapter<String> buildFactoryClassAdapter() {
		return buildAnnotationElementAdapter(DECLARATION_ANNOTATION_ADAPTER, JAXB.XML_TYPE__FACTORY_CLASS, SimpleTypeStringExpressionConverter.instance());
	}

	static DeclarationAnnotationElementAdapter<String> buildAnnotationElementAdapter(DeclarationAnnotationAdapter annotationAdapter, String elementName, ExpressionConverter<String> converter) {
		return new ConversionDeclarationAnnotationElementAdapter<String>(annotationAdapter, elementName, false, converter);
	}

	private static DeclarationAnnotationElementAdapter<String> buildFactoryMethodAdapter() {
		return ConversionDeclarationAnnotationElementAdapter.forStrings(DECLARATION_ANNOTATION_ADAPTER, JAXB.XML_TYPE__FACTORY_METHOD, false); // false = do not remove annotation when empty
	}

	private static DeclarationAnnotationElementAdapter<String> buildNameAdapter() {
		return ConversionDeclarationAnnotationElementAdapter.forStrings(DECLARATION_ANNOTATION_ADAPTER, JAXB.XML_TYPE__NAME, false); // false = do not remove annotation when empty
	}

	private static DeclarationAnnotationElementAdapter<String> buildNamespaceAdapter() {
		return ConversionDeclarationAnnotationElementAdapter.forStrings(DECLARATION_ANNOTATION_ADAPTER, JAXB.XML_TYPE__NAMESPACE, false); // false = do not remove annotation when empty
	}

}

Back to the top