Exemplo n.º 1
0
        internal XmlAttribute(
            string prefix,
            string localName,
            string namespaceURI,
            XmlDocument doc,
            bool atomizedNames, bool checkNamespace) : base(doc)
        {
            if (!atomizedNames)
            {
                if (prefix == null)
                {
                    prefix = String.Empty;
                }
                if (namespaceURI == null)
                {
                    namespaceURI = String.Empty;
                }
            }

            // Prefix "xml" should be also checked (http://www.w3.org/XML/xml-names-19990114-errata#NE05)
            // but MS.NET ignores such case
            if (checkNamespace)
            {
                if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
                {
                    if (namespaceURI != XmlNamespaceManager.XmlnsXmlns)
                    {
                        throw new ArgumentException("Invalid attribute namespace for namespace declaration.");
                    }
                    else if (prefix == "xml" && namespaceURI != XmlNamespaceManager.XmlnsXml)
                    {
                        throw new ArgumentException("Invalid attribute namespace for namespace declaration.");
                    }
                }
            }

            if (!atomizedNames)
            {
                // There are no means to identify the DOM is
                // namespace-aware or not, so we can only
                // check Name validity.
                if (prefix != "" && !XmlChar.IsName(prefix))
                {
                    throw new ArgumentException("Invalid attribute prefix.");
                }
                else if (!XmlChar.IsName(localName))
                {
                    throw new ArgumentException("Invalid attribute local name.");
                }

                prefix       = doc.NameTable.Add(prefix);
                localName    = doc.NameTable.Add(localName);
                namespaceURI = doc.NameTable.Add(namespaceURI);
            }
            name = doc.NameCache.Add(prefix, localName, namespaceURI, true);
        }
 private int SkipIgnorableBase64Chars(char [] chars, int charsLength, int i)
 {
     while (chars [i] == '=' || XmlChar.IsWhitespace(chars [i]))
     {
         if (charsLength == ++i)
         {
             break;
         }
     }
     return(i);
 }
Exemplo n.º 3
0
 public static string VerifyPublicId(string publicId)
 {
     if (publicId == null)
     {
         throw new ArgumentNullException("publicId");
     }
     if (XmlChar.IsPubid(publicId))
     {
         return(publicId);
     }
     throw new XmlException(string.Format("'{0}' is not a valid PUBLIC ID", publicId));
 }
Exemplo n.º 4
0
 public static string VerifyWhitespace(string content)
 {
     if (content == null)
     {
         throw new ArgumentNullException("content");
     }
     if (XmlChar.IsWhitespace(content))
     {
         return(content);
     }
     throw new XmlException(string.Format("'{0}' is not whitespace", content));
 }
Exemplo n.º 5
0
        public static string VerifyNCName(string name)
        {
            if (name == null || name.Length == 0)
            {
                throw new ArgumentNullException("name");
            }

            if (!XmlChar.IsNCName(name))
            {
                throw new XmlException("'" + name + "' is not a valid XML NCName");
            }
            return(name);
        }
Exemplo n.º 6
0
        internal static string VerifyNMTOKEN(string name)
#endif
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (!XmlChar.IsNmToken(name))
            {
                throw new XmlException("'" + name + "' is not a valid XML NMTOKEN");
            }
            return(name);
        }
Exemplo n.º 7
0
        public static string VerifyXmlChars(string content)
        {
            if (content == null)
            {
                throw new ArgumentNullException("content");
            }
            var idx = XmlChar.IndexOfInvalid(content, true);

            if (idx < 0)
            {
                return(content);
            }
            throw new XmlException(string.Format("Invalid XML character was found in the content, at index {0}.", idx));
        }
Exemplo n.º 8
0
 private int SkipWhitespace(string input, int index)
 {
     while (index < input.Length)
     {
         if (XmlChar.IsWhitespace(input [index]))
         {
             index++;
         }
         else
         {
             break;
         }
     }
     return(index);
 }
Exemplo n.º 9
0
        internal static bool IsInvalid(char c, bool firstOnlyLetter)
        {
            if (c == ':')             // Special case. allowed in EncodeName, but encoded in EncodeLocalName
            {
                return(false);
            }

            if (firstOnlyLetter)
            {
                return(!XmlChar.IsFirstNameChar(c));
            }
            else
            {
                return(!XmlChar.IsNameChar(c));
            }
        }
