##// END OF EJS Templates
fixed JSONXmlReader disposing under ugly mono...
cin -
r85:abe260860bd6 v2
parent child
Show More
@@ -1,122 +1,122
1 using Implab.Parallels;
1 using Implab.Parallels;
2 using System;
2 using System;
3 using System.Collections.Generic;
3 using System.Collections.Generic;
4 using System.Linq;
4 using System.Linq;
5 using System.Text;
5 using System.Text;
6 using System.Threading;
6 using System.Threading;
7 using System.Threading.Tasks;
7 using System.Threading.Tasks;
8 using System.Windows.Forms;
8 using System.Windows.Forms;
9
9
10 namespace Implab.Diagnostics.Interactive
10 namespace Implab.Diagnostics.Interactive
11 {
11 {
12 public class InteractiveListener: TextListenerBase
12 public class InteractiveListener: TextListenerBase
13 {
13 {
14 TraceForm m_form;
14 TraceForm m_form;
15
15
16 SynchronizationContext m_syncGuiThread;
16 SynchronizationContext m_syncGuiThread;
17 readonly Promise<object> m_guiStarted = new Promise<object>();
17 readonly Promise<object> m_guiStarted = new Promise<object>();
18
18
19 readonly IPromise m_guiFinished;
19 readonly IPromise m_guiFinished;
20 readonly IPromise m_workerFinished = new Promise<object>();
20 // readonly IPromise m_workerFinished = new Promise<object>();
21
21
22 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
22 readonly MTQueue<TraceViewItem> m_queue = new MTQueue<TraceViewItem>();
23 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
23 readonly AutoResetEvent m_queueEvent = new AutoResetEvent(false);
24
24
25 int m_queueLength;
25 int m_queueLength;
26 bool m_exitPending;
26 bool m_exitPending;
27
27
28 readonly object m_pauseLock = new object();
28 readonly object m_pauseLock = new object();
29 bool m_paused;
29 bool m_paused;
30 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
30 readonly ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
31
31
32 public InteractiveListener(bool global) : base(global) {
32 public InteractiveListener(bool global) : base(global) {
33 m_guiFinished = AsyncPool.InvokeNewThread(GuiThread);
33 m_guiFinished = AsyncPool.InvokeNewThread(GuiThread);
34 m_workerFinished = AsyncPool.InvokeNewThread(QueueThread);
34 /*m_workerFinished = */AsyncPool.InvokeNewThread(QueueThread);
35
35
36 m_guiStarted.Join();
36 m_guiStarted.Join();
37 }
37 }
38
38
39 void GuiThread() {
39 void GuiThread() {
40 m_form = new TraceForm(); // will create SynchronizationContext
40 m_form = new TraceForm(); // will create SynchronizationContext
41
41
42 m_form.PauseEvents += (s,a) => Pause();
42 m_form.PauseEvents += (s,a) => Pause();
43 m_form.ResumeEvents += (s, a) => Resume();
43 m_form.ResumeEvents += (s, a) => Resume();
44
44
45 m_syncGuiThread = SynchronizationContext.Current;
45 m_syncGuiThread = SynchronizationContext.Current;
46 m_guiStarted.Resolve();
46 m_guiStarted.Resolve();
47 Application.Run();
47 Application.Run();
48 }
48 }
49
49
50 void QueueThread() {
50 void QueueThread() {
51 while (!m_exitPending) {
51 while (!m_exitPending) {
52 if (m_paused)
52 if (m_paused)
53 m_pauseEvent.WaitOne();
53 m_pauseEvent.WaitOne();
54
54
55 TraceViewItem item;
55 TraceViewItem item;
56 if (m_queue.TryDequeue(out item)) {
56 if (m_queue.TryDequeue(out item)) {
57 Interlocked.Decrement(ref m_queueLength);
57 Interlocked.Decrement(ref m_queueLength);
58
58
59 m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null);
59 m_syncGuiThread.Send(x => m_form.AddTraceEvent(item),null);
60 } else {
60 } else {
61 m_queueEvent.WaitOne();
61 m_queueEvent.WaitOne();
62 }
62 }
63 }
63 }
64 }
64 }
65
65
66 public void Pause() {
66 public void Pause() {
67 // for consistency we need to set this properties atomically
67 // for consistency we need to set this properties atomically
68 lock (m_pauseLock) {
68 lock (m_pauseLock) {
69 m_pauseEvent.Reset();
69 m_pauseEvent.Reset();
70 m_paused = true;
70 m_paused = true;
71 }
71 }
72 }
72 }
73
73
74 public void Resume() {
74 public void Resume() {
75 // for consistency we need to set this properties atomically
75 // for consistency we need to set this properties atomically
76 lock (m_pauseLock) {
76 lock (m_pauseLock) {
77 m_paused = false;
77 m_paused = false;
78 m_pauseEvent.Set();
78 m_pauseEvent.Set();
79 }
79 }
80 }
80 }
81
81
82 void Enqueue(TraceViewItem item) {
82 void Enqueue(TraceViewItem item) {
83 m_queue.Enqueue(item);
83 m_queue.Enqueue(item);
84 if (Interlocked.Increment(ref m_queueLength) == 1)
84 if (Interlocked.Increment(ref m_queueLength) == 1)
85 m_queueEvent.Set();
85 m_queueEvent.Set();
86 }
86 }
87
87
88 public void ShowForm() {
88 public void ShowForm() {
89 m_syncGuiThread.Post(x => m_form.Show(), null);
89 m_syncGuiThread.Post(x => m_form.Show(), null);
90 }
90 }
91
91
92 public void HideForm() {
92 public void HideForm() {
93 m_syncGuiThread.Post(x => m_form.Hide(), null);
93 m_syncGuiThread.Post(x => m_form.Hide(), null);
94 }
94 }
95
95
96 void Terminate() {
96 void Terminate() {
97 m_exitPending = true;
97 m_exitPending = true;
98 Resume();
98 Resume();
99 m_syncGuiThread.Post(x => Application.ExitThread(), null);
99 m_syncGuiThread.Post(x => Application.ExitThread(), null);
100 }
100 }
101
101
102 protected override void Dispose(bool disposing) {
102 protected override void Dispose(bool disposing) {
103 if (disposing) {
103 if (disposing) {
104 Terminate();
104 Terminate();
105 m_guiFinished.Join();
105 m_guiFinished.Join();
106 }
106 }
107 base.Dispose(disposing);
107 base.Dispose(disposing);
108 }
108 }
109
109
110 protected override void WriteEntry(TraceContext context, EventText text, string channel) {
110 protected override void WriteEntry(TraceContext context, EventText text, string channel) {
111 var item = new TraceViewItem {
111 var item = new TraceViewItem {
112 Indent = text.indent,
112 Indent = text.indent,
113 Message = text.content,
113 Message = text.content,
114 Thread = context.ThreadId,
114 Thread = context.ThreadId,
115 Channel = channel,
115 Channel = channel,
116 Timestamp = Environment.TickCount
116 Timestamp = Environment.TickCount
117 };
117 };
118
118
119 Enqueue(item);
119 Enqueue(item);
120 }
120 }
121 }
121 }
122 }
122 }
@@ -1,198 +1,217
1 ο»Ώ<?xml version="1.0" encoding="utf-8"?>
1 ο»Ώ<?xml version="1.0" encoding="utf-8"?>
2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2 <Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3 <PropertyGroup>
3 <PropertyGroup>
4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4 <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
5 <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
6 <ProjectGuid>{F550F1F8-8746-4AD0-9614-855F4C4B7F05}</ProjectGuid>
7 <OutputType>Library</OutputType>
7 <OutputType>Library</OutputType>
8 <RootNamespace>Implab</RootNamespace>
8 <RootNamespace>Implab</RootNamespace>
9 <AssemblyName>Implab</AssemblyName>
9 <AssemblyName>Implab</AssemblyName>
10 </PropertyGroup>
10 </PropertyGroup>
11 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
11 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
12 <DebugSymbols>true</DebugSymbols>
12 <DebugSymbols>true</DebugSymbols>
13 <DebugType>full</DebugType>
13 <DebugType>full</DebugType>
14 <Optimize>false</Optimize>
14 <Optimize>false</Optimize>
15 <OutputPath>bin\Debug</OutputPath>
15 <OutputPath>bin\Debug</OutputPath>
16 <DefineConstants>TRACE;DEBUG;</DefineConstants>
16 <DefineConstants>TRACE;DEBUG;</DefineConstants>
17 <ErrorReport>prompt</ErrorReport>
17 <ErrorReport>prompt</ErrorReport>
18 <WarningLevel>4</WarningLevel>
18 <WarningLevel>4</WarningLevel>
19 <ConsolePause>false</ConsolePause>
19 <ConsolePause>false</ConsolePause>
20 <RunCodeAnalysis>true</RunCodeAnalysis>
20 <RunCodeAnalysis>true</RunCodeAnalysis>
21 </PropertyGroup>
21 </PropertyGroup>
22 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
22 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
23 <DebugType>full</DebugType>
23 <DebugType>full</DebugType>
24 <Optimize>true</Optimize>
24 <Optimize>true</Optimize>
25 <OutputPath>bin\Release</OutputPath>
25 <OutputPath>bin\Release</OutputPath>
26 <ErrorReport>prompt</ErrorReport>
26 <ErrorReport>prompt</ErrorReport>
27 <WarningLevel>4</WarningLevel>
27 <WarningLevel>4</WarningLevel>
28 <ConsolePause>false</ConsolePause>
28 <ConsolePause>false</ConsolePause>
29 </PropertyGroup>
29 </PropertyGroup>
30 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
30 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug 4.5|AnyCPU' ">
31 <DebugSymbols>true</DebugSymbols>
31 <DebugSymbols>true</DebugSymbols>
32 <DebugType>full</DebugType>
32 <DebugType>full</DebugType>
33 <Optimize>false</Optimize>
33 <Optimize>false</Optimize>
34 <OutputPath>bin\Debug</OutputPath>
34 <OutputPath>bin\Debug</OutputPath>
35 <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants>
35 <DefineConstants>TRACE;DEBUG;NET_4_5</DefineConstants>
36 <ErrorReport>prompt</ErrorReport>
36 <ErrorReport>prompt</ErrorReport>
37 <WarningLevel>4</WarningLevel>
37 <WarningLevel>4</WarningLevel>
38 <RunCodeAnalysis>true</RunCodeAnalysis>
38 <RunCodeAnalysis>true</RunCodeAnalysis>
39 <ConsolePause>false</ConsolePause>
39 <ConsolePause>false</ConsolePause>
40 </PropertyGroup>
40 </PropertyGroup>
41 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
41 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
42 <Optimize>true</Optimize>
42 <Optimize>true</Optimize>
43 <OutputPath>bin\Release</OutputPath>
43 <OutputPath>bin\Release</OutputPath>
44 <ErrorReport>prompt</ErrorReport>
44 <ErrorReport>prompt</ErrorReport>
45 <WarningLevel>4</WarningLevel>
45 <WarningLevel>4</WarningLevel>
46 <ConsolePause>false</ConsolePause>
46 <ConsolePause>false</ConsolePause>
47 <DefineConstants>NET_4_5</DefineConstants>
47 <DefineConstants>NET_4_5</DefineConstants>
48 </PropertyGroup>
48 </PropertyGroup>
49 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'DebugMono|AnyCPU' ">
50 <DebugSymbols>true</DebugSymbols>
51 <DebugType>full</DebugType>
52 <Optimize>false</Optimize>
53 <OutputPath>bin\Debug</OutputPath>
54 <DefineConstants>TRACE;DEBUG;NET_4_5;MONO</DefineConstants>
55 <ErrorReport>prompt</ErrorReport>
56 <WarningLevel>4</WarningLevel>
57 <RunCodeAnalysis>true</RunCodeAnalysis>
58 <ConsolePause>false</ConsolePause>
59 </PropertyGroup>
60 <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'ReleaseMono|AnyCPU' ">
61 <Optimize>true</Optimize>
62 <OutputPath>bin\Release</OutputPath>
63 <DefineConstants>NET_4_5;MONO;</DefineConstants>
64 <ErrorReport>prompt</ErrorReport>
65 <WarningLevel>4</WarningLevel>
66 <ConsolePause>false</ConsolePause>
67 </PropertyGroup>
49 <ItemGroup>
68 <ItemGroup>
50 <Reference Include="System" />
69 <Reference Include="System" />
51 <Reference Include="System.Xml" />
70 <Reference Include="System.Xml" />
52 </ItemGroup>
71 </ItemGroup>
53 <ItemGroup>
72 <ItemGroup>
54 <Compile Include="Component.cs" />
73 <Compile Include="Component.cs" />
55 <Compile Include="CustomEqualityComparer.cs" />
74 <Compile Include="CustomEqualityComparer.cs" />
56 <Compile Include="Diagnostics\ConsoleTraceListener.cs" />
75 <Compile Include="Diagnostics\ConsoleTraceListener.cs" />
57 <Compile Include="Diagnostics\EventText.cs" />
76 <Compile Include="Diagnostics\EventText.cs" />
58 <Compile Include="Diagnostics\IEventTextFormatter.cs" />
77 <Compile Include="Diagnostics\IEventTextFormatter.cs" />
59 <Compile Include="Diagnostics\LogChannel.cs" />
78 <Compile Include="Diagnostics\LogChannel.cs" />
60 <Compile Include="Diagnostics\LogicalOperation.cs" />
79 <Compile Include="Diagnostics\LogicalOperation.cs" />
61 <Compile Include="Diagnostics\TextFileListener.cs" />
80 <Compile Include="Diagnostics\TextFileListener.cs" />
62 <Compile Include="Diagnostics\TextListenerBase.cs" />
81 <Compile Include="Diagnostics\TextListenerBase.cs" />
63 <Compile Include="Diagnostics\TraceLog.cs" />
82 <Compile Include="Diagnostics\TraceLog.cs" />
64 <Compile Include="Diagnostics\TraceContext.cs" />
83 <Compile Include="Diagnostics\TraceContext.cs" />
65 <Compile Include="Diagnostics\TraceEvent.cs" />
84 <Compile Include="Diagnostics\TraceEvent.cs" />
66 <Compile Include="Diagnostics\TraceEventType.cs" />
85 <Compile Include="Diagnostics\TraceEventType.cs" />
67 <Compile Include="Disposable.cs" />
86 <Compile Include="Disposable.cs" />
68 <Compile Include="ICancellable.cs" />
87 <Compile Include="ICancellable.cs" />
69 <Compile Include="IProgressHandler.cs" />
88 <Compile Include="IProgressHandler.cs" />
70 <Compile Include="IProgressNotifier.cs" />
89 <Compile Include="IProgressNotifier.cs" />
71 <Compile Include="IPromiseT.cs" />
90 <Compile Include="IPromiseT.cs" />
72 <Compile Include="IPromise.cs" />
91 <Compile Include="IPromise.cs" />
73 <Compile Include="IServiceLocator.cs" />
92 <Compile Include="IServiceLocator.cs" />
74 <Compile Include="ITaskController.cs" />
93 <Compile Include="ITaskController.cs" />
75 <Compile Include="JSON\JSONElementContext.cs" />
94 <Compile Include="JSON\JSONElementContext.cs" />
76 <Compile Include="JSON\JSONElementType.cs" />
95 <Compile Include="JSON\JSONElementType.cs" />
77 <Compile Include="JSON\JSONGrammar.cs" />
96 <Compile Include="JSON\JSONGrammar.cs" />
78 <Compile Include="JSON\JSONParser.cs" />
97 <Compile Include="JSON\JSONParser.cs" />
79 <Compile Include="JSON\JSONScanner.cs" />
98 <Compile Include="JSON\JSONScanner.cs" />
80 <Compile Include="JSON\JsonTokenType.cs" />
99 <Compile Include="JSON\JsonTokenType.cs" />
81 <Compile Include="JSON\JSONWriter.cs" />
100 <Compile Include="JSON\JSONWriter.cs" />
82 <Compile Include="JSON\JSONXmlReader.cs" />
101 <Compile Include="JSON\JSONXmlReader.cs" />
83 <Compile Include="JSON\JSONXmlReaderOptions.cs" />
102 <Compile Include="JSON\JSONXmlReaderOptions.cs" />
84 <Compile Include="JSON\StringTranslator.cs" />
103 <Compile Include="JSON\StringTranslator.cs" />
85 <Compile Include="Parallels\DispatchPool.cs" />
104 <Compile Include="Parallels\DispatchPool.cs" />
86 <Compile Include="Parallels\ArrayTraits.cs" />
105 <Compile Include="Parallels\ArrayTraits.cs" />
87 <Compile Include="Parallels\MTQueue.cs" />
106 <Compile Include="Parallels\MTQueue.cs" />
88 <Compile Include="Parallels\WorkerPool.cs" />
107 <Compile Include="Parallels\WorkerPool.cs" />
89 <Compile Include="Parsing\Alphabet.cs" />
108 <Compile Include="Parsing\Alphabet.cs" />
90 <Compile Include="Parsing\AlphabetBase.cs" />
109 <Compile Include="Parsing\AlphabetBase.cs" />
91 <Compile Include="Parsing\AltToken.cs" />
110 <Compile Include="Parsing\AltToken.cs" />
92 <Compile Include="Parsing\BinaryToken.cs" />
111 <Compile Include="Parsing\BinaryToken.cs" />
93 <Compile Include="Parsing\CatToken.cs" />
112 <Compile Include="Parsing\CatToken.cs" />
94 <Compile Include="Parsing\CDFADefinition.cs" />
113 <Compile Include="Parsing\CDFADefinition.cs" />
95 <Compile Include="Parsing\DFABuilder.cs" />
114 <Compile Include="Parsing\DFABuilder.cs" />
96 <Compile Include="Parsing\DFADefinitionBase.cs" />
115 <Compile Include="Parsing\DFADefinitionBase.cs" />
97 <Compile Include="Parsing\DFAStateDescriptor.cs" />
116 <Compile Include="Parsing\DFAStateDescriptor.cs" />
98 <Compile Include="Parsing\DFAutomaton.cs" />
117 <Compile Include="Parsing\DFAutomaton.cs" />
99 <Compile Include="Parsing\EDFADefinition.cs" />
118 <Compile Include="Parsing\EDFADefinition.cs" />
100 <Compile Include="Parsing\EmptyToken.cs" />
119 <Compile Include="Parsing\EmptyToken.cs" />
101 <Compile Include="Parsing\EndToken.cs" />
120 <Compile Include="Parsing\EndToken.cs" />
102 <Compile Include="Parsing\EnumAlphabet.cs" />
121 <Compile Include="Parsing\EnumAlphabet.cs" />
103 <Compile Include="Parsing\Grammar.cs" />
122 <Compile Include="Parsing\Grammar.cs" />
104 <Compile Include="Parsing\IAlphabet.cs" />
123 <Compile Include="Parsing\IAlphabet.cs" />
105 <Compile Include="Parsing\IDFADefinition.cs" />
124 <Compile Include="Parsing\IDFADefinition.cs" />
106 <Compile Include="Parsing\IVisitor.cs" />
125 <Compile Include="Parsing\IVisitor.cs" />
107 <Compile Include="Parsing\ParserException.cs" />
126 <Compile Include="Parsing\ParserException.cs" />
108 <Compile Include="Parsing\Scanner.cs" />
127 <Compile Include="Parsing\Scanner.cs" />
109 <Compile Include="Parsing\StarToken.cs" />
128 <Compile Include="Parsing\StarToken.cs" />
110 <Compile Include="Parsing\SymbolToken.cs" />
129 <Compile Include="Parsing\SymbolToken.cs" />
111 <Compile Include="Parsing\Token.cs" />
130 <Compile Include="Parsing\Token.cs" />
112 <Compile Include="SafePool.cs" />
131 <Compile Include="SafePool.cs" />
113 <Compile Include="ServiceLocator.cs" />
132 <Compile Include="ServiceLocator.cs" />
114 <Compile Include="TaskController.cs" />
133 <Compile Include="TaskController.cs" />
115 <Compile Include="ProgressInitEventArgs.cs" />
134 <Compile Include="ProgressInitEventArgs.cs" />
116 <Compile Include="Properties\AssemblyInfo.cs" />
135 <Compile Include="Properties\AssemblyInfo.cs" />
117 <Compile Include="Promise.cs" />
136 <Compile Include="Promise.cs" />
118 <Compile Include="Parallels\AsyncPool.cs" />
137 <Compile Include="Parallels\AsyncPool.cs" />
119 <Compile Include="Safe.cs" />
138 <Compile Include="Safe.cs" />
120 <Compile Include="ValueEventArgs.cs" />
139 <Compile Include="ValueEventArgs.cs" />
121 <Compile Include="PromiseExtensions.cs" />
140 <Compile Include="PromiseExtensions.cs" />
122 <Compile Include="TransientPromiseException.cs" />
141 <Compile Include="TransientPromiseException.cs" />
123 <Compile Include="SyncContextPromise.cs" />
142 <Compile Include="SyncContextPromise.cs" />
124 <Compile Include="ObjectPool.cs" />
143 <Compile Include="ObjectPool.cs" />
125 <Compile Include="ObjectPoolWrapper.cs" />
144 <Compile Include="ObjectPoolWrapper.cs" />
126 </ItemGroup>
145 </ItemGroup>
127 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
146 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
128 <ItemGroup />
147 <ItemGroup />
129 <ProjectExtensions>
148 <ProjectExtensions>
130 <MonoDevelop>
149 <MonoDevelop>
131 <Properties>
150 <Properties>
132 <Policies>
151 <Policies>
133 <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
152 <CSharpFormattingPolicy IndentSwitchBody="True" NamespaceBraceStyle="EndOfLine" ClassBraceStyle="EndOfLine" InterfaceBraceStyle="EndOfLine" StructBraceStyle="EndOfLine" EnumBraceStyle="EndOfLine" MethodBraceStyle="EndOfLine" ConstructorBraceStyle="EndOfLine" DestructorBraceStyle="EndOfLine" BeforeMethodDeclarationParentheses="False" BeforeMethodCallParentheses="False" BeforeConstructorDeclarationParentheses="False" NewLineBeforeConstructorInitializerColon="NewLine" NewLineAfterConstructorInitializerColon="SameLine" BeforeIndexerDeclarationBracket="False" BeforeDelegateDeclarationParentheses="False" NewParentheses="False" SpacesBeforeBrackets="False" inheritsSet="Mono" inheritsScope="text/x-csharp" scope="text/x-csharp" />
134 <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
153 <TextStylePolicy FileWidth="120" EolMarker="Unix" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/x-csharp" />
135 <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" />
154 <DotNetNamingPolicy DirectoryNamespaceAssociation="PrefixedHierarchical" ResourceNamePolicy="MSBuild" />
136 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" />
155 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="application/xml" />
137 <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" />
156 <XmlFormattingPolicy inheritsSet="Mono" inheritsScope="application/xml" scope="application/xml" />
138 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" />
157 <TextStylePolicy FileWidth="120" TabsToSpaces="False" inheritsSet="VisualStudio" inheritsScope="text/plain" scope="text/plain" />
139 <NameConventionPolicy>
158 <NameConventionPolicy>
140 <Rules>
159 <Rules>
141 <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
160 <NamingRule Name="Namespaces" AffectedEntity="Namespace" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
142 <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
161 <NamingRule Name="Types" AffectedEntity="Class, Struct, Enum, Delegate" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
143 <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
162 <NamingRule Name="Interfaces" AffectedEntity="Interface" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
144 <RequiredPrefixes>
163 <RequiredPrefixes>
145 <String>I</String>
164 <String>I</String>
146 </RequiredPrefixes>
165 </RequiredPrefixes>
147 </NamingRule>
166 </NamingRule>
148 <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
167 <NamingRule Name="Attributes" AffectedEntity="CustomAttributes" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
149 <RequiredSuffixes>
168 <RequiredSuffixes>
150 <String>Attribute</String>
169 <String>Attribute</String>
151 </RequiredSuffixes>
170 </RequiredSuffixes>
152 </NamingRule>
171 </NamingRule>
153 <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
172 <NamingRule Name="Event Arguments" AffectedEntity="CustomEventArgs" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
154 <RequiredSuffixes>
173 <RequiredSuffixes>
155 <String>EventArgs</String>
174 <String>EventArgs</String>
156 </RequiredSuffixes>
175 </RequiredSuffixes>
157 </NamingRule>
176 </NamingRule>
158 <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
177 <NamingRule Name="Exceptions" AffectedEntity="CustomExceptions" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
159 <RequiredSuffixes>
178 <RequiredSuffixes>
160 <String>Exception</String>
179 <String>Exception</String>
161 </RequiredSuffixes>
180 </RequiredSuffixes>
162 </NamingRule>
181 </NamingRule>
163 <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
182 <NamingRule Name="Methods" AffectedEntity="Methods" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
164 <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" />
183 <NamingRule Name="Static Readonly Fields" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Protected, Public" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True" />
165 <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
184 <NamingRule Name="Fields (Non Private)" AffectedEntity="Field" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
166 <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" />
185 <NamingRule Name="ReadOnly Fields (Non Private)" AffectedEntity="ReadonlyField" VisibilityMask="Internal, Public" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False" />
167 <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
186 <NamingRule Name="Fields (Private)" AffectedEntity="Field, ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
168 <RequiredPrefixes>
187 <RequiredPrefixes>
169 <String>m_</String>
188 <String>m_</String>
170 </RequiredPrefixes>
189 </RequiredPrefixes>
171 </NamingRule>
190 </NamingRule>
172 <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True">
191 <NamingRule Name="Static Fields (Private)" AffectedEntity="Field" VisibilityMask="Private" NamingStyle="CamelCase" IncludeInstanceMembers="False" IncludeStaticEntities="True">
173 <RequiredPrefixes>
192 <RequiredPrefixes>
174 <String>_</String>
193 <String>_</String>
175 </RequiredPrefixes>
194 </RequiredPrefixes>
176 </NamingRule>
195 </NamingRule>
177 <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
196 <NamingRule Name="ReadOnly Fields (Private)" AffectedEntity="ReadonlyField" VisibilityMask="Private, Protected" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="False">
178 <RequiredPrefixes>
197 <RequiredPrefixes>
179 <String>m_</String>
198 <String>m_</String>
180 </RequiredPrefixes>
199 </RequiredPrefixes>
181 </NamingRule>
200 </NamingRule>
182 <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
201 <NamingRule Name="Constant Fields" AffectedEntity="ConstantField" VisibilityMask="VisibilityMask" NamingStyle="AllUpper" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
183 <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
202 <NamingRule Name="Properties" AffectedEntity="Property" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
184 <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
203 <NamingRule Name="Events" AffectedEntity="Event" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
185 <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
204 <NamingRule Name="Enum Members" AffectedEntity="EnumMember" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
186 <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
205 <NamingRule Name="Parameters" AffectedEntity="Parameter, LocalVariable" VisibilityMask="VisibilityMask" NamingStyle="CamelCase" IncludeInstanceMembers="True" IncludeStaticEntities="True" />
187 <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
206 <NamingRule Name="Type Parameters" AffectedEntity="TypeParameter" VisibilityMask="VisibilityMask" NamingStyle="PascalCase" IncludeInstanceMembers="True" IncludeStaticEntities="True">
188 <RequiredPrefixes>
207 <RequiredPrefixes>
189 <String>T</String>
208 <String>T</String>
190 </RequiredPrefixes>
209 </RequiredPrefixes>
191 </NamingRule>
210 </NamingRule>
192 </Rules>
211 </Rules>
193 </NameConventionPolicy>
212 </NameConventionPolicy>
194 </Policies>
213 </Policies>
195 </Properties>
214 </Properties>
196 </MonoDevelop>
215 </MonoDevelop>
197 </ProjectExtensions>
216 </ProjectExtensions>
198 </Project> No newline at end of file
217 </Project>
@@ -1,340 +1,343
1 using Implab;
1 using Implab;
2 using Implab.Parsing;
2 using Implab.Parsing;
3 using System;
3 using System;
4 using System.Collections.Generic;
4 using System.Collections.Generic;
5 using System.Globalization;
5 using System.Globalization;
6 using System.IO;
6 using System.IO;
7 using System.Linq;
7 using System.Linq;
8 using System.Text;
8 using System.Text;
9 using System.Threading.Tasks;
9 using System.Threading.Tasks;
10 using System.Xml;
10 using System.Xml;
11
11
12 namespace Implab.JSON {
12 namespace Implab.JSON {
13 public class JSONXmlReader : XmlReader {
13 public class JSONXmlReader : XmlReader {
14
14
15 enum ValueContext {
15 enum ValueContext {
16 Undefined,
16 Undefined,
17 ElementStart,
17 ElementStart,
18 ElementValue,
18 ElementValue,
19 ElementEnd,
19 ElementEnd,
20 ElementEmpty
20 ElementEmpty
21 }
21 }
22
22
23 struct LocalNameContext {
23 struct LocalNameContext {
24 public string localName;
24 public string localName;
25 public bool isArray;
25 public bool isArray;
26 }
26 }
27
27
28 JSONParser m_parser;
28 JSONParser m_parser;
29 ValueContext m_valueContext;
29 ValueContext m_valueContext;
30 ReadState m_state = ReadState.Initial;
30 ReadState m_state = ReadState.Initial;
31 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>();
31 Stack<LocalNameContext> m_localNameStack = new Stack<LocalNameContext>();
32 LocalNameContext m_localName;
32 LocalNameContext m_localName;
33 int m_depthCorrection = 0;
33 int m_depthCorrection = 0;
34
34
35 readonly string m_rootName;
35 readonly string m_rootName;
36 readonly string m_prefix;
36 readonly string m_prefix;
37 readonly string m_namespaceUri;
37 readonly string m_namespaceUri;
38 readonly bool m_flattenArrays;
38 readonly bool m_flattenArrays;
39 readonly string m_arrayItemName;
39 readonly string m_arrayItemName;
40 readonly XmlNameTable m_nameTable;
40 readonly XmlNameTable m_nameTable;
41
41
42 JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
42 JSONXmlReader(JSONParser parser, JSONXmlReaderOptions options) {
43 m_parser = parser;
43 m_parser = parser;
44
44
45 if (options != null) {
45 if (options != null) {
46 m_prefix = options.NodesPrefix ?? String.Empty;
46 m_prefix = options.NodesPrefix ?? String.Empty;
47 m_namespaceUri = options.NamespaceURI ?? String.Empty;
47 m_namespaceUri = options.NamespaceURI ?? String.Empty;
48 m_rootName = options.RootName ?? "json";
48 m_rootName = options.RootName ?? "json";
49 m_flattenArrays = options.FlattenArrays;
49 m_flattenArrays = options.FlattenArrays;
50 m_arrayItemName = options.ArrayItemName ?? "item";
50 m_arrayItemName = options.ArrayItemName ?? "item";
51 m_nameTable = options.NameTable ?? new NameTable();
51 m_nameTable = options.NameTable ?? new NameTable();
52 } else {
52 } else {
53 m_prefix = String.Empty;
53 m_prefix = String.Empty;
54 m_namespaceUri = String.Empty;
54 m_namespaceUri = String.Empty;
55 m_rootName = "json";
55 m_rootName = "json";
56 m_flattenArrays = false;
56 m_flattenArrays = false;
57 m_arrayItemName = "item";
57 m_arrayItemName = "item";
58 m_nameTable = new NameTable();
58 m_nameTable = new NameTable();
59 }
59 }
60 }
60 }
61
61
62 /// <summary>
62 /// <summary>
63 /// Always 0, JSON doesn't support attributes
63 /// Always 0, JSON doesn't support attributes
64 /// </summary>
64 /// </summary>
65 public override int AttributeCount {
65 public override int AttributeCount {
66 get { return 0; }
66 get { return 0; }
67 }
67 }
68
68
69 public override string BaseURI {
69 public override string BaseURI {
70 get { return String.Empty; }
70 get { return String.Empty; }
71 }
71 }
72
72
73 public override int Depth {
73 public override int Depth {
74 get {
74 get {
75 return m_localNameStack.Count + m_depthCorrection;
75 return m_localNameStack.Count + m_depthCorrection;
76 }
76 }
77 }
77 }
78
78
79 public override bool EOF {
79 public override bool EOF {
80 get { return m_parser.EOF; }
80 get { return m_parser.EOF; }
81 }
81 }
82
82
83 /// <summary>
83 /// <summary>
84 /// Always throws an exception
84 /// Always throws an exception
85 /// </summary>
85 /// </summary>
86 /// <param name="i"></param>
86 /// <param name="i"></param>
87 /// <returns></returns>
87 /// <returns></returns>
88 public override string GetAttribute(int i) {
88 public override string GetAttribute(int i) {
89 throw new ArgumentOutOfRangeException();
89 throw new ArgumentOutOfRangeException();
90 }
90 }
91
91
92 /// <summary>
92 /// <summary>
93 /// Always returns empty string
93 /// Always returns empty string
94 /// </summary>
94 /// </summary>
95 /// <param name="name"></param>
95 /// <param name="name"></param>
96 /// <param name="namespaceURI"></param>
96 /// <param name="namespaceURI"></param>
97 /// <returns></returns>
97 /// <returns></returns>
98 public override string GetAttribute(string name, string namespaceURI) {
98 public override string GetAttribute(string name, string namespaceURI) {
99 return String.Empty;
99 return String.Empty;
100 }
100 }
101
101
102 /// <summary>
102 /// <summary>
103 /// Always returns empty string
103 /// Always returns empty string
104 /// </summary>
104 /// </summary>
105 /// <param name="name"></param>
105 /// <param name="name"></param>
106 /// <returns></returns>
106 /// <returns></returns>
107 public override string GetAttribute(string name) {
107 public override string GetAttribute(string name) {
108 return String.Empty;
108 return String.Empty;
109 }
109 }
110
110
111 public override bool IsEmptyElement {
111 public override bool IsEmptyElement {
112 get { return m_parser.ElementType == JSONElementType.Value && m_valueContext == ValueContext.ElementEmpty; }
112 get { return m_parser.ElementType == JSONElementType.Value && m_valueContext == ValueContext.ElementEmpty; }
113 }
113 }
114
114
115 public override string LocalName {
115 public override string LocalName {
116 get { return m_localName.localName; }
116 get { return m_localName.localName; }
117 }
117 }
118
118
119 public override string LookupNamespace(string prefix) {
119 public override string LookupNamespace(string prefix) {
120 if (String.IsNullOrEmpty(prefix) || prefix == m_prefix)
120 if (String.IsNullOrEmpty(prefix) || prefix == m_prefix)
121 return m_namespaceUri;
121 return m_namespaceUri;
122 else
122 else
123 return String.Empty;
123 return String.Empty;
124 }
124 }
125
125
126 public override bool MoveToAttribute(string name, string ns) {
126 public override bool MoveToAttribute(string name, string ns) {
127 return false;
127 return false;
128 }
128 }
129
129
130 public override bool MoveToAttribute(string name) {
130 public override bool MoveToAttribute(string name) {
131 return false;
131 return false;
132 }
132 }
133
133
134 public override bool MoveToElement() {
134 public override bool MoveToElement() {
135 return false;
135 return false;
136 }
136 }
137
137
138 public override bool MoveToFirstAttribute() {
138 public override bool MoveToFirstAttribute() {
139 return false;
139 return false;
140 }
140 }
141
141
142 public override bool MoveToNextAttribute() {
142 public override bool MoveToNextAttribute() {
143 return false;
143 return false;
144 }
144 }
145
145
146 public override XmlNameTable NameTable {
146 public override XmlNameTable NameTable {
147 get { return m_nameTable; }
147 get { return m_nameTable; }
148 }
148 }
149
149
150 public override string NamespaceURI {
150 public override string NamespaceURI {
151 get { return m_namespaceUri; }
151 get { return m_namespaceUri; }
152 }
152 }
153
153
154 public override XmlNodeType NodeType {
154 public override XmlNodeType NodeType {
155 get {
155 get {
156 switch (m_parser.ElementType) {
156 switch (m_parser.ElementType) {
157 case JSONElementType.BeginObject:
157 case JSONElementType.BeginObject:
158 case JSONElementType.BeginArray:
158 case JSONElementType.BeginArray:
159 return XmlNodeType.Element;
159 return XmlNodeType.Element;
160 case JSONElementType.EndObject:
160 case JSONElementType.EndObject:
161 case JSONElementType.EndArray:
161 case JSONElementType.EndArray:
162 return XmlNodeType.EndElement;
162 return XmlNodeType.EndElement;
163 case JSONElementType.Value:
163 case JSONElementType.Value:
164 switch (m_valueContext) {
164 switch (m_valueContext) {
165 case ValueContext.ElementStart:
165 case ValueContext.ElementStart:
166 case ValueContext.ElementEmpty:
166 case ValueContext.ElementEmpty:
167 return XmlNodeType.Element;
167 return XmlNodeType.Element;
168 case ValueContext.ElementValue:
168 case ValueContext.ElementValue:
169 return XmlNodeType.Text;
169 return XmlNodeType.Text;
170 case ValueContext.ElementEnd:
170 case ValueContext.ElementEnd:
171 return XmlNodeType.EndElement;
171 return XmlNodeType.EndElement;
172 default:
172 default:
173 throw new InvalidOperationException();
173 throw new InvalidOperationException();
174 }
174 }
175 default:
175 default:
176 throw new InvalidOperationException();
176 throw new InvalidOperationException();
177 }
177 }
178 }
178 }
179 }
179 }
180
180
181 public override string Prefix {
181 public override string Prefix {
182 get { return m_prefix; }
182 get { return m_prefix; }
183 }
183 }
184
184
185 public override bool Read() {
185 public override bool Read() {
186 if (m_state != System.Xml.ReadState.Interactive && m_state != System.Xml.ReadState.Initial)
186 if (m_state != System.Xml.ReadState.Interactive && m_state != System.Xml.ReadState.Initial)
187 return false;
187 return false;
188
188
189 if (m_state == ReadState.Initial)
189 if (m_state == ReadState.Initial)
190 m_state = System.Xml.ReadState.Interactive;
190 m_state = System.Xml.ReadState.Interactive;
191
191
192 try {
192 try {
193 switch (m_parser.ElementType) {
193 switch (m_parser.ElementType) {
194 case JSONElementType.Value:
194 case JSONElementType.Value:
195 switch (m_valueContext) {
195 switch (m_valueContext) {
196 case ValueContext.ElementStart:
196 case ValueContext.ElementStart:
197 SetLocalName(String.Empty);
197 SetLocalName(String.Empty);
198 m_valueContext = ValueContext.ElementValue;
198 m_valueContext = ValueContext.ElementValue;
199 return true;
199 return true;
200 case ValueContext.ElementValue:
200 case ValueContext.ElementValue:
201 RestoreLocalName();
201 RestoreLocalName();
202 m_valueContext = ValueContext.ElementEnd;
202 m_valueContext = ValueContext.ElementEnd;
203 return true;
203 return true;
204 case ValueContext.ElementEmpty:
204 case ValueContext.ElementEmpty:
205 case ValueContext.ElementEnd:
205 case ValueContext.ElementEnd:
206 RestoreLocalName();
206 RestoreLocalName();
207 break;
207 break;
208 }
208 }
209 break;
209 break;
210 case JSONElementType.EndArray:
210 case JSONElementType.EndArray:
211 case JSONElementType.EndObject:
211 case JSONElementType.EndObject:
212 RestoreLocalName();
212 RestoreLocalName();
213 break;
213 break;
214 }
214 }
215 string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName;
215 string itemName = m_parser.ElementType == JSONElementType.None ? m_rootName : m_flattenArrays ? m_localName.localName : m_arrayItemName;
216 while (m_parser.Read()) {
216 while (m_parser.Read()) {
217 if (!String.IsNullOrEmpty(m_parser.ElementName))
217 if (!String.IsNullOrEmpty(m_parser.ElementName))
218 itemName = m_parser.ElementName;
218 itemName = m_parser.ElementName;
219
219
220 switch (m_parser.ElementType) {
220 switch (m_parser.ElementType) {
221 case JSONElementType.BeginArray:
221 case JSONElementType.BeginArray:
222 if (m_flattenArrays && !m_localName.isArray) {
222 if (m_flattenArrays && !m_localName.isArray) {
223 m_depthCorrection--;
223 m_depthCorrection--;
224 SetLocalName(itemName, true);
224 SetLocalName(itemName, true);
225 continue;
225 continue;
226 } else {
226 } else {
227 SetLocalName(itemName, true);
227 SetLocalName(itemName, true);
228 }
228 }
229 break;
229 break;
230 case JSONElementType.BeginObject:
230 case JSONElementType.BeginObject:
231 SetLocalName(itemName);
231 SetLocalName(itemName);
232 break;
232 break;
233 case JSONElementType.EndArray:
233 case JSONElementType.EndArray:
234 if (m_flattenArrays && !m_localNameStack.Peek().isArray) {
234 if (m_flattenArrays && !m_localNameStack.Peek().isArray) {
235 RestoreLocalName();
235 RestoreLocalName();
236 m_depthCorrection++;
236 m_depthCorrection++;
237 continue;
237 continue;
238 }
238 }
239 break;
239 break;
240 case JSONElementType.EndObject:
240 case JSONElementType.EndObject:
241 break;
241 break;
242 case JSONElementType.Value:
242 case JSONElementType.Value:
243 SetLocalName(itemName);
243 SetLocalName(itemName);
244 m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart;
244 m_valueContext = m_parser.ElementValue == null ? ValueContext.ElementEmpty : ValueContext.ElementStart;
245 break;
245 break;
246 default:
246 default:
247 break;
247 break;
248 }
248 }
249 return true;
249 return true;
250 }
250 }
251
251
252 m_state = System.Xml.ReadState.EndOfFile;
252 m_state = System.Xml.ReadState.EndOfFile;
253 return false;
253 return false;
254 } catch {
254 } catch {
255 m_state = System.Xml.ReadState.Error;
255 m_state = System.Xml.ReadState.Error;
256 throw;
256 throw;
257 }
257 }
258 }
258 }
259
259
260 public override bool ReadAttributeValue() {
260 public override bool ReadAttributeValue() {
261 return false;
261 return false;
262 }
262 }
263
263
264 public override ReadState ReadState {
264 public override ReadState ReadState {
265 get { return m_state; }
265 get { return m_state; }
266 }
266 }
267
267
268 public override void ResolveEntity() {
268 public override void ResolveEntity() {
269 // do nothing
269 // do nothing
270 }
270 }
271
271
272 public override string Value {
272 public override string Value {
273 get {
273 get {
274 if (m_parser.ElementValue == null)
274 if (m_parser.ElementValue == null)
275 return String.Empty;
275 return String.Empty;
276 if (Convert.GetTypeCode(m_parser.ElementValue) == TypeCode.Double)
276 if (Convert.GetTypeCode(m_parser.ElementValue) == TypeCode.Double)
277 return ((double)m_parser.ElementValue).ToString(CultureInfo.InvariantCulture);
277 return ((double)m_parser.ElementValue).ToString(CultureInfo.InvariantCulture);
278 else
278 else
279 return m_parser.ElementValue.ToString();
279 return m_parser.ElementValue.ToString();
280 }
280 }
281 }
281 }
282
282
283 void SetLocalName(string name) {
283 void SetLocalName(string name) {
284 m_localNameStack.Push(m_localName);
284 m_localNameStack.Push(m_localName);
285 m_localName.localName = name;
285 m_localName.localName = name;
286 m_localName.isArray = false;
286 m_localName.isArray = false;
287 }
287 }
288
288
289 void SetLocalName(string name, bool isArray) {
289 void SetLocalName(string name, bool isArray) {
290 m_localNameStack.Push(m_localName);
290 m_localNameStack.Push(m_localName);
291 m_localName.localName = name;
291 m_localName.localName = name;
292 m_localName.isArray = isArray;
292 m_localName.isArray = isArray;
293 }
293 }
294
294
295 void RestoreLocalName() {
295 void RestoreLocalName() {
296 m_localName = m_localNameStack.Pop();
296 m_localName = m_localNameStack.Pop();
297 }
297 }
298
298
299 public override void Close() {
299 public override void Close() {
300
300
301 }
301 }
302
302
303 protected override void Dispose(bool disposing) {
303 protected override void Dispose(bool disposing) {
304 #if MONO
305 disposing = true;
306 #endif
304 if (disposing) {
307 if (disposing) {
305 m_parser.Dispose();
308 m_parser.Dispose();
306 }
309 }
307 base.Dispose(disposing);
310 base.Dispose(disposing);
308 }
311 }
309
312
310 public static JSONXmlReader Create(string file, JSONXmlReaderOptions options) {
313 public static JSONXmlReader Create(string file, JSONXmlReaderOptions options) {
311 return Create(File.OpenText(file), options);
314 return Create(File.OpenText(file), options);
312 }
315 }
313
316
314 /// <summary>
317 /// <summary>
315 /// Creates the XmlReader for the specified text stream with JSON data.
318 /// Creates the XmlReader for the specified text stream with JSON data.
316 /// </summary>
319 /// </summary>
317 /// <param name="reader">Text reader.</param>
320 /// <param name="reader">Text reader.</param>
318 /// <param name="options">Options.</param>
321 /// <param name="options">Options.</param>
319 /// <remarks>
322 /// <remarks>
320 /// The reader will be disposed when the XmlReader is disposed.
323 /// The reader will be disposed when the XmlReader is disposed.
321 /// </remarks>
324 /// </remarks>
322 public static JSONXmlReader Create(TextReader reader, JSONXmlReaderOptions options) {
325 public static JSONXmlReader Create(TextReader reader, JSONXmlReaderOptions options) {
323 return new JSONXmlReader(new JSONParser(reader, true), options);
326 return new JSONXmlReader(new JSONParser(reader, true), options);
324 }
327 }
325
328
326 /// <summary>
329 /// <summary>
327 /// Creates the XmlReader for the specified stream with JSON data.
330 /// Creates the XmlReader for the specified stream with JSON data.
328 /// </summary>
331 /// </summary>
329 /// <param name="stream">Stream.</param>
332 /// <param name="stream">Stream.</param>
330 /// <param name="options">Options.</param>
333 /// <param name="options">Options.</param>
331 /// <remarks>
334 /// <remarks>
332 /// The stream will be disposed when the XmlReader is disposed.
335 /// The stream will be disposed when the XmlReader is disposed.
333 /// </remarks>
336 /// </remarks>
334 public static JSONXmlReader Create(Stream stream, JSONXmlReaderOptions options) {
337 public static JSONXmlReader Create(Stream stream, JSONXmlReaderOptions options) {
335 Safe.ArgumentNotNull(stream, "stream");
338 Safe.ArgumentNotNull(stream, "stream");
336 // HACK don't dispose StreaReader to keep stream opened
339 // HACK don't dispose StreaReader to keep stream opened
337 return Create(new StreamReader(stream), options);
340 return Create(new StreamReader(stream), options);
338 }
341 }
339 }
342 }
340 }
343 }
@@ -1,92 +1,95
1 using System;
1 using System;
2 using Implab.Parallels;
2 using Implab.Parallels;
3 using System.Threading;
3 using System.Threading;
4 using System.Diagnostics;
5 using System.Diagnostics.CodeAnalysis;
4
6
5 namespace Implab {
7 namespace Implab {
6 public class ObjectPool<T> : IDisposable {
8 public abstract class ObjectPool<T> : IDisposable {
7 readonly Func<T> m_factory;
8 readonly Action<T> m_cleanup;
9 readonly int m_size;
9 readonly int m_size;
10 readonly MTQueue<T> m_queue = new MTQueue<T>();
10 readonly MTQueue<T> m_queue = new MTQueue<T>();
11
11
12 [SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
13 static readonly bool _isValueType = typeof(T).IsValueType;
14
12 bool m_disposed;
15 bool m_disposed;
13
16
14 int m_count;
17 int m_count;
15
18
16 public ObjectPool(Func<T> factory, Action<T> cleanup, int size) {
19 protected ObjectPool(int size) {
17 Safe.ArgumentNotNull(factory, "factory");
18 Safe.ArgumentInRange(size, 1, size, "size");
19
20 m_factory = factory;
21 m_cleanup = cleanup;
22 m_size = size;
20 m_size = size;
23 }
21 }
24
22
25 public ObjectPool(Func<T> factory, Action<T> cleanup) : this(factory,cleanup,Environment.ProcessorCount+1) {
23 protected ObjectPool() : this(Environment.ProcessorCount+1) {
26 }
27
28 public ObjectPool(Func<T> factory) : this(factory,null,Environment.ProcessorCount+1) {
29 }
24 }
30
25
31 public ObjectPoolWrapper<T> AllocateAuto() {
26 public ObjectPoolWrapper<T> AllocateAuto() {
32
27
33 return new ObjectPoolWrapper<T>(Allocate(), this);
28 return new ObjectPoolWrapper<T>(Allocate(), this);
34 }
29 }
35
30
36 public T Allocate() {
31 public T Allocate() {
37 if (m_disposed)
32 if (m_disposed)
38 throw new ObjectDisposedException(this.ToString());
33 throw new ObjectDisposedException(ToString());
39
34
40 T instance;
35 T instance;
41 if (m_queue.TryDequeue(out instance)) {
36 if (m_queue.TryDequeue(out instance)) {
42 Interlocked.Decrement(ref m_count);
37 Interlocked.Decrement(ref m_count);
43 } else {
38 } else {
44 instance = m_factory();
39 instance = CreateInstance();
40 Debug.Assert(!Object.Equals(instance, default(T)) || _isValueType);
45 }
41 }
46 return instance;
42 return instance;
47 }
43 }
48
44
45 protected abstract T CreateInstance();
46
47 protected virtual void CleanupInstance(T instance) {
48 }
49
49 public void Release(T instance) {
50 public void Release(T instance) {
51 if ( Object.Equals(instance,default(T)) && !_isValueType)
52 return;
53
50 Thread.MemoryBarrier();
54 Thread.MemoryBarrier();
51 if (m_count < m_size && !m_disposed) {
55 if (m_count < m_size && !m_disposed) {
52 Interlocked.Increment(ref m_count);
56 Interlocked.Increment(ref m_count);
53
57
54 if (m_cleanup != null)
58 CleanupInstance(instance);
55 m_cleanup(instance);
56
59
57 m_queue.Enqueue(instance);
60 m_queue.Enqueue(instance);
58
61
59 // ΠΏΠΎΠΊΠ° элСмСнт возвращался Π² кСш, Π±Ρ‹Π»Π° Π½Π°Ρ‡Π°Ρ‚Π° опСрация освобоТдСния всСго кСша
62 // ΠΏΠΎΠΊΠ° элСмСнт возвращался Π² кСш, Π±Ρ‹Π»Π° Π½Π°Ρ‡Π°Ρ‚Π° опСрация освобоТдСния всСго кСша
60 // ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡƒΠΆΠ΅ Π·Π°ΠΊΠΎΠ½Ρ†Π΅Π½Π°, Π² Ρ‚Π°ΠΊΠΎΠΌ случаС слСдуСт ΠΈΠ·Π²Π»Π΅Ρ‡ΡŒ элСмСнт ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎ ΠΈ
63 // ΠΈ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎ ΡƒΠΆΠ΅ Π·Π°ΠΊΠΎΠ½Ρ†Π΅Π½Π°, Π² Ρ‚Π°ΠΊΠΎΠΌ случаС слСдуСт ΠΈΠ·Π²Π»Π΅Ρ‡ΡŒ элСмСнт ΠΎΠ±Ρ€Π°Ρ‚Π½ΠΎ ΠΈ
61 // ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ Π΅Π³ΠΎ. Если опСрация освобоТдСния кСша Π΅Ρ‰Π΅ Π½Π΅ Π·Π°Π²Ρ€Π΅ΡˆΠΈΠ»Π°ΡΡŒ, Ρ‚ΠΎ Π±ΡƒΠ΄Π΅Ρ‚
64 // ΠΎΡΠ²ΠΎΠ±ΠΎΠ΄ΠΈΡ‚ΡŒ Π΅Π³ΠΎ. Если опСрация освобоТдСния кСша Π΅Ρ‰Π΅ Π½Π΅ Π·Π°Π²Ρ€Π΅ΡˆΠΈΠ»Π°ΡΡŒ, Ρ‚ΠΎ Π±ΡƒΠ΄Π΅Ρ‚
62 // ΠΈΠ·ΡŠΡΡ‚ ΠΈ освобоТдСн ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ элСмСн, Ρ‡Ρ‚ΠΎ Π½Π΅ повлияСт Π½Π° Ρ…ΠΎΠ΄ всСго процСсса.
65 // ΠΈΠ·ΡŠΡΡ‚ ΠΈ освобоТдСн ΠΏΡ€ΠΎΠΈΠ·Π²ΠΎΠ»ΡŒΠ½Ρ‹ΠΉ элСмСн, Ρ‡Ρ‚ΠΎ Π½Π΅ повлияСт Π½Π° Ρ…ΠΎΠ΄ всСго процСсса.
63 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable)
66 if (m_disposed && m_queue.TryDequeue(out instance) && instance is IDisposable)
64 ((IDisposable)instance).Dispose() ;
67 ((IDisposable)instance).Dispose() ;
65
68
66 } else {
69 } else {
67 if (instance is IDisposable)
70 if (instance is IDisposable)
68 ((IDisposable)instance).Dispose();
71 ((IDisposable)instance).Dispose();
69 }
72 }
70 }
73 }
71
74
72 protected virtual void Dispose(bool disposing) {
75 protected virtual void Dispose(bool disposing) {
73 if (disposing) {
76 if (disposing) {
74 m_disposed = true;
77 m_disposed = true;
75 T instance;
78 T instance;
76 while (m_queue.TryDequeue(out instance))
79 while (m_queue.TryDequeue(out instance))
77 if (instance is IDisposable)
80 if (instance is IDisposable)
78 ((IDisposable)instance).Dispose();
81 ((IDisposable)instance).Dispose();
79 }
82 }
80 }
83 }
81
84
82 #region IDisposable implementation
85 #region IDisposable implementation
83
86
84 public void Dispose() {
87 public void Dispose() {
85 Dispose(true);
88 Dispose(true);
86 GC.SuppressFinalize(this);
89 GC.SuppressFinalize(this);
87 }
90 }
88
91
89 #endregion
92 #endregion
90 }
93 }
91 }
94 }
92
95
General Comments 0
You need to be logged in to leave comments. Login now