コード例 #1
0
        internal override void FixDefaultNSNames()
        {
            String value = GetValue();

            // should not allow any prefix (even empty string prefix) to be bound to the reserved namespace
            if (value == XmlReservedNs.NsXmlNs)
            {
                throw new XmlException(Res.Xml_CanNotBindToReservedNamespace, ValueLineNum, ValueLinePos);
            }

            _NamespaceURI = XmlReservedNs.NsXmlNs;

            //Xmlns:
            if (value == String.Empty && _Name.Length > 0)
            {
                throw new XmlException(Res.Xml_BadAttributeChar, XmlException.BuildCharExceptionStr(' '));
            }

            if ((_Name.Length > 0 && _Name[0] != '_' && XmlCharType.IsLetter(_Name[0]) == false) ||
                (_Name.Length > 2 && String.Compare(_Name, 0, "xml", 0, 3, true, CultureInfo.InvariantCulture) == 0))
            {
                throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(_Name[0]));
            }

            _NsMgr.AddNamespace(_Name, value);
        }
コード例 #2
0
        internal override void FixNSNames()
        {
            String value = GetValue();

            // should not allow any prefix (even empty string prefix) to be bound to the reserved namespace
            if (value == XmlReservedNs.NsXmlNs)
            {
                throw new XmlException(Res.Xml_CanNotBindToReservedNamespace, ValueLineNum, ValueLinePos);
            }

            // This functionis only called when we encounter an attribute of the type "xmlns..."
            Debug.Assert(5 <= _NameWPrefix.Length);
            _NamespaceURI = XmlReservedNs.NsXmlNs;
            if (5 == _NameWPrefix.Length)
            {
                Debug.Assert((object)_NameWPrefix == (Object)_XmlNs);
                _Name   = _NameWPrefix;
                _Prefix = String.Empty;

                // We push the prefix that the elments belonging to this namespace will use.
                // We don't have to check for value == String.Empty because it is legal to have a empty value for default namespace.
                _NsMgr.AddNamespace(String.Empty, value);
            }
            else
            {
                _Prefix = _Scanner.GetTextAtom(_NameWPrefix.Substring(0, 5));
                _Name   = _Scanner.GetTextAtom(_NameWPrefix.Substring(6));
                if ((object)_Name == (Object)_XmlNs)
                {
                    throw new XmlException(Res.Xml_ReservedNs, _Name, LineNum, LinePos);
                }

                //Xmlns:
                if (value == String.Empty && _Name.Length > 0)
                {
                    throw new XmlException(Res.Xml_BadNamespaceDecl, ValueLineNum, ValueLinePos);
                }

                if ((_Name.Length > 0 && _Name[0] != '_' && XmlCharType.IsLetter(_Name[0]) == false))
                {
                    throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(_Name[0]));
                }

                if (_Name.Length > 2 && String.Compare(_Name, 0, "xml", 0, 3, true, CultureInfo.InvariantCulture) == 0)
                {
                    throw new XmlException(Res.Xml_CanNotStartWithXmlInNamespace, ValueLineNum, ValueLinePos);
                }


                _NsMgr.AddNamespace(_Name, value);
            }
        }
コード例 #3
0
        private void ValidateQName(string name)
        {
            if (name.Length == 0)
            {
                throw new ArgumentException(Res.GetString(Res.Xml_EmptyName));
            }
            int colonPos;
            int len = ValidateNames.ParseQName(name, 0, out colonPos);

            if (len != name.Length)
            {
                string res = (len == 0 || (colonPos > -1 && len == colonPos + 1)) ? Res.Xml_BadStartNameChar : Res.Xml_BadNameChar;
                throw new ArgumentException(Res.GetString(res, XmlException.BuildCharExceptionStr(name[len])));
            }
        }
