Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 320a18f87d733d3dc10707929de4c7f8c666ce83 (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
/*******************************************************************************
 *  Copyright (c) 2011, 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.xsd;

import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable;
import org.eclipse.jpt.common.utility.internal.iterables.TransformationIterable;
import org.eclipse.xsd.XSDEnumerationFacet;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDVariety;


public class XsdSimpleTypeDefinition
		extends XsdTypeDefinition<XSDSimpleTypeDefinition> {
	
	XsdSimpleTypeDefinition(XSDSimpleTypeDefinition xsdSimpleTypeDefinition) {
		super(xsdSimpleTypeDefinition);
	}
	
	
	@Override
	public org.eclipse.jpt.jaxb.core.xsd.XsdTypeDefinition.Kind getKind() {
		return Kind.SIMPLE;
	}
	
	@Override
	public boolean hasTextContent() {
		return true;
	}
	
	@Override
	public boolean typeIsValid(XsdTypeDefinition xsdType, boolean isItemType, boolean allowExtension, boolean allowRestriction) {
		if (isItemType) {
			XsdSimpleTypeDefinition itemType = getItemType();
			return (itemType == null) ? false : itemType.typeIsValid(xsdType, false, allowExtension, allowRestriction);
		}
		if (getXSDComponent().getVariety() == XSDVariety.UNION_LITERAL) {
			if (XsdUtil.getSchemaForSchema().getTypeDefinition("string").typeIsValid(xsdType, isItemType, allowExtension, allowRestriction)) {
				return true;
			}
		}
		return super.typeIsValid(xsdType, isItemType, allowExtension, allowRestriction);
	}
	
	/**
	 * If the type is of variety LIST, this will return the item type of the list
	 */
	public XsdSimpleTypeDefinition getItemType() {
		XSDSimpleTypeDefinition xsdItemType = getXSDComponent().getItemTypeDefinition();
		return (xsdItemType == null) ? null : (XsdSimpleTypeDefinition) XsdUtil.getAdapter(xsdItemType);
	}
	
	/**
	 * If the type is of variety UNION, this will return the member types of the union
	 */
	public Iterable<XsdSimpleTypeDefinition> getMemberTypes() {
		return new TransformationIterable<XSDSimpleTypeDefinition, XsdSimpleTypeDefinition>(
				getXSDComponent().getMemberTypeDefinitions()) {
			@Override
			protected XsdSimpleTypeDefinition transform(XSDSimpleTypeDefinition o) {
				return (XsdSimpleTypeDefinition) XsdUtil.getAdapter(o);
			}
		};
	}
	
	@Override
	public XsdAttributeUse getAttribute(String namespace, String name) {
		// simple types have no attributes
		return null;
	}
	
	@Override
	public Iterable<String> getAttributeNames(String namespace) {
		// simple types have no attributes
		return EmptyIterable.instance();
	}
	
	@Override
	public XsdElementDeclaration getElement(String namespace, String name, boolean recurseChildren) {
		// simple types have no elements
		return null;
	}
	
	@Override
	public Iterable<String> getElementNames(String namespace, boolean recurseChildren) {
		// simple types have no elements
		return EmptyIterable.instance();
	}
	
	public Iterable<String> getEnumValueProposals() {
		return StringTools.convertToJavaStringLiteralContents(
					new TransformationIterable<XSDEnumerationFacet, String>(getXSDComponent().getEnumerationFacets()) {
						@Override
						protected String transform(XSDEnumerationFacet enumFacet) {
							return enumFacet.getLexicalValue();
						}
					});
	}
}

Back to the top