Exemplo n.º 10
0
        internal virtual void WriteNmTokenInternal(string name)
        {
            bool valid = true;

            switch (Settings.ConformanceLevel)
            {
            case ConformanceLevel.Document:
            case ConformanceLevel.Fragment:
                valid = XmlChar.IsNmToken(name);
                break;
            }
            if (!valid)
            {
                throw new ArgumentException("Argument name is not a valid NMTOKEN.");
            }
            WriteString(name);
        }
Exemplo n.º 11
0
 public static bool IsNCNameChar(char ch)
 {
     return(XmlChar.IsNCNameChar(ch));
 }
Exemplo n.º 12
0
        private void Initialize(Stream stream)
        {
            buffer      = new byte [6];
            this.stream = stream;
            enc         = StrictUTF8;     // Default to UTF8 if we can't guess it
            bufLength   = stream.Read(buffer, 0, buffer.Length);
            if (bufLength == -1 || bufLength == 0)
            {
                return;
            }

            int c = ReadByteSpecial();

            switch (c)
            {
            case 0xFF:
                c = ReadByteSpecial();
                if (c == 0xFE)
                {
                    // BOM-ed little endian utf-16
                    enc = Encoding.Unicode;
                }
                else
                {
                    // It doesn't start from "<?xml" then its encoding is utf-8
                    bufPos = 0;
                }
                break;

            case 0xFE:
                c = ReadByteSpecial();
                if (c == 0xFF)
                {
                    // BOM-ed big endian utf-16
                    enc = Encoding.BigEndianUnicode;
                    return;
                }
                else
                {
                    // It doesn't start from "<?xml" then its encoding is utf-8
                    bufPos = 0;
                }
                break;

            case 0xEF:
                c = ReadByteSpecial();
                if (c == 0xBB)
                {
                    c = ReadByteSpecial();
                    if (c != 0xBF)
                    {
                        bufPos = 0;
                    }
                }
                else
                {
                    buffer [--bufPos] = 0xEF;
                }
                break;

            case 0:
                // It could still be 1234/2143/3412 variants of UTF32, but only 1234 version is available on .NET.
                c = ReadByteSpecial();
                if (c == 0)
                {
                    enc = Strict1234UTF32;
                }
                else
                {
                    enc = StrictBigEndianUTF16;
                }
                break;

            case '<':
                c = ReadByteSpecial();
                if (c == 0)
                {
                    if (ReadByteSpecial() == 0)
                    {
                        enc = Encoding.UTF32;                         // little endian UTF32
                    }
                    else
                    {
                        enc = Encoding.Unicode;                         // little endian UTF16
                    }
                }
                else if (bufLength >= 4 && GetStringFromBytes(1, 4) == "?xml")
                {
                    // try to get encoding name from XMLDecl.
                    bufPos += 4;
                    c       = SkipWhitespace();

                    // version. It is optional here.
                    if (c == 'v')
                    {
                        while (c >= 0)
                        {
                            c = ReadByteSpecial();
                            if (c == '0')                               // 0 of 1.0
                            {
                                ReadByteSpecial();
                                break;
                            }
                        }
                        c = SkipWhitespace();
                    }

                    if (c == 'e')
                    {
                        if (GetStringFromBytes(bufPos, 7) == "ncoding")
                        {
                            bufPos += 7;
                            c       = SkipWhitespace();
                            if (c != '=')
                            {
                                throw encodingException;
                            }
                            c = SkipWhitespace();
                            int           quoteChar = c;
                            StringBuilder sb        = new StringBuilder();
                            while (true)
                            {
                                c = ReadByteSpecial();
                                if (c == quoteChar)
                                {
                                    break;
                                }
                                else if (c < 0)
                                {
                                    throw encodingException;
                                }

                                sb.Append((char)c);
                            }
                            string encodingName = sb.ToString();
                            if (!XmlChar.IsValidIANAEncoding(encodingName))
                            {
                                throw encodingException;
                            }
                            enc = Encoding.GetEncoding(encodingName);
                        }
                    }
                }
#if TARGET_JVM
                else
                {
                    if (bufLength >= 10 && Encoding.Unicode.GetString(buffer, 2, 8) == "?xml")
                    {
                        enc = Encoding.Unicode;
                    }
                }
#endif
                bufPos = 0;
                break;

            default:
                if (c == 0)
                {
                    enc = StrictUTF16;
                }
                bufPos = 0;
                break;
            }
        }