コード例 #4
0
        //Called only on xmlns or xmlns: attribute
        internal override void FixNSNames()
        {
            String value = GetValue();

            // should not binding to the reserved xmlns namespace
            if (value == XmlReservedNs.NsXmlNs)
            {
                throw new XmlException(Res.Xml_CanNotBindToReservedNamespace, ValueLineNum, ValueLinePos);
            }

            _NamespaceURI = XmlReservedNs.NsXmlNs;

            if ((object)_NameWPrefix == (Object)_XmlNs)
            {
                _Prefix = String.Empty;
                _Name   = _NameWPrefix;
                _NsMgr.AddNamespace(String.Empty, value);
            }
            else
            {
                _Prefix = _Scanner.GetTextAtom(_NameWPrefix.Substring(0, 5));
                Debug.Assert((object)_Prefix == (Object)_XmlNs);
                _Name = _Scanner.GetTextAtom(_NameWPrefix.Substring(6));
                //xmlns:xmlns not allowed
                if (((object)_Name == (Object)_XmlNs) ||
                    (String.Compare(_Name, "xml", false, CultureInfo.InvariantCulture) == 0))
                {
                    throw new XmlException(Res.Xml_ReservedNs, _Name, LineNum, LinePos);
                }

                if (value == String.Empty && _Name.Length > 0)
                {
                    throw new XmlException(Res.Xml_BadNamespaceDecl, ValueLineNum, ValueLinePos);
                }

                if ((_Name.Length > 0 && _Name[0] != '_' && XmlCharType.IsLetter(_Name[0]) == false))
                {
                    throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(_Name[0]));
                }

                _NsMgr.AddNamespace(_Name, value);
            }
        }
コード例 #5
0
        /// <summary>
        /// Split a QualifiedName into prefix and localname, w/o any checking.
        /// (Used for XmlReader/XPathNavigator MoveTo(name) methods)
        /// </summary>
        internal static void SplitQName(string name, out string prefix, out string lname)
        {
            int colonPos = name.IndexOf(':');

            if (-1 == colonPos)
            {
                prefix = string.Empty;
                lname  = name;
            }
            else if (0 == colonPos || (name.Length - 1) == colonPos)
            {
                throw new ArgumentException(Res.GetString(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(':')), "name");
            }
            else
            {
                prefix = name.Substring(0, colonPos);
                colonPos++; // move after colon
                lname = name.Substring(colonPos, name.Length - colonPos);
            }
        }
コード例 #6
0
        public static Exception GetInvalidNameException(string s, int offsetStartChar, int offsetBadChar)
        {
            // If the name is empty, throw an exception
            if (offsetStartChar >= s.Length)
            {
                return(new XmlException(Res.Xml_EmptyName, string.Empty));
            }

            Debug.Assert(offsetBadChar < s.Length);

            if (XmlCharType.Instance.IsNCNameChar(s[offsetBadChar]) && !XmlCharType.Instance.IsStartNCNameChar(s[offsetBadChar]))
            {
                // The error character is a valid name character, but is not a valid start name character
                return(new XmlException(Res.Xml_BadStartNameChar, XmlException.BuildCharExceptionStr(s[offsetBadChar])));
            }
            else
            {
                // The error character is an invalid name character
                return(new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(s[offsetBadChar])));
            }
        }
コード例 #7
0
        internal static string VerifyNMTOKEN(string name)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            if (name == string.Empty)
            {
                throw new XmlException(Res.Xml_InvalidNmToken, name);
            }
            int nameLength = name.Length;
            int position   = 0;

            while (position < nameLength)
            {
                if (!XmlCharType.IsNameChar(name[position]))
                {
                    throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(name[position]));
                }
                position++;
            }
            return(name);
        }
コード例 #8
0
        /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.VerifyNCName"]/*' />
        /// <devdoc>
        ///    <para>
        ///    </para>
        /// </devdoc>
        public static string VerifyNCName(string name)
        {
            if (name == null || name == string.Empty)
            {
                throw new ArgumentNullException("name");
            }

            if (!XmlCharType.IsStartNCNameChar(name[0]))
            {
                throw new XmlException(Res.Xml_BadStartNameChar, XmlException.BuildCharExceptionStr(name[0]));
            }
            int nameLength = name.Length;
            int position   = 1;

            while (position < nameLength)
            {
                if (!XmlCharType.IsNCNameChar(name[position]))
                {
                    throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(name[position]));
                }
                position++;
            }
            return(name);
        }
コード例 #9
0
 private void CheckWhitespace( string value ) {
     int i;
     if ( ( i = xmlCharType.IsOnlyWhitespaceWithPos( value ) ) != -1 ) {
         Throw( Res.Xml_InvalidWhitespaceCharacter, XmlException.BuildCharExceptionStr( base.reader.Value[i] ) );
     }
 }
