1
|
|
/*
|
2
|
|
* JBind
|
3
|
|
*
|
4
|
|
* Copyright (c) by Stefan Wachter. All rights reserved.
|
5
|
|
*
|
6
|
|
* Usage, modification, and redistribution is subject to license terms that are
|
7
|
|
* available at 'http://www.jbind.org'. The JBind license is like the
|
8
|
|
* 'Apache Software License V 1.1'.
|
9
|
|
*/
|
10
|
|
package org.jbind.xml.schema.cmp;
|
11
|
|
|
12
|
|
import java.util.Collections;
|
13
|
|
import java.util.HashMap;
|
14
|
|
import java.util.Iterator;
|
15
|
|
import java.util.Map;
|
16
|
|
import java.util.Set;
|
17
|
|
|
18
|
|
import org.jbind.xml.base.BlockType;
|
19
|
|
import org.jbind.xml.base.FinalType;
|
20
|
|
import org.jbind.xml.base.IRange;
|
21
|
|
import org.jbind.xml.base.IRef;
|
22
|
|
import org.jbind.xml.base.Range;
|
23
|
|
import org.jbind.xml.core.cmp.IComponentVisitor;
|
24
|
|
import org.jbind.xml.core.cmp.ISourceInfo;
|
25
|
|
import org.jbind.xml.core.content.IElemDecl;
|
26
|
|
import org.jbind.xml.core.content.IElemRefOrDecl;
|
27
|
|
import org.jbind.xml.core.data.ISimpleData;
|
28
|
|
import org.jbind.xml.core.type.IAnyType;
|
29
|
|
import org.jbind.xml.instance.data.StringDataImpl;
|
30
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
31
|
|
import org.jbind.xml.msg.XmlException;
|
32
|
|
import org.jbind.xml.msg.XmlMessages;
|
33
|
|
|
34
|
|
public class ElemDecl extends ElemRefOrDecl implements IElemDecl {
|
35
|
|
|
36
|
|
private Set myFinalTypes = null;
|
37
|
|
private Set myBlockTypes = null;
|
38
|
|
|
39
|
|
private boolean myIsAbstract = false;
|
40
|
|
private boolean myIsNillable = false;
|
41
|
|
|
42
|
|
/**
|
43
|
|
* Maps strings of the form aNamespace+":"+aName to element declarations.
|
44
|
|
* The mapped element declarations can be used as a substitute for this element
|
45
|
|
* declaration.
|
46
|
|
*/
|
47
|
|
private Map mySubstitutes = null;
|
48
|
|
|
49
|
|
private IElemDecl mySubstitutionGroup = null;
|
50
|
|
private IAnyType myType = null;
|
51
|
|
|
52
|
705
|
public ElemDecl(ISourceInfo aSourceInfo, String aNamespace, String aName, IRange aRange, Set aFinalTypes, Set aBlockTypes, boolean anIsAbstract, boolean anIsNillable) {
|
53
|
705
|
super(aSourceInfo, aNamespace, aName, aRange);
|
54
|
705
|
myFinalTypes = aFinalTypes;
|
55
|
705
|
myBlockTypes = aBlockTypes;
|
56
|
705
|
myIsAbstract = anIsAbstract;
|
57
|
705
|
myIsNillable = anIsNillable;
|
58
|
|
}
|
59
|
|
|
60
|
1500
|
public void accept(IComponentVisitor aVisitor) throws XmlException {
|
61
|
1500
|
aVisitor.visitElemDeclStart(this);
|
62
|
1500
|
visitSubComponents(aVisitor);
|
63
|
1500
|
aVisitor.visitElemDeclEnd(this);
|
64
|
|
}
|
65
|
|
|
66
|
10
|
public String getDesc() {
|
67
|
10
|
return getGlobalRef().toString();
|
68
|
|
}
|
69
|
|
|
70
|
22322
|
public IAnyType getType() {
|
71
|
22322
|
return myType;
|
72
|
|
}
|
73
|
696
|
public void setType(IAnyType aType) {
|
74
|
696
|
myType = aType;
|
75
|
|
}
|
76
|
|
|
77
|
775
|
public IElemDecl getSubstitutionGroup() {
|
78
|
775
|
return mySubstitutionGroup;
|
79
|
|
}
|
80
|
37
|
public void setSubstitutionGroup(IElemDecl aContent) {
|
81
|
37
|
mySubstitutionGroup = aContent;
|
82
|
|
}
|
83
|
|
|
84
|
498
|
public boolean isNillable() {
|
85
|
498
|
return myIsNillable;
|
86
|
|
}
|
87
|
|
|
88
|
2419
|
public boolean isAbstract() {
|
89
|
2419
|
return myIsAbstract;
|
90
|
|
}
|
91
|
|
|
92
|
46
|
public void addSubstitute(IElemDecl aDecl) {
|
93
|
46
|
if (null == mySubstitutes) {
|
94
|
21
|
mySubstitutes = new HashMap(17);
|
95
|
|
}
|
96
|
46
|
IRef ref = aDecl.getGlobalRef();
|
97
|
46
|
IElemRefOrDecl d = aDecl.getRange().isOne() ? (IElemRefOrDecl)aDecl : (IElemRefOrDecl)(new ElemRef(this, Range.ONE, this));
|
98
|
46
|
mySubstitutes.put(ref.getNamespace() + ":" + ref.getLocalPart(), d);
|
99
|
|
}
|
100
|
|
|
101
|
3177
|
public IElemRefOrDecl getSubstitute(String aNamespace, String aName) {
|
102
|
3177
|
IElemRefOrDecl res = null;
|
103
|
3177
|
if (null != mySubstitutes) {
|
104
|
103
|
res = (IElemRefOrDecl)mySubstitutes.get(aNamespace + ":" + aName);
|
105
|
|
}
|
106
|
3177
|
return res;
|
107
|
|
}
|
108
|
|
|
109
|
318
|
public Iterator iterSubstitutes() {
|
110
|
318
|
return (null != mySubstitutes) ? mySubstitutes.values().iterator() : Collections.EMPTY_LIST.iterator();
|
111
|
|
}
|
112
|
|
|
113
|
74
|
public boolean isActive(FinalType aFinalType) {
|
114
|
74
|
return myFinalTypes.contains(aFinalType);
|
115
|
|
}
|
116
|
1612
|
public boolean isActive(BlockType aBlockType) {
|
117
|
1612
|
return myBlockTypes.contains(aBlockType);
|
118
|
|
}
|
119
|
|
|
120
|
129
|
private void addSimpleValueConstraints(String aDefault, String aFixed, IConstraintViolations aViolations) {
|
121
|
129
|
try {
|
122
|
129
|
myDefault = (null != aDefault) ? (ISimpleData)getType().createData(new StringDataImpl(aDefault, getPrefixToNamespaceMapping(), this)) : null;
|
123
|
129
|
myFixed = (null != aFixed) ? (ISimpleData)getType().createData(new StringDataImpl(aFixed, getPrefixToNamespaceMapping(), this)) : null;
|
124
|
129
|
if (null != myFixed) {
|
125
|
9
|
myDefault = myFixed;
|
126
|
|
}
|
127
|
|
} catch (XmlException e) {
|
128
|
0
|
aViolations.add(e.getXmlMessage());
|
129
|
|
}
|
130
|
|
}
|
131
|
|
|
132
|
660
|
public void addValueConstraints(String aDefault, String aFixed, IConstraintViolations aViolations) {
|
133
|
660
|
IAnyType type = getType();
|
134
|
660
|
if (!type.isSimple()) {
|
135
|
531
|
if (null != aFixed) {
|
136
|
2
|
aViolations.add(XmlMessages.fixedNotAllowedForComplexType(this));
|
137
|
|
}
|
138
|
531
|
if (type.getContentModel().canContainText()) {
|
139
|
102
|
myDefaultTextContent = aDefault;
|
140
|
429
|
} else if (null != aDefault) {
|
141
|
0
|
aViolations.add(XmlMessages.defaultNotAllowedForNonMixedComplexType(this));
|
142
|
|
}
|
143
|
|
} else {
|
144
|
129
|
addSimpleValueConstraints(aDefault, aFixed, aViolations);
|
145
|
|
}
|
146
|
|
}
|
147
|
|
|
148
|
692
|
public boolean isReference() {
|
149
|
692
|
return false;
|
150
|
|
}
|
151
|
|
|
152
|
|
}
|
153
|
|
|