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.instance.builder;
|
11
|
|
|
12
|
|
import java.util.HashMap;
|
13
|
|
import java.util.Iterator;
|
14
|
|
import java.util.Map;
|
15
|
|
import java.util.Stack;
|
16
|
|
|
17
|
|
import org.jbind.util.collection.ConcatIterator;
|
18
|
|
import org.jbind.util.collection.UnsynchronizedStack;
|
19
|
|
import org.jbind.xml.core.base.IKeyStore;
|
20
|
|
import org.jbind.xml.core.bridge.IElementImpl;
|
21
|
|
import org.jbind.xml.core.cmp.IUseStateIterator;
|
22
|
|
import org.jbind.xml.core.constraint.ConstraintType;
|
23
|
|
import org.jbind.xml.core.constraint.IConstraints;
|
24
|
|
import org.jbind.xml.core.constraint.ITypeInstanceConstraint;
|
25
|
|
import org.jbind.xml.core.constraint.ITypeKeyRefConstraint;
|
26
|
|
import org.jbind.xml.core.content.IElemDesc;
|
27
|
|
import org.jbind.xml.core.data.IAnyTypeData;
|
28
|
|
import org.jbind.xml.core.data.IDataContext;
|
29
|
|
import org.jbind.xml.instance.data.DataVisitor;
|
30
|
|
import org.jbind.xml.msg.IConstraintViolations;
|
31
|
|
import org.jbind.xml.msg.XmlException;
|
32
|
|
import org.jbind.xml.msg.XmlMessages;
|
33
|
|
import org.jbind.xml.schema.constraint.GlobalCheckContext;
|
34
|
|
|
35
|
|
public class ValidationVisitor extends DataVisitor implements IDataValidator {
|
36
|
|
|
37
|
|
private IDataContext myDataContext = null;
|
38
|
|
private IConstraintViolations myViolations = null;
|
39
|
|
private UnsynchronizedStack myKeyStores = new UnsynchronizedStack();
|
40
|
|
private boolean myValidateContentModel;
|
41
|
|
private UnsynchronizedStack myUseStateIterators = null;
|
42
|
|
|
43
|
|
/**
|
44
|
|
* Maps key/unique constraint names to lists of maps.
|
45
|
|
*/
|
46
|
|
private Map myLocalTypeInstancesMaps = null;
|
47
|
|
|
48
|
222
|
public ValidationVisitor(boolean aValidateContentModel) {
|
49
|
222
|
myValidateContentModel = aValidateContentModel;
|
50
|
|
}
|
51
|
|
|
52
|
2051
|
public void visitAnyTypeDataStarts(IAnyTypeData aData) {
|
53
|
2051
|
if (!aData.isNil_()) {
|
54
|
2049
|
GlobalCheckContext context = new GlobalCheckContext(myDataContext, aData, myViolations);
|
55
|
2049
|
aData.getType_().checkConstraints(context);
|
56
|
2049
|
myKeyStores.push(new KeyStore());
|
57
|
|
|
58
|
|
// Build indices for local type key/unique constraints.
|
59
|
2049
|
IConstraints constraints = aData.getType_().getConstraints();
|
60
|
2049
|
for (Iterator i = new ConcatIterator(constraints.iterConstraints(ConstraintType.TYPE_KEY), constraints.iterConstraints(ConstraintType.TYPE_UNIQUE));
|
61
|
2059
|
i.hasNext(); ) {
|
62
|
10
|
ITypeInstanceConstraint c = (ITypeInstanceConstraint)i.next();
|
63
|
10
|
if (ITypeInstanceConstraint.GLOBAL.equals(c.getScope())) {
|
64
|
10
|
continue;
|
65
|
|
}
|
66
|
0
|
HashMap map = new HashMap();
|
67
|
0
|
c.collectLocalTypeInstances(aData, map, myViolations);
|
68
|
0
|
Stack s = (Stack)myLocalTypeInstancesMaps.get(c.getGlobalRef());
|
69
|
0
|
if (null == s) {
|
70
|
0
|
s = new Stack();
|
71
|
0
|
myLocalTypeInstancesMaps.put(c.getGlobalRef(), s);
|
72
|
|
}
|
73
|
0
|
s.push(map);
|
74
|
|
}
|
75
|
|
|
76
|
|
// Check local type keyref constraints.
|
77
|
2049
|
for (Iterator i = constraints.iterConstraints(ConstraintType.TYPE_KEY_REF);
|
78
|
2060
|
i.hasNext(); ) {
|
79
|
11
|
ITypeKeyRefConstraint c = (ITypeKeyRefConstraint)i.next();
|
80
|
11
|
ITypeInstanceConstraint referredConstraint = c.getReferredConstraint();
|
81
|
11
|
if (ITypeInstanceConstraint.GLOBAL.equals(referredConstraint.getScope())) {
|
82
|
11
|
continue;
|
83
|
|
}
|
84
|
0
|
Stack s = (Stack)myLocalTypeInstancesMaps.get(referredConstraint.getGlobalRef());
|
85
|
0
|
if ((null == s) || (s.size() == 0)) {
|
86
|
0
|
myViolations.add(XmlMessages.referedTypeInstanceConstraintNotInScope(referredConstraint.getGlobalRef(), aData.getImpl_()));
|
87
|
0
|
continue;
|
88
|
|
}
|
89
|
0
|
c.resolveLocalTypeInstances((Map)s.peek(), aData, myViolations);
|
90
|
|
}
|
91
|
|
|
92
|
2049
|
if (myValidateContentModel) {
|
93
|
0
|
if (myUseStateIterators == null) {
|
94
|
0
|
myUseStateIterators = new UnsynchronizedStack();
|
95
|
|
} else {
|
96
|
0
|
IElementImpl impl = (IElementImpl)aData.getImpl_();
|
97
|
0
|
IUseStateIterator usi = (IUseStateIterator)myUseStateIterators.peek();
|
98
|
0
|
String namespace = impl.getNamespace();
|
99
|
0
|
String name = impl.getPartName();
|
100
|
0
|
try {
|
101
|
0
|
usi.getNextElemDesc(namespace, name, aData.getType_(), impl);
|
102
|
|
} catch (XmlException e) {
|
103
|
0
|
myViolations.add(e.getXmlMessage());
|
104
|
|
}
|
105
|
|
}
|
106
|
0
|
myUseStateIterators.push(aData.getType_().getContentModel().createUseIterator());
|
107
|
|
}
|
108
|
|
}
|
109
|
|
}
|
110
|
|
|
111
|
2051
|
public void visitAnyTypeDataEnds(IAnyTypeData aData) {
|
112
|
2051
|
if (!aData.isNil_()) {
|
113
|
|
// Remove indices for local type key/unique constraints.
|
114
|
2049
|
IConstraints constraints = aData.getType_().getConstraints();
|
115
|
2049
|
for (Iterator i = new ConcatIterator(constraints.iterConstraints(ConstraintType.TYPE_KEY), constraints.iterConstraints(ConstraintType.TYPE_UNIQUE));
|
116
|
2059
|
i.hasNext(); ) {
|
117
|
10
|
ITypeInstanceConstraint c = (ITypeInstanceConstraint)i.next();
|
118
|
10
|
if (ITypeInstanceConstraint.GLOBAL.equals(c.getScope())) {
|
119
|
10
|
continue;
|
120
|
|
}
|
121
|
0
|
Stack s = (Stack)myLocalTypeInstancesMaps.get(c.getGlobalRef());
|
122
|
0
|
s.pop();
|
123
|
|
}
|
124
|
|
|
125
|
2049
|
IKeyStore keyStore = (IKeyStore)myKeyStores.pop();
|
126
|
2049
|
((IElemDesc)aData.getDataDesc_()).checkIdentityConstraints(aData, keyStore, myViolations);
|
127
|
2049
|
IKeyStore parentKeyStore = (IKeyStore)myKeyStores.peek();
|
128
|
2049
|
parentKeyStore.merge(keyStore);
|
129
|
2049
|
if (myValidateContentModel) {
|
130
|
0
|
IUseStateIterator usi = (IUseStateIterator)myUseStateIterators.pop();
|
131
|
0
|
try {
|
132
|
0
|
usi.isValidEnd(aData.getImpl_());
|
133
|
|
} catch (XmlException e) {
|
134
|
0
|
myViolations.add(e.getXmlMessage());
|
135
|
|
}
|
136
|
|
}
|
137
|
|
}
|
138
|
|
}
|
139
|
|
|
140
|
93
|
public synchronized void validate(IAnyTypeData aData, IDataContext aDataContext, IConstraintViolations aViolations) {
|
141
|
93
|
myDataContext = aDataContext;
|
142
|
93
|
if (null == myDataContext) {
|
143
|
0
|
myDataContext = new DataContext(false);
|
144
|
|
}
|
145
|
93
|
myViolations = aViolations;
|
146
|
93
|
myKeyStores.push(new KeyStore());
|
147
|
93
|
myLocalTypeInstancesMaps = new HashMap();
|
148
|
93
|
((IAnyTypeData)aData).accept_(this);
|
149
|
93
|
myLocalTypeInstancesMaps = null;
|
150
|
93
|
myKeyStores.pop();
|
151
|
93
|
myDataContext.resolveReferences(myViolations);
|
152
|
93
|
myUseStateIterators = null;
|
153
|
|
}
|
154
|
|
|
155
|
|
}
|
156
|
|
|