1 | // Copyright (c) 2009-2013 AlphaSierraPapa for the SharpDevelop Team |
---|
2 | // |
---|
3 | // Permission is hereby granted, free of charge, to any person obtaining a copy of this |
---|
4 | // software and associated documentation files (the "Software"), to deal in the Software |
---|
5 | // without restriction, including without limitation the rights to use, copy, modify, merge, |
---|
6 | // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
---|
7 | // to whom the Software is furnished to do so, subject to the following conditions: |
---|
8 | // |
---|
9 | // The above copyright notice and this permission notice shall be included in all copies or |
---|
10 | // substantial portions of the Software. |
---|
11 | // |
---|
12 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
13 | // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
14 | // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
---|
15 | // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
---|
16 | // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
17 | // DEALINGS IN THE SOFTWARE. |
---|
18 | |
---|
19 | using System; |
---|
20 | |
---|
21 | namespace ICSharpCode.NRefactory.Xml |
---|
22 | { |
---|
23 | internal abstract class InternalObject |
---|
24 | { |
---|
25 | public int StartRelativeToParent; |
---|
26 | public int Length; |
---|
27 | /// <summary>Length that was touched to parsed this object.</summary> |
---|
28 | public int LengthTouched; |
---|
29 | public InternalSyntaxError[] SyntaxErrors; |
---|
30 | public InternalObject[] NestedObjects; |
---|
31 | |
---|
32 | public InternalObject SetStartRelativeToParent(int newStartRelativeToParent) |
---|
33 | { |
---|
34 | if (newStartRelativeToParent == StartRelativeToParent) |
---|
35 | return this; |
---|
36 | InternalObject obj = (InternalObject)MemberwiseClone(); |
---|
37 | obj.StartRelativeToParent = newStartRelativeToParent; |
---|
38 | return obj; |
---|
39 | } |
---|
40 | |
---|
41 | public abstract AXmlObject CreatePublicObject(AXmlObject parent, int parentStartOffset); |
---|
42 | } |
---|
43 | |
---|
44 | sealed class InternalDocument : InternalObject |
---|
45 | { |
---|
46 | public override AXmlObject CreatePublicObject(AXmlObject parent, int parentStartOffset) |
---|
47 | { |
---|
48 | return new AXmlDocument(parent, (parent != null ? parentStartOffset + StartRelativeToParent : parentStartOffset), this); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | sealed class InternalText : InternalObject |
---|
53 | { |
---|
54 | public TextType Type; |
---|
55 | public bool ContainsOnlyWhitespace; |
---|
56 | public string Value; |
---|
57 | |
---|
58 | public override AXmlObject CreatePublicObject(AXmlObject parent, int parentStartOffset) |
---|
59 | { |
---|
60 | return new AXmlText(parent, (parent != null ? parentStartOffset + StartRelativeToParent : parentStartOffset), this); |
---|
61 | } |
---|
62 | |
---|
63 | public override string ToString() |
---|
64 | { |
---|
65 | return "Text: " + this.Value.Replace("\n", "\\n").Replace("\r", "\\r"); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | sealed class InternalTag : InternalObject |
---|
70 | { |
---|
71 | public string OpeningBracket; |
---|
72 | public int RelativeNameStart; |
---|
73 | public string Name; |
---|
74 | public string ClosingBracket; |
---|
75 | |
---|
76 | /// <summary> True if tag starts with "<" </summary> |
---|
77 | public bool IsStartOrEmptyTag { get { return OpeningBracket == "<"; } } |
---|
78 | /// <summary> True if tag starts with "<" and ends with ">" </summary> |
---|
79 | public bool IsStartTag { get { return OpeningBracket == "<" && ClosingBracket == ">"; } } |
---|
80 | /// <summary> True if tag starts with "<" and does not end with ">" </summary> |
---|
81 | public bool IsEmptyTag { get { return OpeningBracket == "<" && ClosingBracket != ">" ; } } |
---|
82 | /// <summary> True if tag starts with "</" </summary> |
---|
83 | public bool IsEndTag { get { return OpeningBracket == "</"; } } |
---|
84 | /// <summary> True if tag starts with "<?" </summary> |
---|
85 | public bool IsProcessingInstruction { get { return OpeningBracket == "<?"; } } |
---|
86 | /// <summary> True if tag starts with "<!--" </summary> |
---|
87 | public bool IsComment { get { return OpeningBracket == "<!--"; } } |
---|
88 | /// <summary> True if tag starts with "<![CDATA[" </summary> |
---|
89 | public bool IsCData { get { return OpeningBracket == "<![CDATA["; } } |
---|
90 | /// <summary> True if tag starts with one of the DTD starts </summary> |
---|
91 | public bool IsDocumentType { get { return AXmlTag.DtdNames.Contains(OpeningBracket); } } |
---|
92 | /// <summary> True if tag starts with "<!" </summary> |
---|
93 | public bool IsUnknownBang { get { return OpeningBracket == "<!"; } } |
---|
94 | |
---|
95 | public override AXmlObject CreatePublicObject(AXmlObject parent, int parentStartOffset) |
---|
96 | { |
---|
97 | return new AXmlTag(parent, (parent != null ? parentStartOffset + StartRelativeToParent : parentStartOffset), this); |
---|
98 | } |
---|
99 | |
---|
100 | public override string ToString() |
---|
101 | { |
---|
102 | return "Tag: " + OpeningBracket + Name + ClosingBracket; |
---|
103 | } |
---|
104 | |
---|
105 | public InternalTag AddSyntaxError(string description) |
---|
106 | { |
---|
107 | if (this.SyntaxErrors != null && this.SyntaxErrors.Length > 0) |
---|
108 | return this; // don't add error if there already is one |
---|
109 | InternalTag tag = (InternalTag)MemberwiseClone(); |
---|
110 | tag.SyntaxErrors = new InternalSyntaxError[] { new InternalSyntaxError(0, Length, description) }; |
---|
111 | return tag; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | struct InternalSyntaxError |
---|
116 | { |
---|
117 | public readonly int RelativeStart; |
---|
118 | public readonly int RelativeEnd; |
---|
119 | public readonly string Description; |
---|
120 | |
---|
121 | public InternalSyntaxError(int relativeStart, int relativeEnd, string description) |
---|
122 | { |
---|
123 | this.RelativeStart = relativeStart; |
---|
124 | this.RelativeEnd = relativeEnd; |
---|
125 | this.Description = description; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | sealed class InternalAttribute : InternalObject |
---|
130 | { |
---|
131 | public string Name; |
---|
132 | public int EqualsSignLength; // length of equals sign including the surrounding whitespace |
---|
133 | public string Value; |
---|
134 | |
---|
135 | public override AXmlObject CreatePublicObject(AXmlObject parent, int parentStartOffset) |
---|
136 | { |
---|
137 | return new AXmlAttribute(parent, (parent != null ? parentStartOffset + StartRelativeToParent : parentStartOffset), this); |
---|
138 | } |
---|
139 | |
---|
140 | public override string ToString() |
---|
141 | { |
---|
142 | return "Attribute: " + Name + "='" + Value + "'"; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | sealed class InternalElement : InternalObject |
---|
147 | { |
---|
148 | public bool HasEndTag; |
---|
149 | public bool IsPropertyNested; |
---|
150 | public readonly string Name; |
---|
151 | |
---|
152 | public InternalElement(InternalTag tag) |
---|
153 | { |
---|
154 | this.Name = tag.Name; |
---|
155 | } |
---|
156 | |
---|
157 | public string Prefix { |
---|
158 | get { return AXmlObject.GetNamespacePrefix(Name); } |
---|
159 | } |
---|
160 | |
---|
161 | public string LocalName { |
---|
162 | get { return AXmlObject.GetLocalName(Name); } |
---|
163 | } |
---|
164 | |
---|
165 | public override AXmlObject CreatePublicObject(AXmlObject parent, int parentStartOffset) |
---|
166 | { |
---|
167 | return new AXmlElement(parent, (parent != null ? parentStartOffset + StartRelativeToParent : parentStartOffset), this); |
---|
168 | } |
---|
169 | |
---|
170 | public override string ToString() |
---|
171 | { |
---|
172 | return "Element: " + NestedObjects[0].ToString(); |
---|
173 | } |
---|
174 | } |
---|
175 | } |
---|