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 59 public class XML11NSDTDValidator extends XML11DTDValidator { 60 61 62 private QName fAttributeQName = new QName(); 63 64 65 protected final void startNamespaceScope(QName element, XMLAttributes attributes, Augmentations augs) 66 throws XNIException { 67 68 fNamespaceContext.pushContext(); 70 71 if (element.prefix == XMLSymbols.PREFIX_XMLNS) { 72 fErrorReporter.reportError( 73 XMLMessageFormatter.XMLNS_DOMAIN, 74 "ElementXMLNSPrefix", 75 new Object [] { element.rawname }, 76 XMLErrorReporter.SEVERITY_FATAL_ERROR); 77 } 78 79 int length = attributes.getLength(); 81 for (int i = 0; i < length; i++) { 82 String localpart = attributes.getLocalName(i); 83 String prefix = attributes.getPrefix(i); 84 if (prefix == XMLSymbols.PREFIX_XMLNS || prefix == XMLSymbols.EMPTY_STRING 87 && localpart == XMLSymbols.PREFIX_XMLNS) { 88 89 String uri = fSymbolTable.addSymbol(attributes.getValue(i)); 91 92 if (prefix == XMLSymbols.PREFIX_XMLNS && localpart == XMLSymbols.PREFIX_XMLNS) { 94 fErrorReporter.reportError( 95 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( 104 XMLMessageFormatter.XMLNS_DOMAIN, 105 "CantBindXMLNS", 106 new Object [] { attributes.getQName(i)}, 107 XMLErrorReporter.SEVERITY_FATAL_ERROR); 108 } 109 110 if (localpart == XMLSymbols.PREFIX_XML) { 112 if (uri != NamespaceContext.XML_URI) { 113 fErrorReporter.reportError( 114 XMLMessageFormatter.XMLNS_DOMAIN, 115 "CantBindXML", 116 new Object [] { attributes.getQName(i)}, 117 XMLErrorReporter.SEVERITY_FATAL_ERROR); 118 } 119 } 120 else { 122 if (uri == NamespaceContext.XML_URI) { 123 fErrorReporter.reportError( 124 XMLMessageFormatter.XMLNS_DOMAIN, 125 "CantBindXML", 126 new Object [] { attributes.getQName(i)}, 127 XMLErrorReporter.SEVERITY_FATAL_ERROR); 128 } 129 } 130 131 prefix = localpart != XMLSymbols.PREFIX_XMLNS ? localpart : XMLSymbols.EMPTY_STRING; 132 133 fNamespaceContext.declarePrefix(prefix, uri.length() != 0 ? uri : null); 137 } 138 } 139 140 String prefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; 142 element.uri = fNamespaceContext.getURI(prefix); 143 if (element.prefix == null && element.uri != null) { 144 element.prefix = XMLSymbols.EMPTY_STRING; 145 } 146 if (element.prefix != null && element.uri == null) { 147 fErrorReporter.reportError( 148 XMLMessageFormatter.XMLNS_DOMAIN, 149 "ElementPrefixUnbound", 150 new Object [] { element.prefix, element.rawname }, 151 XMLErrorReporter.SEVERITY_FATAL_ERROR); 152 } 153 154 for (int i = 0; i < length; i++) { 156 attributes.getName(i, fAttributeQName); 157 String aprefix = fAttributeQName.prefix != null ? fAttributeQName.prefix : XMLSymbols.EMPTY_STRING; 158 String arawname = fAttributeQName.rawname; 159 if (arawname == XMLSymbols.PREFIX_XMLNS) { 160 fAttributeQName.uri = fNamespaceContext.getURI(XMLSymbols.PREFIX_XMLNS); 161 attributes.setName(i, fAttributeQName); 162 } else if (aprefix != XMLSymbols.EMPTY_STRING) { 163 fAttributeQName.uri = fNamespaceContext.getURI(aprefix); 164 if (fAttributeQName.uri == null) { 165 fErrorReporter.reportError( 166 XMLMessageFormatter.XMLNS_DOMAIN, 167 "AttributePrefixUnbound", 168 new Object [] { element.rawname, arawname, aprefix }, 169 XMLErrorReporter.SEVERITY_FATAL_ERROR); 170 } 171 attributes.setName(i, fAttributeQName); 172 } 173 } 174 175 int attrCount = attributes.getLength(); 178 for (int i = 0; i < attrCount - 1; i++) { 179 String auri = attributes.getURI(i); 180 if (auri == null || auri == NamespaceContext.XMLNS_URI) { 181 continue; 182 } 183 String alocalpart = attributes.getLocalName(i); 184 for (int j = i + 1; j < attrCount; j++) { 185 String blocalpart = attributes.getLocalName(j); 186 String buri = attributes.getURI(j); 187 if (alocalpart == blocalpart && auri == buri) { 188 fErrorReporter.reportError( 189 XMLMessageFormatter.XMLNS_DOMAIN, 190 "AttributeNSNotUnique", 191 new Object [] { element.rawname, alocalpart, auri }, 192 XMLErrorReporter.SEVERITY_FATAL_ERROR); 193 } 194 } 195 } 196 197 } 199 200 protected void endNamespaceScope(QName element, Augmentations augs, boolean isEmpty) 201 throws XNIException { 202 203 String eprefix = element.prefix != null ? element.prefix : XMLSymbols.EMPTY_STRING; 205 element.uri = fNamespaceContext.getURI(eprefix); 206 if (element.uri != null) { 207 element.prefix = eprefix; 208 } 209 210 if (fDocumentHandler != null) { 212 if (!isEmpty) { 213 fDocumentHandler.endElement(element, augs); 214 } 215 } 216 217 fNamespaceContext.popContext(); 219 220 } } 222 | Popular Tags |