Пример #1
0
        // Writes the start of an attribute.
        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            try
            {
                AutoComplete(Token.StartAttribute);

                _specialAttr = SpecialAttr.None;
                if (_namespaces)
                {
                    if (prefix != null && prefix.Length == 0)
                    {
                        prefix = null;
                    }

                    if (ns == XmlReservedNs.NsXmlNs && prefix == null && localName != "xmlns")
                    {
                        prefix = "xmlns";
                    }

                    if (prefix == "xml")
                    {
                        if (localName == "lang")
                        {
                            _specialAttr = SpecialAttr.XmlLang;
                        }
                        else if (localName == "space")
                        {
                            _specialAttr = SpecialAttr.XmlSpace;
                        }
                        /* bug54408. to be fwd compatible we need to treat xml prefix as reserved
                        and not really insist on a specific value. Who knows in the future it
                        might be OK to say xml:blabla
                        else {
                            throw new ArgumentException(SR.Xml_InvalidPrefix);
                        }*/
                    }
                    else if (prefix == "xmlns")
                    {
                        if (XmlReservedNs.NsXmlNs != ns && ns != null)
                        {
                            throw new ArgumentException(SR.Xml_XmlnsBelongsToReservedNs);
                        }
                        if (localName == null || localName.Length == 0)
                        {
                            localName = prefix;
                            prefix = null;
                            _prefixForXmlNs = null;
                        }
                        else
                        {
                            _prefixForXmlNs = localName;
                        }
                        _specialAttr = SpecialAttr.XmlNs;
                    }
                    else if (prefix == null && localName == "xmlns")
                    {
                        if (XmlReservedNs.NsXmlNs != ns && ns != null)
                        {
                            // add the below line back in when DOM is fixed
                            throw new ArgumentException(SR.Xml_XmlnsBelongsToReservedNs);
                        }
                        _specialAttr = SpecialAttr.XmlNs;
                        _prefixForXmlNs = null;
                    }
                    else
                    {
                        if (ns == null)
                        {
                            // use defined prefix
                            if (prefix != null && (LookupNamespace(prefix) == -1))
                            {
                                throw new ArgumentException(SR.Xml_UndefPrefix);
                            }
                        }
                        else if (ns.Length == 0)
                        {
                            // empty namespace require null prefix
                            prefix = string.Empty;
                        }
                        else
                        { // ns.Length != 0
                            VerifyPrefixXml(prefix, ns);
                            if (prefix != null && LookupNamespaceInCurrentScope(prefix) != -1)
                            {
                                prefix = null;
                            }
                            // Now verify prefix validity
                            string definedPrefix = FindPrefix(ns);
                            if (definedPrefix != null && (prefix == null || prefix == definedPrefix))
                            {
                                prefix = definedPrefix;
                            }
                            else
                            {
                                if (prefix == null)
                                {
                                    prefix = GeneratePrefix(); // need a prefix if
                                }
                                PushNamespace(prefix, ns, false);
                            }
                        }
                    }
                    if (prefix != null && prefix.Length != 0)
                    {
                        _textWriter.Write(prefix);
                        _textWriter.Write(':');
                    }
                }
                else
                {
                    if ((ns != null && ns.Length != 0) || (prefix != null && prefix.Length != 0))
                    {
                        throw new ArgumentException(SR.Xml_NoNamespaces);
                    }
                    if (localName == "xml:lang")
                    {
                        _specialAttr = SpecialAttr.XmlLang;
                    }
                    else if (localName == "xml:space")
                    {
                        _specialAttr = SpecialAttr.XmlSpace;
                    }
                }
                _xmlEncoder.StartAttribute(_specialAttr != SpecialAttr.None);

                _textWriter.Write(localName);
                _textWriter.Write('=');
                if (_curQuoteChar != _quoteChar)
                {
                    _curQuoteChar = _quoteChar;
                    _xmlEncoder.QuoteChar = _quoteChar;
                }
                _textWriter.Write(_curQuoteChar);
            }
            catch
            {
                _currentState = State.Error;
                throw;
            }
        }
