/// <summary> /// Determine the prefix for an element or attribute name. /// TODO: this method probably needs some cleanup. /// </summary> /// <param name="uri"> /// The Namespace URI. /// </param> /// <param name="qName"> /// The qualified name (optional); this will be used /// to indicate the preferred prefix if none is currently /// bound. /// </param> /// <param name="isElement"> /// true if this is an element name, false /// if it is an attribute name (which cannot use the /// default Namespace). /// </param> private string DoPrefix(string uri, string qName, bool isElement) { string defaultNS = _nsSupport.GetUri(""); if ("".Equals(uri)) { if (isElement && defaultNS != null) { _nsSupport.DeclarePrefix("", ""); } return null; } string prefix; if (isElement && defaultNS != null && uri.Equals(defaultNS)) { prefix = ""; } else { prefix = _nsSupport.GetPrefix(uri); } if (prefix != null) { return prefix; } bool containsPrefix = _doneDeclTable.ContainsKey(uri); prefix = (string)(containsPrefix ? _doneDeclTable[uri] : null); if (containsPrefix && ((!isElement || defaultNS != null) && "".Equals(prefix) || _nsSupport.GetUri(prefix) != null)) { prefix = null; } if (prefix == null) { containsPrefix = _prefixTable.ContainsKey(uri); prefix = (string)(containsPrefix ? _prefixTable[uri] : null); if (containsPrefix && ((!isElement || defaultNS != null) && "".Equals(prefix) || _nsSupport.GetUri(prefix) != null)) { prefix = null; } } if (prefix == null && qName != null && !"".Equals(qName)) { int i = qName.IndexOf(':'); if (i == -1) { if (isElement && defaultNS == null) { prefix = ""; } } else { prefix = qName.Substring(0, i); } } for (; prefix == null || _nsSupport.GetUri(prefix) != null; prefix = "__NS" + ++_prefixCounter) { } _nsSupport.DeclarePrefix(prefix, uri); _doneDeclTable[uri] = prefix; return prefix; }
private void declarePrefix(string prefix, string uri) { int index = uri.IndexOf(':'); // many versions of nwalsh docbook stylesheets // have bogus URLs; so this can't be an error... if (index < 1 && uri.Length != 0) { warn("relative URI for namespace: " + uri); } // FIXME: char [0] must be ascii alpha; chars [1..index] // must be ascii alphanumeric or in "+-." [RFC 2396] uri = string.Intern(uri); if (namespaceSupport.DeclarePrefix(prefix, uri)) { contentHandler.StartPrefixMapping(prefix, uri); } }
public virtual void Attribute(string aname, string value, bool isSpecified) { // Code changed by MHK 16 April 2001. // The only safe thing to do is to process all the namespace declarations // first, then process the ordinary attributes. So if this is a namespace // declaration, we deal with it now, otherwise we save it till we get the // startElement call. if (_attributeCount++ == 0) { if (_namespaces) { _prefixStack.PushContext(); } } // set nsTemp [0] == namespace URI (or empty) // set nsTemp [1] == local name (or empty) if (value == null) { // MHK: I think this can only happen on an error recovery path // MHK: I was wrong: AElfred was notifying null values of attribute // declared in the DTD as #IMPLIED. But I've now changed it so it doesn't. return; } if (_namespaces && aname.StartsWith("xmlns")) { if (aname.Length == 5) { _prefixStack.DeclarePrefix("", value); //System.err.println("Declare default prefix = "+value); _contentHandler.StartPrefixMapping("", value); } else if (aname[5] == ':' && !aname.Equals("xmlns:xml")) { if (aname.Length == 6) { _errorHandler.Error(new SAXParseException("Missing namespace prefix in namespace declaration: " + aname, this)); return; } string prefix = aname.Substring(6); if (value.Length == 0) { _errorHandler.Error(new SAXParseException("Missing URI in namespace declaration: " + aname, this)); return; } _prefixStack.DeclarePrefix(prefix, value); //System.err.println("Declare prefix " +prefix+"="+value); _contentHandler.StartPrefixMapping(prefix, value); } if (!_xmlNames) { // if xmlNames option wasn't selected, // we don't report xmlns:* declarations as attributes return; } } _attributeNames.Add(aname); _attributeValues.Add(value); }