Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3ec1e2b91d018bcead8f7f59095cd656632b3733 (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
/*******************************************************************************
 * Copyright (c) 2010, 2012 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.core.resource.java.JavaResourceEnumConstant;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.Filter;
import org.eclipse.jpt.common.utility.internal.CollectionTools;
import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable;
import org.eclipse.jpt.jaxb.core.context.JaxbEnumConstant;
import org.eclipse.jpt.jaxb.core.context.JaxbEnumMapping;
import org.eclipse.jpt.jaxb.core.internal.validation.DefaultValidationMessages;
import org.eclipse.jpt.jaxb.core.internal.validation.JaxbValidationMessages;
import org.eclipse.jpt.jaxb.core.resource.java.JAXB;
import org.eclipse.jpt.jaxb.core.resource.java.XmlEnumValueAnnotation;
import org.eclipse.jpt.jaxb.core.xsd.XsdSimpleTypeDefinition;
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 GenericJavaEnumConstant
		extends AbstractJavaContextNode
		implements JaxbEnumConstant {

	final protected JavaResourceEnumConstant resourceEnumConstant;
	
	protected String specifiedValue;

	public GenericJavaEnumConstant(JaxbEnumMapping parent, JavaResourceEnumConstant resourceEnumConstant) {
		super(parent);
		this.resourceEnumConstant = resourceEnumConstant;
		this.specifiedValue = this.getResourceEnumValue();
	}
	
	
	protected JaxbEnumMapping getEnumMapping() {
		return (JaxbEnumMapping) getParent();
	}
	
	public JavaResourceEnumConstant getResourceEnumConstant() {
		return this.resourceEnumConstant;
	}


	// ********** synchronize/update **********

	@Override
	public void synchronizeWithResourceModel() {
		super.synchronizeWithResourceModel();
		this.setSpecifiedValue_(this.getResourceEnumValue());
	}


	// ********** xml enum value annotation **********

	protected XmlEnumValueAnnotation getXmlEnumValueAnnotation() {
		return (XmlEnumValueAnnotation) this.getResourceEnumConstant().getNonNullAnnotation(JAXB.XML_ENUM_VALUE);
	}


	// ********** name **********

	public String getName() {
		return this.resourceEnumConstant.getName();
	}

	// ********** value **********

	public String getValue() {
		return this.getSpecifiedValue() != null ? this.getSpecifiedValue() : this.getDefaultValue();
	}

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

	public String getSpecifiedValue() {
		return this.specifiedValue;
	}

	public void setSpecifiedValue(String value) {
		this.getXmlEnumValueAnnotation().setValue(value);
		this.setSpecifiedValue_(value);	
	}

	protected void setSpecifiedValue_(String value) {
		String old = this.specifiedValue;
		this.specifiedValue = value;
		this.firePropertyChanged(SPECIFIED_VALUE_PROPERTY, old, value);
	}

	protected String getResourceEnumValue() {
		return this.getXmlEnumValueAnnotation().getValue();
	}
	
	
	// ***** content assist *****
	
	@Override
	public Iterable<String> getJavaCompletionProposals(int pos, Filter<String> filter, CompilationUnit astRoot) {
		Iterable<String> result = super.getJavaCompletionProposals(pos, filter, astRoot);
		if (! CollectionTools.isEmpty(result)) {
			return result;
		}
		
		if (valueTouches(pos, astRoot)) {
			XsdTypeDefinition<?> xsdType = getEnumMapping().getXsdTypeDefinition();
			if (xsdType != null && xsdType.getKind() == XsdTypeDefinition.Kind.SIMPLE) {
				XsdSimpleTypeDefinition xsdSimpleType = (XsdSimpleTypeDefinition) xsdType;
				return xsdSimpleType.getEnumValueProposals(filter);
			}
		}
		
		return EmptyIterable.instance();
	}
	
	protected boolean valueTouches(int pos, CompilationUnit astRoot) {
		return getXmlEnumValueAnnotation().valueTouches(pos, astRoot);
	}
	
	
	// ***** validation *****
	
	@Override
	public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
		super.validate(messages, reporter, astRoot);
		
		String value = getValue();
		XsdTypeDefinition<?> xsdType = getEnumMapping().getXsdTypeDefinition();
		
		if (xsdType == null || xsdType.getKind() != XsdTypeDefinition.Kind.SIMPLE) {
			return;
		}
		
		if (! ((XsdSimpleTypeDefinition) xsdType).getXSDComponent().isValidLiteral(value)) {
			messages.add(
					DefaultValidationMessages.buildMessage(
							IMessage.HIGH_SEVERITY,
							JaxbValidationMessages.XML_ENUM_VALUE__INVALID_LEXICAL_VALUE,
							new String[] { value, xsdType.getName() },
							this,
							getValueTextRange(astRoot)));
		}
	}
	
	@Override
	public TextRange getValidationTextRange(CompilationUnit astRoot) {
		return getResourceEnumConstant().getTextRange(astRoot);
	}
	
	protected TextRange getValueTextRange(CompilationUnit astRoot) {
		TextRange enumValueTextRange = getXmlEnumValueAnnotation().getValueTextRange();
		return enumValueTextRange != null ? enumValueTextRange : getValidationTextRange(astRoot);
	}
}

Back to the top