Пример #2
0
        /// <include file='doc\XmlTextWriter.uex' path='docs/doc[@for="XmlTextWriter.WriteStartAttribute"]/*' />
        /// <devdoc>
        ///    <para>Writes the start of an attribute.</para>
        /// </devdoc>
        public override  void WriteStartAttribute(string prefix, string localName, string ns) {
            AutoComplete(Token.StartAttribute);

            this.specialAttr = SpecialAttr.None;
            if (this.namespaces) {

                if (prefix == String.Empty) {
                    prefix = null;
                }

                if (ns == XmlReservedNs.NsXmlNs && prefix == null && localName != "xmlns") {
                    prefix = "xmlns";
                }

                if (prefix == "xml") {
                    if (localName == "lang") {
                        this.specialAttr = SpecialAttr.XmlLang;
                    }
                    else if (localName == "space") {
                        this.specialAttr = SpecialAttr.XmlSpace;
                    }
                    /* bug54408. to be fwd compatible we need to treat xml prefix as reserved
                       and not really insist on a specific value. Who knows in the future it
                       might be OK to say xml:blabla
                    else {
                        throw new ArgumentException(Res.GetString(Res.Xml_InvalidPrefix));
                    }*/
                }
                else if (prefix == "xmlns") {

                    if (XmlReservedNs.NsXmlNs != ns && ns != null) {
                        throw new ArgumentException(Res.GetString(Res.Xml_XmlnsBelongsToReservedNs));
                    }
                    if (localName == null || localName == String.Empty) {
                        localName = prefix;
                        prefix = null;
                        this.prefixForXmlNs = null;
                    }
                    else {
                        VerifyPrefixXml(localName);
                        this.prefixForXmlNs = localName;
                    }
                    this.specialAttr = SpecialAttr.XmlNs;
                }
                else if (prefix == null && localName == "xmlns") {
                    if (XmlReservedNs.NsXmlNs != ns && ns != null) {
                        // add the below line back in when DOM is fixed
                        throw new ArgumentException(Res.GetString(Res.Xml_XmlnsBelongsToReservedNs));
                    }
                    this.specialAttr = SpecialAttr.XmlNs;
                    this.prefixForXmlNs = null;
                }
                else {
                    if (ns == null) {
                        // use defined prefix
                        if (prefix != null && (FindScopeForPrefix(prefix) == null)) {
                            throw new ArgumentException(Res.GetString(Res.Xml_UndefPrefix));
                        }
                    }
                    else if (ns == String.Empty) {
                        // empty namespace require null prefix
                        if (prefix != null) {
                            throw new ArgumentException(Res.GetString(Res.Xml_PrefixForEmptyNs));
                        }
                    }
                    else { // ns.Length != 0
                        VerifyPrefixXml(prefix);
                        if (prefix != null && null != FindTopScopeForPrefix(prefix)) {
                            prefix = null;
                        }
                        // Now verify prefix validity
                        string definedPrefix = FindPrefix(ns);
                        if (definedPrefix != null && (prefix == null || prefix == definedPrefix)) {
                            prefix = definedPrefix;
                        }
                        else {
                            if (prefix == null) {
                                prefix = GeneratePrefix(); // need a prefix if
                            }
                            PushNamespace(prefix, ns, false);
                        }
                    }
                }
                if (prefix != null && prefix != String.Empty) {
                    textWriter.Write(prefix);
                    textWriter.Write(':');
                }
            }
            else {
                if ((ns != null && ns != String.Empty) || (prefix != null && prefix != String.Empty)) {
                    throw new ArgumentException(Res.GetString(Res.Xml_NoNamespaces));
                }
                if (localName == "xml:lang") {
                    this.specialAttr = SpecialAttr.XmlLang;
                }
                else if (localName == "xml:space") {
                    this.specialAttr = SpecialAttr.XmlSpace;
                }
            }
            xmlEncoder.StartAttribute(this.specialAttr != SpecialAttr.None);

            textWriter.Write(localName);
            textWriter.Write('=');
            if (this.curQuoteChar != this.quoteChar) {
                this.curQuoteChar = this.quoteChar;
                xmlEncoder.QuoteChar = this.quoteChar;
            }
            textWriter.Write(this.curQuoteChar);
        }
