@@ -0,0 +1,278 | |||
|
1 | using Implab; | |
|
2 | using Implab.JSON; | |
|
3 | using Implab.Parsing; | |
|
4 | using System; | |
|
5 | using System.Collections.Generic; | |
|
6 | using System.Linq; | |
|
7 | using System.Text; | |
|
8 | using System.Threading.Tasks; | |
|
9 | using System.Xml; | |
|
10 | ||
|
11 | namespace ConsPlay { | |
|
12 | public class JSONXmlReader : XmlReader { | |
|
13 | ||
|
14 | enum ValueContext { | |
|
15 | Undefined, | |
|
16 | ElementStart, | |
|
17 | ElementValue, | |
|
18 | ElementEnd, | |
|
19 | ElementEmpty | |
|
20 | } | |
|
21 | ||
|
22 | struct LocalNameContext { | |
|
23 | public string localName; | |
|
24 | public bool isArray; | |
|
25 | } | |
|
26 | ||
|
27 | JSONParser m_parser; | |
|
28 | ValueContext m_valueContext; | |
|
29 | ReadState m_state = ReadState.Initial; | |
|
30 | Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>(); | |
|
31 | LocalNameContext m_localName; | |
|
32 | string m_rootName = "json"; | |
|
33 | string m_prefix = String.Empty; | |
|
34 | string m_namespaceUri = String.Empty; | |
|
35 | bool m_flattenArrays = false; | |
|
36 | NameTable m_nameTable = new NameTable(); | |
|
37 | int m_depthCorrection = 0; | |
|
38 | ||
|
39 | public JSONXmlReader(JSONParser parser) { | |
|
40 | Safe.ArgumentNotNull(parser, "parser"); | |
|
41 | m_parser = parser; | |
|
42 | } | |
|
43 | ||
|
44 | /// <summary> | |
|
45 | /// Always 0, JSON doesn't support attributes | |
|
46 | /// </summary> | |
|
47 | public override int AttributeCount { | |
|
48 | get { return 0; } | |
|
49 | } | |
|
50 | ||
|
51 | public override string BaseURI { | |
|
52 | get { return String.Empty; } | |
|
53 | } | |
|
54 | ||
|
55 | public override int Depth { | |
|
56 | get { | |
|
57 | return m_localNameStack.Count+m_depthCorrection; | |
|
58 | } | |
|
59 | } | |
|
60 | ||
|
61 | public override bool EOF { | |
|
62 | get { return m_parser.EOF; } | |
|
63 | } | |
|
64 | ||
|
65 | /// <summary> | |
|
66 | /// Always throws an exception | |
|
67 | /// </summary> | |
|
68 | /// <param name="i"></param> | |
|
69 | /// <returns></returns> | |
|
70 | public override string GetAttribute(int i) { | |
|
71 | throw new ArgumentOutOfRangeException(); | |
|
72 | } | |
|
73 | ||
|
74 | /// <summary> | |
|
75 | /// Always returns empty string | |
|
76 | /// </summary> | |
|
77 | /// <param name="name"></param> | |
|
78 | /// <param name="namespaceURI"></param> | |
|
79 | /// <returns></returns> | |
|
80 | public override string GetAttribute(string name, string namespaceURI) { | |
|
81 | return String.Empty; | |
|
82 | } | |
|
83 | ||
|
84 | /// <summary> | |
|
85 | /// Always returns empty string | |
|
86 | /// </summary> | |
|
87 | /// <param name="name"></param> | |
|
88 | /// <returns></returns> | |
|
89 | public override string GetAttribute(string name) { | |
|
90 | return String.Empty; | |
|
91 | } | |
|
92 | ||
|
93 | public override bool IsEmptyElement { | |
|
94 | get { return m_parser.ElementType == JSONElementType.Value && m_valueContext == ValueContext.ElementEmpty; } | |
|
95 | } | |
|
96 | ||
|
97 | public override string LocalName { | |
|
98 | get { return m_localName.localName; } | |
|
99 | } | |
|
100 | ||
|
101 | public override string LookupNamespace(string prefix) { | |
|
102 | if (String.IsNullOrEmpty(prefix) || prefix == m_prefix) | |
|
103 | return m_namespaceUri; | |
|
104 | else | |
|
105 | return String.Empty; | |
|
106 | } | |
|
107 | ||
|
108 | public override bool MoveToAttribute(string name, string ns) { | |
|
109 | return false; | |
|
110 | } | |
|
111 | ||
|
112 | public override bool MoveToAttribute(string name) { | |
|
113 | return false; | |
|
114 | } | |
|
115 | ||
|
116 | public override bool MoveToElement() { | |
|
117 | return false; | |
|
118 | } | |
|
119 | ||
|
120 | public override bool MoveToFirstAttribute() { | |
|
121 | return false; | |
|
122 | } | |
|
123 | ||
|
124 | public override bool MoveToNextAttribute() { | |
|
125 | return false; | |
|
126 | } | |
|
127 | ||
|
128 | public override XmlNameTable NameTable { | |
|
129 | get { return m_nameTable; } | |
|
130 | } | |
|
131 | ||
|
132 | public override string NamespaceURI { | |
|
133 | get { return m_namespaceUri; } | |
|
134 | } | |
|
135 | ||
|
136 | public override XmlNodeType NodeType { | |
|
137 | get { | |
|
138 | switch (m_parser.ElementType) { | |
|
139 | case JSONElementType.BeginObject: | |
|
140 | case JSONElementType.BeginArray: | |
|
141 | return XmlNodeType.Element; | |
|
142 | case JSONElementType.EndObject: | |
|
143 | case JSONElementType.EndArray: | |
|
144 | return XmlNodeType.EndElement; | |
|
145 | case JSONElementType.Value: | |
|
146 | switch (m_valueContext) { | |
|
147 | case ValueContext.ElementStart: | |
|
148 | case ValueContext.ElementEmpty: | |
|
149 | return XmlNodeType.Element; | |
|
150 | case ValueContext.ElementValue: | |
|
151 | return XmlNodeType.Text; | |
|
152 | case ValueContext.ElementEnd: | |
|
153 | return XmlNodeType.EndElement; | |
|
154 | default: | |
|
155 | throw new InvalidOperationException(); | |
|
156 | } | |
|
157 | default: | |
|
158 | throw new InvalidOperationException(); | |
|
159 | } | |
|
160 | } | |
|
161 | } | |
|
162 | ||
|
163 | public override string Prefix { | |
|
164 | get { return m_prefix; } | |
|
165 | } | |
|
166 | ||
|
167 | public override bool Read() { | |
|
168 | if (m_state != System.Xml.ReadState.Interactive && m_state != System.Xml.ReadState.Initial) | |
|
169 | return false; | |
|
170 | ||
|
171 | if (m_state == ReadState.Initial) | |
|
172 | m_state = System.Xml.ReadState.Interactive; | |
|
173 | ||
|
174 | try { | |
|
175 | switch (m_parser.ElementType) { | |
|
176 | case JSONElementType.Value: | |
|
177 | switch (m_valueContext) { | |
|
178 | case ValueContext.ElementStart: | |
|
179 | SetLocalName(String.Empty); | |
|
180 | m_valueContext = ValueContext.ElementValue; | |
|
181 | return true; | |
|
182 | case ValueContext.ElementValue: | |
|
183 | RestoreLocalName(); | |
|
184 | m_valueContext = ValueContext.ElementEnd; | |
|
185 | return true; | |
|
186 | case ValueContext.ElementEmpty: | |
|
187 | case ValueContext.ElementEnd: | |
|
188 | RestoreLocalName(); | |
|
189 | break; | |
|
190 | } | |
|
191 | break; | |
|
192 | case JSONElementType.EndArray: | |
|
193 | case JSONElementType.EndObject: | |
|
194 | RestoreLocalName(); | |
|
195 | break; | |
|
196 | } | |
|
197 | string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : "item"; | |
|
198 | while (m_parser.Read()) { | |
|
199 | if (!String.IsNullOrEmpty(m_parser.ElementName)) | |
|
200 | itemName = m_parser.ElementName; | |
|
201 | ||
|
202 | switch (m_parser.ElementType) { | |
|
203 | case JSONElementType.BeginArray: | |
|
204 | if (m_flattenArrays && !m_localName.isArray) { | |
|
205 | m_depthCorrection--; | |
|
206 | SetLocalName(itemName, true); | |
|
207 | continue; | |
|
208 | } else { | |
|
209 | SetLocalName(itemName, true); | |
|
210 | } | |
|
211 | break; | |
|
212 | case JSONElementType.BeginObject: | |
|
213 | SetLocalName(itemName); | |
|
214 | break; | |
|
215 | case JSONElementType.EndArray: | |
|
216 | if (m_flattenArrays && !m_localNameStack.Peek().isArray) { | |
|
217 | RestoreLocalName(); | |
|
218 | m_depthCorrection++; | |
|
219 | continue; | |
|
220 | } | |
|
221 | break; | |
|
222 | case JSONElementType.EndObject: | |
|
223 | break; | |
|
224 | case JSONElementType.Value: | |
|
225 | SetLocalName(itemName); | |
|
226 | m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart; | |
|
227 | break; | |
|
228 | default: | |
|
229 | break; | |
|
230 | } | |
|
231 | return true; | |
|
232 | } | |
|
233 | ||
|
234 | m_state = System.Xml.ReadState.EndOfFile; | |
|
235 | return false; | |
|
236 | } catch { | |
|
237 | m_state = System.Xml.ReadState.Error; | |
|
238 | throw; | |
|
239 | } | |
|
240 | } | |
|
241 | ||
|
242 | public override bool ReadAttributeValue() { | |
|
243 | return false; | |
|
244 | } | |
|
245 | ||
|
246 | public override ReadState ReadState { | |
|
247 | get { return m_state; } | |
|
248 | } | |
|
249 | ||
|
250 | public override void ResolveEntity() { | |
|
251 | // do nothing | |
|
252 | } | |
|
253 | ||
|
254 | public override string Value { | |
|
255 | get { return m_parser.ElementValue == null ? String.Empty : m_parser.ElementValue.ToString(); } | |
|
256 | } | |
|
257 | ||
|
258 | void SetLocalName(string name) { | |
|
259 | m_localNameStack.Push(m_localName); | |
|
260 | m_localName.localName = name; | |
|
261 | m_localName.isArray = false; | |
|
262 | } | |
|
263 | ||
|
264 | void SetLocalName(string name, bool isArray) { | |
|
265 | m_localNameStack.Push(m_localName); | |
|
266 | m_localName.localName = name; | |
|
267 | m_localName.isArray = isArray; | |
|
268 | } | |
|
269 | ||
|
270 | void RestoreLocalName() { | |
|
271 | m_localName = m_localNameStack.Pop(); | |
|
272 | } | |
|
273 | ||
|
274 | public override void Close() { | |
|
275 | m_state = System.Xml.ReadState.EndOfFile ; | |
|
276 | } | |
|
277 | } | |
|
278 | } |
@@ -1,102 +1,104 | |||
|
1 | 1 | ο»Ώ<?xml version="1.0" encoding="utf-8"?> |
|
2 | 2 | <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
|
3 | 3 | <PropertyGroup> |
|
4 | 4 | <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> |
|
5 | 5 | <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> |
|
6 | 6 | <ProductVersion>10.0.0</ProductVersion> |
|
7 | 7 | <SchemaVersion>2.0</SchemaVersion> |
|
8 | 8 | <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid> |
|
9 | 9 | <OutputType>Library</OutputType> |
|
10 | 10 | <RootNamespace>Implab</RootNamespace> |
|
11 | 11 | <AssemblyName>Implab</AssemblyName> |
|
12 | 12 | </PropertyGroup> |
|
13 | 13 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> |
|
14 | 14 | <DebugSymbols>true</DebugSymbols> |
|
15 | 15 | <DebugType>full</DebugType> |
|
16 | 16 | <Optimize>false</Optimize> |
|
17 | 17 | <OutputPath>bin\Debug</OutputPath> |
|
18 | 18 | <DefineConstants>TRACE;DEBUG;</DefineConstants> |
|
19 | 19 | <ErrorReport>prompt</ErrorReport> |
|
20 | 20 | <WarningLevel>4</WarningLevel> |
|
21 | 21 | <ConsolePause>false</ConsolePause> |
|
22 | 22 | </PropertyGroup> |
|
23 | 23 | <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> |
|
24 | 24 | <DebugType>full</DebugType> |
|
25 | 25 | <Optimize>true</Optimize> |
|
26 | 26 | <OutputPath>bin\Release</OutputPath> |
|
27 | 27 | <ErrorReport>prompt</ErrorReport> |
|
28 | 28 | <WarningLevel>4</WarningLevel> |
|
29 | 29 | <ConsolePause>false</ConsolePause> |
|
30 | 30 | </PropertyGroup> |
|
31 | 31 | <ItemGroup> |
|
32 | 32 | <Reference Include="System" /> |
|
33 | <Reference Include="System.XML" /> | |
|
33 | 34 | </ItemGroup> |
|
34 | 35 | <ItemGroup> |
|
35 | 36 | <Compile Include="Component.cs" /> |
|
36 | 37 | <Compile Include="CustomEqualityComparer.cs" /> |
|
37 | 38 | <Compile Include="Diagnostics\ConsoleTraceListener.cs" /> |
|
38 | 39 | <Compile Include="Diagnostics\EventText.cs" /> |
|
39 | 40 | <Compile Include="Diagnostics\IEventTextFormatter.cs" /> |
|
40 | 41 | <Compile Include="Diagnostics\LogChannel.cs" /> |
|
41 | 42 | <Compile Include="Diagnostics\LogicalOperation.cs" /> |
|
42 | 43 | <Compile Include="Diagnostics\TextFileListener.cs" /> |
|
43 | 44 | <Compile Include="Diagnostics\TextListenerBase.cs" /> |
|
44 | 45 | <Compile Include="Diagnostics\TraceLog.cs" /> |
|
45 | 46 | <Compile Include="Diagnostics\TraceContext.cs" /> |
|
46 | 47 | <Compile Include="Diagnostics\TraceEvent.cs" /> |
|
47 | 48 | <Compile Include="Diagnostics\TraceEventType.cs" /> |
|
48 | 49 | <Compile Include="Disposable.cs" /> |
|
49 | 50 | <Compile Include="ICancellable.cs" /> |
|
50 | 51 | <Compile Include="IProgressHandler.cs" /> |
|
51 | 52 | <Compile Include="IProgressNotifier.cs" /> |
|
52 | 53 | <Compile Include="IPromise.cs" /> |
|
53 | 54 | <Compile Include="IPromiseBase.cs" /> |
|
54 | 55 | <Compile Include="IServiceLocator.cs" /> |
|
55 | 56 | <Compile Include="ITaskController.cs" /> |
|
56 | 57 | <Compile Include="JSON\JSONElementContext.cs" /> |
|
57 | 58 | <Compile Include="JSON\JSONElementType.cs" /> |
|
58 | 59 | <Compile Include="JSON\JSONGrammar.cs" /> |
|
59 | 60 | <Compile Include="JSON\JSONParser.cs" /> |
|
60 | 61 | <Compile Include="JSON\JSONScanner.cs" /> |
|
61 | 62 | <Compile Include="JSON\JsonTokenType.cs" /> |
|
62 | 63 | <Compile Include="JSON\JSONWriter.cs" /> |
|
64 | <Compile Include="JSON\JSONXmlReader.cs" /> | |
|
63 | 65 | <Compile Include="JSON\StringTranslator.cs" /> |
|
64 | 66 | <Compile Include="Parallels\DispatchPool.cs" /> |
|
65 | 67 | <Compile Include="Parallels\ArrayTraits.cs" /> |
|
66 | 68 | <Compile Include="Parallels\MTQueue.cs" /> |
|
67 | 69 | <Compile Include="Parallels\WorkerPool.cs" /> |
|
68 | 70 | <Compile Include="Parsing\Alphabet.cs" /> |
|
69 | 71 | <Compile Include="Parsing\AlphabetBase.cs" /> |
|
70 | 72 | <Compile Include="Parsing\AltToken.cs" /> |
|
71 | 73 | <Compile Include="Parsing\BinaryToken.cs" /> |
|
72 | 74 | <Compile Include="Parsing\CatToken.cs" /> |
|
73 | 75 | <Compile Include="Parsing\CDFADefinition.cs" /> |
|
74 | 76 | <Compile Include="Parsing\DFABuilder.cs" /> |
|
75 | 77 | <Compile Include="Parsing\DFADefinitionBase.cs" /> |
|
76 | 78 | <Compile Include="Parsing\DFAStateDescriptor.cs" /> |
|
77 | 79 | <Compile Include="Parsing\DFAutomaton.cs" /> |
|
78 | 80 | <Compile Include="Parsing\EDFADefinition.cs" /> |
|
79 | 81 | <Compile Include="Parsing\EmptyToken.cs" /> |
|
80 | 82 | <Compile Include="Parsing\EndToken.cs" /> |
|
81 | 83 | <Compile Include="Parsing\EnumAlphabet.cs" /> |
|
82 | 84 | <Compile Include="Parsing\Grammar.cs" /> |
|
83 | 85 | <Compile Include="Parsing\IAlphabet.cs" /> |
|
84 | 86 | <Compile Include="Parsing\IDFADefinition.cs" /> |
|
85 | 87 | <Compile Include="Parsing\IVisitor.cs" /> |
|
86 | 88 | <Compile Include="Parsing\ParserException.cs" /> |
|
87 | 89 | <Compile Include="Parsing\Scanner.cs" /> |
|
88 | 90 | <Compile Include="Parsing\StarToken.cs" /> |
|
89 | 91 | <Compile Include="Parsing\SymbolToken.cs" /> |
|
90 | 92 | <Compile Include="Parsing\Token.cs" /> |
|
91 | 93 | <Compile Include="ServiceLocator.cs" /> |
|
92 | 94 | <Compile Include="TaskController.cs" /> |
|
93 | 95 | <Compile Include="ProgressInitEventArgs.cs" /> |
|
94 | 96 | <Compile Include="Properties\AssemblyInfo.cs" /> |
|
95 | 97 | <Compile Include="Promise.cs" /> |
|
96 | 98 | <Compile Include="Parallels\AsyncPool.cs" /> |
|
97 | 99 | <Compile Include="Safe.cs" /> |
|
98 | 100 | <Compile Include="ValueEventArgs.cs" /> |
|
99 | 101 | </ItemGroup> |
|
100 | 102 | <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> |
|
101 | 103 | <ItemGroup /> |
|
102 | 104 | </Project> No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now