Welcome to mirror list, hosted at ThFree Co, Russian Federation.

XmlComplianceUtil.cs « Xml « System « System.Xml « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c204926ddf6841b26f97c8e687bcf45c4e34598 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//------------------------------------------------------------------------------
// <copyright file="XmlComplianceUtil.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------

using System;
using System.IO;

namespace System.Xml {
    using System.Text;

    internal static class XmlComplianceUtil {

        // Replaces \r\n, \n, \r and \t with single space (0x20) and then removes spaces
        // at the beggining and and the end of the string and replaces sequences of spaces
        // with a single space.
        public static string NonCDataNormalize( string value ) {
            int len = value.Length;
            if ( len <= 0 ) {
               return string.Empty;
            }

            int startPos = 0;
            StringBuilder norValue = null;
            XmlCharType xmlCharType = XmlCharType.Instance;
            while ( xmlCharType.IsWhiteSpace( value[startPos] ) ) {
                startPos++;
                if ( startPos == len ) {
                    return " ";
                }
            }

            int i = startPos;
            while ( i < len ) {
                if ( !xmlCharType.IsWhiteSpace( value[i] ) ) {
                    i++;
                    continue;
                }

                int j = i + 1;
                while ( j < len && xmlCharType.IsWhiteSpace( value[j] ) ) {
                    j++;
                }
                if ( j == len ) {
                    if ( norValue == null ) {
                        return value.Substring( startPos, i - startPos );
                    }
                    else {
                        norValue.Append( value, startPos, i - startPos );
                        return norValue.ToString();
                    }
                }
                if (j > i + 1 || value[i] != 0x20 ) {
                    if ( norValue == null ) {
                        norValue = new StringBuilder( len );
                    }
                    norValue.Append( value, startPos, i - startPos );
                    norValue.Append( (char)0x20 );
                    startPos = j;
                    i = j;
                }
                else {
                    i++;
                }
            }

            if ( norValue != null ) {
                if ( startPos < i ) {
                    norValue.Append( value, startPos, i - startPos );
                }
                return norValue.ToString();
            }
            else {
                if ( startPos > 0 ) {
                    return value.Substring( startPos, len - startPos );
                }
                else {
                    return value;
                }
            }
        }

        // Replaces \r\n, \n, \r and \t with single space (0x20) 
        public static string CDataNormalize( string value ) {
            int len = value.Length;

            if ( len <= 0 ) {
               return string.Empty;
            }

            int i = 0;
            int startPos = 0;
            StringBuilder norValue = null;

            while ( i < len ) {
                char ch = value[i];
                if ( ch >= 0x20 || ( ch != 0x9 && ch != 0xA && ch != 0xD ) ) {
                    i++;
                    continue;
                }

                if ( norValue == null ) {
                    norValue = new StringBuilder( len );
                }
                if ( startPos < i ) {
                    norValue.Append( value, startPos, i - startPos );
                }
                norValue.Append( (char)0x20 );

                if ( ch == 0xD && ( i+1 < len && value[i+1] == 0xA ) ) {
                    i += 2;
                }
                else {
                    i++;
                }
                startPos = i;
            }

            if ( norValue == null ) {
                return value;
            }
            else {
               if ( i > startPos ) {
                   norValue.Append(value, startPos, i-startPos);
               }
               return norValue.ToString();
            }
        }

        public static bool IsValidLanguageID( char[] value, int startPos, int length ) {
            int len = length;
            if ( len < 2 ) {
                return false;
            }

            bool fSeenLetter = false;
            int i = startPos;
            XmlCharType xmlCharType = XmlCharType.Instance;

            char ch = value[i];
            if ( xmlCharType.IsLetter(ch) ) {
                if ( xmlCharType.IsLetter( value[++i] ) ) {
                    if ( len == 2 ) {
                        return true;
                    }
                    len--;
                    i++;
                }
                else if ( ('I' != ch && 'i' != ch ) && ( 'X' != ch && 'x' != ch ) ) {  //IANA or custom Code
                    return false;
                }

                if ( value[i] != '-' ) {
                    return false;
                }

                len -= 2;
                while ( len-- > 0 ) {
                    ch = value[++i];
                    if ( xmlCharType.IsLetter( ch ) ) {
                        fSeenLetter = true;
                    }
                    else if ( ch == '-' && fSeenLetter ) {
                        fSeenLetter = false;
                    }
                    else {
                        return false;
                    }
                }
                if ( fSeenLetter ) {
                    return true;
                }
            }
            return false;
        }
    }
}