Пример #3
0
        // Writes the start of an attribute.
        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            try
            {
                AutoComplete(Token.StartAttribute);

                this.specialAttr = SpecialAttr.None;
                if (this.namespaces)
                {
                    if (prefix != null && prefix.Length == 0)
                    {
                        prefix = null;
                    }

                    if (ns == XmlConst.ReservedNsXmlNs && prefix == null && localName != "xmlns")
                    {
                        prefix = "xmlns";
                    }

                    if (prefix == "xml")
                    {
                        if (localName == "lang")
                        {
                            this.specialAttr = SpecialAttr.XmlLang;
                        }
                        else if (localName == "space")
                        {
                            this.specialAttr = SpecialAttr.XmlSpace;
                        }
                    }
                    else if (prefix == "xmlns")
                    {
                        if (XmlConst.ReservedNsXmlNs != ns && ns != null)
                        {
                            throw new ArgumentException(SR.Xml_XmlnsBelongsToReservedNs);
                        }
                        if (string.IsNullOrEmpty(localName))
                        {
                            localName = prefix;
                            prefix = null;
                            this.prefixForXmlNs = null;
                        }
                        else
                        {
                            this.prefixForXmlNs = localName;
                        }
                        this.specialAttr = SpecialAttr.XmlNs;
                    }
                    else if (prefix == null && localName == "xmlns")
                    {
                        if (XmlConst.ReservedNsXmlNs != ns && ns != null)
                        {
                            // add the below line back in when DOM is fixed
                            throw new ArgumentException(SR.Xml_XmlnsBelongsToReservedNs);
                        }
                        this.specialAttr = SpecialAttr.XmlNs;
                        this.prefixForXmlNs = null;
                    }
                    else
                    {
                        if (ns == null)
                        {
                            // use defined prefix
                            if (prefix != null && (LookupNamespace(prefix) == -1))
                            {
                                throw new ArgumentException(SR.Xml_UndefPrefix);
                            }
                        }
                        else if (ns.Length == 0)
                        {
                            // empty namespace require null prefix
                            prefix = string.Empty;
                        }
                        else
                        { // ns.Length != 0
                            VerifyPrefixXml(prefix, ns);
                            if (prefix != null && LookupNamespaceInCurrentScope(prefix) != -1)
                            {
                                prefix = null;
                            }
                            // Now verify prefix validity
                            string definedPrefix = FindPrefix(ns);
                            if (definedPrefix != null && (prefix == null || prefix == definedPrefix))
                            {
                                prefix = definedPrefix;
                            }
                            else
                            {
                                if (prefix == null)
                                {
                                    prefix = GeneratePrefix(); // need a prefix if
                                }
                                PushNamespace(prefix, ns, false);
                            }
                        }
                    }
                    if (!string.IsNullOrEmpty(prefix))
                    {
                        textWriter.Write(prefix);
                        textWriter.Write(':');
                    }
                }
                else
                {
                    if (!string.IsNullOrEmpty(ns) || !string.IsNullOrEmpty(prefix))
                    {
                        throw new ArgumentException(SR.Xml_NoNamespaces);
                    }
                    if (localName == "xml:lang")
                    {
                        this.specialAttr = SpecialAttr.XmlLang;
                    }
                    else if (localName == "xml:space")
                    {
                        this.specialAttr = SpecialAttr.XmlSpace;
                    }
                }
                xmlEncoder.StartAttribute(this.specialAttr != SpecialAttr.None);

                textWriter.Write(localName);
                textWriter.Write('=');
                if (this.curQuoteChar != this.quoteChar)
                {
                    this.curQuoteChar = this.quoteChar;
                    xmlEncoder.QuoteChar = this.quoteChar;
                }
                textWriter.Write(this.curQuoteChar);
            }
            catch
            {
                currentState = State.Error;
                throw;
            }
        }
 public override void WriteStartAttribute(string prefix, string localName, string ns)
 {
     try
     {
         this.AutoComplete(Token.StartAttribute);
         this.specialAttr = SpecialAttr.None;
         if (this.namespaces)
         {
             if ((prefix != null) && (prefix.Length == 0))
             {
                 prefix = null;
             }
             if (((ns == "http://www.w3.org/2000/xmlns/") && (prefix == null)) && (localName != "xmlns"))
             {
                 prefix = "xmlns";
             }
             if (prefix == "xml")
             {
                 if (localName == "lang")
                 {
                     this.specialAttr = SpecialAttr.XmlLang;
                 }
                 else if (localName == "space")
                 {
                     this.specialAttr = SpecialAttr.XmlSpace;
                 }
             }
             else if (prefix == "xmlns")
             {
                 if (("http://www.w3.org/2000/xmlns/" != ns) && (ns != null))
                 {
                     throw new ArgumentException(Res.GetString("Xml_XmlnsBelongsToReservedNs"));
                 }
                 if ((localName == null) || (localName.Length == 0))
                 {
                     localName = prefix;
                     prefix = null;
                     this.prefixForXmlNs = null;
                 }
                 else
                 {
                     this.prefixForXmlNs = localName;
                 }
                 this.specialAttr = SpecialAttr.XmlNs;
             }
             else if ((prefix == null) && (localName == "xmlns"))
             {
                 if (("http://www.w3.org/2000/xmlns/" != ns) && (ns != null))
                 {
                     throw new ArgumentException(Res.GetString("Xml_XmlnsBelongsToReservedNs"));
                 }
                 this.specialAttr = SpecialAttr.XmlNs;
                 this.prefixForXmlNs = null;
             }
             else if (ns == null)
             {
                 if ((prefix != null) && (this.LookupNamespace(prefix) == -1))
                 {
                     throw new ArgumentException(Res.GetString("Xml_UndefPrefix"));
                 }
             }
             else if (ns.Length == 0)
             {
                 prefix = string.Empty;
             }
             else
             {
                 this.VerifyPrefixXml(prefix, ns);
                 if ((prefix != null) && (this.LookupNamespaceInCurrentScope(prefix) != -1))
                 {
                     prefix = null;
                 }
                 string str = this.FindPrefix(ns);
                 if ((str != null) && ((prefix == null) || (prefix == str)))
                 {
                     prefix = str;
                 }
                 else
                 {
                     if (prefix == null)
                     {
                         prefix = this.GeneratePrefix();
                     }
                     this.PushNamespace(prefix, ns, false);
                 }
             }
             if ((prefix != null) && (prefix.Length != 0))
             {
                 this.textWriter.Write(prefix);
                 this.textWriter.Write(':');
             }
         }
         else
         {
             if (((ns != null) && (ns.Length != 0)) || ((prefix != null) && (prefix.Length != 0)))
             {
                 throw new ArgumentException(Res.GetString("Xml_NoNamespaces"));
             }
             if (localName == "xml:lang")
             {
                 this.specialAttr = SpecialAttr.XmlLang;
             }
             else if (localName == "xml:space")
             {
                 this.specialAttr = SpecialAttr.XmlSpace;
             }
         }
         this.xmlEncoder.StartAttribute(this.specialAttr != SpecialAttr.None);
         this.textWriter.Write(localName);
         this.textWriter.Write('=');
         if (this.curQuoteChar != this.quoteChar)
         {
             this.curQuoteChar = this.quoteChar;
             this.xmlEncoder.QuoteChar = this.quoteChar;
         }
         this.textWriter.Write(this.curQuoteChar);
     }
     catch
     {
         this.currentState = State.Error;
         throw;
     }
 }