Debian Codegraph @

src/main/java/com/ctc/wstx/msv/AttributeProxy.java

raw
1 /* Woodstox XML processor
2 *
3 * Copyright (c) 2004- Tatu Saloranta, [email protected]
4 *
5 * Licensed under the License specified in the file LICENSE which is
6 * included with the source code.
7 * You may not use this file except in compliance with the License.
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 package com.ctc.wstx.msv;
17
18 import org.codehaus.stax2.validation.ValidationContext;
19
20 /**
21 * This is an implementation of SAX Attributes interface, that proxies
22 * requests to the {@link ValidationContext}.
23 * It is needed by some MSV components (specifically, W3C Schema Validator)
24 * for limited access to attribute values during start element validation.
25 */
26 final class AttributeProxy
27 implements org.xml.sax.Attributes
28 {
29 private final ValidationContext mContext;
30
31 public AttributeProxy(ValidationContext ctxt)
32 {
33 mContext = ctxt;
34 }
35
36 /*
37 ///////////////////////////////////////////////
38 // Attributes implementation
39 ///////////////////////////////////////////////
40 */
41
42 @Override
43 public int getIndex(String qName)
44 {
45 int cix = qName.indexOf(':');
46 int acount = mContext.getAttributeCount();
47 if (cix < 0) { // no prefix
48 for (int i = 0; i < acount; ++i) {
49 if (qName.equals(mContext.getAttributeLocalName(i))) {
50 String prefix = mContext.getAttributePrefix(i);
51 if (prefix == null || prefix.length() == 0) {
52 return i;
53 }
54 }
55 }
56 } else {
57 String prefix = qName.substring(0, cix);
58 String ln = qName.substring(cix+1);
59
60 for (int i = 0; i < acount; ++i) {
61 if (ln.equals(mContext.getAttributeLocalName(i))) {
62 String p2 = mContext.getAttributePrefix(i);
63 if (p2 != null && prefix.equals(p2)) {
64 return i;
65 }
66 }
67 }
68 }
69 return -1;
70 }
71
72 @Override
73 public int getIndex(String uri, String localName) {
74 return mContext.findAttributeIndex(uri, localName);
75 }
76
77 @Override
78 public int getLength() {
79 return mContext.getAttributeCount();
80 }
81
82 @Override
83 public String getLocalName(int index) {
84 return mContext.getAttributeLocalName(index);
85 }
86
87 @Override
88 public String getQName(int index)
89 {
90 String prefix = mContext.getAttributePrefix(index);
91 String ln = mContext.getAttributeLocalName(index);
92
93 if (prefix == null || prefix.length() == 0) {
94 return ln;
95 }
96 StringBuilder sb = new StringBuilder(prefix.length() + 1 + ln.length());
97 sb.append(prefix);
98 sb.append(':');
99 sb.append(ln);
100 return sb.toString();
101 }
102
103 @Override
104 public String getType(int index)
105 {
106 return mContext.getAttributeType(index);
107 }
108
109 @Override
110 public String getType(String qName)
111 {
112 return getType(getIndex(qName));
113 }
114
115 @Override
116 public String getType(String uri, String localName)
117 {
118 return getType(getIndex(uri, localName));
119 }
120
121 @Override
122 public String getURI(int index)
123 {
124 return mContext.getAttributeNamespace(index);
125 }
126
127 @Override
128 public String getValue(int index)
129 {
130 return mContext.getAttributeValue(index);
131 }
132
133 @Override
134 public String getValue(String qName)
135 {
136 return getValue(getIndex(qName));
137 }
138
139 @Override
140 public String getValue(String uri, String localName)
141 {
142 return mContext.getAttributeValue(uri, localName);
143 }
144 }
145
146