-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathatom1.0.go
195 lines (156 loc) · 5.17 KB
/
atom1.0.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package feedreader
/*
Atom 1.0 parser
Only support feed documents, not support entry documents.
The elements below are not supported.
feed/entry/source
*/
import "fmt"
import "time"
import "html"
// Person Constructs: author, contributor element
type Atom10Person struct {
Name string `xml:"name"` // required
Email string `xml:"email"`
Uri string `xml:"uri"`
}
// Text constructs: rights, title, subtitle, summary
// Type is "xhtml", "html" or "text"
type Atom10Text struct {
Content string `xml:",chardata"`
Type string `xml:"type,attr"`
}
// Attribute's of category element
type Atom10Category struct {
Term string `xml:"term,attr"` // required
Scheme string `xml:"scheme,attr"`
Label string `xml:"label,attr"`
}
// Value and attributes of generator element
type Atom10Generator struct {
Generator string `xml:",chardata"`
Uri string `xml:"uri,attr"`
Version string `xml:"version,attr"`
}
// Attributes of link element
type Atom10Link struct {
Href string `xml:"href,attr"` // required
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
Hreflang string `xml:"hreflang,attr"`
Title string `xml:"title,attr"`
Length uint64 `xml:"length,attr"`
}
type Atom10Content struct {
Atom10Text
Src string `xml:"src,attr"`
}
// Entry element, not support source element
type Atom10Entry struct {
Title *Atom10Text `xml:"title"` // required
Content *Atom10Content `xml:"content"`
Author []*Atom10Person `xml:"author"`
Category []*Atom10Category `xml:"category"`
Contributor []*Atom10Person `xml:"contributor"`
Id string `xml:"id"` // required
Link []*Atom10Link `xml:"link"`
PublishedRaw string `xml:"published"`
Published time.Time
Rights *Atom10Text `xml:"rights"`
Summary *Atom10Text `xml:"summary"`
UpdatedRaw string `xml:"updated"` // required
Updated time.Time
}
// Feed element
type Atom10Feed struct {
Xmlns string `xml:"xmlns,attr"` // feed's attribute
Author []*Atom10Person `xml:"author"`
Category []*Atom10Category `xml:"category"`
Contributor []*Atom10Person `xml:"contributor"`
Generator *Atom10Generator `xml:"generator"`
Icon string `xml:"icon"`
Logo string `xml:"logo"`
Id string `xml:"id"` // required
Link []*Atom10Link `xml:"link"`
Rights *Atom10Text `xml:"rights"`
Title *Atom10Text `xml:"title"` // required
Subtitle *Atom10Text `xml:"subtitle"`
UpdatedRaw string `xml:"updated"` // required
Updated time.Time
Entry []*Atom10Entry `xml:"entry"` // required
}
func (t *Atom10Text) String() string {
if t == nil {
return ""
}
if t.Type == "xhtml" {
var inner struct {
Content string `xml:",innerxml"`
}
err := unmarshal([]byte(t.Content), &inner)
if err != nil {
return ""
}
return html.EscapeString(inner.Content)
} else {
return t.Content
}
}
func (t *Atom10Text) Html() string {
if t == nil {
return ""
}
if t.Type == "xhtml" {
var inner struct {
Content string `xml:",innerxml"`
}
err := unmarshal([]byte(t.Content), &inner)
if err != nil {
return ""
}
return inner.Content
} else {
return transformContent(t.Content)
}
}
/* Parse atom feed document
arguments:
b bytes of atom xml
strict strict mode or not
return value:
feed point to a Atom10Feed struct
err error message which type is ParseError
*/
func Atom10Parse(b []byte) (feed *Atom10Feed, err error) {
const xmlns = "http://www.w3.org/2005/Atom"
e := unmarshal(b, &feed)
if e != nil {
err = &ParseError{Err: e}
return
}
if feed.Xmlns != xmlns {
err = &ParseError{Err: fmt.Errorf("Atom 1.0's xmlns should be '%s', '%s' is not correct.", xmlns, feed.Xmlns)}
return
}
feed.Updated, _ = ParseTime(feed.UpdatedRaw)
for i := range(feed.Link) {
if feed.Link[i].Rel == "" {
feed.Link[i].Rel = "alternate"
}
}
for i := range(feed.Entry) {
if feed.Entry[i].Link != nil {
for j := range(feed.Entry[i].Link) {
if feed.Entry[i].Link[j].Rel == "" {
feed.Entry[i].Link[j].Rel = "alternate"
}
}
}
feed.Entry[i].Published, _ = ParseTime(feed.Entry[i].PublishedRaw)
feed.Entry[i].Updated, _ = ParseTime(feed.Entry[i].UpdatedRaw)
}
return
}
func Atom10ParseString(s string) (*Atom10Feed, error) {
return Atom10Parse([]byte(s))
}