~ubuntu-branches/ubuntu/utopic/jing-trang/utopic

« back to all changes in this revision

Viewing changes to mod/nvdl/src/main/com/thaiopensource/validate/nrl/Mode.java

  • Committer: Bazaar Package Importer
  • Author(s): Samuel Thibault
  • Date: 2009-09-01 15:53:03 UTC
  • Revision ID: james.westby@ubuntu.com-20090901155303-2kweef05h5v9j3ni
Tags: upstream-20090818
ImportĀ upstreamĀ versionĀ 20090818

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.validate.nrl;
 
2
 
 
3
import org.xml.sax.Locator;
 
4
import org.xml.sax.helpers.LocatorImpl;
 
5
 
 
6
import java.util.Hashtable;
 
7
import java.util.Enumeration;
 
8
 
 
9
import com.thaiopensource.validate.nrl.ActionSet;
 
10
import com.thaiopensource.validate.nrl.AttributeActionSet;
 
11
 
 
12
class Mode {
 
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);
 
18
 
 
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;
 
27
 
 
28
  Mode(String name, Mode baseMode) {
 
29
    this.name = name;
 
30
    this.baseMode = baseMode;
 
31
  }
 
32
 
 
33
  String getName() {
 
34
    return name;
 
35
  }
 
36
 
 
37
  Mode getBaseMode() {
 
38
    return baseMode;
 
39
  }
 
40
 
 
41
  void setBaseMode(Mode baseMode) {
 
42
    this.baseMode = baseMode;
 
43
  }
 
44
 
 
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);
 
51
    }
 
52
    return actions;
 
53
  }
 
54
 
 
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);
 
62
      }
 
63
    }
 
64
    return actions;
 
65
  }
 
66
 
 
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);
 
73
    }
 
74
    return actions;
 
75
  }
 
76
 
 
77
  private AttributeActionSet getAttributeActionsExplicit(String ns) {
 
78
    AttributeActionSet actions = (AttributeActionSet)attributeMap.get(ns);
 
79
    if (actions == null && baseMode != null) {
 
80
      actions = baseMode.getAttributeActionsExplicit(ns);
 
81
      if (actions != null)
 
82
        attributeMap.put(ns, actions);
 
83
    }
 
84
    return actions;
 
85
  }
 
86
 
 
87
  int getAttributeProcessing() {
 
88
    if (attributeProcessing == -1) {
 
89
      if (baseMode != null)
 
90
        attributeProcessing = baseMode.getAttributeProcessing();
 
91
      else
 
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);
 
102
      }
 
103
    }
 
104
    return attributeProcessing;
 
105
  }
 
106
 
 
107
  Locator getWhereDefined() {
 
108
    return whereDefined;
 
109
  }
 
110
 
 
111
  boolean isDefined() {
 
112
    return defined;
 
113
  }
 
114
 
 
115
  Locator getWhereUsed() {
 
116
    return whereUsed;
 
117
  }
 
118
 
 
119
  void noteUsed(Locator locator) {
 
120
    if (whereUsed == null && locator != null)
 
121
      whereUsed = new LocatorImpl(locator);
 
122
  }
 
123
 
 
124
  void noteDefined(Locator locator) {
 
125
    defined = true;
 
126
    if (whereDefined == null && locator != null)
 
127
      whereDefined = new LocatorImpl(locator);
 
128
  }
 
129
 
 
130
  boolean bindElement(String ns, ActionSet actions) {
 
131
    if (elementMap.get(ns) != null)
 
132
      return false;
 
133
    elementMap.put(ns, actions);
 
134
    return true;
 
135
  }
 
136
 
 
137
  boolean bindAttribute(String ns, AttributeActionSet actions) {
 
138
    if (attributeMap.get(ns) != null)
 
139
      return false;
 
140
    attributeMap.put(ns, actions);
 
141
    return true;
 
142
  }
 
143
 
 
144
}