~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to src/addins/MonoDevelop.XmlEditor/MonoDevelop.Xml.StateEngine/XmlParser.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// XmlParser.cs
 
3
// 
 
4
// Author:
 
5
//   Michael Hutchinson <mhutchinson@novell.com>
 
6
// 
 
7
// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 
8
// 
 
9
// Permission is hereby granted, free of charge, to any person obtaining
 
10
// a copy of this software and associated documentation files (the
 
11
// "Software"), to deal in the Software without restriction, including
 
12
// without limitation the rights to use, copy, modify, merge, publish,
 
13
// distribute, sublicense, and/or sell copies of the Software, and to
 
14
// permit persons to whom the Software is furnished to do so, subject to
 
15
// the following conditions:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Diagnostics;
 
31
 
 
32
namespace MonoDevelop.Xml.StateEngine
 
33
{
 
34
        
 
35
        
 
36
        public class XmlParser : Parser2
 
37
        {
 
38
                public XmlParser () : base (false) {}
 
39
                public XmlParser (bool buildTree) : base (buildTree) {}
 
40
                
 
41
                protected XState CurrentXState {
 
42
                        get {
 
43
                                Debug.Assert (CurrentState < StateOffset && CurrentState > -1);
 
44
                                return (XState) CurrentState;
 
45
                        }
 
46
                }
 
47
                
 
48
                protected virtual int StateOffset {
 
49
                        get { return (int) XState.ProcessingInstructionClosing + 1; }
 
50
                }
 
51
                
 
52
                protected override int Dispatch (char c)
 
53
                {
 
54
                        switch (CurrentXState) {
 
55
                        case XState.Free:
 
56
                                switch (c) {
 
57
                                case '<': return (int)XState.OpeningBracket;
 
58
//                              case '&': return (int) XState.Entity;
 
59
                                default: return (int)XState.Free;
 
60
                                }
 
61
 
 
62
                        case XState.Malformed:
 
63
                                if (StateLength == 0) {
 
64
                                        if (BuildTree)
 
65
                                                ConnectAll ();
 
66
                                        EndAll (true);
 
67
                                }
 
68
                                switch (c) {
 
69
                                case '>': return (int)XState.Free;
 
70
                                case '<': return (int)XState.OpeningBracket;
 
71
                                default: return (int)XState.Malformed;
 
72
                                }
 
73
 
 
74
                        case XState.OpeningBracket:
 
75
                                if (StateLength == 0) {
 
76
                                        switch (c) {
 
77
                                        case '!': return (int)XState.OpeningBracket;
 
78
                                        case '?':
 
79
                                                Nodes.Push (new XProcessingInstruction (this.Position - 2));
 
80
                                                return (int)XState.ProcessingInstruction;
 
81
                                        
 
82
                                        case '/':
 
83
                                                Nodes.Push (new XClosingTag (this.Position - 2));
 
84
                                                return (int)XState.ClosingTagName;
 
85
                                        
 
86
                                        if (char.IsLetter (c) || c == '_') {
 
87
                                                        Nodes.Push (new XElement (this.Position - 2));
 
88
                                                        KeywordBuilder.Append (c);
 
89
                                                        return (int) XState.TagName;
 
90
                                                }
 
91
                                        }
 
92
                                                
 
93
                                } else if (StateLength == 1) {
 
94
                                        switch (c) {
 
95
                                        case '-':
 
96
                                                return (int)XState.CommentOpening;
 
97
                                        case '[':
 
98
                                                return (int)XState.CDataOpening;
 
99
                                        }
 
100
                                }
 
101
                                LogError ("Malformed tag start.");
 
102
                                if (c == '>') return (int)XState.Free;
 
103
                                else if (c == '<') return (int)XState.OpeningBracket;
 
104
                                else return (int)XState.Malformed;
 
105
 
 
106
                        case XState.TagName:
 
107
return (int)XState.TagName;             
 
108
/*
 
109
                                if (char.IsLetterOrDigit (c) || c == '_') {
 
110
                                        KeywordBuilder.Append (c);
 
111
                                        return (int)XState.TagName;
 
112
                                }
 
113
                                
 
114
                                if (c == ':') {
 
115
                                        XElement el = Nodes.Peek () as INamedXObject;
 
116
                                        if (namedObject.Name.Name != null || KeywordBuilder.Length == 0) {
 
117
                                                LogError ("Unexpected ':' in name.");
 
118
                                                return GetStateForCurrentObject ();
 
119
                                        } else {
 
120
                                                namedObject.Name = new XName (context.KeywordBuilder.ToString ());
 
121
                                                context.KeywordBuilder.Length = 0;
 
122
                                        }
 
123
                                }
 
124
                                
 
125
                                else if (char.IsWhiteSpace (c)) return (int)XState.Tag;
 
126
                                else if (c == '>') return (int)XState.Free;
 
127
                                else if (c == '/') return (int)XState.SelfClosing;
 
128
                                else break;
 
129
 
 
130
                        case XState.Tag:
 
131
                                if (char.IsWhiteSpace (c)) return (int)XState.Tag;
 
132
                                else if (char.IsLetter (c)) return PushCurrent (XState.AttributeName);
 
133
                                else if (c == '>') return (int)XState.Free;
 
134
                                else if (c == '/') return (int)XState.SelfClosing;
 
135
                                else break;
 
136
 
 
137
                        case XState.SelfClosing:
 
138
                                if (c == '>') return (int)XState.Free;
 
139
                                else break;
 
140
 
 
141
                        case XState.AttributeName:
 
142
                                if (char.IsWhiteSpace (c))
 
143
                                        return (int)XState.AttributeNamed;
 
144
                                else if (char.IsLetterOrDigit (c) | c == ':')
 
145
                                        return (int)XState.AttributeName;
 
146
                                else if (c == '=')
 
147
                                        return (int)XState.AttributeValue;
 
148
                                else return (int)XState.Malformed;
 
149
 
 
150
                        case XState.AttributeNamed:
 
151
                                if (c == '=') return (int)XState.AttributeValue;
 
152
                                else if (char.IsWhiteSpace (c)) return (int)XState.AttributeNamed;
 
153
                                else return (int)XState.Malformed;
 
154
 
 
155
                        case XState.AttributeValue:
 
156
                                if (c == '\'') return (int)XState.AttributeQuotes;
 
157
                                else if (c == '"') return (int)XState.AttributeDoubleQuotes;
 
158
                                else if (c == '<') break;
 
159
                                else if (char.IsWhiteSpace (c)) return (int)XState.AttributeValue;
 
160
                                else if (char.IsLetterOrDigit (c)) {
 
161
                                        LogWarning ("Unquoted attribute value.");
 
162
                                        return (int)XState.AttributeUnquoted;
 
163
                                } else {
 
164
                                        LogWarning ("Incomplete attribute.");
 
165
                                        return PopState ();
 
166
                                }
 
167
 
 
168
                        case XState.AttributeQuotes:
 
169
                                switch (c) {
 
170
                                case '\'': return PopState ();
 
171
                                case '<': return UnexpectedOpeningTag;
 
172
                                case '&': return PushCurrent (XState.Entity);
 
173
                                default: return (int)XState.AttributeQuotes;
 
174
                                }
 
175
 
 
176
                        case XState.AttributeDoubleQuotes:
 
177
                                switch (c) {
 
178
                                case '"': return PopState ();
 
179
                                case '<': return UnexpectedOpeningTag;
 
180
                                case '&': return PushCurrent (XState.Entity);
 
181
                                default: return (int)XState.AttributeDoubleQuotes;
 
182
                                }
 
183
 
 
184
                        case XState.AttributeUnquoted:
 
185
                                switch (c) {
 
186
                                case '<': return UnexpectedOpeningTag;
 
187
                                case '&': return PushCurrent (XState.Entity);
 
188
                                case '.': case '_': case ':': case ';': return CurrentState;
 
189
                                case '>': return (int)XState.Free;
 
190
                                }
 
191
                                if (char.IsLetterOrDigit (c)) return CurrentState;
 
192
                                else if (char.IsWhiteSpace (c)) return PopState ();
 
193
                                break;
 
194
*/
 
195
                        case XState.CData:
 
196
                                return (int)((c == ']')? XState.CDataClosing: XState.CData);
 
197
 
 
198
                        case XState.CDataOpening:
 
199
                                if (StateLength < "CDATA[".Length) {
 
200
                                        if ("CDATA["[StateLength] == c) return (int)XState.CDataOpening;
 
201
                                        else break;
 
202
                                } else {
 
203
                                        Nodes.Push (new XCData (Position - "<![CDATA[".Length));
 
204
                                        return (int)XState.CData;
 
205
                                }
 
206
 
 
207
                        case XState.CDataClosing:
 
208
                                if ("]>"[StateLength] == c) {
 
209
                                        if (StateLength == 1) {
 
210
                                                XCData n = (XCData) Nodes.Pop ();
 
211
                                                n.End (Position);
 
212
                                                ((XContainer)Nodes.Peek ()).AddChildNode (n);
 
213
                                                return (int) XState.Free;
 
214
                                        } else {
 
215
                                                return (int)XState.CDataClosing;
 
216
                                        }
 
217
                                } else return (int)XState.CData;
 
218
/*
 
219
                        case XState.Entity:
 
220
                                if ((StateLength == 0 && c == '#') || char.IsLetterOrDigit (c))
 
221
                                        return (int)XState.Entity;
 
222
                                else if (c == ';') return PopState ();
 
223
                                else LogWarning ("Malformed entity");
 
224
                                break;
 
225
*/
 
226
                        case XState.ProcessingInstruction:
 
227
                                switch (c) {
 
228
                                case '<': break;
 
229
                                case '?': return (int)XState.ProcessingInstructionClosing;
 
230
                                default: return (int)XState.ProcessingInstruction;
 
231
                                }
 
232
                                break;
 
233
 
 
234
                        case XState.ProcessingInstructionClosing:
 
235
                                switch (c) {
 
236
                                case '<': break;
 
237
                                case '?': return (int)XState.ProcessingInstructionClosing;
 
238
                                case '>': {
 
239
                                        XProcessingInstruction n = (XProcessingInstruction) Nodes.Pop ();
 
240
                                        n.End (Position);
 
241
                                        if (BuildTree)
 
242
                                                ((XContainer)Nodes.Peek ()).AddChildNode (n);
 
243
                                        return (int) XState.Free; }
 
244
                                default: return (int)XState.ProcessingInstruction;
 
245
                                }
 
246
                                break;
 
247
 
 
248
                        case XState.CommentOpening:
 
249
                                if (c == '-') {
 
250
                                        Nodes.Push (new XComment (Position - "<!--".Length));
 
251
                                        return (int)XState.Comment;
 
252
                                }
 
253
                                else break;
 
254
 
 
255
                        case XState.Comment:
 
256
                                if (c == '-') return (int)XState.CommentClosing;
 
257
                                else return (int)XState.Comment;
 
258
 
 
259
                        case XState.CommentClosing:
 
260
                                if (StateLength == 0) {
 
261
                                        if (c == '-') return (int)XState.CommentClosing;
 
262
                                        else return (int)XState.Comment;
 
263
                                } else if (StateLength > 0) {
 
264
                                        if (c == '>') {
 
265
                                                XComment n = (XComment) Nodes.Pop ();
 
266
                                                n.End (Position);
 
267
                                                if (BuildTree)
 
268
                                                        ((XContainer)Nodes.Peek ()).AddChildNode (n);
 
269
                                        }
 
270
                                        else LogWarning ("The string '--' should not appear in comments.");
 
271
                                        if (c == '-') return (int)XState.CommentClosing;
 
272
                                        else return (int)XState.Comment;
 
273
                                }
 
274
                                else break;
 
275
                        }
 
276
                        
 
277
                        if (c == '<')
 
278
                                return UnexpectedOpeningTag;
 
279
                        return (int)XState.Malformed;
 
280
                }
 
281
                
 
282
                protected override object CreateNew ()
 
283
                {
 
284
                        return new XmlParser ();
 
285
                }
 
286
                
 
287
                protected virtual int UnexpectedOpeningTag {
 
288
                        get {
 
289
                                LogError ("Unexpected opening tag");
 
290
                                return (int)XState.OpeningBracket;
 
291
                        }
 
292
                }
 
293
                
 
294
                protected override XDocument CreateDocument ()
 
295
                {
 
296
                        return new XDocument ();
 
297
                }
 
298
        }
 
299
}