1
package com.thaiopensource.validate.nrl;
3
import org.xml.sax.Locator;
4
import org.xml.sax.helpers.LocatorImpl;
6
import java.util.Hashtable;
7
import java.util.Enumeration;
9
import com.thaiopensource.validate.nrl.ActionSet;
10
import com.thaiopensource.validate.nrl.AttributeActionSet;
13
static final String ANY_NAMESPACE = "##any";
14
static final int ATTRIBUTE_PROCESSING_NONE = 0;
15
static final int ATTRIBUTE_PROCESSING_QUALIFIED = 1;
16
static final int ATTRIBUTE_PROCESSING_FULL = 2;
17
static final Mode CURRENT = new Mode("#current", null);
19
private final String name;
20
private Mode baseMode;
21
private boolean defined;
22
private Locator whereDefined;
23
private Locator whereUsed;
24
private final Hashtable elementMap = new Hashtable();
25
private final Hashtable attributeMap = new Hashtable();
26
private int attributeProcessing = -1;
28
Mode(String name, Mode baseMode) {
30
this.baseMode = baseMode;
41
void setBaseMode(Mode baseMode) {
42
this.baseMode = baseMode;
45
ActionSet getElementActions(String ns) {
46
ActionSet actions = getElementActionsExplicit(ns);
47
if (actions == null) {
48
actions = getElementActionsExplicit(ANY_NAMESPACE);
49
// this is not correct: it breaks a derived mode that use anyNamespace
50
// elementMap.put(ns, actions);
55
private ActionSet getElementActionsExplicit(String ns) {
56
ActionSet actions = (ActionSet)elementMap.get(ns);
57
if (actions == null && baseMode != null) {
58
actions = baseMode.getElementActionsExplicit(ns);
59
if (actions != null) {
60
actions = actions.changeCurrentMode(this);
61
elementMap.put(ns, actions);
67
AttributeActionSet getAttributeActions(String ns) {
68
AttributeActionSet actions = getAttributeActionsExplicit(ns);
69
if (actions == null) {
70
actions = getAttributeActionsExplicit(ANY_NAMESPACE);
71
// this is not correct: it breaks a derived mode that use anyNamespace
72
// attributeMap.put(ns, actions);
77
private AttributeActionSet getAttributeActionsExplicit(String ns) {
78
AttributeActionSet actions = (AttributeActionSet)attributeMap.get(ns);
79
if (actions == null && baseMode != null) {
80
actions = baseMode.getAttributeActionsExplicit(ns);
82
attributeMap.put(ns, actions);
87
int getAttributeProcessing() {
88
if (attributeProcessing == -1) {
90
attributeProcessing = baseMode.getAttributeProcessing();
92
attributeProcessing = ATTRIBUTE_PROCESSING_NONE;
93
for (Enumeration e = attributeMap.keys(); e.hasMoreElements() && attributeProcessing != ATTRIBUTE_PROCESSING_FULL;) {
94
String ns = (String)e.nextElement();
95
AttributeActionSet actions = (AttributeActionSet)attributeMap.get(ns);
96
if (!actions.getAttach()
97
|| actions.getReject()
98
|| actions.getSchemas().length > 0)
99
attributeProcessing = ((ns.equals("") || ns.equals(ANY_NAMESPACE))
100
? ATTRIBUTE_PROCESSING_FULL
101
: ATTRIBUTE_PROCESSING_QUALIFIED);
104
return attributeProcessing;
107
Locator getWhereDefined() {
111
boolean isDefined() {
115
Locator getWhereUsed() {
119
void noteUsed(Locator locator) {
120
if (whereUsed == null && locator != null)
121
whereUsed = new LocatorImpl(locator);
124
void noteDefined(Locator locator) {
126
if (whereDefined == null && locator != null)
127
whereDefined = new LocatorImpl(locator);
130
boolean bindElement(String ns, ActionSet actions) {
131
if (elementMap.get(ns) != null)
133
elementMap.put(ns, actions);
137
boolean bindAttribute(String ns, AttributeActionSet actions) {
138
if (attributeMap.get(ns) != null)
140
attributeMap.put(ns, actions);