-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
DTDIdAttr.java
136 lines (120 loc) · 4.22 KB
/
DTDIdAttr.java
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.ctc.wstx.dtd;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import com.ctc.wstx.cfg.ErrorConsts;
import com.ctc.wstx.io.WstxInputData;
import com.ctc.wstx.sr.InputProblemReporter;
import com.ctc.wstx.util.ElementId;
import com.ctc.wstx.util.ElementIdMap;
import com.ctc.wstx.util.PrefixedName;
/**
* Specific attribute class for attributes that contain (unique)
* identifiers.
*/
public final class DTDIdAttr
extends DTDAttribute
{
/*
///////////////////////////////////////////////////////////////////////
// Life-cycle
///////////////////////////////////////////////////////////////////////
*/
/**
* Main constructor. Note that id attributes can never have
* default values.
*<p>
* note: although ID attributes are not to have default value,
* this is 'only' a validity constraint, and in dtd-aware-but-
* not-validating mode it is apparently 'legal' to add default
* values.
*/
public DTDIdAttr(PrefixedName name, DefaultAttrValue defValue, int specIndex,
boolean nsAware, boolean xml11)
{
super(name, defValue, specIndex, nsAware, xml11);
}
@Override
public DTDAttribute cloneWith(int specIndex) {
return new DTDIdAttr(mName, mDefValue, specIndex, mCfgNsAware, mCfgXml11);
}
/*
///////////////////////////////////////////////////////////////////////
// Public API
///////////////////////////////////////////////////////////////////////
*/
@Override
public int getValueType() {
return TYPE_ID;
}
@Override
public boolean typeIsId() {
return true;
}
/*
///////////////////////////////////////////////////////////////////////
// Public API, validation
///////////////////////////////////////////////////////////////////////
*/
/**
* Method called by the validator
* to let the attribute do necessary normalization and/or validation
* for the value.
*/
@SuppressWarnings("cast")
@Override
public String validate(DTDValidatorBase v, char[] cbuf, int start, int end, boolean normalize)
throws XMLStreamException
{
// Let's trim leading white space first...
while (start < end && WstxInputData.isSpaceChar(cbuf[start])) {
++start;
}
// No id?
if (start >= end) {
return reportValidationProblem(v, "Empty ID value");
}
--end; // so that it now points to the last char
while (end > start && WstxInputData.isSpaceChar(cbuf[end])) {
--end;
}
// Ok, need to check char validity, and also calc hash code:
char c = cbuf[start];
if (!WstxInputData.isNameStartChar(c, mCfgNsAware, mCfgXml11)) {
return reportInvalidChar(v, c, "not valid as the first ID character");
}
int hash = (int) c;
for (int i = start+1; i <= end; ++i) {
c = cbuf[i];
if (!WstxInputData.isNameChar(c, mCfgNsAware, mCfgXml11)) {
return reportInvalidChar(v, c, "not valid as an ID character");
}
hash = (hash * 31) + (int) c;
}
// Either way, we do need to validate characters, and calculate hash
ElementIdMap m = v.getIdMap();
PrefixedName elemName = v.getElemName();
Location loc = v.getLocation();
ElementId id = m.addDefined(cbuf, start, (end - start + 1), hash,
loc, elemName, mName);
// We can detect dups by checking if Location is the one we passed:
if (id.getLocation() != loc) {
return reportValidationProblem(v, "Duplicate id '"+id.getId()+"', first declared at "
+id.getLocation());
}
if (normalize) {
return id.getId();
}
return null;
}
/**
* Method called by the validator
* to ask attribute to verify that the default it has (if any) is
* valid for such type.
*/
@Override
public void validateDefault(InputProblemReporter rep, boolean normalize)
{
// Should never get called
throw new IllegalStateException(ErrorConsts.ERR_INTERNAL);
}
}