示例#1
0
        internal void CheckWhitespaceFacets(ref string s,
                                            SimpleTypeValidator type,
                                            XmlSchemaWhiteSpace wsPattern)
        {
            // before parsing, check whitespace facet
            RestrictionFacets restriction = type.RestrictionFacets;

            if (type.Variety == XmlSchemaDatatypeVariety.List)
            {
                s = s.Trim();
                return;
            }
            else if (type.Variety == XmlSchemaDatatypeVariety.Atomic)
            {
                XmlSchemaDatatype datatype = type.DataType;
                if (datatype.GetBuiltInWSFacet() == XmlSchemaWhiteSpace.Collapse)
                {
                    s = XmlComplianceUtil.NonCDataNormalize(s);
                }
                else if (datatype.GetBuiltInWSFacet() == XmlSchemaWhiteSpace.Replace)
                {
                    s = XmlComplianceUtil.CDataNormalize(s);
                }
                else if (restriction != null & (restriction.Flags & RestrictionFlags.WhiteSpace) != 0)
                {
                    //Restriction has whitespace facet specified
                    if (restriction.WhiteSpace == XmlSchemaWhiteSpace.Replace)
                    {
                        s = XmlComplianceUtil.CDataNormalize(s);
                    }
                    else if (restriction.WhiteSpace == XmlSchemaWhiteSpace.Collapse)
                    {
                        s = XmlComplianceUtil.NonCDataNormalize(s);
                    }
                }
            }

            return;
        }
        public static string NonCDataNormalize(string value)
        {
            string str;
            int    len = value.Length;

            if (len > 0)
            {
                int           startPos = 0;
                StringBuilder norValue = null;
                while (XmlComplianceUtil.IsWhiteSpace(value[startPos]))
                {
                    startPos++;
                    if (startPos == len)
                    {
                        str = " ";
                        return(str);
                    }
                }
                int i = startPos;
                while (i < len)
                {
                    if (XmlComplianceUtil.IsWhiteSpace(value[i]))
                    {
                        int j = i + 1;
                        while (true)
                        {
                            if ((j >= len ? true : !XmlComplianceUtil.IsWhiteSpace(value[j])))
                            {
                                break;
                            }
                            j++;
                        }
                        if (j == len)
                        {
                            if (norValue != null)
                            {
                                norValue.Append(value, startPos, i - startPos);
                                str = norValue.ToString();
                                return(str);
                            }
                            else
                            {
                                str = value.Substring(startPos, i - startPos);
                                return(str);
                            }
                        }
                        else if ((j > i + 1 ? false : value[i] == ' '))
                        {
                            i++;
                        }
                        else
                        {
                            if (norValue == null)
                            {
                                norValue = new StringBuilder(len);
                            }
                            norValue.Append(value, startPos, i - startPos);
                            norValue.Append(' ');
                            startPos = j;
                            i        = j;
                        }
                    }
                    else
                    {
                        i++;
                    }
                }
                if (norValue == null)
                {
                    str = (startPos <= 0 ? value : value.Substring(startPos, len - startPos));
                }
                else
                {
                    if (startPos < i)
                    {
                        norValue.Append(value, startPos, i - startPos);
                    }
                    str = norValue.ToString();
                }
            }
            else
            {
                str = string.Empty;
            }
            return(str);
        }