1 #Region "Microsoft.VisualBasic::f99adc89c5dd6da4545b9a3904732d2f, Microsoft.VisualBasic.Core\Serialization\JSON\Formatter\JsonFormatterStrategyContext.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class JsonFormatterStrategyContext
35     
36     '         Properties: Indent, IsInArrayScope, IsProcessingDoubleQuoteInitiatedString, IsProcessingSingleQuoteInitiatedString, IsProcessingString
37     '                     IsStart, WasLastCharacterABackSlash
38     
39     '         Sub: AddCharacterStrategy, AppendCurrentChar, AppendIndents, AppendNewLine, AppendSpace
40     '              BuildContextIndents, ClearStrategies, CloseCurrentScope, EnterArrayScope, EnterObjectScope
41     '              InitializeIndent, PrettyPrintCharacter
42     
43     
44     ' /********************************************************************************/
45
46 #End Region
47
48 Imports System.Collections.Generic
49 Imports System.Text
50 Imports Microsoft.VisualBasic.Serialization.JSON.Formatter.Internals.Strategies
51
52 Namespace Serialization.JSON.Formatter.Internals
53
54     Friend NotInheritable Class JsonFormatterStrategyContext
55
56         Const Space As String = " "
57         Const SpacesPerIndent As Integer = 4
58
59         Private m_indent As String = String.Empty
60
61         Private currentCharacter As Char
62         Private previousChar As Char
63
64         Private outputBuilder As StringBuilder
65
66         Private ReadOnly scopeState As New FormatterScopeState()
67         Private ReadOnly strategies As IDictionary(Of Char, ICharacterStrategy) = New Dictionary(Of Char, ICharacterStrategy)()
68
69
70         Public ReadOnly Property Indent() As String
71             Get
72                 If Me.m_indent = String.Empty Then
73                     Me.InitializeIndent()
74                 End If
75
76                 Return Me.m_indent
77             End Get
78         End Property
79
80         Private Sub InitializeIndent()
81             For i As Integer = 0 To SpacesPerIndent - 1
82                 Me.m_indent += Space
83             Next
84         End Sub
85
86
87         Public ReadOnly Property IsInArrayScope() As Boolean
88             Get
89                 Return Me.scopeState.IsTopTypeArray
90             End Get
91         End Property
92
93         Private Sub AppendIndents(indents As Integer)
94             For i As Integer = 0 To indents - 1
95                 Me.outputBuilder.Append(Indent)
96             Next
97         End Sub
98
99         Public IsProcessingVariableAssignment As Boolean
100         Public Property IsProcessingDoubleQuoteInitiatedString() As Boolean
101         Public Property IsProcessingSingleQuoteInitiatedString() As Boolean
102
103         Public ReadOnly Property IsProcessingString() As Boolean
104             Get
105                 Return Me.IsProcessingDoubleQuoteInitiatedString OrElse Me.IsProcessingSingleQuoteInitiatedString
106             End Get
107         End Property
108
109         Public ReadOnly Property IsStart() As Boolean
110             Get
111                 Return Me.outputBuilder.Length = 0
112             End Get
113         End Property
114
115         Public ReadOnly Property WasLastCharacterABackSlash() As Boolean
116             Get
117                 Return Me.previousChar = "\"c
118             End Get
119         End Property
120
121         Public Sub PrettyPrintCharacter(curChar As Char, output As StringBuilder)
122             Me.currentCharacter = curChar
123
124             Dim strategy As ICharacterStrategy = If(Me.strategies.ContainsKey(curChar), strategies(curChar), New DefaultCharacterStrategy())
125
126             Me.outputBuilder = output
127
128             strategy.Execute(Me)
129
130             Me.previousChar = curChar
131         End Sub
132
133         Public Sub AppendCurrentChar()
134             Me.outputBuilder.Append(Me.currentCharacter)
135         End Sub
136
137         Public Sub AppendNewLine()
138             Me.outputBuilder.Append(Environment.NewLine)
139         End Sub
140
141         Public Sub BuildContextIndents()
142             Me.AppendNewLine()
143             Me.AppendIndents(Me.scopeState.ScopeDepth)
144         End Sub
145
146         Public Sub EnterObjectScope()
147             Me.scopeState.PushObjectContextOntoStack()
148         End Sub
149
150         Public Sub CloseCurrentScope()
151             Me.scopeState.PopJsonType()
152         End Sub
153
154         Public Sub EnterArrayScope()
155             Me.scopeState.PushJsonArrayType()
156         End Sub
157
158         Public Sub AppendSpace()
159             Me.outputBuilder.Append(Space)
160         End Sub
161
162         Public Sub ClearStrategies()
163             Me.strategies.Clear()
164         End Sub
165
166         Public Sub AddCharacterStrategy(strategy As ICharacterStrategy)
167             Me.strategies(strategy.ForWhichCharacter) = strategy
168         End Sub
169     End Class
170 End Namespace