Exemplo n.º 13
0
        void ParseInput(string input)
        {
            int index = SkipWhitespace(input, 0);

            if (index + 7 > input.Length || input.IndexOf("version", index, 7) != index)
            {
                throw new XmlException("Missing 'version' specification.");
            }
            index = SkipWhitespace(input, index + 7);

            char c = input [index];

            if (c != '=')
            {
                throw new XmlException("Invalid 'version' specification.");
            }
            index++;
            index = SkipWhitespace(input, index);
            c     = input [index];
            if (c != '"' && c != '\'')
            {
                throw new XmlException("Invalid 'version' specification.");
            }
            index++;
            int end = input.IndexOf(c, index);

            if (end < 0 || input.IndexOf("1.0", index, 3) != index)
            {
                throw new XmlException("Invalid 'version' specification.");
            }
            index += 4;
            if (index == input.Length)
            {
                return;
            }
            if (!XmlChar.IsWhitespace(input [index]))
            {
                throw new XmlException("Invalid XML declaration.");
            }
            index = SkipWhitespace(input, index + 1);
            if (index == input.Length)
            {
                return;
            }

            if (input.Length > index + 8 && input.IndexOf("encoding", index, 8) > 0)
            {
                index = SkipWhitespace(input, index + 8);
                c     = input [index];
                if (c != '=')
                {
                    throw new XmlException("Invalid 'version' specification.");
                }
                index++;
                index = SkipWhitespace(input, index);
                c     = input [index];
                if (c != '"' && c != '\'')
                {
                    throw new XmlException("Invalid 'encoding' specification.");
                }
                end = input.IndexOf(c, index + 1);
                if (end < 0)
                {
                    throw new XmlException("Invalid 'encoding' specification.");
                }
                Encoding = input.Substring(index + 1, end - index - 1);
                index    = end + 1;
                if (index == input.Length)
                {
                    return;
                }
                if (!XmlChar.IsWhitespace(input [index]))
                {
                    throw new XmlException("Invalid XML declaration.");
                }
                index = SkipWhitespace(input, index + 1);
            }

            if (input.Length > index + 10 && input.IndexOf("standalone", index, 10) > 0)
            {
                index = SkipWhitespace(input, index + 10);
                c     = input [index];
                if (c != '=')
                {
                    throw new XmlException("Invalid 'version' specification.");
                }
                index++;
                index = SkipWhitespace(input, index);
                c     = input [index];
                if (c != '"' && c != '\'')
                {
                    throw new XmlException("Invalid 'standalone' specification.");
                }
                end = input.IndexOf(c, index + 1);
                if (end < 0)
                {
                    throw new XmlException("Invalid 'standalone' specification.");
                }
                string tmp = input.Substring(index + 1, end - index - 1);
                switch (tmp)
                {
                case "yes":
                case "no":
                    break;

                default:
                    throw new XmlException("Invalid standalone specification.");
                }
                Standalone = tmp;
                index      = end + 1;
                index      = SkipWhitespace(input, index);
            }
            if (index != input.Length)
            {
                throw new XmlException("Invalid XML declaration.");
            }
        }
Exemplo n.º 14
0
 public static bool IsXmlChar(char ch)
 {
     return(XmlChar.IsValid(ch));
 }
Exemplo n.º 15
0
 public static bool IsWhitespaceChar(char ch)
 {
     return(XmlChar.IsWhitespace(ch));
 }
Exemplo n.º 16
0
 public static bool IsStartNCNameChar(char ch)
 {
     return(XmlChar.IsFirstNameChar(ch));
 }
Exemplo n.º 17
0
 public static bool IsPublicIdChar(char ch)
 {
     return(XmlChar.IsPubidChar(ch));
 }