forked from rainliu/sip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WarningParser.go
114 lines (91 loc) · 2.55 KB
/
WarningParser.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package parser
import (
"github.com/tutuvss/sip/core"
"github.com/tutuvss/sip/header"
"strconv"
)
/** SIPParser for Warning header.
*/
type WarningParser struct {
HeaderParser
}
/** Constructor
* @param warning - Warning header to parse
*/
func NewWarningParser(warning string) *WarningParser {
this := &WarningParser{}
this.HeaderParser.super(warning)
return this
}
/** Cosntructor
* @param lexer - the lexer to use.
*/
func NewWarningParserFromLexer(lexer core.Lexer) *WarningParser {
this := &WarningParser{}
this.HeaderParser.superFromLexer(lexer)
return this
}
/** parse the String message
* @return SIPHeader (WarningList object)
* @throws SIPParseException if the message does not respect the spec.
*/
func (this *WarningParser) Parse() (sh header.Header, ParseException error) {
warningList := header.NewWarningList()
var ch byte
lexer := this.GetLexer()
this.HeaderName(TokenTypes_WARNING)
for ch, _ = lexer.LookAheadK(0); ch != '\n'; ch, _ = lexer.LookAheadK(0) {
warning := header.NewWarning()
warning.SetHeaderName(core.SIPHeaderNames_WARNING)
// Parsing the 3digits code
lexer.Match(TokenTypes_ID)
token := lexer.GetNextToken()
var code int
if code, ParseException = strconv.Atoi(token.GetTokenValue()); ParseException != nil {
return nil, ParseException
}
if ParseException = warning.SetCode(code); ParseException != nil {
return nil, ParseException
}
lexer.SPorHT()
// Parsing the agent
lexer.Match(TokenTypes_ID)
token = lexer.GetNextToken()
warning.SetAgent(token.GetTokenValue())
lexer.SPorHT()
// Parsing the text
var text string
if text, ParseException = lexer.QuotedString(); ParseException != nil {
return nil, ParseException
}
warning.SetText(text)
lexer.SPorHT()
warningList.PushBack(warning)
for ch, _ = lexer.LookAheadK(0); ch == ','; ch, _ = lexer.LookAheadK(0) {
lexer.Match(',')
lexer.SPorHT()
warning = header.NewWarning()
// Parsing the 3digits code
lexer.Match(TokenTypes_ID)
tok := lexer.GetNextToken()
if code, ParseException = strconv.Atoi(tok.GetTokenValue()); ParseException != nil {
return nil, ParseException
}
warning.SetCode(code)
lexer.SPorHT()
// Parsing the agent
lexer.Match(TokenTypes_ID)
tok = lexer.GetNextToken()
warning.SetAgent(tok.GetTokenValue())
lexer.SPorHT()
// Parsing the text
if text, ParseException = lexer.QuotedString(); ParseException != nil {
return nil, ParseException
}
warning.SetText(text)
lexer.SPorHT()
warningList.PushBack(warning)
}
}
return warningList, nil
}