2024-10-16 21:28:04 +02:00
|
|
|
package atom
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
import (
|
2024-10-17 20:10:18 +02:00
|
|
|
"encoding/xml"
|
2024-10-13 17:19:40 +02:00
|
|
|
"fmt"
|
2024-10-15 19:32:14 +02:00
|
|
|
"strings"
|
2024-10-16 17:38:03 +02:00
|
|
|
"time"
|
2024-10-13 17:19:40 +02:00
|
|
|
)
|
|
|
|
|
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 {
|
2024-10-17 20:10:18 +02:00
|
|
|
XMLName xml.Name `xml:"entry"`
|
2024-10-13 17:19:40 +02:00
|
|
|
*CommonAttributes
|
2024-10-17 20:10:18 +02:00
|
|
|
Authors []*Person `xml:"author,omitempty"`
|
|
|
|
Categories []*Category `xml:",omitempty"`
|
|
|
|
Content Content `xml:",omitempty"`
|
|
|
|
Contributors []*Person `xml:"contributors,omitempty"`
|
|
|
|
ID *ID
|
|
|
|
Links []*Link `xml:",omitempty"`
|
2024-10-13 17:19:40 +02:00
|
|
|
Published *Date `xml:"published,omitempty"`
|
2024-10-15 18:07:06 +02:00
|
|
|
Rights Text `xml:"rights,omitempty"`
|
2024-10-17 20:10:18 +02:00
|
|
|
Source *Source `xml:",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-16 19:59:28 +02:00
|
|
|
// checkAuthors checks the entry's authors for incompatibilities with RFC4287.
|
|
|
|
// It returns an errors.
|
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-17 05:05:14 +02:00
|
|
|
func (e *Entry) checkAuthors(authorInFeed bool) error {
|
2024-10-13 17:19:40 +02:00
|
|
|
if e.Authors == nil {
|
2024-10-17 05:05:14 +02:00
|
|
|
if !authorInFeed {
|
|
|
|
if e.Source == nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no authors set in entry %v", e.ID.URI)
|
2024-10-17 05:05:14 +02:00
|
|
|
}
|
|
|
|
if e.Source.Authors == nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no authors set in entry %v", e.ID.URI)
|
2024-10-17 05:05:14 +02:00
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i, a := range e.Authors {
|
|
|
|
if err := a.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("author element %v of entry %v: %v", i, e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-19 14:12:51 +02:00
|
|
|
// NewEntry creates a new Entry. It returns a *Entry.
|
|
|
|
func NewEntry(title string) *Entry {
|
2024-10-16 19:59:28 +02:00
|
|
|
return &Entry{
|
2024-10-19 14:52:19 +02:00
|
|
|
CommonAttributes: newCommonAttributes(),
|
|
|
|
ID: NewID(NewURN()),
|
|
|
|
Title: NewText("text", title),
|
|
|
|
Updated: NewDate(time.Now()),
|
2024-10-19 12:28:09 +02:00
|
|
|
}
|
2024-10-19 14:12:51 +02:00
|
|
|
}
|
2024-10-19 12:28:09 +02:00
|
|
|
|
2024-10-20 14:31:54 +02:00
|
|
|
// AddAuthor adds the Person as an author to the Entry. It returns its index as
|
|
|
|
// an int.
|
2024-10-20 12:57:24 +02:00
|
|
|
func (e *Entry) AddAuthor(p *Person) int {
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:57:24 +02:00
|
|
|
return addToSlice(&e.Authors, p)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:41:09 +02:00
|
|
|
// DeleteAuthor deletes the Person at index from the Entry. It return an error.
|
2024-10-20 12:35:26 +02:00
|
|
|
func (e *Entry) DeleteAuthor(index int) error {
|
|
|
|
if err := deleteFromSlice(&e.Authors, index); err != nil {
|
|
|
|
return fmt.Errorf("error deleting author %v from entry %v: %v", index, e.ID.URI, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
return nil
|
2024-10-16 21:35:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:31:54 +02:00
|
|
|
// AddCategory adds the Category to the Entry. It returns ts index as an int.
|
2024-10-20 12:57:24 +02:00
|
|
|
func (e *Entry) AddCategory(c *Category) int {
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:57:24 +02:00
|
|
|
return addToSlice(&e.Categories, c)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:41:09 +02:00
|
|
|
// DeleteCategory deletes the Category at index from the Entry. It return an
|
|
|
|
// error.
|
2024-10-20 12:35:26 +02:00
|
|
|
func (e *Entry) DeleteCategory(index int) error {
|
|
|
|
if err := deleteFromSlice(&e.Categories, index); err != nil {
|
|
|
|
return fmt.Errorf("error deleting category %v from entry %v: %v", index, e.ID.URI, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
return nil
|
2024-10-16 21:35:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:31:54 +02:00
|
|
|
// AddContributor adds the Person as a contributor to the Entry. It returns its
|
|
|
|
// index as an int.
|
2024-10-20 12:57:24 +02:00
|
|
|
func (e *Entry) AddContributor(c *Person) int {
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:57:24 +02:00
|
|
|
return addToSlice(&e.Contributors, c)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:41:09 +02:00
|
|
|
// DeleteContributor deletes the Person at index from the Entry. It return an
|
|
|
|
// error.
|
2024-10-20 12:35:26 +02:00
|
|
|
func (e *Entry) DeleteContributor(index int) error {
|
|
|
|
if err := deleteFromSlice(&e.Contributors, index); err != nil {
|
|
|
|
return fmt.Errorf("error deleting contributor %v from entry %v: %v", index, e.ID.URI, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
return nil
|
2024-10-16 21:35:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:31:54 +02:00
|
|
|
// AddLink adds the Link to the Entry. It returns its index as an int.
|
2024-10-20 12:57:24 +02:00
|
|
|
func (e *Entry) AddLink(l *Link) int {
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:57:24 +02:00
|
|
|
return addToSlice(&e.Links, l)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:41:09 +02:00
|
|
|
// DeleteLink deletes the Link at index from the Entry. It return an error.
|
2024-10-20 12:35:26 +02:00
|
|
|
func (e *Entry) DeleteLink(index int) error {
|
|
|
|
if err := deleteFromSlice(&e.Links, index); err != nil {
|
|
|
|
return fmt.Errorf("error deleting link %v from entry %v: %v", index, e.ID.URI, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
return nil
|
2024-10-16 21:35:24 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 14:31:54 +02:00
|
|
|
// AddExtension adds the ExtensionElement to the Entry. It returns its index as
|
|
|
|
// an int.
|
2024-10-20 12:57:24 +02:00
|
|
|
func (e *Entry) AddExtension(x *ExtensionElement) int {
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 12:57:24 +02:00
|
|
|
return addToSlice(&e.Extensions, x)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 12:41:09 +02:00
|
|
|
// DeleteExtension deletes the Extension at index from the Entry. It return an
|
|
|
|
// error.
|
2024-10-20 12:35:26 +02:00
|
|
|
func (e *Entry) DeleteExtension(index int) error {
|
|
|
|
if err := deleteFromSlice(&e.Extensions, index); err != nil {
|
|
|
|
return fmt.Errorf("error deleting extension %v from entry %v: %v", index, e.ID.URI, err)
|
2024-10-20 10:49:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-20 15:54:49 +02:00
|
|
|
if e.Updated == nil {
|
|
|
|
e.Updated = NewDate(time.Now())
|
|
|
|
} else {
|
|
|
|
e.Updated.DateTime = DateTime(time.Now())
|
|
|
|
}
|
|
|
|
|
2024-10-20 10:49:29 +02:00
|
|
|
return nil
|
2024-10-15 16:20:11 +02:00
|
|
|
}
|
|
|
|
|
2024-10-16 19:59:28 +02:00
|
|
|
// Check checks the Entry for incompatibilities with RFC4287. It returns an
|
|
|
|
// error.
|
2024-10-13 17:19:40 +02:00
|
|
|
func (e *Entry) Check() error {
|
|
|
|
if e.ID == nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("no id element of entry %v", e)
|
2024-10-13 17:19:40 +02:00
|
|
|
} else {
|
|
|
|
if err := e.ID.Check(); err != nil {
|
2024-10-18 19:04:08 +02:00
|
|
|
return fmt.Errorf("id element of entry %v: %v", e, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-17 05:05:14 +02:00
|
|
|
if err := e.checkAuthors(true); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-15 18:35:53 +02:00
|
|
|
for i, c := range e.Categories {
|
|
|
|
if err := c.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
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 {
|
2024-10-15 19:32:14 +02:00
|
|
|
if err := e.Content.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("content element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
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) {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no content element of entry %v and no link element with rel \"alternate\"", e.ID.URI)
|
2024-10-15 18:34:19 +02:00
|
|
|
}
|
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 {
|
2024-10-20 10:49:29 +02:00
|
|
|
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 {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("link element %v of entry %v: %v", i, e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
2024-10-15 18:46:19 +02:00
|
|
|
if hasAlternateDuplicateLinks(e.Links) {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("links with a rel attribute value of \"alternate\" and duplicate type and hreflang attribute values found in entry %v", e.ID.URI)
|
2024-10-15 18:46:19 +02:00
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
|
|
|
|
if e.Published != nil {
|
|
|
|
if err := e.Published.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("published element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Rights != nil {
|
2024-10-15 18:07:06 +02:00
|
|
|
if err := e.Rights.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("rights element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Source != nil {
|
|
|
|
if err := e.Source.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("source element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Summary != nil {
|
2024-10-15 18:07:06 +02:00
|
|
|
if err := e.Summary.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("summary element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
2024-10-15 19:32:14 +02:00
|
|
|
} else {
|
|
|
|
// atom:entry elements MUST contain an atom:summary element in either
|
|
|
|
// of the following cases:
|
|
|
|
// the atom:entry contains an atom:content that has a "src" attribute
|
|
|
|
// (and is thus empty).
|
|
|
|
if e.Content.hasSRC() {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no summary element of entry %v but content of type out of line content", e.ID.URI)
|
2024-10-15 19:32:14 +02:00
|
|
|
}
|
|
|
|
// the atom:entry contains content that is encoded in Base64; i.e., the
|
|
|
|
// "type" attribute of atom:content is a MIME media type [MIMEREG], but
|
|
|
|
// is not an XML media type [RFC3023], does not begin with "text/", and
|
|
|
|
// does not end with "/xml" or "+xml".
|
2024-10-15 19:46:26 +02:00
|
|
|
mediaType := e.Content.getType()
|
2024-10-17 05:34:32 +02:00
|
|
|
if isValidMediaType(mediaType) && !isXMLMediaType(mediaType) && !strings.HasPrefix(mediaType, "text/") {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no summary element of entry %v but media type not xml", e.ID.URI)
|
2024-10-15 19:32:14 +02:00
|
|
|
}
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.Title == nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no title element of entry %v", e.ID.URI)
|
2024-10-13 17:19:40 +02:00
|
|
|
} else {
|
2024-10-15 18:07:06 +02:00
|
|
|
if err := e.Title.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("title element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if e.Updated == nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("no updated element of entry %v", e.ID.URI)
|
2024-10-13 17:19:40 +02:00
|
|
|
} else {
|
|
|
|
if err := e.Updated.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return fmt.Errorf("updated element of entry %v: %v", e.ID.URI, err)
|
2024-10-13 17:19:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-15 18:35:53 +02:00
|
|
|
for i, x := range e.Extensions {
|
|
|
|
if err := x.Check(); err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
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
|
|
|
|
}
|
2024-10-18 05:00:24 +02:00
|
|
|
|
|
|
|
// ToXML converts the Feed to XML. It returns a string and an error.
|
|
|
|
func (e *Entry) ToXML(encoding string) (string, error) {
|
|
|
|
xml, err := xml.MarshalIndent(e, "", " ")
|
|
|
|
if err != nil {
|
2024-10-20 10:49:29 +02:00
|
|
|
return "", fmt.Errorf("error xml encoding entry %v: %v", e.ID.URI, err)
|
2024-10-18 05:00:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintln(`<?xml version="1.0" encoding="`+encoding+`"?>`) + string(xml), nil
|
|
|
|
}
|