1 57 58 package com.sun.org.apache.xerces.internal.impl.dtd; 59 60 import com.sun.org.apache.xerces.internal.impl.XMLErrorReporter; 61 import com.sun.org.apache.xerces.internal.impl.msg.XMLMessageFormatter; 62 import com.sun.org.apache.xerces.internal.util.XMLSymbols; 63 import com.sun.org.apache.xerces.internal.xni.Augmentations; 64 import com.sun.org.apache.xerces.internal.xni.NamespaceContext; 65 import com.sun.org.apache.xerces.internal.xni.QName; 66 import com.sun.org.apache.xerces.internal.xni.XMLAttributes; 67 import com.sun.org.apache.xerces.internal.xni.XNIException; 68 69 70 98 public class XMLNSDTDValidator 99 extends XMLDTDValidator{ 100 101 102 private QName fAttributeQName = new QName(); 103 104 105 106 protected final void startNamespaceScope (QName element, XMLAttributes attributes, 107 Augmentations augs) throws XNIException { 108 109 fNamespaceContext.pushContext(); 111 112 if (element.prefix == XMLSymbols.PREFIX_XMLNS) { 113 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 114 "ElementXMLNSPrefix", 115 new Object []{element.rawname}, 116 XMLErrorReporter.SEVERITY_FATAL_ERROR); 117 } 118 119 int length = attributes.getLength(); 121 for (int i = 0; i < length; i++) { 122 String localpart = attributes.getLocalName(i); 123 String prefix = attributes.getPrefix(i); 124 if (prefix == XMLSymbols.PREFIX_XMLNS || 127 prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) { 128 129 String uri = fSymbolTable.addSymbol(attributes.getValue(i)); 131 132 if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { 134 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 135 "CantBindXMLNS", 136 new Object []{attributes.getQName(i)}, 137 XMLErrorReporter.SEVERITY_FATAL_ERROR); 138 } 139 140 if (uri == NamespaceContext.XMLNS_URI) { 142 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 143 "CantBindXMLNS", 144 new Object []{attributes.getQName(i)}, 145 XMLErrorReporter.SEVERITY_FATAL_ERROR); 146 } 147 148 if (localpart == XMLSymbols.PREFIX_XML) { 150 if (uri != NamespaceContext.XML_URI) { 151 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 152 "CantBindXML", 153 new Object []{attributes.getQName(i)}, 154 XMLErrorReporter.SEVERITY_FATAL_ERROR); 155 } 156 } 157 else { 159 if (uri ==NamespaceContext.XML_URI) { 160 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 161 "CantBindXML", 162 new Object []{attributes.getQName(i)}, 163 XMLErrorReporter.SEVERITY_FATAL_ERROR); 164 } 165 } 166 167 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; 168 169 if (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS) { 173 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 174 "EmptyPrefixedAttName", 175 new Object []{attributes.getQName(i)}, 176 XMLErrorReporter.SEVERITY_FATAL_ERROR); 177 continue; 178 } 179 180 fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); 182 } 183 } 184 185 String prefix = element.prefix != null 187 ? element.prefix : XMLSymbols.EMPTY_STRING; 188 element.uri = fNamespaceContext.getURI(prefix); 189 if (element.prefix == null && element.uri != null) { 190 element.prefix = XMLSymbols.EMPTY_STRING; 191 } 192 if (element.prefix != null && element.uri == null) { 193 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 194 "ElementPrefixUnbound", 195 new Object []{element.prefix, element.rawname}, 196 XMLErrorReporter.SEVERITY_FATAL_ERROR); 197 } 198 199 for (int i = 0; i < length; i++) { 201 attributes.getName(i, fAttributeQName); 202 String aprefix = fAttributeQName.prefix != null 203 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 204 String arawname = fAttributeQName.rawname; 205 if (arawname == XMLSymbols.PREFIX_XMLNS) { 206 fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS); 207 attributes.setName(i, fAttributeQName); 208 } 209 else if (aprefix != XMLSymbols.EMPTY_STRING) { 210 fAttributeQName.uri = fNamespaceContext.getURI(aprefix); 211 if (fAttributeQName.uri == null) { 212 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 213 "AttributePrefixUnbound", 214 new Object []{element.rawname,arawname,aprefix}, 215 XMLErrorReporter.SEVERITY_FATAL_ERROR); 216 } 217 attributes.setName(i, fAttributeQName); 218 } 219 } 220 221 int attrCount = attributes.getLength(); 224 for (int i = 0; i < attrCount - 1; i++) { 225 String auri = attributes.getURI(i); 226 if (auri == null || auri == NamespaceContext.XMLNS_URI) { 227 continue; 228 } 229 String alocalpart = attributes.getLocalName(i); 230 for (int j = i + 1; j < attrCount; j++) { 231 String blocalpart = attributes.getLocalName(j); 232 String buri = attributes.getURI(j); 233 if (alocalpart == blocalpart && auri == buri) { 234 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 235 "AttributeNSNotUnique", 236 new Object []{element.rawname,alocalpart, auri}, 237 XMLErrorReporter.SEVERITY_FATAL_ERROR); 238 } 239 } 240 } 241 242 243 } 245 246 247 protected void endNamespaceScope(QName element, Augmentations augs, boolean isEmpty) 248 throws XNIException { 249 250 String eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; 252 element.uri = fNamespaceContext.getURI(eprefix); 253 if (element.uri != null) { 254 element.prefix = eprefix; 255 } 256 257 if (fDocumentHandler != null) { 259 if (!isEmpty) { 260 fDocumentHandler.endElement(element, augs); 261 } 262 } 263 264 fNamespaceContext.popContext(); 266 267 } 269 } 270 | Popular Tags |