~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// XmlFreeState.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.Collections.Generic;
 
31
using System.Text;
 
32
 
 
33
namespace MonoDevelop.Xml.StateEngine
 
34
{
 
35
        public class XmlFreeState : RootState
 
36
        {
 
37
                protected const int FREE = 0;
 
38
                protected const int BRACKET = FREE + 1;
 
39
                protected const int BRACKET_EXCLAM = BRACKET + 1;
 
40
                protected const int COMMENT = BRACKET_EXCLAM + 1;
 
41
                protected const int CDATA = COMMENT + 1;
 
42
                protected const int DOCTYPE = CDATA + 1;
 
43
                protected const int MAXCONST = DOCTYPE;
 
44
                
 
45
                XmlTagState tagState;
 
46
                XmlClosingTagState closingTagState;
 
47
                XmlCommentState commentState;
 
48
                XmlCDataState cDataState;
 
49
                XmlDocTypeState docTypeState;
 
50
                XmlProcessingInstructionState processingInstructionState;
 
51
                
 
52
                public XmlFreeState () : this (new XmlTagState (), new XmlClosingTagState ()) {}
 
53
                
 
54
                public XmlFreeState (XmlTagState tagState, XmlClosingTagState closingTagState)
 
55
                        : this (tagState, closingTagState, new XmlCommentState (), new XmlCDataState (),
 
56
                          new XmlDocTypeState (), new XmlProcessingInstructionState ()) {}
 
57
                
 
58
                public XmlFreeState (
 
59
                        XmlTagState tagState,
 
60
                        XmlClosingTagState closingTagState,
 
61
                        XmlCommentState commentState,
 
62
                        XmlCDataState cDataState,
 
63
                        XmlDocTypeState docTypeState,
 
64
                        XmlProcessingInstructionState processingInstructionState)
 
65
                {
 
66
                        this.tagState = tagState;
 
67
                        this.closingTagState = closingTagState;
 
68
                        this.commentState = commentState;
 
69
                        this.cDataState = cDataState;
 
70
                        this.docTypeState = docTypeState;
 
71
                        this.processingInstructionState = processingInstructionState;
 
72
                        
 
73
                        Adopt (this.TagState);
 
74
                        Adopt (this.ClosingTagState);
 
75
                        Adopt (this.CommentState);
 
76
                        Adopt (this.CDataState);
 
77
                        Adopt (this.DocTypeState);
 
78
                        Adopt (this.ProcessingInstructionState);
 
79
                }
 
80
                
 
81
                protected XmlTagState TagState { 
 
82
                        get { return tagState; } 
 
83
                }
 
84
                
 
85
                protected XmlClosingTagState ClosingTagState { 
 
86
                        get { return closingTagState; } 
 
87
                }
 
88
                
 
89
                protected XmlCommentState CommentState { 
 
90
                        get { return commentState; }
 
91
                }
 
92
                
 
93
                protected XmlCDataState CDataState { 
 
94
                        get { return cDataState; } 
 
95
                }
 
96
                
 
97
                protected XmlDocTypeState DocTypeState { 
 
98
                        get { return docTypeState; }
 
99
                }
 
100
                
 
101
                protected XmlProcessingInstructionState ProcessingInstructionState { 
 
102
                        get { return processingInstructionState; } 
 
103
                }
 
104
                
 
105
                public override State PushChar (char c, IParseContext context, ref string rollback)
 
106
                {
 
107
                        if (c == '<') {
 
108
                                if (context.StateTag != FREE)
 
109
                                        context.LogError ("Incomplete tag opening; encountered unexpected '<'.",
 
110
                                                new MonoDevelop.Projects.Dom.DomRegion (
 
111
                                                        context.LocationMinus (LengthFromOpenBracket (context) + 1),
 
112
                                                        context.LocationMinus (1)));
 
113
                                context.StateTag = BRACKET;
 
114
                                return null;
 
115
                        }
 
116
                        
 
117
                        switch (context.StateTag) {
 
118
                        case FREE:
 
119
                                //FIXME: handle entities?
 
120
                                return null;
 
121
                                
 
122
                        case BRACKET:
 
123
                                if (c == '?') {
 
124
                                        rollback = string.Empty;
 
125
                                        return this.ProcessingInstructionState;
 
126
                                } else if (c == '!') {
 
127
                                        context.StateTag = BRACKET_EXCLAM;
 
128
                                        return null;
 
129
                                } else if (c == '/') {
 
130
                                        return this.ClosingTagState;
 
131
                                } else if (char.IsLetter (c) || c == '_') {
 
132
                                        rollback = string.Empty;
 
133
                                        return TagState;
 
134
                                }
 
135
                                break;
 
136
                                
 
137
                        case BRACKET_EXCLAM:
 
138
                                if (c == '[') {
 
139
                                        context.StateTag = CDATA;
 
140
                                        return null;
 
141
                                } else if (c == '-') {
 
142
                                        context.StateTag = COMMENT;
 
143
                                        return null;
 
144
                                } else if (c == 'D') {
 
145
                                        context.StateTag = DOCTYPE;
 
146
                                        return null;
 
147
                                }
 
148
                                break;
 
149
                        
 
150
                        case COMMENT:
 
151
                                if (c == '-')
 
152
                                        return CommentState;
 
153
                                break;
 
154
                                
 
155
                        case CDATA:
 
156
                                string cdataStr = "CDATA[";
 
157
                                if (c == cdataStr [context.KeywordBuilder.Length]) {
 
158
                                        context.KeywordBuilder.Append (c);
 
159
                                        if (context.KeywordBuilder.Length < cdataStr.Length)
 
160
                                                return null;
 
161
                                        else
 
162
                                                return CDataState;
 
163
                                } else {
 
164
                                        context.KeywordBuilder.Length = 0;
 
165
                                }
 
166
                                break;
 
167
                                
 
168
                        case DOCTYPE:
 
169
                                string docTypeStr = "OCTYPE";
 
170
                                if (c == docTypeStr [context.KeywordBuilder.Length]) {
 
171
                                        context.KeywordBuilder.Append (c);
 
172
                                        if (context.KeywordBuilder.Length < docTypeStr.Length)
 
173
                                                return null;
 
174
                                        else
 
175
                                                return DocTypeState;
 
176
                                } else {
 
177
                                        context.KeywordBuilder.Length = 0;
 
178
                                }
 
179
                                break;
 
180
                        }
 
181
                        
 
182
                        context.LogError ("Incomplete tag opening; encountered unexpected character '" + c + "'.",
 
183
                                new MonoDevelop.Projects.Dom.DomRegion (
 
184
                                        context.LocationMinus (LengthFromOpenBracket (context)),
 
185
                                        context.Location));
 
186
                        
 
187
                        context.StateTag = FREE;
 
188
                        return null;
 
189
                }
 
190
                
 
191
                static int LengthFromOpenBracket (IParseContext context)
 
192
                {
 
193
                        switch (context.StateTag) {
 
194
                        case BRACKET:
 
195
                                return 1;
 
196
                        case BRACKET_EXCLAM:
 
197
                                return 2;
 
198
                        case COMMENT:
 
199
                                return 3;
 
200
                        case CDATA:
 
201
                        case DOCTYPE:
 
202
                                return 3 + context.KeywordBuilder.Length;
 
203
                        default:
 
204
                                return 1;
 
205
                        }
 
206
                }
 
207
                
 
208
                public override XDocument CreateDocument ()
 
209
                {
 
210
                        return new XDocument ();
 
211
                }
 
212
        }
 
213
}