2024-10-13 17:19:40 +02:00
|
|
|
package atomfeed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2024-10-15 18:04:03 +02:00
|
|
|
// It is advisable that each atom:entry element contain a non-empty atom:title
|
|
|
|
// element, a non-empty atom:content element when that element is present, and
|
|
|
|
// a non-empty atom:summary element when the entry contains no atom:content
|
|
|
|
// element.
|
2024-10-13 17:19:40 +02:00
|
|
|
type Entry struct {
|
|
|
|
*CommonAttributes
|
|
|
|
Authors []*Person `xml:"author,omitempty"`
|
|
|
|
Categories []*Category `xml:"category,omitempty"`
|
|
|
|
Content *Content `xml:"content,omitempty"`
|
|
|
|
Contributors []*Person `xml:"contributors,omitempty"`
|
|
|
|
ID *ID `xml:"id"`
|
|
|
|
Links []*Link `xml:"link,omitempty"`
|
|
|
|
Published *Date `xml:"published,omitempty"`
|
2024-10-15 18:07:06 +02:00
|
|
|
Rights Text `xml:"rights,omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Source *Source `xml:"source,omitempty"`
|
2024-10-15 18:07:06 +02:00
|
|
|
Summary Text `xml:"summary,omitempty"`
|
|
|
|
Title Text `xml:"title"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Updated *Date `xml:"updated"`
|
2024-10-13 20:42:17 +02:00
|
|
|
Extensions []*ExtensionElement `xml:",any,omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-15 18:40:32 +02:00
|
|
|
// atom:entry elements MUST contain one or more atom:author elements, unless
|
|
|
|
// the atom:entry contains an atom:source element that contains an atom:author
|
|
|
|
// element or, in an Atom Feed Document, the atom:feed element contains an
|
|
|
|
// atom:author element itself.
|
2024-10-13 17:19:40 +02:00
|
|
|
func (e *Entry) checkAuthors() error {
|
|
|
|
if e.Authors == nil {
|
|
|
|
if e.Source.Authors == nil {
|
|
|
|
return errors.New("no authors set in entry")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, a := range e.Authors {
|
|
|
|
if err := a.Check(); err != nil {
|
|
|
|
return fmt.Errorf("author element %v of entry: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-15 18:34:19 +02:00
|
|
|
func alternateRelExists(l []*Link) bool {
|
|
|
|
for _, link := range l {
|
|
|
|
if link.Rel == "alternate" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-15 16:20:11 +02:00
|
|
|
func (e *Entry) AddExtension(name string, value any) {
|
|
|
|
e.Extensions = append(e.Extensions, &ExtensionElement{XMLName: xml.Name{Local: name}, Value: value})
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:19:40 +02:00
|
|
|
func (e *Entry) Check() error {
|
|
|
|
if e.ID == nil {
|
|
|
|
return errors.New("no id element of entry")
|
|
|
|
} else {
|
|
|
|
if err := e.ID.Check(); err != nil {
|
|
|
|
return fmt.Errorf("id element of entry: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := e.checkAuthors(); err != nil {
|
|
|
|
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
|
2024-10-15 18:35:53 +02:00
|
|
|
for i, c := range e.Categories {
|
|
|
|
if err := c.Check(); err != nil {
|
|
|
|
return fmt.Errorf("category element %v of entry %v: %v", i, e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Content != nil {
|
|
|
|
if err := (*e.Content).Check(); err != nil {
|
|
|
|
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
2024-10-15 18:34:19 +02:00
|
|
|
} else {
|
2024-10-15 18:37:54 +02:00
|
|
|
// atom:entry elements that contain no child atom:content element MUST
|
|
|
|
// contain at least one atom:link element with a rel attribute value of
|
|
|
|
// "alternate".
|
2024-10-15 18:34:19 +02:00
|
|
|
if !alternateRelExists(e.Links) {
|
|
|
|
return errors.New("no content element of entry %v and no link element with rel \"alternate\"")
|
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-15 18:35:53 +02:00
|
|
|
for i, c := range e.Contributors {
|
|
|
|
if err := c.Check(); err != nil {
|
|
|
|
return fmt.Errorf("contributor element %v of entry %v: %v", i, e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 18:35:53 +02:00
|
|
|
for i, l := range e.Links {
|
|
|
|
if err := l.Check(); err != nil {
|
|
|
|
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Published != nil {
|
|
|
|
if err := e.Published.Check(); err != nil {
|
|
|
|
return fmt.Errorf("published element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Rights != nil {
|
2024-10-15 18:07:06 +02:00
|
|
|
if err := e.Rights.Check(); err != nil {
|
2024-10-13 17:19:40 +02:00
|
|
|
return fmt.Errorf("rights element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Source != nil {
|
|
|
|
if err := e.Source.Check(); err != nil {
|
|
|
|
return fmt.Errorf("source element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Summary != nil {
|
2024-10-15 18:07:06 +02:00
|
|
|
if err := e.Summary.Check(); err != nil {
|
2024-10-13 17:19:40 +02:00
|
|
|
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Title == nil {
|
|
|
|
return fmt.Errorf("no title element of entry %v", e.ID.URI)
|
|
|
|
} else {
|
2024-10-15 18:07:06 +02:00
|
|
|
if err := e.Title.Check(); err != nil {
|
2024-10-13 17:19:40 +02:00
|
|
|
return fmt.Errorf("title element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Updated == nil {
|
|
|
|
return fmt.Errorf("no updated element of entry %v", e.ID.URI)
|
|
|
|
} else {
|
|
|
|
if err := e.Updated.Check(); err != nil {
|
|
|
|
return fmt.Errorf("updated element of entry %v: %v", e.ID.URI, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 18:35:53 +02:00
|
|
|
for i, x := range e.Extensions {
|
|
|
|
if err := x.Check(); err != nil {
|
|
|
|
return fmt.Errorf("extension element %v of entry %v: %v", i, e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|