コード例 #10
0
        public override bool Read() {

            switch ( state ) {
                case State.Initial:
                    state = State.Interactive;
                    if ( base.reader.ReadState == ReadState.Initial ) {
                        goto case State.Interactive;
                    }
                    break;

                case State.Error:
                    return false;

                case State.InReadBinary:
                    FinishReadBinary();
                    state = State.Interactive;
                    goto case State.Interactive;

                case State.Interactive:
                    if ( !base.reader.Read() ) {
                        return false;
                    }
                    break;

                default:
                    Debug.Assert( false );
                    return false;
            }

            XmlNodeType nodeType = base.reader.NodeType;

            if ( !checkCharacters ) {
                switch ( nodeType ) {
                    case XmlNodeType.Comment:
                        if ( ignoreComments ) {
                            return Read();
                        }
                        break;
                    case XmlNodeType.Whitespace:
                        if ( ignoreWhitespace ) {
                            return Read();
                        }
                        break;
                    case XmlNodeType.ProcessingInstruction:
                        if ( ignorePis ) {
                            return Read();
                        }
                        break;
                    case XmlNodeType.DocumentType:
                        if ( prohibitDtd ) {
                            Throw( Res.Xml_DtdIsProhibited, string.Empty );
                        }
                        break;
                }
                return true;
            }
            else {
                switch ( nodeType ) {
                    case XmlNodeType.Element:
                        if ( checkCharacters ) {
                            // check element name
                            ValidateQName( base.reader.Prefix, base.reader.LocalName );

                            // check values of attributes
                            if ( base.reader.MoveToFirstAttribute() ) {
                                do {
                                    ValidateQName( base.reader.Prefix, base.reader.LocalName );
                                    CheckCharacters( base.reader.Value );
                                } while ( base.reader.MoveToNextAttribute() );

                                base.reader.MoveToElement();
                            }
                        }
                        break;

                    case XmlNodeType.Text:
                    case XmlNodeType.CDATA:
                        if ( checkCharacters ) {
                            CheckCharacters( base.reader.Value );
                        }
                        break;

                    case XmlNodeType.EntityReference:
                        if ( checkCharacters ) {
                            // check name
                            ValidateQName( base.reader.Name );
                        }
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        if ( ignorePis ) {
                            return Read();
                        }
                        if ( checkCharacters ) {
                            ValidateQName( base.reader.Name );
                            CheckCharacters( base.reader.Value );
                        }
                        break;

                    case XmlNodeType.Comment:
                        if ( ignoreComments ) {
                            return Read();
                        }
                        if ( checkCharacters ) {
                            CheckCharacters( base.reader.Value );
                        }
                        break;

                    case XmlNodeType.DocumentType:
                        if ( prohibitDtd ) {
                            Throw( Res.Xml_DtdIsProhibited, string.Empty );
                        }
                        if ( checkCharacters ) {
                            ValidateQName( base.reader.Name );
                            CheckCharacters( base.reader.Value );
                            
                            string str;
                            str = base.reader.GetAttribute( "SYSTEM" );
                            if ( str != null ) {
                                CheckCharacters( str );
                            }

                            str = base.reader.GetAttribute( "PUBLIC" );
                            if ( str != null ) {
                                int i;
                                if ( ( i = xmlCharType.IsPublicId( str ) ) >= 0 ) {
                                    Throw( Res.Xml_InvalidCharacter, XmlException.BuildCharExceptionStr( str[i] ) );
                                }
                            }
                        }
                        break;

                    case XmlNodeType.Whitespace:
                        if ( ignoreWhitespace ) {
                            return Read();
                        }
                        if ( checkCharacters ) {
                            CheckWhitespace( base.reader.Value );
                        }
                        break;

                    case XmlNodeType.SignificantWhitespace:
                        if ( checkCharacters ) {
                            CheckWhitespace( base.reader.Value );
                        }
                        break;

                    case XmlNodeType.EndElement:
                        if ( checkCharacters ) {
                            ValidateQName( base.reader.Prefix, base.reader.LocalName );
                        }
                        break;

                    default:
                        break;
                }
                lastNodeType = nodeType;
                return true;
            }
        }
コード例 #11
0
        private void ValidateNCName(string ncname)
        {
            if (ncname.Length == 0)
            {
                throw new ArgumentException(Res.GetString(Res.Xml_EmptyName));
            }
            int len = ValidateNames.ParseNCName(ncname, 0);

            if (len != ncname.Length)
            {
                throw new ArgumentException(Res.GetString(len == 0 ? Res.Xml_BadStartNameChar : Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(ncname[len])));
            }
        }
コード例 #12
0
 public override void WriteWhitespace(string ws)
 {
     // "checkNames" is intentional here; if false, the whitespaces are checked in XmlWellformedWriter
     if (checkNames)
     {
         int i;
         if ((i = xmlCharType.IsOnlyWhitespaceWithPos(ws)) != -1)
         {
             throw new ArgumentException(Res.GetString(Res.Xml_InvalidWhitespaceCharacter, XmlException.BuildCharExceptionStr(ws[i])));
         }
     }
     writer.WriteWhitespace(ws);
 }