1 16 17 package org.apache.xerces.impl.dtd; 18 19 import org.apache.xerces.impl.XMLErrorReporter; 20 import org.apache.xerces.impl.msg.XMLMessageFormatter; 21 import org.apache.xerces.util.XMLSymbols; 22 import org.apache.xerces.xni.Augmentations; 23 import org.apache.xerces.xni.NamespaceContext; 24 import org.apache.xerces.xni.QName; 25 import org.apache.xerces.xni.XMLAttributes; 26 import org.apache.xerces.xni.XNIException; 27 28 29 59 public class XMLNSDTDValidator 60 extends XMLDTDValidator{ 61 62 63 private QName fAttributeQName = new QName(); 64 65 66 67 protected final void startNamespaceScope (QName element, XMLAttributes attributes, 68 Augmentations augs) throws XNIException { 69 70 fNamespaceContext.pushContext(); 72 73 if (element.prefix == XMLSymbols.PREFIX_XMLNS) { 74 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 75 "ElementXMLNSPrefix", 76 new Object []{element.rawname}, 77 XMLErrorReporter.SEVERITY_FATAL_ERROR); 78 } 79 80 int length = attributes.getLength(); 82 for (int i = 0; i < length; i++) { 83 String localpart = attributes.getLocalName(i); 84 String prefix = attributes.getPrefix(i); 85 if (prefix == XMLSymbols.PREFIX_XMLNS || 88 prefix == XMLSymbols.EMPTY_STRING && localpart == XMLSymbols.PREFIX_XMLNS) { 89 90 String uri = fSymbolTable.addSymbol(attributes.getValue(i)); 92 93 if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { 95 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 96 "CantBindXMLNS", 97 new Object []{attributes.getQName(i)}, 98 XMLErrorReporter.SEVERITY_FATAL_ERROR); 99 } 100 101 if (uri == NamespaceContext.XMLNS_URI) { 103 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 104 "CantBindXMLNS", 105 new Object []{attributes.getQName(i)}, 106 XMLErrorReporter.SEVERITY_FATAL_ERROR); 107 } 108 109 if (localpart == XMLSymbols.PREFIX_XML) { 111 if (uri != NamespaceContext.XML_URI) { 112 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 113 "CantBindXML", 114 new Object []{attributes.getQName(i)}, 115 XMLErrorReporter.SEVERITY_FATAL_ERROR); 116 } 117 } 118 else { 120 if (uri ==NamespaceContext.XML_URI) { 121 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 122 "CantBindXML", 123 new Object []{attributes.getQName(i)}, 124 XMLErrorReporter.SEVERITY_FATAL_ERROR); 125 } 126 } 127 128 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; 129 130 if (uri == XMLSymbols.EMPTY_STRING && localpart != XMLSymbols.PREFIX_XMLNS) { 134 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 135 "EmptyPrefixedAttName", 136 new Object []{attributes.getQName(i)}, 137 XMLErrorReporter.SEVERITY_FATAL_ERROR); 138 continue; 139 } 140 141 fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); 143 } 144 } 145 146 String prefix = element.prefix != null 148 ? element.prefix : XMLSymbols.EMPTY_STRING; 149 element.uri = fNamespaceContext.getURI(prefix); 150 if (element.prefix == null && element.uri != null) { 151 element.prefix = XMLSymbols.EMPTY_STRING; 152 } 153 if (element.prefix != null && element.uri == null) { 154 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 155 "ElementPrefixUnbound", 156 new Object []{element.prefix, element.rawname}, 157 XMLErrorReporter.SEVERITY_FATAL_ERROR); 158 } 159 160 for (int i = 0; i < length; i++) { 162 attributes.getName(i, fAttributeQName); 163 String aprefix = fAttributeQName.prefix != null 164 ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 165 String arawname = fAttributeQName.rawname; 166 if (arawname == XMLSymbols.PREFIX_XMLNS) { 167 fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS); 168 attributes.setName(i, fAttributeQName); 169 } 170 else if (aprefix != XMLSymbols.EMPTY_STRING) { 171 fAttributeQName.uri = fNamespaceContext.getURI(aprefix); 172 if (fAttributeQName.uri == null) { 173 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 174 "AttributePrefixUnbound", 175 new Object []{element.rawname,arawname,aprefix}, 176 XMLErrorReporter.SEVERITY_FATAL_ERROR); 177 } 178 attributes.setName(i, fAttributeQName); 179 } 180 } 181 182 int attrCount = attributes.getLength(); 185 for (int i = 0; i < attrCount - 1; i++) { 186 String auri = attributes.getURI(i); 187 if (auri == null || auri == NamespaceContext.XMLNS_URI) { 188 continue; 189 } 190 String alocalpart = attributes.getLocalName(i); 191 for (int j = i + 1; j < attrCount; j++) { 192 String blocalpart = attributes.getLocalName(j); 193 String buri = attributes.getURI(j); 194 if (alocalpart == blocalpart && auri == buri) { 195 fErrorReporter.reportError(XMLMessageFormatter.XMLNS_DOMAIN, 196 "AttributeNSNotUnique", 197 new Object []{element.rawname,alocalpart, auri}, 198 XMLErrorReporter.SEVERITY_FATAL_ERROR); 199 } 200 } 201 } 202 203 204 } 206 207 208 protected void endNamespaceScope(QName element, Augmentations augs, boolean isEmpty) 209 throws XNIException { 210 211 String eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; 213 element.uri = fNamespaceContext.getURI(eprefix); 214 if (element.uri != null) { 215 element.prefix = eprefix; 216 } 217 218 if (fDocumentHandler != null) { 220 if (!isEmpty) { 221 fDocumentHandler.endElement(element, augs); 222 } 223 } 224 225 fNamespaceContext.popContext(); 227 228 } 230 } 231 | Popular Tags |